问题是:请写一个循环来计算当前存储在$ t0中的正数的阶乘,并将结果存储在4个指令中的$ t1中。
这是我到目前为止我很确定它有效,但它有6条指令。
li $t3, 1
move $t1, $t0
move $t2, $t0
LOOP: addi $t2, $t2, -1
mul $t1, $t1, $t2
bne $t2, $t3, LOOP
编辑。 这是解决方案
li $t1 1
LOOP: mul $t1 $t1 $t0
addi $t0 $t0 -1
bgez $t0 LOOP
答案 0 :(得分:0)
li $t1 1
LOOP: mul $t1 $t1 $t0
addi $t0 $t0 -1
bgez $t0 LOOP
答案 1 :(得分:0)
const arrayToSort = [
{
Country: "Cyprus",
Date: new Date(2001, 0, 1),
CreateBy: "William",
},
{
Country: "Belarus",
Date: new Date(1999, 0, 1),
CreateBy: "Yuliana",
},
{
Country: "Denmark",
Date: new Date(2019, 0, 1),
CreateBy: "Ava",
},
{
Country: "Albania",
Date: new Date(2000, 0, 1),
CreateBy: "Zachary",
}
];
const sortFields = ["Country", "CreateBy", "Date"];
const descending = [true, false, true];
const sortBy = (a,b,field,desc) => {
if(a[field]<b[field]) return desc ? 1 : -1
else if(a[field]>b[field]) return desc ? -1 : 1
else return 0;
};
const result = arrayToSort.sort( (a,b) => {
for(var i=0;i<sortFields.length;i++){
var res = sortBy(a,b, sortFields[i], descending[i]);
if(res != 0) return res;
}
return 0;
})
console.log(result);
答案 2 :(得分:0)
在 qtspim 上运行良好。每行都有小注释,以便更好地理解。
请记住,当调用一个函数时,我们应该将它临时存储在堆栈中,这样即使函数更改了寄存器值,我们仍然可以保留以前主函数中的数据。
.data
Enter: .asciiz "Enter value of n: \n"
.text
.globl main
main:
lui $s0, 0x1001
li $v0, 4 # print message for i/p
la $a0, Enter
syscall
li $v0, 5 # get n
syscall
add $s1, $v0, $zero # init n = 5
li $s2, 1 # s2 = 1 for base case
add $a0, $s1, $zero # pass fib(a0)
jal factorial
li $v0, 1 # print result
add $a0, $v1, $zero
syscall
li $v0, 10 # exit
syscall
factorial:
addi $sp, $sp, -8 # grow sp below
sw $a0, 0($sp) # store arguments and return addr
sw $ra, 4($sp)
bne $a0, $s2, recursion # n != 1
add $v1, $s2, $zero
addi $sp, $sp, 8 # restore $sp and remove stack frame
jr $ra
recursion:
addi $a0, $a0, -1 # fib(a0 - 1)
jal factorial
lw $a0, 0($sp) # load back a0 to use as a0 * fib(a0-1)
lw $ra, 4($sp)
addi $sp, $sp, 8
mul $v1, $a0, $v1
jr $ra