Bucle simple en ensamblador para Linux/x86

[ permalink ] [ download ]
section         .data

i       dd      0Ah                     ;10 veces
msg     db      'Repitiendo...', 0Ah    ;mensaje
len     equ     $ - msg                 ;longitud

section         .text

global _start

_start:
        ; for(i=10; i>0; i--)
        mov     ecx, [i]                ;ecx = 0Ah = 10d

bucle:
        test    ecx, ecx
        jz      exit                    ;ecx==0? Sí, salta a exit. No, sigue.

        push    ecx                     ;lo guardamos en la pila, para no perderlo :)

        mov     eax,04h                 ;syscall 4 (write)
        mov     ebx,01h                 ;donde? en STDOUT (1)
        mov     ecx,msg                 ;que? el msg
        mov     edx,len                 ;cuantos bytes? len
        int     80h                     ;llamamos al kernel...

        pop     ecx                     ;recuperamos ecx de la pila
        dec     ecx                     ;ecx-- (i--)

        jmp     bucle

exit:
        mov     eax,01h                 ;syscall 1 (exit)
        int     80h                     ;llamamos al kernel...
hits counter