GNU/Linux xterm-256color bash 142 views

.section .data
buffer:     .skip 12          // Suficiente para almacenar un entero como string con '\n' y '\0'

.section .text
.global _start

_start:
    mov x19, #9               // Inicio del contador (x19 = i = 9)

loop:
    // Convertir número a string (decimal)
    mov x0, x19               // Pasar el número a x0
    ldr x1, =buffer           // Dirección del buffer de salida
    bl itoa                   // Llamada a función itoa(x0, buffer)

    // Escribir a consola con syscall write
    mov x8, #64               // syscall número para write (Linux AArch64)
    mov x0, #1                // STDOUT
    ldr x1, =buffer           // dirección del string
    bl strlen                 // longitud del string en x0
    mov x2, x0                // x2 = longitud
    mov x0, #1                // STDOUT de nuevo
    mov x8, #64               // syscall write
    svc 0                     // llamada al sistema

    add x19, x19, #1          // i++
    cmp x19, #44              // ¿i <= 43?
    blt loop                  // si sí, repetir

    // Exit
    mov x8, #93               // syscall exit
    mov x0, #0                // código de salida 0
    svc 0

// ------------------ Función strlen ------------------
strlen:
    // entrada: x1 = puntero al string
    // salida: x0 = longitud
    mov x2, x1
.strlen_loop:
    ldrb w3, [x2], #1
    cmp w3, #0
    b.ne .strlen_loop
    sub x0, x2, x1
    ret

// ------------------ Función itoa ------------------
itoa:
    // entrada: x0 = entero, x1 = destino buffer
    // salida: buffer con string decimal terminado en '\n' y '\0'
    // usa x2-x6 internos
    mov x2, x0        // copia del número
    mov x3, x1        // ptr inicio buffer
    add x4, x1, #10   // puntero temporal al final del buffer
    mov x5, #0x0A     // '\n'
    strb w5, [x4]     // escribir '\n'
    mov x5, #0x00
    strb w5, [x4, #1] // escribir '\0'
    mov x6, x4        // x6 apunta al último dígito (antes de '\n')

.itoa_loop:
    mov x5, #10
    udiv x0, x2, x5
    msub x5, x0, x5, x2   // x5 = x2 - (x0 * 10) → x5 = x2 % 10
    add x5, x5, #'0'
    sub x6, x6, #1
    strb w5, [x6]
    mov x2, x0
    cbnz x2, .itoa_loop

    // Copiar desde x6 a x1
.copy_loop:
    ldrb w5, [x6], #1
    strb w5, [x1], #1
    cmp w5, #0
    b.ne .copy_loop
    ret