2 years ago

#16918

test-img

Ammar

GNU Assembly scan and print float

I am new to Assembly and have been trying to write a program to read a float, store it and then print it, the problem is when I print it with %f flag it prints 0.0 although the scan flag is also %f and the type is .float

.section .data        # memory variables
format: .asciz "%f"    # string terminated by 0 that will be used for scanf parameter
n: .float 0.0             # the variable n which we will get from user using scanf and will be printed by printf

.section .text        # instructions
.globl _main          # make _main accessible from external
_main:                # the label indicating the start of the program
   pushl $n           # push to stack the second parameter to scanf (the address of the integer variable n) the char "l" in pushl means 32-bits address
   pushl $format       # push to stack the first parameter to scanf
   call _scanf        # call scanf, it will use the two parameters on the top of the stack in the reverse order
   add $8, %esp       # pop the above two parameters from the stack (the esp register keeps track of the stack top, 8=2*4 bytes popped as param was 4 bytes)

   pushl n
   pushl $format
   call _printf
   add $8, %esp
   ret

Can someone tell me what is the problem here? Thanks!

assembly

gnu

att

0 Answers

Your Answer

Accepted video resources