Linux Intel 64bit Assembly Division

倖福魔咒の 提交于 2019-12-08 10:19:14

问题


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.


回答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\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.



来源:https://stackoverflow.com/questions/32139441/linux-intel-64bit-assembly-division

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!