UMBC CMSC 211 |
However, it is still a little more complex than before. First of all, it takes longer. There is also the problem with the size of numbers (even more so than addition and subtraction!) 99 times 99 is 9801. A byte times a byte can result in a word! Multiplying two n-digit numbers will probably result in a 2n-digit number. Division goes the other direction, but is even more difficult because we have to keep track of the remainder. Then comes the wrinkle of signs!
So when you multiply two sixteen bit numbers, expect the answer to require more bits! The 80x86 CPU requires special registers for this:
Operand Size | Multiplicand | Multiplier | Product |
---|---|---|---|
BYTE | AL | Reg or Memory | AX |
WORD | AX | Reg or Memory | AX (low) and DX (high) |
Division is similar:
Operand Size | Dividend | Divisor | Quotient | Remainder |
---|---|---|---|---|
BYTE | AX | Reg or Memory | AL | AH |
WORD | AX and DX | Reg or Memory | AX | DX |
You can not use a constant operand with multiplication or division. Use the mov instruction to put a constant into the appropriate register.
Did I mention something about the sign? There are different instructions based on whether or not it is a signed or unsigned operation. That leads us to the following:
Instruction | Description |
---|---|
mul | unsigned multiplication |
div | unsigned division |
imul | signed multiplication |
idiv | signed division |
OK, lets put it together:
mov ax, miles cwd ;ax is assumed idiv gallons mov mpg, ax
mov al, nrDays sub ah, ah ;don't sign extend because that ; would change the value mov bl, 7 div bl ;can't use a constant here! mov weeks, al mov days, ah ;the remainder is the portion ; of a week