x86 ASM Linux - создание цикла - PullRequest
       12

x86 ASM Linux - создание цикла

5 голосов
/ 15 октября 2011

Я работаю над программой - она ​​должна быть простой - в ОС Linux с использованием NASM и синтаксиса Intel x86 для сборки.

Проблема, с которой я столкнулся, заключается в том, что я не могу создать рабочий цикл для своей программы:

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?

Спасибо.

Ответы [ 2 ]

1 голос
/ 15 октября 2011
_start:
    jmp welcome

Это означает, что весь код ниже JMP не выполняется, особенно mov ecx, 0 (который должен быть xor ecx, ecx для более короткой инструкции)

Не начинайте с прыжка, начните с некоторого кода. JMP - это прыжок, он не возвращается после прыжка, он просто продолжает выполнение.

Так что после перехода к Welcome: вы сразу переходите к Loop:, пропуская код ecx = 0.

cmp ecx, 10
jl loop

ECX не 0, оно определенно больше 10 ч, поэтому цикл не берется.

Попробуйте это:

_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:
1 голос
/ 15 октября 2011

Вы устанавливаете начальное значение 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
...