GNU/Linux xterm-256color bash 141 views

// ****************************************************************************
// * Nombre del archivo: practica.s
// * Descripción: Convierte un numero de decimal a hexadecimal
// * Autor: Roldan Castro Luis Alberto
// * Fecha: 07-04-2025
// * Plataforma: Raspberry Pi OS (64 bits)
// * Asciinema: https://asciinema.org/a/dLfBUYBK9Pis6LfzMNP2V7jzg
// ****************************************************************************

// ****************************************************************************
// VERSION EN PYTHON
//def celsius_to_fahrenheit(celsius):
//    return (celsius * 9 // 5) + 32
//
//def main():
//    try:
//        celsius = int(input("Ingresa los grados Celsius: "))
//        fahrenheit = celsius_to_fahrenheit(celsius)
//        print(f"Grados Fahrenheit: {fahrenheit}")
//    except ValueError:
//        print("Por favor, ingresa un número válido.")
//
//if __name__ == "__main__":
//    main()
// ****************************************************************************

.global _start

.section .bss
hex_buffer: .skip 16     // espacio para el número en texto
dec_buffer: .skip 16     // espacio para número decimal como texto

.section .text

_start:
    // =====================================
    // Número decimal a convertir
    // =====================================
    MOV X6, #255              // Número decimal
    MOV X20, X6               // Lo guardamos para reutilizar

    // ========================
    // Mostrar mensaje base
    // ========================
    LDR X1, =msg1
    MOV X2, #8                // "Decimal "
    MOV X0, #1
    MOV X8, #64
 SVC #0

    // ========================
    // Mostrar número decimal
    // ========================
    MOV X6, X20
    BL print_decimal

    // ========================
    // Mostrar " en hexadecimal es: 0x"
    // ========================
    LDR X1, =msg2
    MOV X2, #23               // " en hexadecimal es: 0x"
    MOV X0, #1
    MOV X8, #64
    SVC #0

    // ========================
    // Mostrar número hexadecimal
    // ========================
    MOV X6, X20
    BL print_hex

    // Salto de línea
    LDR X1, =newline
    MOV X2, #1
    MOV X0, #1
    MOV X8, #64
    SVC #0
 // ========================
    // Salida limpia
    // ========================
    MOV X0, #0
    MOV X8, #93
    SVC #0

// ========================
// print_decimal (X6)
// ========================
print_decimal:
    ADR X7, dec_buffer
    ADD X7, X7, #15
    MOV X8, X6
    MOV X9, #0

dec_loop:
    MOV X10, #10
    UDIV X11, X8, X10
    MUL X12, X11, X10
    SUB X13, X8, X12
    ADD X13, X13, #'0'
    STRB W13, [X7], #-1
    MOV X8, X11
    ADD X9, X9, #1
    CBNZ X8, dec_loop

    ADD X1, X7, #1
    MOV X2, X9
    MOV X0, #1
    MOV X8, #64
 SVC #0
    RET

// ========================
// print_hex (X6)
// ========================
print_hex:
    ADR X7, hex_buffer
    ADD X7, X7, #15
    MOV X8, X6
    MOV X9, #0

hex_loop:
    MOV X10, #16
    UDIV X11, X8, X10
    MUL X12, X11, X10
    SUB X13, X8, X12
    CMP X13, #9
    BLE hex_digit
    ADD X13, X13, #'A' - 10
    B store_hex
hex_digit:
    ADD X13, X13, #'0'
store_hex:
    STRB W13, [X7], #-1
    MOV X8, X11
    ADD X9, X9, #1
    CBNZ X8, hex_loop

    ADD X1, X7, #1
 MOV X2, X9
    MOV X0, #1
    MOV X8, #64
    SVC #0
    RET

.section .rodata
msg1:    .asciz "Decimal "
msg2:    .asciz " en hexadecimal es: 0x"
newline: .asciz "\n"