【发布时间】:2015-07-20 17:54:34
【问题描述】:
在下文中,我试图获取用户的选择并使用它来调用其他函数。我将选择压入堆栈,然后压入格式行,然后调用 scanf,但我似乎无法使用输入的内容。
;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
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 to enter an integer from 0 to 20',0x0D,0x0a,'Enter F to display the first N+1 numbers (beginning with zero) on the console',0x0D,0x0a,'Enter X to quit the program',0x0D,0x0a,0
choicemsg: db "Your Choice: ",0
LC5: db "%s",0
enterintmsg: db "Enter and integer 0-20: ",0
enternummsg: db 'Enter a valid number between 0 and 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 to zero section',0x0D,
value: dw 0
choice: dw 0
section .text
main:
menu:
push rbp
mov rbp, rsp
sub rsp, 16
mov edi, menuprompt
call puts ;display menu
mov edi,choicemsg
mov eax, 0
call printf ;display "Your choice: "
;call getn
push choice
push LC5 ;string format
call scanf ;stores input in choice
;GetLInt [choice]
mov ebx, choice
cmp ebx, 78
je correct
correct:
mov edi, ebx
mov eax,0
call printf
(编者注:section.data 只是一个类似于foo.bar: 的标签声明,.code 也是如此。可能你想要section .data 和section .text 而不是将所有内容都放在只读的.text 部分,因为您希望 scanf 将结果存储在那里。我为您解决了这个问题,因为这个旧问题和答案与这些错误无关。)
【问题讨论】:
-
你可能想要
mov bl, [choice]- 只有一个字节。 -
请参阅stackoverflow.com/tags/x86/info 了解有关将 gdb 与程序集结合使用的信息,以防您没有良好的调试设置。
标签: assembly nasm x86-64 calling-convention