MIPS——循环语句

有关指令

 

1 add $t1,$t2,$t3              #寄存器+寄存器,$t1 = $t2 + $t3
2 add $t1,$t2,immediate        #寄存器+立即数,$t1 = $t2 + immediate
3 ble $t1,immediate            #Branch if less or equal, $t1 <= immediate
4 syscall                      #将要输出的整数值放入$a0,将$v0设为1,输出$a0

 

 C语言代码

 1 #include<stdio.h>
 2 
 3 int main()
 4 {
 5     int i = 1;
 6     int sum = 0;
 7     do
 8     {
 9         sum = sum + i;
10         i = i + 1;
11     } while (i <= 100);
12 
13     printf("%d\n", sum);
14 
15     return 0;
16 }

C语言中的循环语句有for循环、while循环、do-while循环,貌似do-while循环更接近MIPS的风格

MIPS代码

 1 li $t0,1                 #$t0: i
 2 li $t1,0                 #$t1: sum
 3 
 4 loop:
 5     add $t1,$t1,$t0
 6     add $t0,$t0,1
 7     
 8 ble $t0,100,loop         #如果i <= 100,跳转到loop继续循环
 9 
10 move $a0,$t1
11 li $v0,1
12 syscall

  

 

posted @ 2018-09-17 22:00  Rogn  阅读(7531)  评论(0编辑  收藏  举报