Assembly
8086 ASM
Sources:
Registers: Storage
- 16 bits wide
- Named: AX, BX, CX, ...
Instruction decoding: Converting mnemonics and operands (assembly) into machine code - MOV AX, BX = Move (copy) data from BX to AX
The Stack
Block of memory that stores state of registers.
- The address of this block is kept in the register SP -> Stack Pointer
; initialize stack
mov [sp], 40_000 ; number is obviously an example
; push ax
sub sp, 2 ; move stack pointer by size of moved data. 2 bytes in this case.
mov [sp], ax ; move data in register ax to stack
; pop ax
mov ax, [sp] ; move data into register from stack
add sp, 2 ; move stack pointer by size of moved data
; call n ; pushes value of IP register into the stack, and changes IP to n
push ip ; illegal, as it "moves" IP, that's why call exists!
; ret ; takes the top value of the stack and sets it as the IP register
pop ip ; illegal, as it "moves" IP, that's why ret exists!
Either the caller or the function can move the data into the stack, but it has to be defined. This is the "Calling convention" or the Applicaton Binary Interface (ABI)
From 8086 to x64
|----------- QUAD -------------|-----DWORD-----|--WORD-|--BYTE-|----
/ R \ / E \ / H \ / L \
|------------------------------|---------------|-------|-------|
| RAX | EAX | AH | AL | AX
|------------------------------|---------------|-------|-------|
| | | BH | BL | BX
|------------------------------|---------------|-------|-------|
| | | CH | CL | CX
|------------------------------|---------------|-------|-------|
| | | DH | DL | DX
|------------------------------|---------------|-------|-------|
| | | | SP
|------------------------------|---------------|-------|-------|
| | | | BP
|------------------------------|---------------|-------|-------|
| | | | SI
|------------------------------|---------------|-------|-------|
| | | | DI
################################ Registers introduced in 64
################################ The high bits of a word
################################ size are not addressable
| | R8D | R8W | R8B | R8
|------------------------------|---------------|-------|-------|
| | " | " " | R9
|------------------------------|---------------|-------|-------|
| | " | " " | R10
|------------------------------|---------------|-------|-------|
| | " | " " | R11
|------------------------------|---------------|-------|-------|
| | " | " " | R12
|------------------------------|---------------|-------|-------|
| | " | " " | R13
|------------------------------|---------------|-------|-------|
| | " | " " | R14
|------------------------------|---------------|-------|-------|
| | " | " " | R15
|------------------------------|---------------|-------|-------|