我无法尝试获得每加仑程序的简单里程,输入浮点数,划分它们并打印结果。我还没有在NASM中使用花车,所以我不确定它是不是正确读取数字还是没有将它们分开。一旦程序划分两个数字,我就会出现分段错误。有什么想法吗?
extern printf, scanf, puts
SECTION .text
global main
main:
push rbp
mov rbp, rsp ; for correct debugging
xor eax, eax
;welcome message
mov rdi, welcome
call puts
;prompt
mov rdi, miles_prompt
call puts
;input
mov rdi, fmt
movq xmm0, [miles]
mov al, 1 ;1 float argument
mov rax, 1 ;no other arguments...is this right?
call scanf
;prompt
mov rdi, gallons_prompt
call puts
;input
mov rdi, fmt
movq xmm0, [gallons]
mov al, 1
mov rax, 1
call scanf
;divide floats
movq xmm0, [miles]
movq xmm1, [gallons]
divsd xmm0, xmm1
;print answer
mov rdi, answer
mov rsi, rax
mov rax, 1
call printf
;exit(0)
mov rsp, rbp
pop rbp
ret
SECTION .data
welcome: db "*** MPG Calc ***", 10, 0
miles_prompt: db "How many miles did you travel: ", 0
gallons_prompt: db "How many gallons of gas did you use: ", 0
answer: db "MPG: %lf", 0
fmt: db "%lf", 0
miles: dq 0, 0
gallons: dq 0, 0
这是我的终端:
$ nasm -f elf64 -s mpg.asm
$ g++ mpg.o -o ./mpg
$ ./mpg
*** MPG Calc ***
How many miles did you travel:
6
How many gallons of gas did you use:
4
Segmentation fault (core dumped)