Linux的英特尔64位大会师英特尔、Linux

2023-09-08 01:02:07 作者:余温

我挣扎着理解为什么我司不工作,下面是我目前的code,它只是接受两个个位数,并试图将它们:

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的比值。我在想什么吗?任何帮助将大大AP preciated。

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 \ n和2 \ N。然后你减去'0',但是你离开了'\ N',让你的操作数将 0×08的0x0A 0×02的0x0A ,这是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\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.