2015-05-30 81 views
0

我必須製作一個程序,用鍵盤輸入30個整數來填充數組。然後用戶鍵入'c'將陣列複製到另一個陣列。我已完成第一步,但我不能設法複製數組到另一個。將陣列元素複製到MIPS程序集中的另一個陣列

這裏是我的代碼

.data 
msg1: .asciiz "> " 
msg2: .asciiz "type 'c' to copy \n>" 

.align 2 
array: .space 400 
.text 

main: 

    la $t3 array 
    loop: 


     la $a0, msg1 #output message 1 
     li $v0, 4 
     syscall 
     li $v0, 5 #read integer input 
     syscall 
     move $t0, $v0 
     beq $t0, -99, endloop #loop until user types -99 
     beq $t1,30,endloop #get user input up to 30 times 

     addi $t1, $t1, 1 #counter 
     sw $t0,($t3) 
     addi $t3,$t3,4 

     b loop #loop until it reaches 30 

    endloop: 

    la $a0, msg2 #output message 2 
    li $v0, 4 
    syscall 

    li $v0, 12 #read character input 
    syscall 


    beq $v0, 'c', COPY 

    j NEXT 

    COPY: 


    NEXT: 

回答

0

最原始的方式來做到這一點是

la $t1, dst_array 
la $t3, src_array 
addu $t0, $t3, 30*4  # setup a 'ceiling' 


copy_loop: 
    lw $at, 0($t3) 
    sw $at, 0($t1) 

    addu $t1, $t1, 4 
    addu $t3, $t3, 4 

    blt $t1, $t0, copy_loop # if load pointer < src_array + 30*4 

然而,MIPS的一些實現不使用轉發,因此,你必須等待,直到$at被寫回。爲此,有可以是一檔(你可以擺脫掉)

subu $t1, $t1, 4 
copy_loop: 
    lw $at, 0($t3) 
    addu $t1, $t1, 4 
    addu $t3, $t3, 4 
    sw $at, 0($t1) 

或負載延遲槽,通常需要1個週期,使其

copy_loop: 
    lw $at, 0($t3) 
    addu $t1, $t1, 4 

    sw $at, 0($t1) 
    addu $t3, $t3, 4 

一般來說,取決於:)

相關問題