NASM 使用变量而不是实际值进行除法
NASM division using variables instead of actual values
我正在 Linux 上使用 NASM 学习一些基本算术。我需要使用变量 NUMBER1 和 NUMBER2 来划分两个数字。如果我输入实际值而不是变量,我的代码就可以工作。例如,如果我输入“6”而不是 NUMBER2,输入“2”而不是 NUMBER1,程序会除法并给出答案 3。
运行 带有变量的代码给出浮动异常(核心已转储)。请解释一下如何在这段代码中正确使用变量?
调试时,我发现问题出在 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
因为给定的示例应该处理数字的 ASCII 码,而不是数字本身。如果您输入 6
而不是 '6'
,6 - '0'
计算结果为 65494
(而不是 6
)。如果您尝试进一步除以 2
,处理器无法将商存储在 ax
寄存器的下半部分。
如果您不打算将结果输出到控制台,而只是尝试了解如何使用汇编程序对一个字节整数进行除法,请选择您最喜欢的调试器,在除法运算后放置断点并享受您的结果。
section .text
global main ;must be declared for using gcc
main: ;tell linker entry point
mov ax,[NUMBER2]
mov bl, [NUMBER1]
div bl
nop ; quotient at al, remainder at ah
; remove nop instruction if your code is larger than few lines
; place output code here
section .data
NUMBER2: dw 6 ; dw means two bytes
NUMBER1: db 2 ; db means one byte
segment .bss
res resb 1
我正在 Linux 上使用 NASM 学习一些基本算术。我需要使用变量 NUMBER1 和 NUMBER2 来划分两个数字。如果我输入实际值而不是变量,我的代码就可以工作。例如,如果我输入“6”而不是 NUMBER2,输入“2”而不是 NUMBER1,程序会除法并给出答案 3。 运行 带有变量的代码给出浮动异常(核心已转储)。请解释一下如何在这段代码中正确使用变量? 调试时,我发现问题出在 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
因为给定的示例应该处理数字的 ASCII 码,而不是数字本身。如果您输入 6
而不是 '6'
,6 - '0'
计算结果为 65494
(而不是 6
)。如果您尝试进一步除以 2
,处理器无法将商存储在 ax
寄存器的下半部分。
如果您不打算将结果输出到控制台,而只是尝试了解如何使用汇编程序对一个字节整数进行除法,请选择您最喜欢的调试器,在除法运算后放置断点并享受您的结果。
section .text
global main ;must be declared for using gcc
main: ;tell linker entry point
mov ax,[NUMBER2]
mov bl, [NUMBER1]
div bl
nop ; quotient at al, remainder at ah
; remove nop instruction if your code is larger than few lines
; place output code here
section .data
NUMBER2: dw 6 ; dw means two bytes
NUMBER1: db 2 ; db means one byte
segment .bss
res resb 1