I am trying to figure out how the imul and idiv instructions of the 8086 microprocessor work.
I know this: 1. mul and div are multiplications and division for unsigned numbers 2. imul and idiv, are also multiplications and divisions but for signed numbers
I searched all the web, and what I just wrote above, that's the only info that I've found, but written in different ways.
I have this:
mov AX, 0FFCEh
idiv AH
Because ah it's a byte, AL=AX/AH (the result) and AH=remainder
After the instruction I get AX=0032h, obviously 0 remainder, and the result 32. Can somebody explain how it got to this result ? I need to know how to explain how the instruction works (bit by bit).
Same goes with the imul instruction.
I have:
mov AX, 0FF10h
imul AL
Because AL is a byte, the result of the multiplication will be saved into AX. After the execution of the imul instruction AX=0100h, why isn't it F100h ?
I don't how the CPU actually does mul, div, imul and idiv. If somebody could illustrate for me the differences, would really appreciate it.
Thanks!
mov AX, FFCE
idiv AH
AX=FFCE
AH=FF
definition of negative is that their highest ranked bit equals 1, so FFCE is negative,
because 1111 1111 1100 1110
and processor cannot work with negatives, we need to have positive, that means we negate this number (or processor does this based on highest ranked bit automaticallly)
NEG FFCE
or
1111 1111 1100 1110 =>
=> 0000 0000 0011 0001 +1 =>
=> 0000 0000 0011 0010 =>
=> 0032h
then next - AH register is FF - also negative and we need positive version, we negate
1111 1111 =>
=> 0000 0000 +1 =>
=> 0000 0001 =>
=> 01h
then when all numbers are positive, we calculate division
32h div 1h, 0h remainder => AL, 32h result => AH
both are negative that means that result is positive and needs no more conversions.
mov AX, FF10
imul AL
AL=10h
imul/mul uses AL when argument is 8bit (which it is)
so imul AL is same as
AL * AL => 10h * 10h => 0100h = AX
mov AX, FF10
imul AH
AH=FF
AL=10h
so now we have AL * AH => AX
AL = 10h
AH = FF which is negative, we need positive, we negate and get => 01h
multiplication of positives
10h * 01h => 0010h
but as only one of them was negative, we have to negate result, do not lose starting zeros, or your answer will be incorrect
0010h => FFEF +1 => FFF0 = AX