Commit 14f2a283 authored by kjc015's avatar kjc015
Browse files

Lab 06, Complete

parent 868565cc
all: collatz CC = gcc
CC=gcc CFLAGS = -g -Wall -Werror
CFLAGS=-Wall -Werror EXE = collatz
all: $(EXE)
collatz: collatz.o collatz: collatz.o
$(CC) $(CFLAGS) collatz.o -o collatz <tab>$(CC) -o collatz collatz.o
collatz.o: collatz.c
<tab>$(CC) $(CFLAGS) -c collatz.c -o collatz.o
# Student name: Kyle Chrysler
#
#
# This program uses two different procedures, collatz and find length, to compute the
# Collatz procedure.
#
.data
printString: .asciz " ==> "
ivar: .word 1
return: .asciz "\n"
.text
main:
li s1, 100 #Max values
la t1, ivar #Keep track of i variable
lw s2, 0(t1)
jal loop
loop:
beq s2, s1, exit #If it reaches max value, go to exit condition
mv a0, s2 #Move i to be printed
li a7, 1
ecall
la a0, printString
li a7, 4
ecall
mv s4, s2 #s4 is value used in find length computations (n)
j findlength
back:
mv a0, s3 #storing count in a0 to be printed
li a7, 1
ecall
la a0, return #printing a new line
li a7, 4
ecall
addi s2, s2, 1 #increment i
j loop
findlength:
li t0, 1
li s3, 1
bgt s4, t0, while #jump to while if your n is not 1
while:
li t0, 1
ble s4, t0, back #if n is 1, return and print it out
addi s3, s3, 1 #increment our count since it was not 1
j collatz
collatz:
li t3, 0
li t1, 1
li t2, 2
beq s4, t1, one #jump to my condition where n is one
rem t4, s4, t2 #check remainder of n
beq t4, t3, even #jump to even case
beq t4, t1, odd #jump to odd case
one:
li s4, 1 #make n value 1
j back
even:
li t0, 2
div s4, s4, t0 #divide current n value by 2
j while
odd:
li t0, 3
mul t1, s4, t0 #compute 3n
addi s4, t1, 1 #compute 3n+1
j while
exit:
li a7, 10
ecall
\ No newline at end of file
...@@ -125,7 +125,7 @@ main: ...@@ -125,7 +125,7 @@ main:
# call to xfind # call to xfind
li a7, 34 li a7, 34 #printing out hex value
ecall ecall
...@@ -143,14 +143,14 @@ xfind: ...@@ -143,14 +143,14 @@ xfind:
# if found, return the address of the first match # if found, return the address of the first match
# if not found, return 0 (NULL) # if not found, return 0 (NULL)
lb t2, 0(a0) lb t2, 0(a0) #load the first character of the string into t2
beq t2, a1, exit beq t2, a1, exit #branch if the character is found
beq t2, zero, exitNull beq t2, zero, exitNull #branch if we reach the end of the string (null character)
addi a0, a0, 1 addi a0, a0, 1 #increment to next character in string
j xfind j xfind
exitNull: exitNull:
li a0, 0 li a0, 0 #load null character (0) into
exit: exit:
jr ra jr ra #return to original function call
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment