Last active
April 10, 2025 23:48
-
-
Save Joc193/e06f9025b5b3133464c38c5ce2c47628 to your computer and use it in GitHub Desktop.
Programa ARM64 - Suma de los enteros del 1 al 20
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 los enteros del 1 al 20. | |
* Demostración: [ https://asciinema.org/a/QedkzJO2ErYu9Om3ElYLAwTrH ] | |
* --------------------------------------------------------------------------------- | |
*/ | |
.global _start | |
.section .data | |
mensaje: .asciz "La suma del 1 al 20 es: " | |
newline: .asciz "\n" | |
buffer: .skip 20 | |
.section .text | |
_start: | |
mov x20, #1 // contador desde 1 | |
mov x21, #20 // límite | |
mov x22, #0 // acumulador de la suma | |
loop: | |
add x22, x22, x20 // acumular en x22 | |
add x20, x20, #1 // siguiente número | |
cmp x20, x21 | |
ble loop | |
// imprimir mensaje fijo | |
mov x0, #1 | |
ldr x1, =mensaje | |
mov x2, #26 | |
mov x8, #64 | |
svc #0 | |
// convertir x22 a ASCII y mostrar | |
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 | |
// -------------------------------------------------------- | |
// 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, #19 | |
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