爱吃羊的猫猫

导航

【MIPS】实现冒泡排序

编程环境:PCspim

 1 .text 
 2 .globl main 
 3 main: 
 4 la $a0,input_message 
 5 li $v0,4 
 6 syscall 
 7  
 8 li $v0,5   
 9 syscall 
10  
11 la $t6,array      
12 move $t7,$zero    
13 move $t8,$v0        
14 move $t9,$zero    
15  
16 input:                
17 la $a0,int_message 
18 li $v0,4 
19 syscall 
20  
21 li $v0,5 
22 syscall 
23  
24 move $t0,$t7      
25 mul $t0,$t0,4     
26 addu $t1,$t0,$t6 
27 sw $v0,0($t1) 
28  
29 addi $t7,$t7,1 
30 blt $t7,$t8,input 
31 move $t7,$zero   
32  
33 loop1: 
34 move $t9,$zero     
35 loop2: 
36 move $t0,$t9       
37 mul $t0,$t0,4 
38 addu $t1,$t0,$t6 
39 lw $t2,0($t1) 
40  
41 addi $t0,$t9,1     
42 mul $t0,$t0,4 
43 addu $t4,$t0,$t6 
44 lw $t3,0($t4) 
45  
46 bge $t2,$t3,skip   
47 sw $t3,0($t1)    
48 sw $t2,0($t4)     
49  
50 skip: 
51 addi $t9,$t9,1    
52 addi $t0,$t9,1    
53 sub $t1,$t8,$t7     
54 blt $t0,$t1,loop2   
55 addi $t7,$t7,1      
56 sub $t2,$t8,1 
57 blt $t7,$t2,loop1 
58 
59  
60 output: 
61 la $a0,output_message  
62 li $v0,4 
63 syscall 
64  
65 move $t7,$zero    
66  
67 print:           
68 move $t0,$t7 
69 mul $t0,$t0,4 
70 addu $t1,$t0,$t6 
71 lw $a0,0($t1) 
72 li $v0,1 
73 syscall 
74  
75 la $a0,seperator 
76 li $v0,4 
77 syscall 
78  
79 addi $t7,$t7,1 
80 blt $t7,$t8,print    
81 
82  
83 .data 
84 array:.space 1024 
85 input_message:.asciiz "Please enter the number of integers:\n" 
86 int_message:.asciiz "Please enter a number:\n" 
87 output_message:.asciiz "The sorted results are:\n" 
88 seperator: .asciiz " " 

效果演示:(先输入数字个数以开辟相应空间)

 

posted on 2019-10-24 12:18  爱吃羊的猫猫  阅读(1329)  评论(0编辑  收藏  举报