0
votes

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 
1
The .size directive does not do what you think it does. Refer to the assembler manual for details.fuz
Well, i wanted 100 bytes of 0 to write there a string. ".space size , fill This directive emits size bytes, each of value fill. Both size and fill are absolute expressions. If the comma and fill are omitted, fill is assumed to be zero. This is the same as ‘.skip’."Fenrir
Yes, that would be correct. However, you wrote .size instead of .space. Is this whole question just a typo?fuz
I'm so blind. Ok gonna check it outFenrir
Another problem is the calling convention. Your compiler does not necessarily pass the arguments to the function (including the variadic ones) via the stack (or at least not all of them).0___________

1 Answers

4
votes

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.