Aiden
Aiden

Reputation: 11

Dot Product without using branch inside the function

I am stuck at the Dot Product without using branch inside the function. I just think that I will go to the each element of the two array and calculate it step by step. Is there any ways that possible to do that? Thank you so much! DotProduct function Please read and understand the following specifications of the DotProduct function

A function name, a label in your code must be DotProduct.

The function has three parameters passed in registers $a0, $a1 and $a2. The parameters are:

Address of the first vector in $a0.

Address of the second vector in $a1

Number of components of the vectors in $a2. (You cannot always assume that the vectors will have three components, but it is safe to assume that the vectors will have the same number of components)

The function must return the calculated dot product in $v0.

The function must not do any other output and there must not be any branches or jumps to any labels outside the function

Main Routine For each call to the DotProduct function, the main routine must

Determine the number of components of a vector

Set up the $a0, $a1, and $a2 registers with the addresses of the two vectors (in $a0 and $a1) and the size of both vectors (in $a2)

Call the subroutine

Use the returned value in $v0 to display a message to the console stating whether the vectors are perpendicular or not perpendicular.

.data 
    vector1: .word 2, 6, 2
    vector2: .word 4, -3, 5
    vector3: .word 5, 15, 5
    arrayEnd: .word 0
    per: .asciiz "Two vectors are perpendicular"
    n_per: .asciiz "Two vectors are not perpendicular"
    newLine: .asciiz "\n"
         .align 3
.text
main:
    la $a0, vector1
    la $a1, vector2
    la $a2, arrayEnd
    sub $a2, $a2, $a1 #get the total byte 
    srl $a2, $a2, 0 #number of elements = total byte/ 4
    
    #calculate dot product of vector 1 and 2
    jal DotProduct
    move $s0, $v0
    
    #display the message
    j display
    
    #clear
    li $a0, 0
    li $a1, 0
    li $a2, 0
    li $s0, 0
    li $v0, 0
    
    #calculate dot product of vector 1 and 3
    la $a0, vector1
    la $a1, vector2
    la $a2, arrayEnd
    sub $a2, $a2, $a1 #get the total byte 
    srl $a2, $a2, 0 #number of elements = total byte/ 4
    jal DotProduct
    move $s0, $v0
    
    #display the message
    j display
    li $v0, 10
    syscall
DotProduct:
    lw $t0, ($a0)
    add $v0, $v0, $t0
    addi $a0, $a0, 4
    
    jr $ra
display:    
    beq $s0, 0, not_perpendicular
    li $v0, 4
    la $a0, per
    li $v0, 4
    la $a0, newLine
    syscall
    j display
not_perpendicular:
    li $v0, 4
    la $a0, n_per
    li $v0, 4
    la $a0, newLine
    syscall

    
    

Upvotes: 0

Views: 325

Answers (1)

Aiden
Aiden

Reputation: 11

This is the final code, it works fine for me.

.data 
    vector1: .word 2, 6, 2
    vector2: .word 4, -3, 5
    vector3: .word 5, 15, 5
    arrayEnd: .word 0
    per: .asciiz "Two vectors are perpendicular"
    n_per: .asciiz "Two vectors are not perpendicular"
    message1: .asciiz "Comparing vector 1 and vector 2: \n"
    message2: .asciiz "Comparing vector 1 and vector 3: \n"
    newLine: .asciiz "\n"
         .align 3
.text
main:
    la $a0, vector1
    la $a1, vector2
    la $a2, 12
    #calculate dot product of vector 1 and 2
    jal DotProduct
    move $s0, $v0
    #display the message
    li $v0, 4
    la $a0, message1
    syscall
    #display the result
    beq $s0, 0, perpendicular
    li $v0, 4
    la $a0, n_per
    syscall
    li $v0, 4
    la $a0, newLine
    syscall
    j not_perpendicular
return_here:
    #clear all variables
    li $a0, 0 
    li $a1, 0
    li $a2, 0
    li $s0, 0
    li $v0, 0
    la $t5, 0 #clear counter
    #calculate dot product of vector 1 and 3
    la $a0, vector1
    la $a1, vector3
    la $a2, 12
    jal DotProduct
    move $s0, $v0
    #display the message
    li $v0, 4
    la $a0, message2
    syscall
    #display the result
    beq $s0, 0, perpendicular
    li $v0, 4
    la $a0, n_per
    syscall
    li $v0, 4
    la $a0, newLine
    syscall
    j not_perpendicular
    li $v0, 10
    syscall
DotProduct:
    beq $t5, 3, exit
    lw $t0, ($a0)
    lw $t1, ($a1)
    mul $t2 , $t0, $t1 #multiply the element of each vector
    add $v0, $v0, $t2 #add to the product
    addi $a0, $a0, 4 #update address
    addi $a1, $a1, 4 #update address
    addi $t5, $t5, 1 #increase counter
    j DotProduct
exit:
    jr $ra
perpendicular:
    li $v0, 4
    la $a0, per
    syscall
    li $v0, 4
    la $a0, newLine
    syscall
    j return_here
not_perpendicular:
    
    

    
    

Upvotes: 0

Related Questions