【问题标题】:AT&T assembly + C functions. Using Scanf for string inputAT&T 汇编 + C 函数。使用 Scanf 进行字符串输入
【发布时间】: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。这整个问题只是一个错字吗?
  • 我太瞎了。好的去看看
  • 另一个问题是调用约定。您的编译器不一定通过堆栈(或至少不是全部)将参数传递给函数(包括可变参数)。

标签: c function assembly scanf


【解决方案1】:

在 GNU 汇编器中,.size 指令指定符号的大小。这仅用于非正式目的,对程序没有任何影响。最重要的是,它没有指定缓冲区或变量的大小或其他任何内容。

在 GNU 汇编器中,没有可变大小或类似的概念。要创建所需长度的缓冲区,请组合所需数量的空白字节并在前面添加标签,如下所示:

buffer: .space 100

.space 指令将给定数量的 NUL 字节组装到对象中。或者,您应该随后为buffer 设置符号大小,以便nm -S 的输出有意义:

.size buffer, 100

忽略这一点不会对您造成伤害,但 nm -S 不会显示符号的大小数据,这样做可能会降低某些调试实用程序的效率。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 2021-07-08
    相关资源
    最近更新 更多