6
rinn7e
7y

Need help. Explain why assembly code is wrong.
C code:
long f(long n) {return n + 1} ;

Assembly:
f:
movq rdi, rbp
addq $1, rbp
movq rbp, rax
ret

Comments
  • 0
    Don't assembly instructions typically have the destination register on the left? Example:
    movq foo, bar
    would move from bar into foo.
    Looks like you've got your operands the wrong way around.
  • 0
    @fbomb ah; good to know, thank you!
  • 2
    Www.Google.com
    Www.stackoverflow.com
  • 0
    If you mean it cant compile, try add % for registers.

    If you mean the style is wrong, it is becoz you should use a stack frame for function http://stackoverflow.com/questions/...
  • 3
    @BikerMouse awesome sites.
    Never seen these before. Will use.
  • 1
    You should not use rpb since is the base pointer of the stack frame. If you use it you need to restore it before the ret instruction. Either do
    movq %rdi, %rax
    addq $1, %rax
    ret

    Or
    push %rbp
    movq %rdi, %rbp
    addq $1, %rbp
    movq %rbp, %rax
    pop %rbp
    ret
Add Comment