问题描述
在以下代码中,
MOV AL,NUMBER1
ADD AL,NUMBER2
MOV AH, 00H
ADC AH, 00H
第3行和第4行做什么?他们是做什么的?
what are lines 3 and 4 for? What do they do?
此外,为什么代码会清除AH? (我认为是因为AL的"ADD"操作可能产生进位.)
Also, why does the code clear AH? (I assume because AL's "ADD" operation may produce carry.)
推荐答案
要弄清楚这一点,请先查找每条指令的作用:
To figure this out, start by looking up what each instruction does:
-
MOV AH, 00H
此 MOV
指令会将AH
寄存器设置为0 没有影响标志.
This MOV
instruction will set the AH
register to 0 without affecting flags.
ADC AH, 00H
此 ADC
指令将添加源操作数(0),进位标志(CF)和目标操作数(AH
),将结果存储在目标操作数(AH
)中.
This ADC
instruction will add the source operand (0), the carry flag (CF), and the destination operand (AH
), storing the result in the destination operand (AH
).
然后,它象征性地:AH = AH + 0 + CF
请记住,MOV
不会影响标志,因此ADC
指令使用的CF值是 ADD
指令(第2行).
Remember that the MOV
did not affect the flags, so the value of CF that is used by the ADC
instruction is whatever was set previously by the ADD
instruction (in line 2).
此外,此时AH
为0,所以实际上就是:AH = CF
.
Also, AH
is 0 at this point, so this is really just: AH = CF
.
现在您知道代码的作用:
And now you know what the code does:
-
它将
NUMBER1
移到AL
寄存器中:AL = NUMBER1
它将NUMBER2
添加到AL
寄存器:AL = NUMBER1 + NUMBER2
它清除AH
:AH = 0
通过将NUMBER1
和NUMBER2
相加来设置AH
等于CF.因此,如果加法需要进位,则AH
将为1,否则为0. (AH = CF
)
It sets AH
equal to CF, as set by the addition of NUMBER1
and NUMBER2
. Thus, AH
will be 1 if the addition required a carry, or 0 otherwise. (AH = CF
)
该代码的用途显然是对两个8位数字进行16位加法运算.在伪C语言中,基本上是:
As for the purpose of this code, it clearly performs a 16-bit addition of two 8-bit numbers. In a pseudo-C, it would basically be:
BYTE NUMBER1;
BYTE NUMBER2;
WORD RESULT = (WORD)NUMBER1 + (WORD)NUMBER2;
其中BYTE大小的输入扩展为WORD并加在一起.为什么要这样?好吧,处理溢出.如果将两个8位值相加,则结果可能会大于8位中的值.
where the BYTE-sized inputs are extended to WORDs and added together. Why do this? Well, to handle overflow. If you add together two 8-bit values, the result may be larger than will fit in 8 bits.
理解这一点的真正技巧可能是AL
和AH
寄存器分别是AX
寄存器的低位和高位.因此,紧接这些说明之后,您可能会看到正在使用AX
.其中包含添加NUMBER1
和NUMBER2
的16位结果.
The real trick to understanding this may be that the AL
and AH
registers are the lower and upper bits, respectively, of the AX
registers. So immediately after these instructions, you may see AX
being used. This contains the 16-bit result of the addition of NUMBER1
and NUMBER2
.
这篇关于ASM中的ADC指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!