Infinite loop while trying to print numbers 1 to 10 in assembly x86 64 bits

25 Views Asked by At

i'm new programming in ISA x86 64 bits, so i'm trying to print the numbers 1 to 10 in assembly using a conversion in hexadecimal, but I get an infinite loop cause the 8 bit register $cl can't increment. I've been using gdb to analyze the code but I dont find a way to run my code and dont have a infinite loop in the process. I hope y'all can help me, and remember and new into this so bear with me please.

section .data
    number db 0; Buffer 
    nl db 10; \n

section .text
    global _start

    _start:
        ; syscall information
        mov rax, 1
        mov rdi, 1
        mov rsi, number
        mov rdi, 1

        mov byte[number], "0" ; Inicializando the buffer in "0" = 46 Dec 

        .loop:

            add byte[number], cl ; cl contains the data of the number in ASCII format
            mov rdx, 1 ; lenght of bytes
            syscall

            inc cl; ASCII data

            cmp cl, 0x39; 0x39 = 57 Dec = 9

            jle .loop; lower equal jump

        ; \n
        mov rsi, nl
        mov rdx, 1
        syscall

        ; Exit
        mov rax, 60
        xor rdi, rdi
        syscall 

I've been using the gdb to analyze the code, even I tried to change the 8 bit register instead of using cl, but nothing works.

1

There are 1 best solutions below

1
Jester On

syscall destroys rcx which includes cl. Pick a different register. rax is used for the return value so that needs to be reloaded as well, although in this case it happens to work. Also, you are always adding to number which won't do what you want. A possible solution with minimal changes:

section .data
    number db 0; Buffer 
    nl db 10; \n

section .text
    global _start

    _start:
        ; syscall information
        mov rdi, 1
        mov rsi, number
        mov rdi, 1

        mov bl, '0'

        .loop:

            mov [number], bl ; bl contains the data of the number in ASCII format
            mov eax, 1 ; need to reload syscall number
            mov rdx, 1 ; lenght of bytes
            syscall

            inc bl; ASCII data
            cmp bl, 0x39; 0x39 = 57 Dec = 9

            jle .loop; lower equal jump

        ; \n
        mov rsi, nl
        mov rdx, 1
        syscall

        ; Exit
        mov rax, 60
        xor rdi, rdi
        syscall