Question: Hello, I am writing a mips program which implements 2 functions: a. A function called match which takes in $a0 and $a1 two positive integers
Hello,
I am writing a mips program which implements 2 functions:
a. A function called "match" which takes in $a0 and $a1 two positive integers and returns in $v0 1 if they match, which by our definition means that the smaller of the two integers is greater than half of the larger of the two integers, and 0 otherwise.
b. A function called "find" which takes in $a0 the starting address of a positive integer array $a1 an integer value, $a2 the length of the array, and prints out all values in the array that matches $a1, obviously by calling the "match" function.
And this was the main portion that was provided:
.data list: .word 193, 77, 31, 22, 51, 17, 16, 3, 0, 359, 67, 82, 9, 17, 1 #this is the list to check against check_values: .word 13, 79, 12, 5, 0 newline: .asciiz " " whitespace: .asciiz " " .text .globl main main: la $s0, list #load address of list la $s1, check_values #load address of check_values li $t0, 0 #counter li $s2, 5 #s2=length of check_values[] ori $a0, $s0, 0 #a0=starting address of list. This does NOT change ori $a2, $0, 15 #a2=length of list. This does NOT change either loop: sll $t1, $t0, 2 add $t1, $t1, $s1 lw $t2, 0($t1) ori $a1, $t2, 0 #a1=integer to check addi $sp, $sp, -12 #make space on stack to hold three temporary registers sw $t0, 0($sp) #store temporary registers on stack sw $t1, 4($sp) sw $t2, 8($sp) jal find #call function lw $t2, 8($sp) #restore temporary registers lw $t1, 4($sp) lw $t0, 0($sp) addi $sp,$sp,12 #restore stack pointer addi $t0, $t0, 1 #increment counter to iterate through loop bne $t0, $s2, loop exit: li $v0, 10 syscall find: #Insert find() function code here jr $ra match: #Insert match() function code here jr $ra
And this is what I have written so far:
.data
list: .word 193, 77, 31, 22, 51, 17, 16, 3, 0, 359, 67, 82, 9, 17, 1 #this is the list to check against
check_values: .word 13, 79, 12, 5, 0
newline: .asciiz " "
whitespace: .asciiz " "
.text
.globl main
main:
la $s0, list #load address of list
la $s1, check_values #load address of check_values
li $t0, 0 #counter
li $s2, 5 #s2=length of check_values[]
ori $a0, $s0, 0 #a0=starting address of list. This does NOT change
ori $a2, $0, 15 #a2=length of list. This does NOT change either
loop:
sll $t1, $t0, 2
add $t1, $t1, $s1
lw $t2, 0($t1)
ori $a1, $t2, 0 #a1=integer to check
addi $sp, $sp, -12 #make space on stack to hold three temporary registers
sw $t0, 0($sp) #store temporary registers on stack
sw $t1, 4($sp)
sw $t2, 8($sp)
jal find #call function
lw $t2, 8($sp) #restore temporary registers
lw $t1, 4($sp)
lw $t0, 0($sp)
addi $sp,$sp,12 #restore stack pointer
addi $t0, $t0, 1 #increment counter to iterate through loop
bne $t0, $s2, loop
exit:
li $v0, 10
syscall
find:
addi $sp, $sp, -24 #calling conventions
sw $s0, 120($sp)
sw $s1, 16($sp)
sw $s2, 12($sp)
sw $s3, 8($sp)
sw $s4, 4($sp)
li $s0, 0 #counter i
li $s4, 1 #register to compare with beq for Print_Element
loop1:
sll $s1, $s0, 2 #s1= 4xi
add $s1, $s1, $a0 #s1= &list[i]
lw $s2, 0($s1) #s2= list[i]
ori $a0, $s2, 0 #a0 = int being compared from list
sw $ra, 0($sp) #calling convention on ra
jal match #call match function
lw $ra, 0($sp) #restore ra
beq $v0, $s4, Print_Element #if v0= 1, jump to Print_Element
addi $s0, $s0, 1 #i++
bne $s0, $a2, loop1 #if i < 15, jump to loop1
exit1:
la $a3, newline #print newline when finished checking for matches on one element of check_values
li $v0, 4
syscall
lw $s0, 20($sp) #restore stack
lw $s1, 16($sp)
lw $s2, 12($sp)
lw $s3, 8($sp)
lw $s4, 4($sp)
addi $sp, $sp, 24
jr $ra #return to main
Print_Element:
ori $s3, $a0, 0 #s3 = matched value from list[i]
li $v0, 1
syscall #print s3
la $a3, whitespace #print whitespace between elements
li $v0, 4
syscall
addi $s0, $s0, 1 #i++
bne $s0, $a2, loop1 #if i < 15, jump to loop 1
j exit1 #if i = 15, jump to exit1
match:
addi $sp, $sp, -12 #calling conventions
sw $s0, 8($sp)
sw $s1, 4($sp)
sw $s2, 0($sp)
slt $s0, $a0, $a1 #s0= 1, if a0 < a1
beq $s0, $0, a0_GreaterThan #if a0 > a1, jump to a0_GreaterThan
sra $s1, $a1, 1 #halve a1
slt $v0, $s1, $a0 #v0= 1, if half of a1 is less than a0 (definition of match)
j exit2
a0_GreaterThan:
sra $s2, $a0, 1 #halve a0
slt $v0, $s2, $a1 #v0 = 1, if half of a0 is less than a1 (definition of match)
exit2:
lw $s0, 8($sp) #restore stack
lw $s1, 4($sp)
lw $s2, 0($sp)
addi $sp, $sp 12
jr $ra #return to find
...................................
I do not understand why my code is not running. It seems logically correct.
Thanks!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
