5

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.

DocMax
  • 12,094
  • 7
  • 44
  • 44
nmagerko
  • 6,586
  • 12
  • 46
  • 71
  • `int 80` is `int 0x50`. You want `int 0x80`. (possible canonical duplicate for that: [Assembler sysTime giving error on executing](https://stackoverflow.com/a/39412096)) – Peter Cordes Jul 28 '21 at 22:09

2 Answers2

1

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
Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
1
_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:
龚元程
  • 417
  • 1
  • 5
  • 14
  • Are the registers changed by the int 80, or is the address of "hello" in ecx after the int 80? – Jens Björnhager Oct 15 '11 at 13:51
  • @Jens Björnhager: EAX will change with a return value. The other registers stay the same. (except EIP of course) The stack is not changed. Maybe with other functions than #4 some registers might change after the call. The link below explain very well how system calls are made on linux with int80: http://asm.sourceforge.net/intro/hello.html – 龚元程 Oct 15 '11 at 14:17
  • Those were some sad mental mistakes in the flow of my program. Thank you very much. – nmagerko Oct 16 '11 at 22:15
  • `int 80` is `int 0x50`. You want `int 0x80`. – Peter Cordes Jul 28 '21 at 22:09