0
我正在嘗試使用scanf
和printf
來編寫一個簡單的程序,但它並未正確存儲我的值。nasm彙編程序上的scanf
extern printf
extern scanf
SECTION .data
str1: db "Enter a number: ",0,10
str2: db "your value is %d, squared = %d",0,10
fmt1: db "%d",0
location: dw 0h
SECTION .bss
input1: resw 1
SECTION .text
global main
main:
push ebp
mov ebp, esp
push str1
call printf
add esp, 4
push location
push fmt1
call scanf
mov ebx, eax ;ebx holds input
mul eax ;eax holds input*input
push eax
push ebx
push dword str2
call printf
add esp, 12
mov esp, ebp
pop ebp
mov eax,0
ret
由於某些原因,當我運行程序時,無論輸入什麼數字,程序都會爲兩個輸入打印1。
我使用NASM,用gcc鏈接
此外,調用約定強制應該保留'ebx'。您可能想使用'ecx'來代替。此外,在調用scanf後還有一個'add esp,8',但在這種情況下這沒有害處,因爲你從'ebp'恢復堆棧指針。 – Jester 2014-11-03 19:18:49