Search code examples
arrayscharactermipscharacter-arrays

Copying character array contents to another array [MIPS]


this is a homework assignment from class. The goal of the project is to split 2 arrays at some pivot point and make a "child" array from that. So if your pivot point is 11 and your two arrays are:

1111111111111111
abcdefghijklmnop

then the child output would be:

111111111111mnop

right now i'm having trouble printing out the child. Here is my source code so far:

.globl main

.data
parentOne: .space 17
parentTwo: .space 17
split: .word 0
childOne: .space 17
childTwo: .space 17
space: .asciiz "\n"

.text
main:
jal getInputOne
jal getInputTwo
jal getIndex
jal makeChildren
jal printChildOne

j endMain

##############################################################################################################
getInputOne:
li $v0, 8
la $a0, parentOne
li $a1, 17
syscall

addi $v0, $zero, 4  # print_string syscall
la $a0, space       # load address of the string
syscall
jr $ra
##############################################################################################################
getInputTwo:
li $v0, 8
la $a0, parentTwo
li $a1, 17
syscall
addi $v0, $zero, 4  # print_string syscall
la $a0, space       # load address of the string
syscall
jr $ra
##############################################################################################################
getIndex:
li $v0, 5
syscall
sw $v0, split
jr $ra
##############################################################################################################
makeChildren:
la $s0, parentOne
la $s1, parentTwo
la $a0, childOne
la $a1, childTwo

li $s2, 0       #intial counter
li $s3, 17      #end counter
lw $s4, split       #split point

childLoop:
    slt $t1, $s2, $s3
    beq $t1, $0, endChildLoop
    if:
        slt $t1, $s2, $s4   # i<pivot
        beq $t1, $0, else   # not less than pivot
        lb $a0, ($s0)       #load childOne[i] <-- parentOne[i]
        lb $a1, ($s1)       #load childTwo[i] <-- parentTwo[i]
        j endif
    else:
        lb $a0, ($s1)       #load childOne[i] <-- parentTwo[i]
        lb $a1, ($s0)       #load childTwo[i] <-- parentOne[i]
    endif:
    #li $v0, 11     #print character       
    #syscall

    addi $s0, $s0, 1    #increment parent1 array
    addi $s1, $s1, 1    #increment parent2 array
    addi $a0, $a0, 1    #increment child array
    addi $a1, $a1, 1    #increment child2 array
    addi $s2, $s2, 1    #counter++
    j childLoop
endChildLoop:
la $a0, childOne
li $v0, 1
syscall
jr $ra

##############################################################################################################
printChildTwo:
la $a0, childOne
li $v0, 4
syscall
jr $ra

endMain:

I think what is happening is that nothing is actually getting saved to the childOne register, but I'm unsure why. I loaded the childOne address to the $a0 register, but once the program goes to the printChildOne function, nothing gets printed.


Solution

  • Your comment here does not match what the instruction actually does:

    lb $a0, ($s0)   #load childOne[i] <-- parentOne[i]
    

    That instruction only sets a new value for $a0, it doesn't write anything to the memory location that $a0 was pointing to.

    It's like if you did this in C:

    char *a0 = childOne;
    ...
    a0 = (char*) parentOne[i];
    

    Which obviously is not the same as:

    *a0 = parentOne[i]; 
    

    What you want is something like this:

    lb $t0, ($s0)       # temp = parentOne[i]
    sb $t0, ($a0)       # childOne[i] = temp
    # similarly for $a1
    

    The code in your question also appears to have a typo here:

    printChildTwo:
    la $a0, childOne
    

    I suspect that label should be printChildOne.