1
mov rax, rdi
mov rbx, [_j]
idiv    rbx

There was a dividend in RDI register and a divisor in the BSS section variable j. To get the calculation result of RDI % j, I wrote the code like the upper example while expecting there would be the remainder in RDX.

However, there was only 0x0 value in RDX register when RDI was having 5 and j was having 2. It seems that the code is not conducting the division calculation as I expected. Can you let me know which point I'm misunderstanding in the division in Assembly?

(My environment: Ubuntu 22.04, 64-bit, WSL2, Windows 11, Intel CPU 64bit/NASM v2.15.05)

KnightChaser
  • 143
  • 1
  • 9
  • You should initialise the `rdx` register before a single-operand `idiv` with a 64-bit operand, but that doesn't solve your question. – ecm Jul 16 '23 at 18:02
  • 1
    As ecm said, you should do `cqo` before `idiv` to [sign-extend RAX into RDX:RAX first](https://stackoverflow.com/questions/36464879/when-and-why-do-we-sign-extend-and-use-cdq-with-mul-div). Are you sure you single-stepped *past* the `idiv` so it actually executed? If you did, and your debugger showed RDX:RAX = 0:5 and RBX=2 before `idiv rbx`, and didn't show RDX=1 afterward, either your debugger is broken or (not plausible) your CPU is broken. – Peter Cordes Jul 16 '23 at 18:06
  • @ecm Thanks, I'll try to fix my codes with your advice. (Also thanks to Peter Cordes. But unfortunately, I can't mention only one person per comment. So I just skipped.) – KnightChaser Jul 16 '23 at 18:57
  • This needs a [mcve] to show how you populated `rdi`, how you defined `j`, and how you are inspecting the result. – Nate Eldredge Jul 16 '23 at 21:10

0 Answers0