質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
アセンブリ言語

アセンブリ言語とは、機械語を人間にわかりやすい形で記述した低水準言語です。

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

Q&A

解決済

2回答

671閲覧

MIPS merge sortを書いたときのエラーが何を指しているのかわからない

wanwanko

総合スコア14

アセンブリ言語

アセンブリ言語とは、機械語を人間にわかりやすい形で記述した低水準言語です。

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

0グッド

0クリップ

投稿2022/12/12 22:44

前提

二つのソートされた配列をinputし、その配列をmerge sortしようと試みています。
先にC言語を書き、mipsにうまく書き直せたと思ったのですか、うまくいきません。エラーはつきものだと言うことは承知の上でかなり懸命に模索したのですか、debugできません。

実現したいこと

エラーが指していることを理解しdebugしたい。

発生している問題・エラーメッセージ

"$a4":operand is of incorrect type "bge":too meny or incorrectly formatted operands. Expected: bge $t1,$t2,label "bge":too meny or incorrectly formatted operands. Expected: bge $t1,$t2,label "bge":too meny or incorrectly formatted operands. Expected: bge $t1,$t2,label "bge":too meny or incorrectly formatted operands. Expected: bge $t1,$t2,label

該当のソースコード

mips

1.data 2array1: 3 .space 20 4array2: 5 .space 20 6array3: 7 .space 40 8 9prompt1: 10 .asciiz "Enter the first array (5 numbers): " 11prompt2: 12 .asciiz "Enter the second array (5 numbers): " 13prompt3: 14 .asciiz "Merged array: " 15 16.text 17main: 18 # Read in the first array from the user 19 la $a0, prompt1 # load address of prompt1 string 20 li $v0, 4 # print string syscall code 21 syscall # print prompt1 22 la $a0, array1 # load address of array1 23 li $a1, 5 # set array length to 5 24 li $v0, 8 # read word syscall code 25 syscall # read in array1 26 27 # Read in the second array from the user 28 la $a0, prompt2 # load address of prompt2 string 29 li $v0, 4 # print string syscall code 30 syscall # print prompt2 31 la $a0, array2 # load address of array2 32 li $a1, 5 # set array length to 5 33 li $v0, 8 # read word syscall code 34 syscall # read in array2 35 36 # Merge the two arrays into array3 37 la $a0, array1 # load address of array1 38 la $a1, array2 # load address of array2 39 la $a2, array3 # load address of array3 40 li $a3, 5 # set length of array1 to 5 41 li $a4, 5 # set length of array2 to 5 42 jal merge # call merge function 43 44 # Print the merged array 45 la $a0, prompt3 # load address of prompt3 string 46 li $v0, 4 # print string syscall code 47 syscall # print prompt3 48 la $a0, array3 # load address of array3 49 li $a1, 10 # set array length to 10 50 li $v0, 1 # print integer syscall code 51 syscall # print array3 52 53 li $v0, 10 # exit syscall code 54 syscall # exit 55 56 57 58 59 60# $a0 = address of a array 61# $a1 = address of b array 62# $a2 = address of c array 63# $a3 = length of a array 64# $a4 = length of b array 65 66merge: 67 addi $sp, $sp, -8 # allocate space on stack for two loop variables 68 sw $zero, 4($sp) # initialize a_index to 0 69 sw $zero, 0($sp) # initialize b_index to 0 70 71 # Compare a and b, and add the smaller value to c in order 72 while_loop: 73 bge $a3, 4($sp), a_done # if a_index >= a_len, jump to a_done 74 bge $a4, 0($sp), b_done # if b_index >= b_len, jump to b_done 75 lw $t0, 0($a0) # load a[a_index] 76 lw $t1, 0($a1) # load b[b_index] 77 blt $t0, $t1, add_a # if a[a_index] < b[b_index], jump to add_a 78 sw $t1, 0($a2) # c[c_index] = b[b_index] 79 addi $a2, $a2, 4 # c_index++ 80 lw $t2, 0($sp) # load b_index 81 addi $t2, $t2, 1 # b_index++ 82 sw $t2, 0($sp) # store b_index 83 j while_loop 84 add_a: 85 sw $t0, 0($a2) # c[c_index] = a[a_index] 86 addi $a2, $a2, 4 # c_index++ 87 lw $t2, 4($sp) # load a_index 88 addi $t2, $t2, 1 # a_index++ 89 sw $t2, 4($sp) # store a_index 90 j while_loop 91 92 # Add remaining elements from the a array to c 93 a_done: 94 bge $a3, 4($sp), b_done # if a_index >= a_len, jump to b_done 95 lw $t0, 0($a0) # load a[a_index] 96 sw $t0, 0($a2) # c[c_index] = a[a_index] 97 addi $a2, $a2, 4 # c_index++ 98 lw $t2, 4($sp) # load a_index 99 addi $t2, $t2, 1 # a_index++ 100 sw $t2, 4($sp) # store a_index 101 j a_done 102 103 # Add remaining elements 104 b_done: 105 bge $a4, 0($sp), end # if b_index >= b_len, jump to end 106 lw $t0, 0($a1) # load b[b_index] 107 sw $t0, 0($a2) # c[c_index] = b[b_index] 108 addi $a2, $a2, 4 # c_index++ 109 lw $t2, 0($sp) # load b_index 110 addi $t2, $t2, 1 # b_index++ 111 sw $t2, 0($sp) # store b_index 112 j b_done 113 114 end: 115 addi $sp, $sp, 8 # deallocate space on stack 116 jr $ra # return to caller 117

C

1#include <stdio.h> 2void merge(int *a, int *b, int *c, int a_len, int b_len) { 3 int a_index = 0, b_index = 0, c_index = 0; 4 5 // Compare a and b, and add the smaller value to c in order 6 while (a_index < a_len && b_index < b_len) { 7 if (a[a_index] < b[b_index]) { 8 c[c_index++] = a[a_index++]; 9 } else { 10 c[c_index++] = b[b_index++]; 11 } 12 } 13 14 // Add remaining elements from the a array to c 15 while (a_index < a_len) { 16 c[c_index++] = a[a_index++]; 17 } 18 19 // Add remaining elements from the b array to c 20 while (b_index < b_len) { 21 c[c_index++] = b[b_index++]; 22 } 23} 24 25int main() 26{ 27 int a[5]; 28 int b[5]; 29 int c[10]; 30 31// Read in the two arrays from the user 32printf("Enter the first array (5 numbers): "); 33for (int i = 0; i < 5; i++) { 34 scanf("%d", &a[i]); 35} 36 37printf("Enter the second array (5 numbers): "); 38for (int i = 0; i < 5; i++) { 39 scanf("%d", &b[i]); 40} 41 42// Merge the two arrays into c 43merge(a, b, c, 5, 5); 44 45// Print the merged array 46printf("Merged array: "); 47for (int i = 0; i < 10; i++) { 48 printf("%d ", c[i]); 49} 50 51 52 return 0; 53}

試したこと

エラーに書いてあることの意味をstack overflowなどで調べたのですが、書き直し方が間違っているのか同じエラーの繰り返しになってしまいます。

補足情報(FW/ツールのバージョンなど)

根本的に書き間違えていたら、ご指摘いただけるとまた書き直すモチベーションになります。
時間を割いて読んでくださりありがとうございます。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

MIPSは知らんのやけど、アセンブラとしては「bge $a3, 4($sp), a_done」ここで文句があって、というのもMIPS命令表には「bgt[u] Rsrc1, Rsrc2, label」って書いてあるんで比較対象は両方レジスタの必要があるんでしょう。(RISCやからメモリを取るオペランドはload/storeしかない、みたいな。)
あとは引数用レジスタは$a0~$a3までで$a4はない、ってことを言ってるみたいな。5個目の引数はスタックに積まれるはず。

http://www.swlab.cs.okayama-u.ac.jp/~nom/lect/p3/concise-mips-instruction-set.html
http://www.swlab.cs.okayama-u.ac.jp/~nom/lect/p3/what-is-calling-convention.html

投稿2022/12/12 23:01

matukeso

総合スコア1590

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

wanwanko

2022/12/23 16:10

ありがとうございました。
guest

0

"$a4":operand is of incorrect type

これはどの行で発生しているのか不明で助言はありません。

"bge":too meny or incorrectly formatted operands. Expected: bge $t1,$t2,label

bge の第1第2オペランドはレジスタです。4($sp) のようなアドレス形式は使えないので、いったんレジスタにロードしてから bge しましょう。

投稿2022/12/12 22:59

int32_t

総合スコア20882

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

wanwanko

2022/12/23 16:10

ありがとうございました、無事に解決いたしました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問