# char S1[] = "This is a string." # int Foo[4]; # char S2[100]; # # void main() # { # strcpy(S2, S1); # } # # void strcpy(char* dst, char* src) # { # int i = 0; # while((dst[i] = src[i]) != 0) # i = i+1; # } .data S1: .ascii "This is a string." # this will be the source string .space 1 # this is the NULL at the end of the above string .align 4 Foo: .space 16 # this is just hear to leave some obvious space S2: .space 100 # desitination string .text .globl main main: addi $sp, $sp, -4 # get space on the stack sw $ra, 0($sp) # save main's return address la $a0, S2 # get the address of S2 in the first argument la $a1, S1 # get the address of S1 in the second argument jal strcpy # call strcpy lw $ra, 0($sp) # restore main's return address addi $sp, $sp, 4 # restore the stack pointer jr $ra # exit main strcpy: move $t0,$zero # i in $t0 = 0 L1: add $t1,$a1,$t0 # address of src[i] in $t1 lb $t2, 0($t1) # t2 = src[i] add $t1,$a0,$t0 # address of dst[i] in $t1 sb $t2, 0($t1) # dst[i] = $t2 addi $t0, $t0, 1 # i = i+1 bne $t2,$zero,L1 # if dst[i] != 0 repeat jr $ra # return