Reputation: 143
I'm still new to assembly and I don't know many command codes in assembly yet. I want to do a division in a 16-bit register. I want to print its content. I know that I need to convert the content of the register into ASCII for printing but again, my problem is the division. Please help me.
For example, the content of cx is 2012 (integer). What should I do?
mov ax, cx
mov bx, 1000
idiv bx
The above code is wrong, right?
Upvotes: 4
Views: 8219
Reputation: 10580
Edit: I noticed that this is signed division idiv
, answer edited accordingly.
The above code is wrong in one aspect: ax
is not sign-extended to dx:ax
.
Just add cwd
(convert word to doubleword) before idiv bx
, and then it's correct. The quotient will be in ax
and the remainder will be in dx
.
Upvotes: 4
Reputation: 63935
Check out this reference (search for idiv)
The idiv instruction divides the contents of the 64 bit integer EDX:EAX (constructed by viewing EDX as the most significant four bytes and EAX as the least significant four bytes) by the specified operand value. The quotient result of the division is stored into EAX, while the remainder is placed in EDX. Syntax idiv idiv
Examples
idiv ebx — divide the contents of EDX:EAX by the contents of EBX. Place the quotient in EAX and the remainder in EDX. idiv DWORD PTR [var] — divide the contents of EDX:EAS by the 32-bit value stored at memory location var. Place the quotient in EAX and the remainder in EDX.
Of course, since you're using 16-bit, cut all of the specified bit-values in half and drop the E off of each register and it's the exact same
Upvotes: 4