# Spim program to copy strings by arrays.
#
# void strcpy(char x[], char x[]) {
# int i;
# i=0;
#
# while ((x[i]=y[i])!=0)
# i++;
#
# }
#
# main() {
# char si[] = "Hello, world.\n";
# char so[80];
#
# strcpy(so,si);
# cout << so;
# }
.text
.globl main
main:
la $a0, so
la $a1, si
jal strcpy
la $a0, so
li $v0, 4
syscall
li $v0, 10
syscall
# strcpy -- copy the string pointed to by $a1 to the location
# pointed to by $a0
strcpy:
addi $sp, $sp, -4 #Use stack to ...
sw $s0, 0($sp) # save $s0
add $s0, $zero, $zero # Zero into $s0 so we can use
# it for a counter
L1: add $t1, $a1, $s0 # address of y[i]
lb $t2, 0($t1) # $t2 = y[i]
add $t3, $a0, $s0 # address of x[i]
sb $t2, 0($t3) # x[i]=y[]
beq $t2, $zero, L2 # If at the null, we are done
addi $s0, $s0, 1 # else increment counter
j L1 # and do it all again
L2: # Prepare to leave strcpy
lw $s0, 0($sp) # restore $s0
addi $sp, $sp, 4 # restore the stack pointer
jr $ra # Return
.data
si: .asciiz "Hello, world.\n"
so: .space 80