const
是一个字节。当你做IDIV byte_divisor
时,商将被放置在AL
中,并且具有-128到127的范围。1000/5是200,这不在允许的范围内。如果使用DIV
,商的范围是0到255,这就是您的示例在这种情况下工作的原因。
如果你想IDIV
1000乘5你应该使用一个16位除数。但是需要注意的是,当使用IDIV r/m16
时,实际上划分的是由DX
和AX
(DX
保存最高有效位,而AX
最低有效位)组成的32位双字。
从Intel的手册:
IDIV r/m16 Signed divide DX:AX by r/m16, with result stored in AX ← Quotient, DX ← Remainder.
IF OperandSize = 16 (* Doubleword/word operation *)
THEN
temp ← DX:AX/SRC; (* Signed division *)
IF (temp > 7FFFH) or (temp < 8000H)
(* If a positive result is greater than 7FFFH
or a negative result is less than 8000H *)
THEN
#DE; (* Divide error *)
ELSE
AX ← temp;
DX ← DX:AX SignedModulus SRC;
所以IDIV
之前,你应该从AX
价值创造DX:AX
一个符号双。有一个名为CWD
所做的正是这一个指令:
的CWD
指令 复制标志在AX
寄存器中的值转换成DX
寄存器的每个位的(第15位)。
即:
.data
const dw 5 ; now a word
.code
mov ax,1000
cwd ; sign-extend ax into dx
idiv const
; quotient is in ax, remainder in dx
完美。谢谢! –