英特尔x86组装中的循环将永远持续下去 [英] Loop in Intel x86 Assembly going on forever

查看:161
本文介绍了英特尔x86组装中的循环将永远持续下去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习Intel x86 Assembly,在尝试构建一个简单的循环(循环10次)时遇到了一个问题.它应该在10个循环后停止,但它会一直持续下去.

I'm currently learning Intel x86 Assembly, and I've run into a problem while trying to construct a simple loop, which loops 10 times. It's supposed to stop after the 10 loops, but it keeps going on and on, forever.

这是我正在使用的代码:

This is the code that I am using:

    section .data
    msg     db      "Hello, World!", 0x0a
    len     equ     $-msg

    section .text
    global _start

    _start:
            mov     cx, 10  ; loop counter

            _loop_start:
                    mov     ebx, 0x01
                    mov     ecx, msg
                    mov     edx, len
                    mov     eax, 0x04
                    int     0x80

                    dec     cx
                    cmp     cx, 0
                    jge     _loop_start

            _done:
                    mov     ebx, 0x00
                    mov     eax, 0x01
                    int     0x80

在尝试编写此代码之前,我查看了本教程简单的算术.

Before attempting to write this code, I looked at this tutorial for doing simple arithmetic.

我这样编译它:

   nasm -f elf64 test.s -o test.o

并这样链接:

   ld -s -o test_exec test.o

预先感谢, 阿尼克扬

推荐答案

cx是ecx的低16位部分.您的代码建议您可能认为循环将运行10次(在循环之前将cx设置为10).但是随后您用mov ecx, msg用msg的地址覆盖了该值.因此,您将从该数字的低16位开始倒数至0. 但是减量甚至没有作用,因为在下一次迭代中,您再次用msg的地址覆盖了ecx.然后循环再次开始.这是一个无限循环. 您是否在调试器中检查了软件?这可以有很大帮助.

cx is the lower 16 bit portion of ecx. Your code suggest that you may think that your loop will run 10 times (you set cx to 10 before the loop). But then you overwrite the value with the address of msg with mov ecx, msg. So you'll start to count down to 0 from the lower 16 bit of that number. But the decrement doesn't even have an effect since during the next iteration you overwrite the ecx again with the msg's address. And the loop start again. It's an infinite loop. Did you check the software in debugger? That can help a lot.

这篇关于英特尔x86组装中的循环将永远持续下去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆