IDIV in assembly isn't giving me the wanted re

2019-09-12 08:45发布

问题:

I'm trying to do a simple division:

mov ebx, 10
mov eax, 1111111111        ;(10 times)
mov edx, 0
idiv  bx

Supposedly I want to get the following results:

edx = 1

eax = 111111111 (9 times)

But the results I'm getting are:

edx = 7

eax = 1111098720

Does anyone know what the problem might be?

Thanks

回答1:

mov ebx, 10
mov eax, 1111111111        ;(10 times)
mov edx, 0
idiv  bx

What immediately strikes me is that you setup all registers for 32-bit operation but then perform a 16-bit division.
Changing it to the following will give the desired results:

mov  ebx, 10
mov  eax, 1111111111
cdq
idiv ebx


标签: assembly nasm