I am doing this project that picks the top color of an array of numbers from 1 t
ID: 3674979 • Letter: I
Question
I am doing this project that picks the top color of an array of numbers from 1 to 7. I have this code done and it works almost all times but I think sometimes gets wrong. We are using MISASIM that works with MIPS, but is limited to the instructions that I uploaded with the files. I would like to know how using that code I can make it faster and solve the problem that makes sometimes give me an error. Thank you
# T o p o f P i l e
#
#
# This routine finds the color of the part on top of a pile.
#
# <3/5/2016> <>
# $2 - answer
# $5 - north pixel color
# $6 - east pixel color
# $7 - south pixel color
# $8 - west pixel color
# $9 - current address
# $10 - temp value
# $12 - current Nums address
# $14 - number 7
# $16
# $17
# $18
.data
Array: .alloc 1024
Nums: .word 50462976, 117835012 # array of color values
.text
TopOfPile: addi $1, $0, Array # point to array base
swi 545 # generate pile
addi $2, $0, 1 # always guess the first color
Start: addi $11, $0, Nums # base address of Nums
addi $3, $0, 2048 # pointer
addi $13, $0, 0 # counter of overlaps
addi $14, $0, 6 # value of 7
addi $15, $0, 4031 # value of 4031
Loop1: lbu $4, Array($3) # current color
addu $9, $1, $3
beq $4, $0, Continue
addu $2, $0, $3
#swi 547 # highlights current pixel
addi $10, $9, -64
lbu $5, 0($10) # north pixel color
beq $5, $0, Continue
addi $10, $9, -1
lbu $6, 0($10) # west pixel color
beq $6, $0, Continue
lbu $12, Nums($5)
beq $12, $0, Continue
addi $10, $9, 64
lbu $7, 0($10) # south pixel color
addi $10, $9, 1
lbu $8, 0($10) # east pixel color
beq $5, $6, Continue
bne $5, $7, Continue
bne $4, $6, Case2
Case1: bne $4, $8, Continue # Case 1 is checking for the first kind of intersection
lbu $12, Nums($5)
beq $12, $0, Continue
sb $0, Nums($5) # deletes color from array
lbu $2, Nums($4) # loads color to answer $2
addi $13, $13, 1
beq $13, $14, End
Continue: bne $3, $15, Continue2
addi $3, $0, 64
j Loop1
Continue2: addi $3, $3, 1
j Loop1
Case2: bne $5, $4, Continue # Case 2 checks for the second kind of intersection
bne $7, $4, Continue
lbu $12, Nums($8)
beq $12, $0, Continue
sb $0, Nums($6) # deletes color from array
addi $13, $13, 1
lbu $2, Nums($4) # loads color to answer $2
beq $13, $14, End
j Continue # lbu $2, Nums($4)
End: swi 546 # submit answer and check
jr $31 # return to caller
Explanation / Answer
In case1 , case2, we are using "sb statement" will handles signed integers ... , but using sbu statement, we can handle for unsigned integers .. sometimes if we have any negative numbers, then it will fail. So it is better to make sbu statement.
Case1: bne $4, $8, Continue # Case 1 is checking for the first kind of intersection
lbu $12, Nums($5)
beq $12, $0, Continue
sbu $0, Nums($5) # deletes color from array
lbu $2, Nums($4) # loads color to answer $2
addi $13, $13, 1
beq $13, $14, End
Remaining code, seemed to be correct and working fine.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.