Abdelrahman Yehia
Abdelrahman Yehia

Reputation: 3

what is wrong with this assembly x86 code

i am using nasm in linux , the code does not print print hello world and i think it enters a infinity loop , but why ?

    section .data
msg db 'Hello World',0Ah

section .text
global _start
    _start:
        print:
            call charcmp 
            mov edx,eax   
            mov eax,4
            mov ebx,1
            mov ecx,msg
            int 80h
            exit:
                mov eax,1
                mov ebx,0
                int 80h
        charcmp:
            mov eax,msg
            mov ebx,msg
            cmp byte [eax],0
            jz finshed
            inc eax
            jmp charcmp
            finshed:
                sub eax,ebx
                ret

Upvotes: 0

Views: 95

Answers (1)

Erik Eidt
Erik Eidt

Reputation: 26766

What is wrong with this assembly x86 code?

  1. The indentation is bad.
  2. charcmp has a loop that includes the initialization of the loop control variables.

Imagine we did a for loop like this:

for ( int i = 0; i < 100; i++ ) { ... }

loop1:
    i = 0;
    if ( i >= 100 ) goto loop1End;
    ...
    i++;
    goto loop1;
loop1End:

Can you see how the i = 0 — being inside the loop — makes it impossible for the loop to proceed?  Here's better:

    i = 0;
loop1:
    if ( i >= 100 ) goto loop1End;
    ...
    i++;
    goto loop1;
loop1End:

You can debug these kinds of problems yourself!  Try single stepping and you'll immediately see what's wrong.  By the second iteration of the loop it will still be comparing the first character of the string — very easy to see in the debugger that eax remains stationary.


Why do people think that they can write assembly code — the absolutely most error prone of programming languages — without debugging skills?  Every line of assembly code is subject to potential errors that will not be caught by the assembler.

Upvotes: 2

Related Questions