简体   繁体   中英

Copy array elements to another array in MIPS assembly

i have to make a program that fills an array with 30 integers entered from keyboard.Then the user type 'c' to copy the array to an other array.i've done with the first step but i cant manage to copy the array to another.

Here is my code

    .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:

The most primitive way to do it is to

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

However, some implementations of MIPS don't use forwarding, and therefore you have to wait until $at is written back. For that purpose, there may be either a stall (which you could get rid off)

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

or a load delay slot, which usually takes 1 cycle, making it

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

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

Generally speaking, it depends :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM