加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

string – x86 ASM Linux – 创建一个循环

发布时间:2020-12-14 02:12:24 所属栏目:Linux 来源:网络整理
导读:我正在开发一个程序 – 应该很简单 – 在使用NASM和x86 Intel汇编语法的 Linux操作系统上. 我遇到的问题是我无法为我的程序创建一个工作循环: section .data hello: db 'Loop started.',0Ah ;string tells the user of start sLength: equ $-hello ;length
我正在开发一个程序 – 应该很简单 – 在使用NASM和x86 Intel汇编语法的 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,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寄存器的价值?

谢谢.

解决方法

_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,sLength
    int 80              ;prints out the string in "hello"
    xor ecx,ecx         ;ecx = 0

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

    pop ecx             ;get loop index back in ECX
    add ecx,repeat

theend:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读