与汇编语言作斗争

发布于 2024-12-05 18:19:08 字数 367 浏览 1 评论 0原文

我很难掌握汇编语言的概念。我得到了这个示例代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淤浪 2024-12-12 18:19:08

几乎是正确的。除非您可能希望它看起来像下面这样:

        mov  ax, 0201h
ploop:  add  al, ah
        cmp  al, 5
        jb   ploop
eloop:

否则它会进入无限循环,因为 alah 在每次循环迭代中都被覆盖。

Almost correct. Except you probably want it to look like the following instead:

        mov  ax, 0201h
ploop:  add  al, ah
        cmp  al, 5
        jb   ploop
eloop:

As it will otherwise go into an infinite loop as al and ah are overwritten in each loop iteration.

心碎的声音 2024-12-12 18:19:08

我几乎敢打赌您转录的代码不正确。就目前而言,你有一个无限循环。它需要更像这样:

        mov  ax, 0201h
ploop:  add  al, ah
        cmp  al, 5
        jb   ploop
eloop:

正如您发布的那样,在循环的每次迭代开始时,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:

        mov  ax, 0201h
ploop:  add  al, ah
        cmp  al, 5
        jb   ploop
eloop:

As you posted it, ax is being re-loaded with 0201h at the beginning of each iteration of the loop. You're then adding the 02 in ah to the 01 in al. 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 in al. At each iteration of the loop, however, we add 02 to the current content of al, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文