Linux Intel 64bit 汇编分部 [英] Linux Intel 64bit Assembly Division
问题描述
我正在努力理解为什么我的除法不起作用,下面是我当前的代码,它只取两个个位数并试图将它们相除:
I am battling to understand why my division is not working, below is my current code, which simply takes in two single digits and attempts to divide them:
STDIN equ 0
SYS_READ equ 0
STDOUT equ 1
SYS_WRITE equ 1
segment .data
num1 dq 0
num2 dq 0
quot dq 0
rem dq 0
segment .text
global _start
_start:
mov rax, SYS_READ
mov rdi, STDIN
mov rsi, num1
mov rdx, 2
syscall
mov rax, SYS_READ
mov rdi, STDIN
mov rsi, num2
mov rdx, 2
syscall
mov rax, [num1]
sub rax, '0'
mov rbx, [num2]
sub rbx, '0'
xor rdx, rdx
div rbx
add rax, '0'
mov [quot], rax
mov [rem], rdx
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, quot
mov rdx, 1
syscall
mov rax, 60
xor rdi, rdi
syscall
现在据我所知,汇编程序将RDX:RAX除以操作数RBX.我只能假设这是问题所在,事实上我将 128 位值除以 64 位值.每当我输入诸如 8/2 或类似的东西时,我都会收到值 1 作为商.我在这里错过了什么?任何帮助将不胜感激.
Now as far as I understand when dividing the assembler will divide RDX:RAX by the operand RBX. I can only assume this is where the problem is coming in, the fact that I am dividing a 128bit value by a 64bit value. Whenever I enter something such as 8 / 2 or something similar, I receive the value 1 as the quotient. What am I missing here? Any help would be greatly appreciated.
推荐答案
您为操作数读取了 2 个字节,但似乎您忽略了第二个字节,而您不应该这样做.
假设您输入 8 和 2 各一行,您将看到8
"和2
".然后减去 '0',但保留 '
',因此您的操作数将是 0x08 0x0A
和 0x02 0x0A
,即 2568 和 2562.和 2568/2562 = 1.
You read 2 bytes for the operands, but it seems you ignore the 2nd, when you shouldn't.
Assuming you type 8 and 2 and one line each, you will read "8
" and "2
". You then subtract '0', but you leave the '
', so your operands will be 0x08 0x0A
and 0x02 0x0A
, which are 2568 and 2562. And 2568 / 2562 = 1.
这篇关于Linux Intel 64bit 汇编分部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!