尝试在 x86 中添加两个整数,但是当我添加时,我得到的是垃圾而不是值。我究竟做错了什么?

Trying to add two ints,in x86, however when i add I get garbage instead of a value. what am I doing wrong?

section .rodata
ask1 db "Enter an integer: ", 0
ask2 db "Enter another Int: ",0
num_format db "%ld",0

section .text
global main
;c-style stuff
extern printf, scanf


main:
    ;getting it ready
    push rbp
    mov  rbp, rsp
    sub  rsp, 16
    push rbx
    push r12
    push r13
    push r14
    push r15
    pushfq

    ;promt user for first int
    mov  rdi, dword ask1
    xor  rax, rax
    call printf
    ;read first int
    lea  rsi, [rbp-8]
    ;initialize location of first int
    mov  rdi, dword num_format
    xor  rax, rax
    call scanf

    ;read second number
    mov  rdi, dword ask2
    xor  rax, rax
    call printf
    ;read second #
    lea  rsi, [rbp-16]
    mov  rdi, dword num_format
    xor  rax, rax
    call scanf
    ; add two numbers together - put sum in RCX
    xor   rbx, rbx      ; effectively zero out rbx 
    xor   rcx, rbx      ; effectively zero out rcx 
    mov   rcx, [rbp-16] ; load rcx with value 16 bytes from base ptr 
    mov   rbx, [rbp-8]  ; load rbx with value 8 bytes from base ptr 
    add   rcx, rbx      ; add num1 + num2 - store in rcx 

    jmp exit

exit:
    call printf
    popfq
    pop r15 
    pop r14 
    pop r13 
    pop r12 
    pop rbx 
    add rsp, 16
    leave
    ret 

你的最后一个 call printf 需要参数(EAX、格式字符串...)。

.rodata中添加一行:

printf_format db "%lld",0        ; lld: 64-bit integer 

并改变

...
exit:
    call printf
...

...
exit:
    mov rsi, rcx
    mov rdi, printf_format
    xor eax, eax
    call printf
...

顺便说一句:您不需要在主程序末尾的一堆 popadd rsp...。一个简单的leave就足以调整堆栈。