I am writing a function in assembly which essentially pushes args to the stack, then creates a stack frame (ie saving previous and moving the stack base pointer to the value of the stack pointer). I then try to access my argument by offsetting the base pointer by 4 + 2 (4 bytes being length of memory address, 2 being length of arg I want).
Here's my program (between the lines is the memory stuff):
section .data
txt dw '25'
section .text
global _start
_exit:
mov rax, 60
mov rdi, 0
syscall
_print_arg:
;; function_initialisation
push rbp ;; save old stackbase value
mov rbp, rsp ;; set new stack base from tail of last value added to stack
;; actual function
mov rax, 1
mov rdi, 1
;;________________________
lea rsi, [rbp + 4 + 2] ;; access stackbase, skip return address (4 bytes long) and go to start of our param (which is 2 bytes long / word)
;;¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
mov rdx, 2
syscall
;; function_finalisation
mov rsp, rbp ;; set stack pointer as this frames stack base (ie set ebp to tail of old function)
pop rbp ;; set stack base pointer as original base of caller
ret ;; return to last value stored on stack, which at this point is the implicitly pushed return address
_start:
push word [txt] ;; push only arg to stack, word
call _print_arg ;; implicitly push return address, call function
pop rax ;; just a way to pop old value off the stack
jmp _exit ;; exit routine, just a goto
I've tried directly printing the variable I push to stack in the first place, which works, so I know it's not a content-that-cannot-be-printed-issue. My guess is that my understanding of the stack and manipulating the pointer registers are fundamentally flawed.