0
我想在linux上編寫一個「猜數字」遊戲在nasm上, 我有一個問題,我不明白... 當scanf執行時,我的變量mysteriousNumber設置爲0,我不知道爲什麼... 有人可以解釋我我做錯了什麼? (對不起,我的英語技能)scanf將變量設置爲0 NASM
; GuessTheNumber
; mainasm : user have to guess the randomly generated number
;
extern printf
extern rand
extern srand
extern time
extern puts
extern scanf
SECTION .data
string_welcome: db "Welcome to the GuestTheNumber game",0xa,0x0
string_ask: db "Enter a number between 1 and 100",0xa,0x0
string_higher: db "The number is higher ",0xa,0x0
string_lower: db "The number is lower",0xa,0x0
string_win: db "YOU WIN",0xa,0x0
string_format: db "%d",0x0
enteredNumber: dw 999
mysteriousNumber: dw 0
SECTION .text ; code section
global main ; make label available to linker
main: ; standard gcc entry point
push ebp
mov ebp,esp
push string_welcome
call printf
add esp,0x4
push 1 ; numberMin
push 100 ; numberMax
call generateNumber ; generateNumber between nubmerMin to numberMax
add esp, 0x8
mov [mysteriousNumber],eax
mov edx , 0
loop:
push string_ask
call printf ; printf("Enter a number ")
add esp,0x4
push enteredNumber
push string_format
call scanf
add esp,0x8
mov ebx, DWORD [enteredNumber]
mov eax, DWORD [mysteriousNumber]
cmp eax,ebx
jb below
jg higher
jmp win
below:
push string_lower
call printf
add esp,0x4
jmp loop
higher:
push string_higher
call printf
add esp,0x4
jmp loop
win:
push string_win
call printf
end:
mov ebx,0 ; exit code, 0=normal
mov eax,1 ; exit command to kernel
int 0x80 ; interrupt 80 hex, call kernel
leave
ret
generateNumber:
push ebp ; prolog
mov ebp,esp
push 0x0
call time ; call time(NULL)
push eax ; set arg1 = time(NULL)
call srand ; call srand(time(NULL))
mov ebx,DWORD [ebp+0x8] ; ebx = numbermax
mov ecx,DWORD [ebp+0xc] ; ecx = numbermin
sub ebx,ecx ; ebx = numbermax - numbermin
inc ebx ; ebx = numbermax -numbermin + 1
call rand
xor edx,edx
div ebx ; edx = rand%ebx
inc edx ; edx =
mov eax,edx ; eax = MysteryNumber
leave
ret
哦,是的,當然,太感謝你了! – Neolex