【发布时间】:2018-01-30 21:35:55
【问题描述】:
我正在学习一些在 Linux 上使用 NASM 的基本算术。我需要使用变量 NUMBER1 和 NUMBER2 来划分两个数字。如果我输入实际值而不是变量,我的代码就可以工作。例如,如果我输入“6”而不是 NUMBER2 和“2”而不是 NUMBER1,程序会进行除法并给出 3 的答案。 使用变量运行代码会产生 FLOATING EXCEPTION (CORE DUMPED)。请解释如何正确使用此代码中的变量? 调试时,我发现问题出在 DIV 行。谢谢!
section .text
global main ;must be declared for using gcc
main: ;tell linker entry point
mov ax,[NUMBER2]
sub ax, '0'
mov bl, [NUMBER1]
div bl
add ax, '0'
mov [res], ax
mov ecx,msg
mov edx, len
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
nwln
mov ecx,res
mov edx, 1
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
NUMBER1: dw 2
NUMBER2: dw 6
msg db "The result is:", 0xA,0xD
len equ $- msg
segment .bss
res resb 1
【问题讨论】:
-
sub ax, '0'-- 为什么会出现在这里?您将原始 6 存储在数据段中,而不是 ascii 字符 6 -
我正在使用 NASM 文档中的这个示例,我相信这是因为剩余部分。
标签: linux math assembly nasm division