简体   繁体   中英

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

I'm trying to use scanf in assembly to get input. 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. Scanf should have 2 arguments. 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.

What is wrong with it? How should i use scanf in asm?

EDIT: I should have used use .space not .size or .size buffer, 100 It compiles now.

EDIT 2: COMPLETE CODE WITH USING SCANF C FUNCTION

#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. 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. 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. Optionally, you should afterwards set a symbol size for buffer so the output of nm -S is meaningful:

.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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