void main(){
int n,i,sum; // assume that $s0 is for n, $t0 for i and $s1 for sum
/* MIPS code for reading an integer. */
addi $v0, $zeros, 5
syscall
add $s0, $zeros, $v0
/*---------------------------------------------*/
// mainly translate the following code into MIPS code
sum=0;
for (i=1; i<=n; i++)
sum+=i;
/* MIPS code for printing an integer */
addi $v0,$zero,1
add $a0,$zeros,$s1
syscall
/*--------------------------------------------*/
addi $v0,$zeros,10
syscall
/*---------------------------------------------*/
}
共2条
1/1 1 跳转至页
用Mars 寫一MIPS 組合語言讀入一正整數n,計算並列印1+2…+n的和。

addi $v0, $zero, 5
syscall
add $s0, $zero, $v0
# mainly translate the following code into MIPS code
#sum=0;
#for (i=1; i<=n; i++)
#sum+=i;
li $t1, 0 #i
li $s1, 0 #sum
for_1: #for
add $t1, $t1, 1#i++
add $s1, $s1, $t1#sum+=i;
bne $t1, $s0, for_1#i<=n
addi $v0,$zero,1
add $a0,$zero,$s1
syscall
addi $v0,$zero,10
syscall
共2条
1/1 1 跳转至页