Add ES/Lab/LabMidsem/PerfectPerf.asm

This commit is contained in:
aadit 2025-09-04 13:13:51 +05:30
parent 55d79fbe6a
commit f91427d180

View file

@ -0,0 +1,72 @@
; question - find all divisors of a number like 6, and store in data memory. find if number is perfect or not. if sum of divisors is the number itself, then perfect. if perfect, store FF next to divisor list, otherwise store 11
; this version includes branching and works properly.
AREA RESET, DATA, READONLY
EXPORT __Vectors
__Vectors
DCD 0x10001000
DCD Reset_Handler
ALIGN
AREA MYCODE, CODE, READONLY
ENTRY
EXPORT Reset_Handler
Reset_Handler
LDR R0, =9
LDR R1, =0x10001000
BL DivisorsSum
MOV R2, R0
MOV R3, R1
LDR R4, =9
CMP R2, R4
BNE not_perfect
LDR R5, =0x000000FF
STR R5, [R3]
B STOP
not_perfect
LDR R5, =0x00000011
STR R5, [R3]
STOP
B STOP
DivisorsSum
PUSH{R4-R7, LR}
MOV R6, R0
MOV R7, R1
MOV R5, #0
LSRS R3, R6, #1
MOV R4, #1
div_loop
CMP R4, R3
BGT div_end
MOV R0, R6
MOV R1, R4
BL IsDivisible
CMP R0, #1
BNE div_skip
STR R4, [R7], #4
ADD R5, R5, R4
div_skip
ADD R4, R4, #1
B div_loop
div_end
MOV R0, R5
MOV R1, R7
POP{R4-R7, LR}
BX LR
IsDivisible
PUSH{LR}
MOV R2, R0
id_loop
SUBS R2, R2, R1
BPL id_loop
ADDS R2, R2, R1
MOV R0, #0
CMP R2, #0
MOVEQ R0, #1
POP{LR}
BX LR
END