2016-08-15 89 views
0

我試圖在MIPS MARS 4.5中進行插入排序。將數組值加載到另一個數組值

我有一些麻煩特別是與:

答:

lw myArray($s4), myArray($s6)# alist[position] = alist[position-1] 

B:

lw myArray($s4), $s5 # alist[position] = current value 

我得到的問題是,「太多或不正確格式化操作數「和」myArray操作數的類型不正確「。

基本上我試圖做與展覽A在評論中提到,我試圖讓myArray($ s4)= myArray($ s6),但它不會讓我lw/la和我也試過添加到其移動英寸

展品BI要更改myArray的($ S4)的值將在$ S5值

任何人能幫助我經營我應該在這裏使用? 謝謝

我已經鏈接了以下功能的其餘部分以防萬一需要。

再次感謝。

sort: 
    addi $sp, $sp, -32  # save values on stack 
    sw $ra, 0($sp)  # Store the saved values on the stack to restore when done 
    sw $s0, 4($sp)  # s0 = base address of array 
    sw $s1, 8($sp)  # s1 = size of array 
    sw $s2, 12($sp)  # s2 = i 
    sw $s3, 16($sp)  # s3 = j 
    sw $s4, 20($sp)  # s4 = position 
    sw $s5, 24($sp)  # s5 = currentvalue 
    sw $s6, 28($sp)  # s6 = position - 1 

    la $a0, myArray  # load array address into a0 
    la $a1, myArray($s2) # load size of array into a1 
    move $s0, $a0  # move array address into s0 
    move $s1, $a1  # move size of array into s1 
    li $s2, 0   # set i to 0 
    li $s3, 0   # set j to 0 
    li $t0, 0   # set t0 to 0 
iloop: 
# if s1 >= size of array go to end 
    slt $t5, $s2, $s1  # check if a1 > s2 
    beq $t5, 0, endWhile # if it is, jump to the end 

    lw $s5, myArray($s2) # currentvalue = alist[i] 
    add $s4, $zero, $s2  # position = i 
    sub $s6, $s4, 4  # position - 1 
    j jloop   # else go to jloop 
jloop: 
# if position is <= 0 
    slt $t5, $s4, $t0  # check if s4 is greater than 0 
    beq $t5, 0, iloop  # if its less, jump to the end     
# and alist[position-1] < currentvalue 
    slt $t5, $s6, $s5  # check if alist[position-1] > current value 
    beq $t5, 0, iloop  # if less than jump back to iloop 
    lw myArray($s4), myArray($s6)# alist[position] = alist[position-1] 
    sub $s4, $s4, 1  # position = position - 1 
    j setArray 
setArray: 
    lw myArray($s4), $s5 # alist[position] = current value 
    j iloop 
endWhile: 

回答

1

(免責聲明:我從來沒有MIPS彙編程序,所以我只是跟隨wiki與我的其他組件的知識,因此我非常容易做一些愚蠢的錯誤或語法錯誤,讓我知道,如果它不起作用)

lw myArray($s4), myArray($s6) - 您無法訪問「load/store」(move)兩側的內存。這是在CPU上太多了,只有1側可以是內存訪問,其他必須是註冊

lw $t5,myArray($s6) # $t5 set to alist[position-1] 
sw $t5,myArray($s4) # alist[position] set to $t5 

lw myArray($s4), $s5 - 這是寫「負載值V到內存」,這是在人類的邏輯相同的「儲值v進入內存「,但MIPS只有第二種方式:sw $s5,myArray($s4)


其他注意事項: 爲什麼你所有雜亂的$s#寄存器,而不是使用臨時$t#?您不必存儲/恢復$t# regs的值,所以它可能會爲您節省長時間的init(並且如果您將其包含在內,則需要長時間退出)。

可能使用$zero而不是0。 (除非彙編程序足夠聰明,可以將li $s2,0更改爲add $s2,$zero,$zero。如果MIPS接近其他舊CPU設計,這應該是「更好的方式」,而不是立即爲零(儘管任何現代的內置MIPS CPU可能不會使兩者之間的差額)

爲什麼myArray硬編碼在你不能讓這樣的一個過程,以數組指針和它的大小作爲參數,從$a0, $a1

slt $t5, $s2, $s1 # check if a1 > s2 - ?不這樣做,我的意思是評論,如果你在一年後會閱讀這篇文章,你會發現這個評論很荒謬(=正確,明顯,無用)。

嘗試# check if size (a1) > i (s2),這是好點,但它翻轉了slt的邏輯。所以# set t5 if i (s2) < array size (s1)是我最後的建議(請注意我怎麼也擺脫了a1,因爲你不使用它了)。

編輯: 在問題的行lw myArray($s4), myArray($s6)# alist[position] = alist[position-1]你這樣做是正確的,你不要評論的指令做什麼(這是任何人誰知道MIPS ASM明顯),但你對此有何評論什麼是你的人意圖通過實現該指示。保持它像這樣。但是,你可能會發現它變得很難評論的每一行以這種方式,因爲一些線路組自然(像slt + beq),然後評論通過提前小組的指令單行你的意圖。

實際上,你可以用它來先在幾個簡單的步驟寫你的算法沒有任何ASM指令,或承諾一定的記錄,只是純粹的意見的使用。然後決定註冊使用情況(對於主值)。然後用實現它的特定指令填寫每條評論。在試圖同時完成所有算法,語法和寄存器分配的同時,通常有助於不會因爲太多的細節而不知所措。

相關問題