Created
May 13, 2019 19:19
-
-
Save autinerd/0c0e25ac35f7a6907a9b5cae6db1224f to your computer and use it in GitHub Desktop.
Skalarprodukt MIPS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main: | |
addi $sp, $sp, -4 # stack für result | |
sw $zero, 0($sp) # result = 0 | |
addi $v0, $zero, 9 # syscall sbrk | |
addi $a0, $zero, 80 # allocate 20 words | |
syscall # malloc | |
or $a1, $v0, $zero # &A[0] | |
addi $a2, $a1, 40 # &B[0] | |
add $a3, $zero, $sp # result | |
addi $a0, $zero, 10 # n = 10 | |
add $t0, $zero, $a1 # i | |
addi $t1, $t0, 80 # | |
addi $t2, $zero, 0x7f # 1.0 | |
main_for: | |
beq $t0, $t1, main_for_end | |
sw $t2, 0($t0) | |
addi $t0, $t0, 4 | |
main_for_end: | |
jal dotprod # jump to dotprod and save position to $ra | |
lwc1 $f12, 0($sp) # result to print | |
ori $v0, $zero, 2 # print_float | |
syscall | |
ori $v0, $zero, 10 # exit | |
syscall | |
dotprod: # $a0 = n, $a1 = &A[0], $a2 = &B[0], $a3 = &result, $t0 = i | |
# Stack: $ra, sum => 8 | |
# $f0 = sum, $f1 = A[i], $f2 = B[i] | |
addi $sp, $sp, -8 | |
sw $ra, 4($sp) # Speichern der Rücksprungadresse | |
sw $zero, 0($sp) # Speichern von sum = 0.0 | |
slti $t0, $a0, 0 # $t0 = (n < 0) | |
beq $t0, $zero, dotprod_start # if n >= 0 then dotprod_start | |
addi $v0, $zero, -1 # return -1 | |
jr $ra # jump to $ra | |
dotprod_start: | |
or $t0, $zero, $zero # i = 0 | |
dotprod_loop: | |
beq $t0, $a0, dotprod_end # if i == n goto end | |
lwc1 $f0, 0($sp) # load sum to $f0 | |
sll $t1, $t0, 2 # i * 4 | |
add $t2, $a1, $t1 # $t2 = &A[i] | |
add $t3, $a2, $t1 # $t3 = &B[i] | |
lwc1 $f1, 0($t2) # load A[i] to $f1 | |
lwc1 $f2, 0($t3) # load B[i] to $f2 | |
mul.s $f1, $f1, $f2 # A[i] * B[i] | |
add.s $f0, $f0, $f1 # sum += (A[i] * B[i]) | |
swc1 $f0, 0($sp) # store $f0 to sum | |
dotprod_end: | |
lw $t0, 0($sp) # load sum to $t0 | |
sw $t0, 0($a3) # store sum to result | |
addi $sp, $sp, 8 # stack freigeben | |
or $v0, $zero, $zero # return 0 | |
jr $ra |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment