Document x86 assembly examples
This commit is contained in:
@@ -3,10 +3,14 @@
|
|||||||
|
|
||||||
_start:
|
_start:
|
||||||
# Store system call 1 in the EAX register.
|
# Store system call 1 in the EAX register.
|
||||||
|
# mov = instruction
|
||||||
|
# $1, %eax = operands
|
||||||
|
# %eax = register
|
||||||
mov $1, %eax
|
mov $1, %eax
|
||||||
|
|
||||||
# Store the exit code 0 in the EBX register.
|
# Store the exit code 0 in the EBX register.
|
||||||
mov $0, %ebx
|
mov $0, %ebx
|
||||||
|
|
||||||
# Call interrupt handler 0x80 (128) -- on Linux, this is the kernel.
|
# Call interrupt handler 0x80 (128) -- on Linux, this is the kernel.
|
||||||
|
# Kernel will take over and see 1 in %eax, 0 in %ebx.
|
||||||
int $0x80
|
int $0x80
|
||||||
|
|||||||
@@ -14,18 +14,28 @@ _start:
|
|||||||
# square expects an integer on the stack, 16 bytes down from RBP.
|
# square expects an integer on the stack, 16 bytes down from RBP.
|
||||||
# The squared value is returned on %rax.
|
# The squared value is returned on %rax.
|
||||||
square:
|
square:
|
||||||
# Function Epilogue --> {
|
# Function Prologue --> {
|
||||||
push %rbp
|
push %rbp
|
||||||
mov %rsp, %rbp
|
mov %rsp, %rbp
|
||||||
|
|
||||||
# Retrieve the parameter from the stack.
|
# Retrieve the parameter from the stack.
|
||||||
# 8 bytes down from RSP is the return address from square.
|
# 8 bytes down from RSP is the return address from square.
|
||||||
# 8 bytes down from that is the parameter to multiple.
|
# 8 bytes down from that is the parameter to multiply.
|
||||||
mov 16(%rbp), %rax
|
mov 16(%rbp), %rax
|
||||||
mul %rax
|
mul %rax
|
||||||
|
|
||||||
# Function Prologue --> }
|
# Function Epilogue --> }
|
||||||
mov %rbp, %rsp
|
mov %rbp, %rsp
|
||||||
pop %rbp
|
pop %rbp
|
||||||
# ret pops and return to address on stack.
|
# ret pops and returns to address on stack.
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
# |----------------+-----------+----------------|
|
||||||
|
# | Address | Data | Stack Pointers |
|
||||||
|
# |----------------+-----------+----------------|
|
||||||
|
# | 0x7fffffffe8f8 | | |
|
||||||
|
# | 0x7fffffffe900 | 0x0 (rbp) | |
|
||||||
|
# | 0x7fffffffe908 | 0x401002 | |
|
||||||
|
# | 0x7fffffffe910 | 3 | ←rsp |
|
||||||
|
# |----------------+-----------+----------------|
|
||||||
|
# ←rbp
|
||||||
|
|||||||
Reference in New Issue
Block a user