我正在Linux 上使用NASM创建一个汇编语言程序。我正在尝试对 4 个数字进行加权平均,其中输入的第 4 个数字的权重是其他数字的两倍。如果我使用数字 30、40、50、60,我将加权平均值计算为 (30+40+50+60+60)/5。我的代码是:
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
segment .data
msg1 db "Enter exam grade 1: ", 0xA,0xD
len1 equ $- msg1
msg2 db "Enter exam grade 2: ", 0xA,0xD
len2 equ $- msg2
msg3 db "Enter exam grade 3: ", 0xA,0xD
len3 equ $- msg3
msg4 db "Enter final exam grade (worth double: ", 0xA,0xD
len4 equ $- msg4
msg5 db "The sum is: "
len5 equ $- msg5
segment .bss
num1 resb 3
num2 resb 3
num3 resb 3
num4 resb 3
res resb 4
section .text
global main
main:
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg1
mov edx, len1
int 0x80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, num1
mov edx, 3
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg2
mov edx, len2
int 0x80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, num2
mov edx, 4
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg3
mov edx, len3
int 0x80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, num3
mov edx, 3
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg4
mov edx, len4
int 0x80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, num4
mov edx, 3
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg5
mov edx, len5
int 0x80
mov eax, [num1] ; move num1 to register eax
sub eax, '0' ; sub '0' to convert eax bl from ASCII to decimal
mov ebx, [num2] ; move num2 to register ebx
sub ebx, '0' ; sub '0' to convert ebx from ASCII to decimal
mov ecx, [num3] ; move num3 to register ecx
sub ecx, '0' ; sub '0' to convert ecx from ASCII to decimal
mov edx, [num4] ; move num4 to register edx
sub edx, '0' ; sub '0' to convert edx from ASCII to decimal
add eax, ebx ; add ebx to eax
add eax, ecx ; add ecx to eax
add eax, edx ; add edx to eax
add eax, edx ; add edx to eax again
mov bl, '5' ; sub '0' to convert bl from ASCII to decimal
sub bl, '0' ; make bl a decimal
div bl ; divide by bl
add eax, '0' ; add '0' to convert the sum from decimal to ASCII
mov [res], eax ; storing the sum in memory location res
; print the sum
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, res
mov edx, 3
int 0x80
exit:
mov eax, SYS_EXIT
xor ebx, ebx
int 0x80
如果我输入这些数字,我会产生这个异常:
Enter exam grade 1:
30
Enter exam grade 2:
40
Enter exam grade 3:
50
Enter final exam grade (worth double:
60
The sum is: Floating point exception
是什么导致我的程序显示浮点异常(SIGFPE),我该如何解决?