diff --git a/cos235-hw03-pease-nicholas-0001.tar.gz b/cos235-hw03-pease-nicholas-0001.tar.gz new file mode 100644 index 0000000..503441d Binary files /dev/null and b/cos235-hw03-pease-nicholas-0001.tar.gz differ diff --git a/cos235-hw03-pease-nicholas-0001/README b/cos235-hw03-pease-nicholas-0001/README new file mode 100644 index 0000000..a522a7c --- /dev/null +++ b/cos235-hw03-pease-nicholas-0001/README @@ -0,0 +1,17 @@ +COS235 HW3 +Nicholas Pease +31 OCT 2024 + + +Description: + +This is a program which runs the Collaz conjecture in RISV assembly in two formats: iterative and recursive. + +Execution: + +This program requires RARS (https://github.com/TheThirdOne/rars) and JDK. + +To execute the program with RARS, run the following command +java -jar rars1_6.jar hw.s + +With rars1_6 being the current version of RARS and in the same directory as the file \ No newline at end of file diff --git a/cos235-hw03-pease-nicholas-0001/hw.s b/cos235-hw03-pease-nicholas-0001/hw.s new file mode 100644 index 0000000..2ecdc67 --- /dev/null +++ b/cos235-hw03-pease-nicholas-0001/hw.s @@ -0,0 +1,131 @@ +# 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 \ No newline at end of file diff --git a/hw.s b/hw.s deleted file mode 100644 index 24a491f..0000000 --- a/hw.s +++ /dev/null @@ -1,68 +0,0 @@ -# COS035 - HW3 -# Nicholas Pease -# 31 OCT 2024 -.data: -inputUInt: .space 32 - -.text: -main: - li a7, 5 # input unsigned int - la a0, inputUInt - ecall - - mv t0, a0 - jal hotpo_i # hotpo_i (non recursive) - - li a7, 11 - li a0, 0x0a - ecall # Newline - - li a7, 10 - ecall # Exit program - -# Collaz Conjecture (Iterative) -# t0: input int -# t1: 2 (divide by 2) -# t2: 0 (const) (compare if even [n / 2] remainder) -# t3: 3 (const) (odd multiply) -# t4: 1 (const) (odd add) -hotpo_i: - mv a0, t0 - li a7, 1 - ecall # Display input int - - li a7, 11 - li a0, 0x0a - ecall # Newline - - li t1, 2 - li t2, 0 - li t3, 3 - li t4, 1 - hotpoi_whileLoop: - ble t0, t4, hotpoi_endLoop # end loop if less than 1 - remu t5,t0,t1 # t5 = t0(int) % 2 remainder - - bne t5, t2, hotpoi_odd - - hotpoi_even: - srl t0, t0,t4 - #divu t0, t0, t1 - j hotpoi_end - - hotpoi_odd: - mul t0, t0, t3 - add t0, t0, t4 - - hotpoi_end: - li a7, 1 - mv a0, t0 - ecall - li a7, 11 - li a0, 0x0a - ecall - - j hotpoi_whileLoop - - hotpoi_endLoop: - ret \ No newline at end of file diff --git a/test b/test new file mode 100755 index 0000000..dfe587d Binary files /dev/null and b/test differ diff --git a/test.c b/test.c new file mode 100644 index 0000000..87eac22 --- /dev/null +++ b/test.c @@ -0,0 +1,37 @@ +# include +# include // for uint32_t +# include // for PRIu32 in printf +// " Half or triple - plus - one " sequence , iterative . +uint32_t hotpo_i ( uint32_t n ) { + printf ("%" PRIu32 "\n" , n ) ; + while ( n > 1) { + if ( n % 2 == 0) n /= 2; + else n = 3 * n + 1; + printf ("%" PRIu32 "\n" , n ) ; + } + return n ; +} + +// " Half or triple - plus - one " sequence , recursive . +uint32_t hotpo_r ( uint32_t n ) { + static uint32_t recur_depth = 1; // recursion depth + uint32_t next_n ; + printf ("%" PRIu32 "\t %" PRIu32 "\n ",n,recur_depth) ; + if ( n == 1) return 1; + recur_depth++; + if ( n % 2 == 0) next_n = hotpo_r ( n /2) ; + else next_n = hotpo_r (3* n + 1) ; + recur_depth--; + return next_n ; +} + +// Read an unsigned integer from stdin and invoke both versions of +// hotpo on it . +int main () { + uint32_t init_n; + scanf ("%"PRIu32, &init_n) ; + hotpo_i ( init_n ) ; + putchar ('\n') ; + hotpo_r ( init_n ) ; + return 0; +} \ No newline at end of file