【发布时间】:2018-09-26 09:30:33
【问题描述】:
我正在尝试在汇编中使用 scanf 来获取输入。 据我所知,我必须以相反的顺序推送函数的堆栈参数,然后调用函数。它适用于 printf 函数,但使用 scanf 和输入位置不太正确。 Scanf 应该有 2 个参数。第一个是输入类型(字符串、整数、字符等),第二个是放置它的地址。
scanf(„%s” , buffer)
我认为是我们的目标。 我的代码:
.data
name: .ascii "What is your name?\n"
name2: .ascii "Your name is:"
formatScanf: .ascii "%s"
.bss
buffer: .size 100 #100 bytes for string input
.text
.globl main
main:
#Printing question #works fine
pushl $name
call printf
#Get answers
push $buffer #2nd argument for scanf
push $formatScanf #1st argument of scanf
call scanf
#Exiting
pushl $0
call exit
错误信息:
lab3.s: Assembler messages:
lab3.s:8: Error: expected comma after name `' in .size directive
作为编译器,我使用 gcc 和:“gcc -m32 Program.s -o run”命令具有 32 位处理器工作类型,并自动链接 C 库。
它有什么问题? 我应该如何在 asm 中使用 scanf?
编辑: 我应该使用 .space 而不是 .size 或 .size 缓冲区,100 现在可以编译了。
编辑 2: 使用 SCANF C 函数完成代码
#printf proba
.data
name2: .string "Your name is: %s "
formatScanf: .string "%s"
name: .string "What is your name?\n"
.bss
buffer: .space 100
.text
.globl main
main:
#Printing question #works fine
pushl $name
call printf
#Get answers
push $buffer #2nd argument for scanf
push $formatScanf #1st argument of scanf
call scanf
push $buffer
push $name2
call printf
#Exiting
pushl $0
call exit
【问题讨论】:
-
.size指令并没有像您认为的那样做。有关详细信息,请参阅汇编器手册。 -
好吧,我想要 100 个字节的 0 来写一个字符串。 ".space size , fill 该指令发出size字节,每个值填充。size和fill都是绝对表达式。如果省略逗号和fill,则假定fill为零。这与'.skip'相同。 "
-
是的,那是正确的。但是,您写的是
.size而不是.space。这整个问题只是一个错字吗? -
我太瞎了。好的去看看
-
另一个问题是调用约定。您的编译器不一定通过堆栈(或至少不是全部)将参数传递给函数(包括可变参数)。