【发布时间】: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