x86 ASM Linux - 创建循环

发布于 2024-12-09 23:38:15 字数 1449 浏览 0 评论 0原文

我正在使用 NASM 和 x86 Intel Assembly Syntax 在 Linux 操作系统上开发一个程序 - 它应该很简单。

我遇到的问题是我无法为我的程序创建工作循环:

section .data
    hello:    db 'Loop started.', 0Ah   ;string tells the user of start
    sLength:  equ $-hello               ;length of string

    notDone:  db 'Loop not finished.', 0Ah ;string to tell user of continue
    nDLength: equ $-notDone                ;length of string

    done:     db 'The loop has finished', 0Ah ;string tells user of end
    dLength:  equ $-done                      ;length of string

section .text

    global _start:
_start:
    jmp welcome         ;jump to label "welcome"

    mov ecx, 0          ;number used for loop index
    jmp loop            ;jump to label "loop"

    jmp theend          ;jump to the last label

welcome:

    mov eax, 4
    mov ebx, 1
    mov ecx, hello
    mov edx, sLength
    int 80              ;prints out the string in "hello"

loop:
    push ecx            ;put ecx on the stack so its value isn't lost

    mov eax, 4
    mov ebx, 1
    mov ecx, notDone
    mov edx, nDLength
    int 80              ;prints out that the loop isn't finished

    pop ecx             ;restore value
    add ecx, 1          ;add one to ecx's value
    cmp ecx, 10
    jl loop             ;if the value is not ten or more, repeat

theend:

;loop for printing out the "done" string

我正在打印第一个字符串,一个“未完成”和最后一个字符串;我还缺少九个“未完成”!有谁知道为什么我失去了 ecx 寄存器的值?

谢谢。

I am working on a program - it should be simple - on a Linux OS using NASM and x86 Intel Assembly Syntax.

The problem I am having is that I cannot create a working loop for my program:

section .data
    hello:    db 'Loop started.', 0Ah   ;string tells the user of start
    sLength:  equ $-hello               ;length of string

    notDone:  db 'Loop not finished.', 0Ah ;string to tell user of continue
    nDLength: equ $-notDone                ;length of string

    done:     db 'The loop has finished', 0Ah ;string tells user of end
    dLength:  equ $-done                      ;length of string

section .text

    global _start:
_start:
    jmp welcome         ;jump to label "welcome"

    mov ecx, 0          ;number used for loop index
    jmp loop            ;jump to label "loop"

    jmp theend          ;jump to the last label

welcome:

    mov eax, 4
    mov ebx, 1
    mov ecx, hello
    mov edx, sLength
    int 80              ;prints out the string in "hello"

loop:
    push ecx            ;put ecx on the stack so its value isn't lost

    mov eax, 4
    mov ebx, 1
    mov ecx, notDone
    mov edx, nDLength
    int 80              ;prints out that the loop isn't finished

    pop ecx             ;restore value
    add ecx, 1          ;add one to ecx's value
    cmp ecx, 10
    jl loop             ;if the value is not ten or more, repeat

theend:

;loop for printing out the "done" string

I am getting the first string printed, one "Not done" and the last string printed; I am missing nine more "Not Done"s! Does anyone have any idea as to why I am losing my value for the ecx register?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

皓月长歌 2024-12-16 23:38:15
_start:
    jmp welcome

这意味着 JMP 下面的所有代码都不会执行,尤其是 mov ecx,0 (对于较短的指令,应该是 xor ecx,ecx

不要从跳转开始,从一些代码开始。 JMP是跳转,跳转后不会返回,只是继续执行。

因此,在跳转到Welcome:之后,您直接进入Loop:,从而错过了ecx=0代码。

cmp ecx, 10
jl loop

ECX不为0,肯定大于10h,所以不循环。

试试这个:

_start:
    mov eax, 4
    mov ebx, 1
    mov ecx, hello
    mov edx, sLength
    int 80              ;prints out the string in "hello"
    xor ecx,ecx         ;ecx = 0

loop:
    push ecx            ;save loop index
    mov eax, 4
    mov ebx, 1
    mov ecx, notDone
    mov edx, nDLength
    int 80              ;prints out that the loop isn't finished

    pop ecx             ;get loop index back in ECX
    add ecx, 1          ;add one to ecx's value
    cmp ecx, 10
    jl loop             ;if the value is not ten or more, repeat

theend:
_start:
    jmp welcome

This means all the code below the JMP is not executed, especially the mov ecx,0 (which should be xor ecx,ecx for a shorter instruction)

Don't start with a jump, start with some code. A JMP is a jump, it's not going back after you've jumped, it just continues the execution.

So after jumping to Welcome:, you go directly to Loop:, thus missing the ecx=0 code.

cmp ecx, 10
jl loop

ECX is not 0, it definitely is greater than 10h, so the loop is not taken.

Try this:

_start:
    mov eax, 4
    mov ebx, 1
    mov ecx, hello
    mov edx, sLength
    int 80              ;prints out the string in "hello"
    xor ecx,ecx         ;ecx = 0

loop:
    push ecx            ;save loop index
    mov eax, 4
    mov ebx, 1
    mov ecx, notDone
    mov edx, nDLength
    int 80              ;prints out that the loop isn't finished

    pop ecx             ;get loop index back in ECX
    add ecx, 1          ;add one to ecx's value
    cmp ecx, 10
    jl loop             ;if the value is not ten or more, repeat

theend:
蓝天白云 2024-12-16 23:38:15

您将循环寄存器 ecx 初始值设置为“hello”的地址,而不是 0:

    jmp welcome
    (mov ecx, 0)        ;number used for loop index <- jumped over
    ...
welcome:
    ...
    mov ecx, hello <- setting
    int 80         <- ecx
    ...
loop:
    push ecx            ;put ecx on the stack so its value isn't lost

You are setting the loop register ecx initial value to the address of "hello", and not 0:

    jmp welcome
    (mov ecx, 0)        ;number used for loop index <- jumped over
    ...
welcome:
    ...
    mov ecx, hello <- setting
    int 80         <- ecx
    ...
loop:
    push ecx            ;put ecx on the stack so its value isn't lost
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文