This question comes out of me writing a toy compiler, but I don't think that it's necesary to show the compiler side of this question - this is strictly an issue of me not being very experienced with assembly.
I am working on implementing functions into my language. I have generated the following assembly (AT&T) which should print 6
. The program segfaults at call *%rax
, which I assume means that I'm not saving the address of my function correctly. What should happen is that the function f0
should read the arguments from the stack, multiply them, and leave the result in %rax
. The print
function is just a wrapper around the printf
function from C to print %rax
.
I'm running Ubuntu 20.04, and the code was compiled with gcc test.S -o test.out -no-pie
, in case it matters.
.data
numfmt:
.asciz "%d\n"
.text
print:
push %rbx
mov %rax, %rsi
mov $numfmt, %rdi
xor %rax, %rax
call printf
pop %rbx
ret
.globl main
main:
push %rbp
mov %rsp, %rbp
jmp s0
f0:
push %rbp
mov %rsp, %rbp
mov 16(%rbp), %rax
push %rax
mov 8(%rbp), %rax
pop %rcx
imul %rcx, %rax
mov %rbp, %rsp
pop %rbp
ret
s0:
mov $f0, %rax
push %rax
mov $1, %rax
push %rax
mov $2, %rax
push %rax
mov -8(%rbp), %rax
call *%rax
add 16, %rsp
call print
mov $0, %rax
mov %rbp, %rsp
pop %rbp
ret