MARS 编写 MIPS指令示例 找最大值 汇编语言

.data                     
a: .space 24   
i: .word 4
.text 
main:
la $t0,a    #t0 = a


li $t1,1                
sw $t1,0($t0)	#indirect address

li $t1,2                
sw $t1,4($t0)	# shift 4 bytes from  t0 a 

li $t1,8                
sw $t1,8($t0)	# shift 4 bytes from  t0 a 

li $t1,4               
sw $t1,12($t0)	# shift 4 bytes from  t0 a    
  
li $t1,16              
sw $t1,16($t0)  
    
li $t1,10                
sw $t1,20($t0)     

move $t2,$t0    #t2 = t0 = a

lw $t3,0($t0)   # t3 = max = a[0]

li $t6,0 	#t6 = i = 0

#supprogram of loop (fetch the element of array)
loop:

add $t2,$t2,4	#t0 = a t2 = &a[i] 
lw $t7,($t2)	#t7 = t2 = a[i]
addi $t6,$t6,1	#i = i+1
 
# teel if have reached the bound
#brance equal
beq $t6,5,end   #if i == 5
 
#branch greater than
bgt $t7,$t3,change #t7 = a[i] if(a[i]>max)

#continue the loop
j loop                

#change the maximum
change:
move $t3,$t7	#t3(max) stores bigger one
j loop                  

#store the result and retrun
end:                      
li $v0 1                   
move $a0 $t3        
syscall

 

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值