# strcpy example for SPIM .data msg: .asciiz "CompOrg Rules!" buf: .space 100 lf: .asciiz "\n" .text # this is program code .align 2 # instructions must be on word boundaries .globl main # main is a global label main: sub $sp,$sp,4 # main can return, so save $ra sw $ra,0($sp) li $v0,4 # print out original message la $a0, msg syscall la $a0,buf # set up for call to strcpy la $a1,msg jal strcpy # call strcpy li $v0,4 # print a line feed la $a0, lf syscall li $v0,4 # print out the new copy la $a0, buf syscall lw $ra,0($sp) # return from main add $sp,$sp,4 jr $ra # ---------------------------- # strcpy from the lecture notes strcpy: sub $sp,$sp,8 # make room for 2 sw $s1,0($sp) # save $s0 sw $s2,4($sp) # save $s1 add $s1,$a0,$zero add $s2,$a1,$zero Loop: lb $t0,0($s2) # $to = *str2 sb $t0,0($s1) # *str1 = $t0 addi $s2,$s2,1 # str2++ addi $s1,$s1,1 # str1++ bne $t0,$zero,Loop # jump if not done lw $s2,4($sp) # restore $s1 lw $s1,0($sp) # restore $s0 addi $sp,$sp,8 # adjust stack jr $ra # return