asm プログラムで c 関数 atof() を動作させるのに問題があります。私はキーボードから 4 つの数字を読み込もうとしており、最終的にそれらの平均を出力しようとしています。ただし、それを行う前に、数値を浮動小数点数に変換する必要があります。「合計」変数を正常に機能させることに固執しています。複数の場所で atof を呼び出してみましたが、役に立ちませんでした。
これは x86 NASM プログラムです
; 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
編集:改訂時に、これらのコードブロックを使用して、入力された値をそれぞれの数値に変換することができました
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
サンプル実行:
./prg2b
enter a test score.
1
enter a test score.
1
enter a test score.
1
enter a test score.
1
41111
私のコードへのこの追加は、atof() 呼び出しに関する私の問題を取り除きますが、数字が 1 桁で、合計が <10 の場合にのみ成功します
atof を適切に使用する方法、または scanf を使用するプログラムで浮動小数点数に適切に変換する方法について、誰かがヒントを与えることができれば、それは大歓迎です。私はx86 asmに非常に慣れていません(読んだ:2週間の学習)。これは、UNIX システムのターミナルでコンパイルされます。