简体   繁体   English

AT&T汇编+ C函数。 使用Scanf进行字符串输入

[英]AT&T assembly + C functions. Using Scanf for string input

I'm trying to use scanf in assembly to get input. 我试图在组装中使用scanf来获取输入。 As I know I have to push on stack arguments of functions in reverse order and then call function. 据我所知,我必须以相反的顺序推入函数的堆栈参数,然后调用函数。 It works fine with printf function but something is not quite right with scanf and place for input. 它可以与printf函数一起正常工作,但是scanf和输入位置不正确。 Scanf should have 2 arguments. Scanf应该有2个参数。 1st is type of input (string,int, char etc) and 2nd is adress where to put it. 第一个是输入的类型(字符串,整数,字符等),第二个是输入地址。

scanf(„%s” , buffer)

Is our goal i think. 我认为是我们的目标。 My code: 我的代码:

.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 

Error message: 错误信息:

lab3.s: Assembler messages:
lab3.s:8: Error: expected comma after name `' in .size directive

As compiler i'm using gcc with : " gcc -m32 Program.s -o run" command to have 32bit procesor work type, and to have C libuary linked automaticly. 作为编译器,我将gcc与以下命令配合使用:“ gcc -m32 Program.s -o run”命令具有32位处理器工作类型,并具有自动链接的C库。

What is wrong with it? 怎么了 How should i use scanf in asm? 我应该如何在asm中使用scanf?

EDIT: I should have used use .space not .size or .size buffer, 100 It compiles now. 编辑:我应该使用.space而不是.size或.size缓冲区,它现在可以编译100。

EDIT 2: COMPLETE CODE WITH USING SCANF C FUNCTION 编辑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 

In the GNU assembler, the .size directive specifies the size of a symbol. 在GNU汇编器中, .size指令指定符号的大小。 This is merely for informal purposes and has no effect whatsoever on the program. 这仅出于非正式目的,对程序没有任何影响。 Most importantly, it does not specify the size of a buffer or variable or whatever. 最重要的是,它没有指定缓冲区或变量的大小。

In the GNU assembler, there is no notion of variable size or similar. 在GNU汇编器中,没有可变大小或类似大小的概念。 To create a buffer of desired length, assemble the desired number of blank bytes and tack a label in front, like this: 要创建所需长度的缓冲区,请组装所需数量的空白字节并在前面添加标签,如下所示:

buffer: .space 100

The .space directive assembles the given number of NUL bytes into the object. .space指令将给定数量的NUL字节组合到对象中。 Optionally, you should afterwards set a symbol size for buffer so the output of nm -S is meaningful: (可选)您之后应为buffer设置符号大小,以便nm -S的输出有意义:

.size buffer, 100

Leaving this out won't hurt you, but then nm -S won't show size data for your symbol and doing so might make certain debug utilities less effective. 忽略这一点不会对您造成伤害,但是nm -S不会显示符号的大小数据,这样做可能会使某些调试实用程序的效果降低。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM