Skip to content

Instantly share code, notes, and snippets.

@Alex-JAML
Created April 11, 2025 06:35
Show Gist options
  • Select an option

  • Save Alex-JAML/d6e5d65f7f0a446668ef699553ce8c27 to your computer and use it in GitHub Desktop.

Select an option

Save Alex-JAML/d6e5d65f7f0a446668ef699553ce8c27 to your computer and use it in GitHub Desktop.
Codigo Assembly ARM64 para RaspbianOS
/*
* ---------------------------------------------------------------------------------
* Lenguajes de Interfaz en TECNM Campus ITT
* Autor: Jorge Alejandro Martinez Lopez
* Fecha: 2025-04-09
* Descripción: Ordena tres números enteros ingresados por el usuario en orden descendente
* Implementación con comparaciones y swaps
* Demostración: [ASCIINEMA.ORG/XXXXXX]
* ---------------------------------------------------------------------------------
*/
/*
* ----------------------------------------------
* C# "Ordenamiento de 3 números" (Referencia)
* ----------------------------------------------
* using System;
*
* class Program {
* static void Main() {
* Console.Write("Ingrese primer número: ");
* int a = int.Parse(Console.ReadLine());
* Console.Write("Ingrese segundo número: ");
* int b = int.Parse(Console.ReadLine());
* Console.Write("Ingrese tercer número: ");
* int c = int.Parse(Console.ReadLine());
*
* // Ordenar descendente
* if (a < b) { int temp = a; a = b; b = temp; }
* if (a < c) { int temp = a; a = c; c = temp; }
* if (b < c) { int temp = b; b = c; c = temp; }
*
* Console.WriteLine($"Ordenados: {a}, {b}, {c}");
* }
* }
*/
.global _start
.section .data
msg1: .asciz "Ingrese primer número: "
msg2: .asciz "Ingrese segundo número: "
msg3: .asciz "Ingrese tercer número: "
msgRes: .asciz "Ordenados: "
comma: .asciz ", "
newline: .asciz "\n"
buffer: .space 12
.section .bss
.lcomm a, 4 /* Primer número */
.lcomm b, 4 /* Segundo número */
.lcomm c, 4 /* Tercer número */
.section .text
_start:
/* Leer los tres números */
ldr x0, =msg1
bl print_string
bl read_int
ldr x1, =a
str w0, [x1]
ldr x0, =msg2
bl print_string
bl read_int
ldr x1, =b
str w0, [x1]
ldr x0, =msg3
bl print_string
bl read_int
ldr x1, =c
str w0, [x1]
/* Ordenar descendente (a >= b >= c) */
ldr w0, =a
ldr w1, =b
bl compare_swap /* if (a < b) swap(a, b) */
ldr w0, =a
ldr w1, =c
bl compare_swap /* if (a < c) swap(a, c) */
ldr w0, =b
ldr w1, =c
bl compare_swap /* if (b < c) swap(b, c) */
/* Mostrar resultados */
ldr x0, =msgRes
bl print_string
ldr w0, =a
bl print_int
ldr x0, =comma
bl print_string
ldr w0, =b
bl print_int
ldr x0, =comma
bl print_string
ldr w0, =c
bl print_int
bl print_newline
/* Terminar programa */
mov x0, #0
mov x8, #93
svc #0
/* ============ SUBRUTINAS DOCUMENTADAS ============ */
/**
* compare_swap - Compara y swap si es necesario
* @x0: Dirección del primer número
* @x1: Dirección del segundo número
*/
compare_swap:
ldr w2, [x0] /* Cargar primer valor */
ldr w3, [x1] /* Cargar segundo valor */
cmp w2, w3
bge compare_end /* No hacer swap si ya está ordenado */
str w3, [x0] /* Hacer swap de valores */
str w2, [x1]
compare_end:
ret
/**
* print_string - Imprime un string terminado en null
* @x0: Dirección del string
*/
print_string:
mov x1, x0
mov x2, #0
ps_loop:
ldrb w3, [x1, x2]
cbz w3, ps_print
add x2, x2, #1
b ps_loop
ps_print:
mov x0, #1
mov x8, #64
svc #0
ret
/**
* read_int - Lee un entero desde stdin
* Retorno: @w0 Entero leído
*/
read_int:
mov x0, #0
ldr x1, =buffer
mov x2, #12
mov x8, #63
svc #0
ldr x1, =buffer
mov w0, #0
mov w3, #10
ri_loop:
ldrb w2, [x1], #1
cmp w2, #10
beq ri_end
sub w2, w2, #'0'
madd w0, w0, w3, w2
b ri_loop
ri_end:
ret
/**
* print_int - Imprime un entero con signo
* @w0: Dirección del entero a imprimir
*/
print_int:
ldr w0, [x0] /* Cargar valor desde memoria */
stp x29, x30, [sp, #-32]!
mov x29, sp
mov x1, x29
add x1, x1, #31
mov w2, #0
strb w2, [x1]
cmp w0, #0
bge pi_pos
mov w2, #'-'
sub x1, x1, #1
strb w2, [x1]
neg w0, w0
pi_pos:
mov w3, #10
cmp w0, #0
bne pi_conv
mov w2, #'0'
sub x1, x1, #1
strb w2, [x1]
b pi_print
pi_conv:
cmp w0, #0
beq pi_print
udiv w2, w0, w3
msub w4, w2, w3, w0
add w4, w4, #'0'
sub x1, x1, #1
strb w4, [x1]
mov w0, w2
b pi_conv
pi_print:
mov x0, #1
mov x2, x29
add x2, x2, #32
sub x2, x2, x1
mov x8, #64
svc #0
ldp x29, x30, [sp], #32
ret
/**
* print_newline - Imprime un salto de línea
*/
print_newline:
ldr x0, =newline
b print_string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment