【问题标题】:Dot Product without using branch inside the function在函数内部不使用分支的点积
【发布时间】:2021-12-06 03:40:11
【问题描述】:

我被困在点积上,没有在函数内使用分支。我只是想我会去两个数组的每个元素,一步一步计算。有没有办法可以做到这一点? 太感谢了! 点积函数 请阅读并理解DotProduct函数的以下规范

代码中的函数名、标签必须是DotProduct。

该函数在寄存器$a0、$a1 和$a2 中传递了三个参数。 参数为:

$a0 中第一个向量的地址。

$a1 中第二个向量的地址

$a2 中向量的分量数。 (您不能总是假设向量将具有三个分量,但可以安全地假设向量将具有相同数量的分量)

函数必须返回 $v0 中计算的点积。

函数不能做任何其他输出,不能有任何分支或跳转到函数外的任何标签

主要例程 对于 DotProduct 函数的每次调用,主例程必须

确定向量的分量数

使用两个向量的地址(在 $a0 和 $a1 中)和两个向量的大小(在 $a2 中)设置 $a0、$a1 和 $a2 寄存器

调用子程序

使用 $v0 中的返回值向控制台显示一条消息,说明向量是否垂直。

.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

    
    

【问题讨论】:

  • "不得有任何分支或跳转到函数之外的任何标签"。据我所知,只要它在函数内,你就可以随意分支。
  • 谢谢,我试试

标签: arrays assembly mips dot-product


【解决方案1】:

这是最终代码,对我来说很好用。

.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:
    
    

    
    

【讨论】:

    猜你喜欢
    • 2015-08-15
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    相关资源
    最近更新 更多