【问题标题】:Mips - Copy the elements of an array to another array using the stackMips - Copy the elements of an array to another array using the stack
【发布时间】:2022-12-01 23:53:46
【问题描述】:

I am trying to copy the elements of an array called src to another array called dest, but going through the stack (as one of the requirements).

I have written the code below which returns the values stored in src in inverse and I can't find a way to return the elements in dest in the same order:

src: 0 1 2 3 4 5 6 7 8 9
dest: 9 8 7 6 5 4 3 2 1 0

Here is my code:

.data
         n:        .word 10
     src:      .word 0 1 2 3 4 5 6 7 8 9 
     dest:     .space 40    
     i:        .word 0      
     space:   .asciiz " "
         
.text
    main:
    la $t0, src
    lw $t1, i   
    la $t2, dest 
    lw $t4, n    
    
    jal Function_CopytoStack
        
        jal Function_copyfrom_Stack_to_Dest
        
        la $t2, dest
        
        jal Print_dest
        
        li $v0, 10
        syscall

###############################################
       Function_CopytoStack:          
    beq $t1, $t4, End_function
    
    lw $fp, 0($t0)
    addi $t0, $t0, 4
    
    subu $sp, $sp, 4
    sw $fp, 0($sp)
    
    addi $t1, $t1, 1
    
    j Function_CopytoStack
    
    End_function:  
        lw $t1, i
        
        jr $ra
 #########################################################           
        Function_copyfrom_Stack_to_Dest:
        beq $t1, $t4, End_function2
    
    lw $fp, 0($sp)  
    addi $sp, $sp, 4
    
    sw $fp, ($t2)
    addi $t2, $t2, 4
    
    addi $t1, $t1, 1
        
        J Function_copyfrom_Stack_to_Dest
        
        End_function2: 
        lw $t1, i
        jr $ra
 #########################################################
        Print_dest: 
        beq $t1, $t4, End
      
        li $v0, 1
    lw $a0, ($t2)   
    syscall 
    
    li $v0, 4
    la $a0, space
    syscall
    
    addi $t2, $t2, 4
        addi $t1, $t1, 1
        
        j Print_dest
        
        End: 
        jr $ra

【问题讨论】:

    标签: arrays assembly stack mips


    【解决方案1】:

    The second half works by (a) popping one item off the stack, then (b) storing that into an pointer, which starts at thebeginningof the destination and advancesforward.

    Do the (b) part backwards, to accomplish a double reversal, which will cancel out the first reversal from the stack use.

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 2018-11-07
      • 1970-01-01
      • 2022-12-19
      • 2022-12-27
      • 1970-01-01
      • 2022-12-02
      • 1970-01-01
      • 2022-12-28
      相关资源
      最近更新 更多