Skip to content

Instantly share code, notes, and snippets.

@arathunku
Created May 23, 2013 20:27
Show Gist options
  • Save arathunku/5639163 to your computer and use it in GitHub Desktop.
Save arathunku/5639163 to your computer and use it in GitHub Desktop.
Dot product assembly
# Test:
# A = [1,2,3,4,5]
# B = [2,4,6,8,10]
# R = A.B = 2+8+18+32+50 = 110
.data
vectorA: .space 128
vectorB: .space 128
binaryA: .space 128
binaryB: .space 128
ask: .asciiz "How many values will the vector have?\n"
int: .asciiz "Give number: "
rozm_A: .asciiz "\nHow many numbers vector?:\n "
num_A: .asciiz "Numbers for first vector.\n"
num_B: .asciiz "Numbers for second vector.\n"
.text
main:
li $s0, 0
li $v0, 4
la $a0, rozm_A
syscall
li $v0, 5
syscall
move $s0, $v0 #store how many numbers we need to take from user
li $v0, 4
la $a0, num_A
syscall
la $a0, vectorA
la $a2, binaryA
add $a1, $s0, $zero
jal move_values
li $v0, 4
la $a0, num_B
syscall
la $a0, vectorB
la $a2, binaryB
add $a1, $s0, $zero
jal move_values
li $t0, 0 #track binary array
li $t1, -4 #track vectorA
li $t2, -4 #track vectorB
li $t3, 0 #check if stop looping
li $s4, 0 #store result
multiply_loop:
bge $t3, $s0, EXIT
lw $t4, binaryA($t0)
lw $t5, binaryB($t0)
beqz $t4, ommit_a
add $t1, $t1, 4
ommit_a:
beqz $t5, cont
add $t2, $t2, 4
beqz $t4, cont
make_work:
lw $t4, vectorA($t1)
lw $t5, vectorB($t2)
mul $s3, $t4, $t5
add $s4, $s4, $s3
j cont
cont:
add $t0, $t0, 4
add $t3, $t3, 1
j multiply_loop
move_values:
#beginning, move data to temporary adress for that loop
# a0 - beginning of reserved space
#,a1 - number of digits
move $t0, $a0
move $t1, $a1
move $t2, $a2
li $t4,0 #hold how many digits in vectorA/B
# when to stop!
li $t6, 0
give_values:
#get out if the number is equal or greater than the number of wanted digits
bge $t6, $t1, return
#request number
li $v0, 4
la $a0, int
syscall
#read number int
li $v0, 5
syscall
move $t9, $v0
# store word
# 0 means adress is not moved by any value
#jal normal_number
beqz $t9, zero
normal_number:
sw $t9, 0($t0)
li $t8, 1
sb $t8, 0($t2)
addi $t0, $t0, 4
j continue
zero:
li $t8, 0
sb $t8, 0($t2)
continue:
addi $t2, $t2, 4
addi $t6, $t6, 1
j give_values
EXIT:
move $a0, $s4
li $v0, 1
syscall
li $v0,10
syscall
return:
jr $ra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment