- #1
trouty323
- 24
- 0
Hello all. My task here was to have an array of floating point numbers, find the largest in the array, and store it back into the array. The first code shows a properly working integer interpretation of the task, and the second shows my interpretation of the floating point. I simply tried to swap out any instruction for integers to make it floating point, but I'm getting several errors. I didn't think it would be this complicated.
Code:
.data
.word 7, 39, 42, 16, 15, 21, 3, 2, 37, 11, 32, 28, 40, 27, 20
.text
.globl main
main:
lui $s0, 0x1001 # base address
add $t0, $t0, $zero # index count
add $t1, $t1, $zero # max element, $t1 = 0
addi $t5, $t5, 15 # number of elements
add $t2, $t2, $s0 # offset plus base
Loop:
beq $t0, $t5, Exit # branch if counter is equal to number of elements in array
lw $t3, 0($t2) # reading first element
addi $t0, $t0, 1 # increment counter by 1
addi $t2, $t2, 4 # increment offset by 4
slt $t4, $t1, $t3 # $t4 = 1 if $t1 is less than $t3, otherwise $t4 = 0
beq $t4, $zero, Loop # branch to loop if value in $t4 is equal to value in $zero
add $t1, $t3, $zero
j Loop
Exit:
sw $t1, 0($t2) # store highest back into array
li $v0, 10 # syscall to terminate
syscall
Code:
.data
.float 7.5, 39.2, 42.4, 16.1, 15.3, 21.6, 3.7, 2.6, 37.7, 11.5, 32.2, 28.3, 40.8, 27.7, 20.3
.text
.globl main
main:
lui $f11, 0x1001 # base address
add $t0, $t0, $zero # index count
add.s $f4, $f4, $zero # max element, $f4 = 0
addi $t5, $t5, 15 # number of elements
add.s $f5, $f5, $f11 # offset plus base
Loop:
beq $t0, $t5, Exit # branch if counter is equal to number of elements in array
l.s $f6, ($f5) # reading first element
addi $t0, $t0, 1 # increment counter by 1
addi.s $f5, $f5, 4 # increment offset by 4
c.lt.s $t4, $f4, $f6 # $t4 = 1 if $f4 is less than $f6, otherwise $t4 = 0
beq $t4, $zero, Loop # branch to loop if value in $t4 is equal to value in $zero
add $f4, $f6, $zero
j Loop
Exit:
sw $f4, 0($f5) # store highest back into array
li $v0, 10 # syscall to terminate
syscall