#void strcpy (char x[], char y[]) #{ # int i; # i = 0; # while ((x[i]=y[i]) != 0) /* copy and test of the i-th byte*/ # i = i + 1; #} #base address of the array x in $a0 and of y in $a1 #$s0 is used for i #t2 holds y[i], pointers $t1 for y[i],$t3 for x[i] .text .globl main strcpy: addi $sp,$sp,-4 # slide (push) the stack pointer by 1 word sw $s0,0($sp) # save $s0 add $s0,$zero,$zero # initialise i with 0 L1: add $t1,$a1,$s0 # address of y[i] in $t1, (y array of bytes) lbu $t2,0($t1) # $t2 = y[i] add $t3,$a0,$s0 # address of x[i] in $t3 sb $t2,0($t3) # x[i] = y[i] beq $t2,$zero,L2 # if y[i] == 0, go to L2 (end of loop) addi $s0,$s0,1 # (else) i = i + 1 j L1 # go to L1 (proceed with next character) L2: lw $s0,0($sp) # (String end:) restore $s0 addi $sp,$sp,4 # remove (pop) one word from stack pointer jr $ra # jump back to caller main: la $a0, x # print initial x li $v0, 4 syscall la $a0, x # first parameter la $a1, y # second parameter jal strcpy # Call strcpy la $a0, x # print final x li $v0, 4 syscall li $v0, 10 # exit syscall .data x: .asciiz "xxxxxxxx\n" y: .asciiz "A string\n" #Check Simulator>Setting>Delayed branches # breakpoint at 0x00400078 jal strcpy # breakpoint at 0x00400040 beq $t2,$zero,L2 # breakpoint at 0x0040007c la $a0, x