X86 ASM Linux - Creating a Loop

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

The problem I am facing is that I cannot create a work cycle 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 value
    cmp ecx, 10
    jl loop             ;if the value is not ten or more, repeat

theend:

;loop for printing out the "done" string

I get the first line printed, one “Not done” and the last line printed; I missed nine more "Not done"! Does anyone know why I am losing my value for ecx register?

Thank.

+5
source share
2 answers
_start:
    jmp welcome

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

, . 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 value
    cmp ecx, 10
    jl loop             ;if the value is not ten or more, repeat

theend:
+1

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
+1

All Articles