-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path29
77 lines (61 loc) · 1.7 KB
/
29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
--29
// Descripción: Programa que cuenta el número de bits activados en un número.
// Definición de cadenas
.data
msg_ingreso: .string "Ingrese un número: "
msg_resultado: .string "Número de bits activados: %d\n"
msg_binario: .string "Representación binaria: "
msg_bit: .string "%d"
msg_newline: .string "\n"
formato_int: .string "%d"
numero: .word 0
.text
.global main
.align 2
main:
// Prólogo
stp x29, x30, [sp, -16]!
mov x29, sp
// Solicitar número
adr x0, msg_ingreso
bl printf
// Leer número
adr x0, formato_int
adr x1, numero
bl scanf
// Cargar número
adr x0, numero
ldr w1, [x0]
mov w19, w1 // Guardar copia para mostrar binario
// Contador de bits
mov w2, #0 // Inicializar contador
contar_loop:
cbz w1, fin_conteo // Si el número es 0, terminar
and w3, w1, #1 // Obtener bit menos significativo
add w2, w2, w3 // Sumar al contador si es 1
lsr w1, w1, #1 // Desplazar a la derecha
b contar_loop
fin_conteo:
// Mostrar resultado (número de bits activados)
mov w1, w2
adr x0, msg_resultado
bl printf
// Mostrar representación binaria
adr x0, msg_binario
bl printf
mov w20, #32
mostrar_bits:
sub w20, w20, #1
lsr w21, w19, w20
and w1, w21, #1
adr x0, msg_bit
bl printf
cmp w20, #0
b.ne mostrar_bits
// Nueva línea después de la representación binaria
adr x0, msg_newline
bl printf
// Retorno
mov w0, #0
ldp x29, x30, [sp], 16
ret