GNU/Linux xterm-256color bash 165 views

**
Nombre: bindec.s
Autor: Karla Itzel Vázquez Cruz
Fecha: 09-04-2025
Descripción: binario a decimal Plataforma: Raspberry Pi OS 64-bit
Asciinema: bindec.s


Versión en C:

int main() {
        char bin[10];
        printf("Ingresa un número binario: ");
        scanf("%s", bin);
        int dec = 0;
        for (int i = 0; bin[i]; i++)
            dec = dec * 2 + (bin[i] - '0');
        printf("Número en decimal: %d\n", dec);
        return 0;

Versión en ARM64 RaspbianOS Linux:

.section .data
msg_input:     .asciz "Ingresa un número binario: "
msg_result:    .asciz "\nNúmero en decimal: "
buffer:        .skip 10
newline:       .asciz "\n"

.section .text
.global _start

_start:
    // Mostrar mensaje de entrada
    mov     x0, 1
    ldr     x1, =msg_input
    mov     x2, 29
    mov     x8, 64
    svc     0

    // Leer cadena binaria del teclado
    mov     x0, 0
    ldr     x1, =buffer
    mov     x2, 10
    mov     x8, 63
    svc     0

    // Convertir cadena binaria a entero decimal (x2)
    ldr     x1, =buffer
    mov     x2, 0              // acumulador decimal
bin_to_dec:
    ldrb    w3, [x1], 1
    cmp     w3, #'0'
    blt     done_bin
    cmp     w3, #'1'
    bgt     done_bin
    sub     x3, x3, '0'
    lsl     x2, x2, #1         // x2 *= 2
    add     x2, x2, x3
    b       bin_to_dec
done_bin:

    // x2 ahora contiene el número decimal

    // Limpiar buffer antes de convertir a ASCII
    mov     x10, 0
    ldr     x11, =buffer
    mov     x12, #10
clear_loop:
    strb    w10, [x11], 1
    subs    x12, x12, 1
    b.ne    clear_loop

    // Convertir decimal a cadena ASCII para imprimir
    mov     x5, x2
    ldr     x6, =buffer+9
    mov     x7, 10
int_to_ascii:
    mov     x8, x5
    udiv    x5, x5, x7
    msub    x9, x5, x7, x8
    add     x9, x9, '0'
    strb    w9, [x6, -1]!
    cmp     x5, 0
    bne     int_to_ascii

    // Mostrar mensaje de salida
    mov     x0, 1
    ldr     x1, =msg_result
    mov     x2, 23
    mov     x8, 64
    svc     0

    // Mostrar número decimal convertido
    mov     x0, 1
    mov     x1, x6
    ldr     x2, =buffer+9
    sub     x2, x2, x6
    mov     x8, 64
    svc     0

    // Nueva línea
    mov     x0, 1
    ldr     x1, =newline
    mov     x2, 1
    mov     x8, 64
    svc     0

    // Salir del programa
    mov     x0, 0
    mov     x8, 93
    svc     0

More recordings by Itzel

Browse all

PAR O IMPAR 3:37

by Itzel

MAYUSCULAS A MINISCULAS 1:02

by Itzel

ROMANO 1:43

by Itzel

CUAL ES EL MENOR? 2:20

by Itzel