16bit registers preserve unsigned numbers in the range 0000h..FFFFh.
The result of adding or subtracting such numbers may overflow this range by one bit, this bit is mediated by CarryFlag.
For instance FFFFh + FFFFh = CF + FFFEh.
The situation is different with multiplication: it may overflow by sixteen bits,
for instance FFFFh * FFFFh = FFFE0001h.
This is why another register (hardwired as DX) is used to hold the upper 16 bits of the result.
即便是旧的16名轨道处理员8086,也可使用传统的16个轨道登记册添加32个轨道号码,但每项作业都需要2个机器指示,而且必须考虑到CarryFlag。 32个轨道号的下调用了<代码>ADD UB<>>>,上限用了<代码>ADCONEBB。
举例来说,请计算(a*b)+(c*d)
,a,b,c,d
分别为16bit number 000Ah,000Bh,000Ch,000Dh。
MOV AX,000Ah ;
MOV CX,000Bh ; Let CX=000Bh
MUL CX ; Let DX:AX= a * b = 0000:006Eh
MOV SI,AX ; Save temporary result DX:AX
MOV DI,DX ; into another pair DI:SI.
MOV AX,000Ch
MOV CX,000Dh
MUL CX ; Let DX:AX = c * d = 0000:009Ch
; Add the temporary result DI:SI to DX:AX.
ADD AX,SI ; Let AX = 009Ch + 006Eh = 010Ah. CF = 0.
ADC DX,DI ; Let DX = 0000h + 0000h + CF = 0. CF = 0.
Result of (a*b)+(c*d)
, which is 0000:010Ah, is now in DX:AX, CF=0.
We may also treat 16bit value as a signed integer in the range -32768..+32767, i.e. 8000h..7FFFh. Intel CPU 8086 couldn t multiply signed numbers, the instruction IMUL was introduced with 80186 and later. In the absence of IMUL
we have to convert each number to its absolute value, perform the unsigned multiplication and, if exactly one of multiplicant was negative, convert the unsigned product to its negative value.
Conversion between a negative value and its absolute (positive) value is provided by the instruction NEG. To negate 32bit number in two joined 16bit register CarryFlag must be taken into account again:
; Negate DX:AX
NEG DX
NEG AX
SBB DX,0
或
; Negate DX:AX
NOT AX
NOT DX
ADD AX,1
ADC DX,0
然而,过去30年提出的所有英特尔/亚MD处理器可直接计算与<条码>IMUL签署的多复制件。 因此,正如Peter Cordes在其评论中所建议的那样,你可以更好地利用。