---
# System prepended metadata

title: Assembly

---

# Assembly

---

[TOC]

---

## Resource
- Look for OPcode [Intel® 64 and IA-32 Architectures Software Developer’s Manual](https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf)
- Look for Syntax [tutorialspoint](https://www.tutorialspoint.com/assembly_programming/index.htm)

### Data
- INTEGER
    - db – BYTE
    - dw – WORD (2-byte)
    - dd – DWORD (4-byte)
    - dq – QWORD (8-byte)
    - dt – tbyte (10-byte)
    - ddq/do – dqword (16-byte)
- FLOATING POINT
    - dd – single-precision (4-byte)
    - dq – double-precision (8-byte)
    - dt – extended-precision (10-byte)

## Calling Convention 
### Intel x86 
- Caller pushes parameters to the stack, from right to left
- Return value -> EAX
:::info
- cdecl: C/C++
    - Caller remove parameters
- stdcall: Windows API
    - Callee remove parameters
:::

### Intel x86_64
:::info
- System V AMD64
    - The first 6 -> RDI, RSI, RDX, RCX, R8, R9
- Microsoft x64
    - The first 4 -> RCX, RDX, R8, R9
:::
- The rest are pushed onto stack from right to left
- Return value -> RAX
- caller remove parameters


## Sample CLI
### gcc
- -m32: output Intel x86 object codes
- -masm=intel: Use Intel syntax instead of AT&T syntax
- -fno-stack-protector: disable stack protector


### objdump
- Disasm all section
```=sh
objdump -M intel -D [program name]
```

### gdb
- Debug program
```=sh
gdb [program]
```
- Run and stop at start
```=sh
gdb-peda$ starti
```
- Set break point at address
```=sh
gdb-peda$ b *0x40200f
```
- Continue
```=sh
gdb-peda$ cont
```
- Step into
```=sh
gdb-peda$ si
```
- Next instruction
```=sh
gdb-peda$ ni
```



### compile assembly
- static library
```=sh
yasm -f elf64 -DYASM -D__x86_64__ -DPIC libasm64.asm -o libasm64.o
ar rc libasm64.a libasm64.o
```
- program
```=sh
yasm -f elf64 -DYASM -D__x86_64__ -DPIC addsub.asm -o addsub.o
ld -m elf_x86_64 -o addsub addsub.o libasm64.a
```