Files
2024-10-31 14:30:55 -04:00

131 lines
2.5 KiB
ArmAsm

# COS035 - HW3
# Nicholas Pease
# 31 OCT 2024
.data:
inputUInt: .space 32
.text:
main:
li a7, 5 # input unsigned int
la a0, inputUInt
ecall
mv s2, a0
mv t0, a0
jal hotpo_i # hotpo_i (non recursive)
li a7, 11
li a0, 0x0a
ecall # Newline
mv t0, s2
li t4, 1 # RECUR DEPTH
jal hotpo_r # hotpo_r (recursive)
li a7, 10
ecall # Exit program
# Collaz Conjecture (Iterative)
# t0: input int
# t1: 2 (divide by 2)
# t2: 3 (const) (odd multiply)
# t3: 1 (const) (odd add)
hotpo_i:
li a7, 1
ecall # Display input int
li a7, 11
li a0, 0x0a
ecall # Newline
li t1, 2
li t2, 3
li t3, 1
hotpoi_whileLoop:
ble t0, t3, hotpoi_endLoop # end loop if less than 1
remu t5,t0,t1 # t5 = t0(int) % 2 remainder
bnez t5, hotpoi_odd # Odd Number Check
hotpoi_even:
srl t0, t0,t3
#divu t0, t0, t1
j hotpoi_end
hotpoi_odd:
mul t0, t0, t2
add t0, t0, t3
hotpoi_end:
li a7, 1
mv a0, t0
ecall
li a7, 11
li a0, 0x0a
ecall
j hotpoi_whileLoop
hotpoi_endLoop:
ret
# Collaz Conjecture (Recursive)
# t0: input
# t1: 2 (divide by 2)
# t2: 3 (const) (odd multiply)
# t3: 1 (const) (odd add)
# t4: Recursion Depth (static so "global")
hotpo_r:
mv a0, t0
li a7, 1
ecall # Display current int
li a7, 11
li a0, 0x09
ecall # Tab
mv a0, t4
li a7, 1
ecall # Display current depth
li a7, 11
li a0, 0x0a
ecall # Newline
li t1, 2
li t2, 3
li t3, 1
ble t0, t3, hotpor_ret1 # return 1
addi t4, t4, 1 # increment depth before odd/even jump
remu t5,t0,t1 # t5 = t0(int) % 2 remainder
bnez t5, hotpor_odd # Odd Number Check
hotpor_even:
srl t0, t0,t3
addi sp,sp,-4
sw ra, 0(sp) # save current loc
jal hotpo_r
j hotpor_end
hotpor_odd:
mul t0, t0, t2
add t0, t0, t3
addi sp,sp,-4
sw ra, 0(sp) # save current loc
jal hotpo_r
j hotpor_end
hotpor_ret1: # returns 1
li t4, 1 # return value
lw ra, 0(sp) # restore last call
addi sp, sp, 4
ret
hotpor_end:
sub t4, t4, t3 # decrement depth
lw ra, 0(sp) # restore last call
addi sp, sp, 4
ret