I am having some trouble getting the c function atof() to work in my asm program. I'm trying to read in 4 numbers from the keyboard, and ultimately print their average. Before i can do that, however, i need to convert the numbers to floats. I'm stuck on successfully getting my 'total' variable to work. I have tried calling atof in multiple spots to no avail.
This is a x86 NASM program
; nasm -f elf -l prg2.lst prg2.asm
; gcc -o prg2 prg2.o
; ./prg2
SECTION .DATA
prompt DB 'enter a test score.', 13,10
fmt DB "%s",0
fmtf DB "%f",0
SECTION .bss
test1 resb 1000 ;reserves variable names to
test2 resb 1000 ;put stuff in
test3 resb 1000
test4 resb 1000
total resb 1000
SECTION .code
extern printf
extern scanf
extern atof
global main
main:
push ebp
mov ebp, esp
push prompt
call printf
add esp, 4 ;prompt user
push test1 ;push test1 variable
push fmt
call scanf
add esp, 8 ;store test1 variable
push prompt
call printf
add esp, 4 ;prompt user
push test2 ;push test2 variable
push fmt
call scanf
add esp, 8 ;store test2 variable
push prompt
call printf
add esp, 4 ;prompt user
push test3 ;push test3 variable
push fmt
call scanf
add esp, 8 ;store test3 variable
push prompt
call printf
add esp, 4 ;prompt user
push test4 ;push test4 variable
push fmt
call scanf
add esp, 8 ;store test4 variable
mov eax,[test1]
add eax,[test2]
add eax,[test3]
add eax,[test4]
call atof
mov [total], eax
push total
call printf ;not printing what i want,
add esp,4 ;or printing anything at all
push test1 ;printing scores for verification
call printf
add esp, 4
push test2
call printf
add esp, 4
push test3
call printf
add esp, 4
push test4
call printf
add esp, 4
mov esp, ebp
pop ebp
ret
EDIT: upon revision, i was able to turn the inputted values in their respective numeric values using these code blocks
mov eax, 0 ;
add eax,[test1] ;put test1 value in eax
mov [total], eax
sub eax, '0'
add eax,[test2]
mov [total], eax
sub eax,'0'
add eax,[test3]
mov [total], eax
sub eax,'0'
add eax,[test4] ;
mov [total], eax
sub eax,'0'
push total
call printf
add esp, 4
Sample run through:
./prg2b
enter a test score.
1
enter a test score.
1
enter a test score.
1
enter a test score.
1
41111
this addition to my code gets rid of my problem with the atof() call, but it is only successful if the numbers are one digit and if total is <10
If anyone could give a hint as to how to properly using atof, or how to properly convert to floating point numbers in a program that uses scanf, it would be greatly appreciated. I'm very new (read: 2 weeks of learning) to x86 asm. This is compiled in the terminal on a UNIX system
You can define a C literal with escape sequences in NASM by using backticks. E.g.
atof
needs a memory address on the stack and returns the result in register ST(0) of the FPU. You have to convert every single string to a number before you can calculate with it.You don't need
atof
. You can letscanf
convert the inputted string with the format string " %lf".