| |||||||||
| |||||||||
The CBW and CWD instructions:
CBW and CWD are two instructions used to facilitate division of 8 and 16 bit signed numbers.
Since division requires a double-width dividend, CBW converts an 8-bit signed number
(in AL) to a word. The MSB of the AL register is duplicated into AH register.
Divide Overflow:Overfow occurs when the result of a division does not fit in a byte if the dividend is a word, and in a word if the dividend is a double word. Such an oberflow terminates the program with "Divide Overflow" message! This needs to be anticipated and hence can be avoided. Overflow occurs if: Byte form: AH>=divisor Word form: DX>=divisorPreparing Dividend
To avoid overflow, the dividend has to be prepared as follows.
If signed: CWD (called sign-extension) If unsigned: MOV DX,0 To divide a word in AL by a byte, AL must be converted to a word in AX. If signed: CBW (these do not affect Flags) If unsigned: MOV AH,0 |