Not getting correct input form either scanf or printf in Nasm assembly program with menu -
in following, i'm trying make user enters n , prompte enter integer. got part working. i've spent few hours trying next part working. after enter integer, goes menu. debugging purposes, if enter f should display number entered. however, when entering n, 3, f, displays 21505.
;nasm -f elf64 fib.asm -o fib.o ;gcc -s -masm=intel fib.c -o fib.s ;./fib bits 64 global main extern puts extern printf extern scanf extern get_kb section.data errormsg: db 'invalid input. enter n,f, or x',0x0d,0x0a,0 numequalsmsg: db 'number equals: ' lc2: db "%d",0 menuprompt: db 0x0d,0x0a,'enter n enter integer 0 20',0x0d,0x0a,'enter f display first n+1 numbers (beginning zero) on console',0x0d,0x0a,'enter x quit program',0x0d,0x0a,0 choicemsg: db "your choice: ",0 lc5: db "%d",0 enterintmsg: db "enter , integer 0-20: ",0 enternummsg: db 'enter valid number between 0 , 20',0x0d,0x0a,0 lc8: db " , ",0 lc9: db 'success!',0x0d,0x0a,0 lc10: db 'in l10!',0x0d,0x0a,0 lc11: db 'in l12!',0x0d,0x0a,0 lc13: db 'in compare 0 section',0x0d,0 value: db 0 choice: db 1 .code main: ;function setup ;push rbp ;mov rbp, rsp ;sub rsp, 16 call menu menu: ;print menu mov edi, menuprompt call puts ;display menu mov edi,choicemsg ;mov eax, 0 ;call printf ;display "your choice:" call puts call get_kb mov bl, al cmp bl, 'n' ;n je read_int_new cmp bl, 'f' ;f je fib cmp bl, 'x' ;x je correct ;leave ;ret jmp menu ret correct: mov edi, lc9 mov eax,0 call printf jmp menu ;leave ;ret entered_n: call read_int_new ;jmp correct jmp menu ;leave ;jmp menu read_int: mov edi, enterintmsg mov eax,0 call printf ;mov rdi, [lc5] ;mov rsi, [value] ;xor eax,eax ;add esp,4 ;remove parameters ;push rsi ;push rdi ;push rbp ;mov rax,0 ;mov rdi, lc5 ;mov rsi, value push value push lc5 call scanf add esp, 8 ;mov eax, value push value push lc5 call printf add esp,8 ;pop rbp ;jmp correct ;mov cl, [value] ;jmp menu ;leave ret ;leave read_int_new: ;push rbp ;mov rbp, rsp ;sub rsp, 16 mov edi, enterintmsg ;display "enter integer 0-20: " mov eax, 0 call printf ;lea rax, [value] ;mov rsi, rax ;mov edi, lc5 ;mov eax, 0 ;call scanf ;get user input ;mov ebx, [value] push value push lc5 call scanf mov esi, [value] mov edi, lc9 ;test see if got here mov eax, 0 call printf ;test ebx, ebx ;compare 0 (eax-eax=0) ;js l9 ;mov edi, lc9 ;test see if got here ;mov eax, 0 ;call printf ;mov ebx, dword [rbp-4] ;cmp ebx, 20 ;jump if greater 20 ;jg l9 ;mov edi, lc9 ;test see if got here ;mov eax, 0 ;call printf ;mov ebx, dword [rbp-4] ;else, jump l10 ;mov edi, lc9 ;test see if got here ;mov eax, 0 ;call printf ;jmp l10 ;leave ;ret jmp menu fib: mov eax, esi mov edi, lc5 mov eax,0 ;mov eax, lc5 ;push [eax] ;push value ;push lc5 call printf jmp menu
code not compile due missing get_kb
. anyway, commented out code in read_int_new
correct 1 except mov ebx, [value]
. if change lc9
print number (use %d
) can see reading works.
if fix fib
replacing mov eax, esi
(which makes no sense whatsoever) mov esi, [value]
number correctly printed there too.
ps: learn use debugger.
Comments
Post a Comment