あなたのプログラムにはいくつかのバグがあります。しかし、私はあなたのループロジックをチェックして、それは正しいです。
lbu
が間違っています。 しかし、、それは間違った結果しか生成されず、ではなく、がクラッシュします。
通常、プログラム開始時のレジスタ値はではなく、に依存します。したがって、表示されているコード以外のコードはのコードを入力する前に設定する必要があります。はでした。
クラッシュが発生している場合は、ではなく、がコードに正しく入力されているためです。 [はポスト] 他のコードです。問題の根本原因です。
コマンドラインオプションを解析して「ソフト」な配列を作成しているとお伝えします。どこかの[アクセス可能な]メモリに配置しなければならないので(つまり、sbrk
システムコール?)、あなたの説明にもかかわらず、その意味がわかりません。
そして、私はmars
[私はそのJavaコードをハッキングします]にもよく慣れています。プログラムに渡すコマンドライン引数を指定すると、GUI [とデバッガなど]が無効になります。したがって、argc
とargv
を使用しようとするとデバッグが難しくなります。なぜなら、デバッガは使用できないためです。
コードは合計を出力していましたが、実際には必要に応じて最初の配列に格納しませんでした。
私は2つのバージョンを作成しました。 1つはバグが付いています。そして、レジスタの設定のためのいくつかのサンプルコードがあり、完全にクリーンアップし、作業バージョン[無償スタイルのクリーンアップをご容赦ください]
ここで注釈付きバージョンです:
# $s0 contains the number of elements per array
# $s1 contains the address of the first array of integers
# $s2 contains the address of the second array of integers
#
# Your job is to implement array addition in-place.
# When the program ends, the first array should contain
# the the sum of each pair of elements.
#
# E.g. (here the value in $s0 would be 3)
# value of the arrays before:
# [1 2 3] [4 5 6]
# value of the arrays after:
# [5 7 9] [4 5 6]
# YOUR SOLUTION GOES HERE
# Counter at $t2 and $t5
# NOTE: $t2 is unused
# NOTE: this is used like index variable
addi $t5,$zero,0
array1Loop:
beq $t5,$s0,exit # done yet? if so, fly
# NOTE/BUG: the arrays are integers, so the inst is "lw" and _not_ "lbu"
lbu $t4,0($s1) # fetch from 1st array
lbu $t6,0($s2) # fetch from 2nd array
add $t7,$t4,$t6 # sum the array values
# NOTE/BUG: we're saving the the results in a register but _not_ writing
# back to the first array
move $s4,$t7 # save the sum
move $s1,$s4 # save the sum
# print the sum
# NOTE/BUG: we should output a space to separate the numbers first
li $v0,1
move $a0,$s4
syscall
# NOTE: these are _pointers_ and not indexes
# NOTE/BUG: when adding addresses/pointers, use the unsigned add to
# prevent possible overflow exceptions
addi $s1,$s1,4 # increases index by 4
addi $s2,$s2,4 # increases index by 4
# NOTE/BUG: this is not needed
addi $s4,$s4,4 # what???
# NOTE: this is an index variable
addi $t5,$t5,1 # increased counter $t5
j array1Loop
exit:
# exit cleanly
li $v0,10
syscall
は、クリーンアップされたバージョンです:
# $s0 contains the number of elements per array
# $s1 contains the address of the first array of integers
# $s2 contains the address of the second array of integers
#
# Your job is to implement array addition in-place.
# When the program ends, the first array should contain
# the the sum of each pair of elements.
#
# E.g. (here the value in $s0 would be 3)
# value of the arrays before:
# [1 2 3] [4 5 6]
# value of the arrays after:
# [5 7 9] [4 5 6]
.globl main
main:
li $s0,7
la $s1,arr1
la $s2,arr2
# YOUR SOLUTION GOES HERE
# registers:
# t4 -- value from first array
# t5 -- array index
# t6 -- value from second array
# t7 -- sum of array values
li $t5,0
array1Loop:
beq $t5,$s0,exit # done yet? if so, fly
lw $t4,0($s1) # fetch from 1st array
lw $t6,0($s2) # fetch from 2nd array
add $t7,$t4,$t6 # sum the array values
sw $t7,0($s1) # store sum back into first array
# print the sum
li $v0,1
move $a0,$t7
syscall
# print a newline
li $v0,4
la $a0,newline
syscall
addiu $s1,$s1,4 # advance first array's pointer
addiu $s2,$s2,4 # advance second array's pointer
addi $t5,$t5,1 # advance array index
j array1Loop
exit:
# exit cleanly
li $v0,10
syscall
.data
arr1: .word 1,2,3,4,5,6,7
arr2: .word 11,22,33,44,55,66,77
newline: .asciiz "\n"