首页 文章

AT&T组装C功能 . 使用Scanf进行字符串输入

提问于
浏览
0

我正在尝试在汇编中使用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

1 回答

  • 4

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

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

    buffer: .space 100
    

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

    .size buffer, 100
    

    抛弃它不会对您造成伤害,但是 nm -S 将不会显示符号的大小数据,这样做可能会使某些调试实用程序的效率降低 .

相关问题