Last active
April 11, 2025 00:51
-
-
Save Joc193/62cd29bd804f1028879c6f1f89d48225 to your computer and use it in GitHub Desktop.
Programa ARM64 - Encontrar la suma de todos los enteros pares del 2 al 2,000.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* --------------------------------------------------------------------------------- | |
* Lenguajes de Interfaz en TECNM Campus ITT | |
* Autor: Jocelyn Alvarez Paniagua | |
* Fecha: 2025-04-10 | |
* Descripción: Calcular e imprimir la suma de todos los enteros pares del 2 al 2000. | |
* Demostración: [ https://asciinema.org/a/IHviXOqILLnvrTB54imJueUgO ] | |
* --------------------------------------------------------------------------------- | |
*/ | |
.global _start | |
.section .data | |
mensaje: .asciz "La suma de los pares del 2 al 2000 es: " | |
newline: .asciz "\n" | |
buffer: .skip 32 | |
.section .text | |
_start: | |
mov x20, #2 // contador desde 2 (primer número par) | |
mov x21, #2000 // límite superior | |
mov x22, #0 // acumulador de la suma | |
loop: | |
add x22, x22, x20 // suma acumulada en x22 | |
add x20, x20, #2 // siguiente número par | |
cmp x20, x21 | |
ble loop | |
// imprimir mensaje inicial | |
mov x0, #1 | |
ldr x1, =mensaje | |
mov x2, #37 | |
mov x8, #64 | |
svc #0 | |
// convertir suma a texto e imprimirla | |
mov x0, x22 | |
bl int_to_ascii | |
mov x0, #1 | |
ldr x1, =buffer | |
mov x2, x23 | |
mov x8, #64 | |
svc #0 | |
// salto de línea | |
mov x0, #1 | |
ldr x1, =newline | |
mov x2, #1 | |
mov x8, #64 | |
svc #0 | |
// salir | |
mov x0, #0 | |
mov x8, #93 | |
svc #0 | |
// -------------------------------------------------------- | |
// Función: int_to_ascii | |
// Convierte número en x0 a ASCII en 'buffer' y devuelve longitud en x23 | |
// -------------------------------------------------------- | |
int_to_ascii: | |
mov x1, x0 | |
ldr x2, =buffer | |
add x2, x2, #31 // empezamos desde el final del buffer | |
mov x23, #0 | |
itoa_loop: | |
mov x3, #10 | |
udiv x4, x1, x3 | |
msub x5, x4, x3, x1 | |
add x5, x5, #'0' | |
strb w5, [x2] | |
sub x2, x2, #1 | |
add x23, x23, #1 | |
mov x1, x4 | |
cmp x1, #0 | |
bne itoa_loop | |
add x2, x2, #1 | |
mov x3, #0 | |
ldr x5, =buffer | |
copy_ascii: | |
ldrb w6, [x2, x3] | |
strb w6, [x5, x3] | |
add x3, x3, #1 | |
cmp x3, x23 | |
blt copy_ascii | |
ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment