From 5fe8381c1fb7ffb5adc64400e9679296deb324ac Mon Sep 17 00:00:00 2001 From: Matthew Faltys Date: Fri, 14 Feb 2020 16:31:07 +0000 Subject: [PATCH] Add working proj 1 program --- prj1.asm | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 prj1.asm diff --git a/prj1.asm b/prj1.asm new file mode 100644 index 0000000..6b53bbd --- /dev/null +++ b/prj1.asm @@ -0,0 +1,63 @@ +# Declare main as a global function +.globl main + +# All program code is placed after the +# .text assembler directive +.text + +# The label 'main' represents the starting point +main: + la $a0, arr # $a0 based address of array + li $a1, 1 # $a1 number to be found (checked) - change the value (7) to test + li $a2, 6 # $a2 max array index to be checked - change the value (3) to test + li $s0, 0 # set counter to 0 + jal lookup # hop to lookup function + # print out result + li $v0, 1 # print_int syscall code = 1 + move $a0, $s0 # load integer into $ao + syscall + j exit # exit out of program + +# lookup (int arr[], int num, int cnt). +# arr[] = 5 2 1 4 6 3 +# 1. lookup (arr, 5, 3) -> 0 (the index of ‘5’ in the array) +# 2. lookup (arr, 1, 3) -> 2 (the index of ‘1’ in the array) +# 3. lookup (arr, 7, 6) -> -1 (‘7’ is not found in the array) +# 4. lookup (arr, 4, 3) -> -1 (the last parameter indicates to check only first 3 values in the +lookup: + addi $t0, $a2, 1 # store one higher value + + # loop to find index + beq $s0, $t0, setoob # jump to oob function if we overrun array + + # find current array value + addi $t1, $s0, 0 # load index into $t1 + sll $t1, $t1, 2 # multiply the offset + add $t1, $t1, $a0 # add the offset to the base address + lw $t2, 0($t1) # get current array value: aka arr[$s0] + + beq $a1, $t2, setval # check if this is the value we are looking for + addi $s0, $s0, 1 # incriment counter + j lookup # back to beginning of loop + +# set value function +setval: + jr $ra + +# set out of bounds function +setoob: + li $s0, -1 # index is oob, set to -1 + jr $ra # jump back to ra in main + +exit: + + + +# All memory structures are placed after the +# .data assembler directive +.data +arr: .word 5, 2, 1, 4, 6, 3 # change array to test +#arr: .word 5, 2, 1, 4, 6, 3 # change array to test +str_found: .asciiz "found" +str_not_found: .asciiz "not_found" +nl: .asciiz "\n"