バブルソートを実現するECOP-MIPSアセンブリ言語



Ecop Mips Assembly Language Achieve Bubble Sort



/ *簡単な説明:これは、コンピューター構成および設計実験クラスの最初の実験です。理論の授業で学んだMIPS文法と比べると本当の戦いです。記録、バックアップ、統合のためにここに書かれています。以下のコンテンツの一部は、他のWebサイトから参照されています。 * /

1.実験目的

1. MIPSアセンブリ言語プログラミングの基本的な方法を認識し、習得します。
2PCSpimシミュレータの使用に精通している。



2.実験内容

キーボードから符号なし単語を10個入力し、大きいものから小さいものへと並べ替えると、並べ替えの結果が画面に表示されます。

3.実験装置

1台のコンピューターと1セットのPCSpimシミュレーターソフトウェア。



4.実験プロセス

1)設計フローチャート:

画像

2)コードの実装:




#C++Code ################################################################### # // Input 10 unsigned words from the keyboard and sort from largest to smallest, and the result of the sort is displayed on the screen # #include # using namespace std # int main() { # int array[10] # for (int i = 0 i <10 i++) # cin >> array[i] # for (int i = 0 i <9 i++) { # for (int j = 0 j <9-i j++) { # if (array[j] # int temp = array[j] # array[j] = array[j+1] # array[j+1] = temp # } # } # } # for (int i = 0 i # cout << array[i] << ' ' # return 0 # } ################################################################### # MIPS assembly language code .text # The code segment starts with .text, that is, each instruction operation .globl main # Define main as a label that can be accessed by external programs main: # Program entry li $v0, 4 # Opcode $v0 = 4, assign the string to be printed to $a0 la $a0, input_msg # la(load address), print a string, prompt the user to enter the length of the sorted array syscall # Call the operating system to complete printing (output) la $t6, array # $t6 is the first address of the array move $t7, $zero # $t7 is the loop variable i, i = 0 move $t8, $zero # $t8 is the length of the array addi $t8, $t8, 10 # $t8 = 10 move $t9, $zero # $t9 is the loop variable j, j = 0 input: # input code block is used to complete the reading of array elements li $v0, 5 # Opcode $v0 = 5, assign the read integer to $v0, cin >> $t0 syscall # array[i] = $t0 move $t0, $t7 # Here is similar to the method of pointer access to array elements in C/C++ mul $t0, $t0, 4 # The second $t0 here should be a subscript addu $t1, $t0, $t6 # $t1 is the address of the element that needs to be accessed sw $v0, 0($t1) # Save the value corresponding to the subscript to $v0 addi $t7, $t7, 1 # i++ blt $t7, $t8, input # if i <10, jump to input move $t7, $zero # End of input, set the loop variable $t7 to 0 to save registers loop1: # This is the outer loop, set the variable of the inner loop to 0, move $t9, $zero # j = 0 loop2: # Get array[j] move $t0, $t9 # $t0 = $t9 = j mul $t0, $t0, 4 # $t0 = The address difference between array[j] and array[0] addu $t1, $t0, $t6 # $t1 = array[j] lw $t2, 0($t1) # Put arrat[j] into register $t2 # Get array[j+1] addi $t0, $t9, 1 # j++ mul $t0, $t0, 4 # $t0 = 4 * j addu $t4, $t0, $t6 # $t4 = array + 4*j, that is, $t4 = array[j] lw $t3, 0($t4) # $t3 = array[j] bge $t2, $t3, continue # if (a[j] >= a[j+1]) jump to the continue module sw $t3, 0($t1) # else sw $t2, 0($t4) # exchange a[j] with a[j+1] continue: # continue the 'for' loop # Realize j <10-i-1 j++ addi $t9, $t9, 1 # j++ move $t0, $t9 # $t0 = j sub $t1, $t8, $t7 # $t1 = $t8 - i = 10 - i sub $t1, $t1, 1 # $t1 = $t1 - 1 = 10 - i - 1 blt $t0, $t1, loop2 # if j <10-i-1, jump to loop2 # Realize i <10-1 i++ addi $t7, $t7, 1 # i++ sub $t2, $t8, 1 # $t2 = $t8 - 1 = 10 - 1 blt $t7, $t2, loop1 # if i <10 - 1, jump to loop1 output: li $v0, 4 # Opcode $v0 = 4, assign the string to be printed to $a0 la $a0, output_msg # Print a string to prompt the user to output the program syscall move $t7, $zero # Set the variable to 0 for the next loop, saving registers print: # Print array elements move $t0, $t7 # $t0 = i mul $t0, $t0, 4 # $t0 = 4 * i addu $t1, $t0, $t6 # $t1 = array + 4*i, ie $t1 = array[i] li $v0, 1 # Opcode $v0 = 1, assign the integer type to be printed to $a0 lw $a0, 0($t1) syscall li $v0, 4 # Opcode $v0 = 4, assign the string to be printed to $a0 la $a0, space # Print spaces to separate array elements syscall addi $t7, $t7, 1 # i++ blt $t7, $t8, print # if i <10, jump to print .data # Data declaration, the data segment starts with .data array:.space 1024 # .space indicates the size of the space, the unit is bytes input_msg:.asciiz 'Please enter 10 numbers to be sorted, each number a single line: ' output_msg:.asciiz 'The sorted numbers are: ' space:.asciiz ' ' # Enclose the string in double quotes # Leave a colon after the variable name: # .asciiz will add a terminator at the end of the string, similar to the'' in C, but .ascii will not

5.実験結果

並べ替える番号を10個入力してください。各番号は、1行です。
1
0

9
3
8
4
7
5
6
ソートされた番号は次のとおりです。
9 8 7 6 5 4 3 2 1 0


次のWebサイトを参照して調べてください。
MIPSコンパイル:バブルソート
コンピューター構成の原則のMIPSコンパイル:バブルソート
[10分でコンパイルを教えてください] MIPSプログラミング入門