Skip to content

Instantly share code, notes, and snippets.

@FranciscoJCE
Last active April 11, 2025 06:34
Show Gist options
  • Select an option

  • Save FranciscoJCE/b0c22e49e30dc0908484feafcf4713b1 to your computer and use it in GitHub Desktop.

Select an option

Save FranciscoJCE/b0c22e49e30dc0908484feafcf4713b1 to your computer and use it in GitHub Desktop.
Codigo Assembly ARM64 para RaspbianOS
/*
* Programa: Cálculo de interés simple en ARM64
* Autor: Francisco Javier Crisostomo Enciso
* Descripción: Calcula el interés simple usando la fórmula I = (Capital × Tasa × Tiempo) / 100
* Parámetros:
* - Capital = 10,000 (entero)
* - Tasa = 5% (entero)
* - Tiempo = 3 años (entero)
* Versión: Usa solo operaciones con enteros
*
* Ejemplo equivalente en C#:
* int capital = 10000;
* int tasa = 5;
* int tiempo = 3;
* int interes = (capital * tasa * tiempo) / 100;
* Console.WriteLine($"Interés generado: {interes}");
*
* Asciinema:https://asciinema.org/a/qXXj3NLYukMmo4skAYtHaBF5H
*/
.section .data
capital: .quad 10000 // 10,000 en formato QWORD
tasa: .quad 5 // 5% como entero
tiempo: .quad 3 // 3 años como entero
mensaje: .asciz "Interés generado: "
newline: .asciz "\n"
.section .bss
.lcomm resultado_str, 64 // Buffer para el string de resultado
.section .text
.global _start
_start:
// ======================================
// Cargar valores iniciales
// ======================================
ldr x1, =capital
ldr x1, [x1] // x1 = capital (10,000)
ldr x2, =tasa
ldr x2, [x2] // x2 = tasa (5%)
ldr x3, =tiempo
ldr x3, [x3] // x3 = tiempo (3 años)
// ======================================
// Cálculo del interés: (capital × tasa × tiempo) / 100
// ======================================
mul x4, x1, x2 // x4 = capital × tasa
mul x4, x4, x3 // x4 = capital × tasa × tiempo
mov x5, #100
udiv x0, x4, x5 // x0 = interés final (dividido entre 100)
// ======================================
// Mostrar el mensaje "Interés generado: "
// ======================================
mov x0, #1 // stdout
ldr x1, =mensaje
mov x2, #18 // longitud del mensaje
mov x8, #64 // syscall write
svc #0
// ======================================
// Convertir el resultado numérico a string
// ======================================
ldr x1, =resultado_str
mov x0, x4 // pasar el resultado a convertir
bl int_to_str
// ======================================
// Mostrar el resultado numérico
// ======================================
mov x0, #1 // stdout
ldr x1, =resultado_str
mov x2, #64 // longitud máxima
mov x8, #64 // syscall write
svc #0
// ======================================
// Imprimir salto de línea final
// ======================================
mov x0, #1
ldr x1, =newline
mov x2, #1
mov x8, #64
svc #0
// ======================================
// Terminar el programa
// ======================================
mov x8, #93 // syscall exit
mov x0, #0 // código de salida 0
svc #0
// ============================================
// Función: int_to_str
// Convierte un entero en su representación ASCII
// Entrada:
// x0 = número a convertir
// x1 = dirección del buffer de salida
// ============================================
int_to_str:
mov x2, #0 // contador de dígitos
mov x3, #10 // divisor
mov x4, x0 // copia del número
// Caso especial para cero
cbz x4, poner_cero
loop_conv:
udiv x5, x4, x3 // x5 = número / 10
msub x6, x5, x3, x4 // x6 = número % 10 (dígito)
add x6, x6, #'0' // convertir a ASCII
strb w6, [x1, x2] // almacenar dígito
add x2, x2, #1 // incrementar contador
mov x4, x5 // actualizar número
cbnz x4, loop_conv // continuar si no es cero
// Invertir la cadena (porque los dígitos se generan en orden inverso)
mov x5, #0 // índice inicial
sub x6, x2, #1 // índice final
inv_loop:
cmp x5, x6
b.ge listo // terminar si los índices se cruzan
ldrb w7, [x1, x5] // intercambiar dígitos
ldrb w8, [x1, x6]
strb w8, [x1, x5]
strb w7, [x1, x6]
add x5, x5, #1 // mover índices
sub x6, x6, #1
b inv_loop
poner_cero:
mov w5, #'0'
strb w5, [x1] // almacenar '0'
mov x2, #1 // longitud = 1
listo:
mov w5, #0
strb w5, [x1, x2] // terminador nulo
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment