Receiving and Saving Exponential Values in an Array
.data
P: .word 8
R: .word -1 # R will be stored here
.text
addi $11, $0, 0x2000 # load start address of data in $11
addi $8, $0, 1
sw $8, 4($11) # save a 1 in the result
lw $9, 0($11) # load exponent in $9
addi $10, $0, 6 # load base (6) in $10
addi $11, $0, 17 # load a 17 in $11 for the modulus calculations
loop1:
beq $9, $0, end # if exponent is zero, end
andi $12, $9, 1 # else, get lowest bit value
beq $12, $0, next # if the bit is zero, go to next
add $16, $0, $8 # multiply current value of R by current base power of 2
add $17, $0, $10
jal multiply
add $8, $0, $18
next:
add $16, $0, $10 # elevate the base to next power of 2
add $17, $0, $10
jal multiply
add $10, $0, $18
jal mod17 # calculate the modulus of current power exponent
srl $9, $9, 1 # advance to next bit in exponent
j loop1 # repeat
#calculate the multiplication of $16 and $17, return result in $18
multiply:
addi $18, $0, 0
beq $16, $0, mulret # if $16 is zero, result is zero
loop2:
beq $17, $0, mulret # if $17 is zero, return
add $18, $18, $16 # add $16
addi $17, $17, -1
j loop2 # repeat while $17 is not zero
mulret:
jr $31
#calculate the modulus 17 of register $10, returns modulus in $10
mod17:
slt $12, $10, $11 # if num < 17, it's already the modulus
bne $12, $0, return
sub $10, $10, $11 # subtract num = num - 17
j mod17 # repeat
return:
jr $31
end:
add $10, $0, $8 # calculate modulus 17 of result
jal mod17
addi $9, $0, 0x2004 # load address of R in $9
sw $10, 0($9) # save result
.data
T: .word 12
best_matching_score: .word -1 # best score = ? within [0, 32]
best_matching_count: .word -1 # how many patterns achieve the best score?
Pattern_Array: .word 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20
Score_Array: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
.text
addi $11, $0, 0x2000 # load start address of data in $11
addi $8, $11, 0xC # load address of array in $8
lw $9, 0($11) # load word to match in $9
lw $10, 4($11) # load best matching score in $10
addi $11, $0, 20 # number of words in array in $11
loop1:
lw $12, 0($8) # load word from array
xor $12, $12, $9 # compare the word to match with a word from an array
add $13, $0, $0 # we will count the bits in $13 (hamming distance)
count:
beq $12, $0, endcount
andi $14, $12, 1 # test the rightmost bit
add $13, $13, $14 # if it's 1, count one more bit
next:
srl $12, $12, 1 # move next bit to the right
j count # repeat
endcount:
addi $14, $0, 32
sub $14, $14, $13 # calculate score = 32 - hamming distance
slt $13, $14, $10 # if the score is below the best, skip
bne $13, $0, skip
add $10, $0, $14 # else, save as best score
skip:
sw $14, 80($8) # save the score in the score array
addi $8, $8, 4 # advance to next position in array
addi $11, $11, -1 # repeat for all words in array
bne $11, $0, loop1
addi $8, $0, 0x2004 # load address of best score in $8
sw $10, 0($8) # save best score
addi $8, $0, 0x205C # save start address of score array in $8
addi $9, $0, 20 # number of words in array in $9
addi $11, $0, 0 # we will count the number of highest scores with $11
loop2:
lw $12, 0($8) # load score from array
bne $10, $12, nxtpos # if it's not equal to the best score, go to next position
addi $11, $11, 1 # if they are equal, increment number of highest scores
nxtpos:
addi $8, $8, 4 # advance to next position in array
addi $9, $9, -1 # repeat for all words in array
bne $9, $0, loop2
addi $8, $0, 0x2008 # load address of best count in $8
sw $11, 0($8) # save best count