In: Computer Science
Write the MIPS assembly codes to implement the following function: copy a block of words from one address to another. Assume that the starting address of the source block be in register $t1 and that the destination address be in $t2. The instruction also requires that the number of words to copy in $t3 (which is >0, that means how many words are needed to copy). Furthermore, assume that the values of these registers as well as register $t4 can be destroyed in executing this instruction (so that the registers can be used as temporaries to execute the instruction, don't need to consider save and restore the value in temporary registers).
Below is complete code with example. Please note that you only require the function part of code at the botom of code (from lable array_copy:) for your assignment code above that is just to test the function code. Let me know if you have any problem or doubt. Thank you.
===========================================================================
.data
   # declare data for program
   orig:   .asciiz "Array: "
   copy:   .asciiz "Copy: "
   space:   .asciiz " "
   endl:   .asciiz "\n"
   ints:   .word   1 2 3 4 5 6 7 8 9
10
   copy_:   .word   0   # 0
is place holder for words
.text
   .globl main
main:
   # main function to test the code for copy_array
function
   # get addresses in rigister and call the
function
   la   $t1, ints
   la   $t2, copy_
   li   $t3, 10      
# copy 10 words
   jal array_copy
   # first print original array and then print copy array
on console
   la   $a0, orig
   li   $v0, 4
   syscall
   li   $t4, 0
orig_loop:
   mul $t5, $t4, 4   # word address are in size
of 4 bytes
   add $t5, $t5, $t1   # $t5 has address of
original array word
   # print given word on console
   lw   $a0, ($t5)
   li   $v0, 1
   syscall
   # print space
   la   $a0, space
   li   $v0, 4
   syscall
   addi $t4, $t4, 1
   # check loop iterator and print next word
   blt $t4, $t3, orig_loop
   # print copy array
   la   $a0, endl
   li   $v0, 4
   syscall
   la   $a0, copy
   li   $v0, 4
   syscall
   li   $t4, 0
copy_loop:
   mul $t5, $t4, 4   # word address are in size
of 4 bytes
   add $t5, $t5, $t2   # $t5 has address of
copy array word
   # print given word on console
   lw   $a0, ($t5)
   li   $v0, 1
   syscall
   # print space
   la   $a0, space
   li   $v0, 4
   syscall
   addi $t4, $t4, 1
   # check loop iterator and print next word
   blt $t4, $t3, copy_loop
  
   # exit program
   li   $v0, 10
   syscall
  
############################################################################################
##          BELOW IS
CODE FOR THE FUNCTION THAT IS REQUIRED FOR THE
QUESTION             
##
############################################################################################
array_copy:
   # source array address is provided in $t1
   # destination address is provided in $t2
   # number of words to copy is provided in $t3(it's >
0)
   # we can use temporary register without saving its
values
   # use $t4 as loop variable count
   li   $t4, 0
loop:   # calculate address of next word to be stored in
$t5
   mul $t5, $t4, 4   # word address are in size
of 4 bytes
   add $t5, $t5, $t1   # $t5 has address of
source word
   # load word in $t6
   lw   $t6, ($t5)
   mul $t5, $t4, 4   # word address are in size
of 4 bytes
   add $t5, $t5, $t2   # $t5 has address of
destination
   # store word in $t6 to destination address
   sw   $t6, ($t5)
   # increament loop counter
   addi $t4, $t4, 1
   # check number of words copied and loop again or
return
   blt $t4, $t3, loop
   jr   $ra       #
return to function call
   
===========================================================================
