x86_64 added
This commit is contained in:
117
x86_64/0_basic.asm
Normal file
117
x86_64/0_basic.asm
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
global _start
|
||||||
|
|
||||||
|
; !! THIS IS INTEL STYLE ASSEMBLY !!
|
||||||
|
|
||||||
|
section .data
|
||||||
|
; This is an array, db = declare bytes
|
||||||
|
my_arr: db 0x12,0x34,0x56,0x78,0x90
|
||||||
|
|
||||||
|
; Some basic types:
|
||||||
|
; byte = 8 bits
|
||||||
|
; word = 16 bits
|
||||||
|
; double word = dword = 32 bits
|
||||||
|
; quad word = qword = 64 bits
|
||||||
|
|
||||||
|
; To declare them:
|
||||||
|
; db
|
||||||
|
; dw
|
||||||
|
; dd
|
||||||
|
; dq
|
||||||
|
|
||||||
|
; Endianness examples, this will be stored in reverse
|
||||||
|
; e.g. 0xbeef -> 0xef 0xbe in memory
|
||||||
|
little_endian: dw 0xbeef
|
||||||
|
|
||||||
|
; Supplying not enough bytes means the remainder will be zero filled
|
||||||
|
zerod_remainder: dw 0x42 ; = 0x42 0x00 in memory
|
||||||
|
|
||||||
|
; Equ works like #define
|
||||||
|
; It does get a symbol however, and can thus be used by other
|
||||||
|
; assemblies (by making it global). This value gets inlined.
|
||||||
|
; Useful for constant expressions
|
||||||
|
PI: equ 3
|
||||||
|
|
||||||
|
section .text
|
||||||
|
|
||||||
|
; Entry point
|
||||||
|
_start:
|
||||||
|
; Move 0 into the register RAX (64 bits)
|
||||||
|
mov rax, 0
|
||||||
|
|
||||||
|
; General purpose registers (A, B, C, D, ...)
|
||||||
|
; rax = accumulator
|
||||||
|
; rbx = base
|
||||||
|
; rcx = counter
|
||||||
|
; rdx = destination
|
||||||
|
; rsp & rbp = stack pointer (sp) and base pointer(bp)
|
||||||
|
; rsi & rdi = source index (si) and destination index (di)
|
||||||
|
; r8 - r15 = plain old registers
|
||||||
|
|
||||||
|
; Note that for all registers except the r8-r15 speficic bits
|
||||||
|
; can be addressed by changing the prefix and postfix, e.g.
|
||||||
|
; for register A: rax, eax, ax, al and ah
|
||||||
|
|
||||||
|
mov eax, 0xdeadbeef ; Copy 32 bits, 4 bytes, 1 dword
|
||||||
|
mov ebx, 0xabcd ; = mov ebx, 0x0000abcd
|
||||||
|
mov cl, 0xFF
|
||||||
|
mov ch, 0x00
|
||||||
|
; ecx = 0x00FF
|
||||||
|
|
||||||
|
; Some basic arithmetic
|
||||||
|
mov rdi, 10
|
||||||
|
mov rsi, 7
|
||||||
|
mov rbx, 14
|
||||||
|
|
||||||
|
inc rdi ; increment
|
||||||
|
dec rsi ; decrement
|
||||||
|
|
||||||
|
; note: operator dest, src
|
||||||
|
add rdi, rbx ; stores the ADD result in the first operand (rdi)
|
||||||
|
sub rsi, rbx ; rsi = rsi - rbx
|
||||||
|
|
||||||
|
; By default all arithmetic assumes unsigned values. Prepend with I to
|
||||||
|
; use signed math (e.g. imul, idiv, etc.)
|
||||||
|
|
||||||
|
; Multiplication
|
||||||
|
; Yields 128 bit values
|
||||||
|
; It multiplies the operand with the value of register A
|
||||||
|
; Stores the lower 64 bits in register A and the upper 64 bits in
|
||||||
|
; Register D
|
||||||
|
mov rax, 7
|
||||||
|
mov rdx, 4
|
||||||
|
mov rdi, 3
|
||||||
|
mul rdi ; rax = rax * rdi, rdx = 0 (because no overflow)
|
||||||
|
|
||||||
|
; Division
|
||||||
|
; Yields a 128 bit value
|
||||||
|
; The result (64 bits) is stored in register A
|
||||||
|
; The remainder (64 bits) is stored in register D
|
||||||
|
mov rax, 22 ; divident, to be result
|
||||||
|
mov rdx, 0 ; to be remainder
|
||||||
|
mov rdi, 5 ; divider
|
||||||
|
|
||||||
|
div rdi ; rax = 22/5 = 4, rdx = 2
|
||||||
|
|
||||||
|
; Bitwise operations
|
||||||
|
; AND
|
||||||
|
; OR
|
||||||
|
; XOR
|
||||||
|
; SHR
|
||||||
|
; SHL
|
||||||
|
|
||||||
|
mov rdi, 0x35
|
||||||
|
mov rsi, 0x44
|
||||||
|
|
||||||
|
and rdi, rsi
|
||||||
|
or rdi, rsi
|
||||||
|
xor rdi, rsi
|
||||||
|
|
||||||
|
shr rsi, 2 ; rsi >> 2
|
||||||
|
shl rsi, 3 ; rsi << 3
|
||||||
|
|
||||||
|
; There is also SAL and SAR for arithmetic left- and right shifts
|
||||||
|
|
||||||
|
mov rax, 60
|
||||||
|
xor rdi, rdi ; zero it
|
||||||
|
syscall
|
||||||
|
|
||||||
15
x86_64/README.md
Normal file
15
x86_64/README.md
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# Build steps
|
||||||
|
|
||||||
|
## Assembling
|
||||||
|
Assembling with NASM
|
||||||
|
```bash
|
||||||
|
nasm -g -f elf64 <file.asm>
|
||||||
|
```
|
||||||
|
- g = include debug info
|
||||||
|
- f = file format (elf64 -> x64)
|
||||||
|
|
||||||
|
## Linking
|
||||||
|
```bash
|
||||||
|
ld <object-files> -o <executable-name> # Default
|
||||||
|
gcc <object-files> -o <executable-name> # When using C library functions
|
||||||
|
```
|
||||||
22
x86_64/debug.sh
Normal file
22
x86_64/debug.sh
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
if [ -z "$1" ]; then
|
||||||
|
echo "Provide the program to debug as first argument"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -x "$1" ]; then
|
||||||
|
echo "$1 is not an executable file"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "I will launch gdbtui soon. Use \"break *<entry-address>\" to debug the program."
|
||||||
|
echo "Use \"layout asm\" to use a more assembly friendly layout"
|
||||||
|
echo "Use \"si\" to single step each instruction"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
echo "$(readelf -h $1 | egrep "Entry point")"
|
||||||
|
read -p "Copy the above address and press any key to continue..."
|
||||||
|
|
||||||
|
gdbtui -q $1
|
||||||
|
|
||||||
21
x86_64/makefile
Normal file
21
x86_64/makefile
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
CC = gcc
|
||||||
|
AS = nasm
|
||||||
|
LD = ld
|
||||||
|
|
||||||
|
.PHONY: all clean
|
||||||
|
|
||||||
|
all: build 0_basic
|
||||||
|
|
||||||
|
clean:
|
||||||
|
-rm -r build
|
||||||
|
-rm 0_basic
|
||||||
|
|
||||||
|
build:
|
||||||
|
mkdir build
|
||||||
|
|
||||||
|
build/%.o: %.asm
|
||||||
|
$(AS) -g -f elf64 $< -o $@
|
||||||
|
|
||||||
|
0_basic: build/0_basic.o
|
||||||
|
$(LD) $< -o $@
|
||||||
|
|
||||||
Reference in New Issue
Block a user