Reputation: 357
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
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.
Upvotes: 0
Views: 599
Reputation: 6145
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\n" and "2\n". You then subtract '0', but you leave the '\n', so your operands will be 0x08 0x0A
and 0x02 0x0A
, which are 2568 and 2562. And 2568 / 2562 = 1.
Upvotes: 2