与汇编语言作斗争
我很难掌握汇编语言的概念。我得到了这个示例代码:
ploop: mov ax, 0201h
add al, ah
cmp al, 5
jb ploop
eloop:
循环末尾的十六进制值为 0205,但我不确定我理解为什么。
对于第一行,我们将 0201 移入 ax,因此 al = 01 且 ah = 02。然后将 ah 添加到 al,使 al = 03。我们将 al 与 5 进行比较,因为 3 < > 5、拟合jb,再次进行ploop。我们完成所有步骤并在 cmp al = 05 == 5 处,因此它不再适合 jb。
这是正确的看待方式吗?
I am having a hard time grasping the concepts of assembly language. I am given this sample code:
ploop: mov ax, 0201h
add al, ah
cmp al, 5
jb ploop
eloop:
The hex value at the end of the loop is 0205, but I'm not sure I understand why.
For the first line, we are moving 0201 into ax, so al = 01 and ah = 02. Then you add ah to al, making al = 03. We compare al to 5 and since 3 < 5, it fits jb and goes through the ploop again. We go through all the steps and at cmp al = 05 == 5, so it no longer fits jb.
Is this the right way of looking at it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
几乎是正确的。除非您可能希望它看起来像下面这样:
否则它会进入无限循环,因为
al
和ah
在每次循环迭代中都被覆盖。Almost correct. Except you probably want it to look like the following instead:
As it will otherwise go into an infinite loop as
al
andah
are overwritten in each loop iteration.我几乎敢打赌您转录的代码不正确。就目前而言,你有一个无限循环。它需要更像这样:
正如您发布的那样,在循环的每次迭代开始时,
ax
正在使用0201h
重新加载。然后将ah
中的 02 添加到al
中的 01。这将给出 3。您将其与 5 进行比较,如果它小于(显然总是如此),您将再次执行循环。标签移动后,我们以
ah
中的 02 和al
中的 01 开始。然而,在循环的每次迭代中,我们将 02 添加到al
的当前内容中,因此它将遵循序列 1, 3, 5。在每次迭代中,我们将其内容与 5 进行比较,然后继续循环当且仅当它小于(视为无符号)时,因此循环执行三次迭代,然后以 al = 5 停止。I would almost bet you've transcribed the code incorrectly. As it stands, you have an infinite loop. It needs to be more like this:
As you posted it,
ax
is being re-loaded with0201h
at the beginning of each iteration of the loop. You're then adding the 02 inah
to the 01 inal
. That'll give 3. You compare that to 5 and if it's less than (obviously it always will be) you execute the loop again.With the label moved, we start with 02 in
ah
and 01 inal
. At each iteration of the loop, however, we add 02 to the current content ofal
, so it will follow the sequence 1, 3, 5. At each iteration we compare its content to 5, and continue the loop if and only if it's less than (viewed as an unsigned), so the loop with execute three iterations, then stop with al = 5.