Question

In: Computer Science

Write the MIPS assembly codes to implement the following function: copy a block of words from...

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).

Solutions

Expert Solution

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
   

===========================================================================


Related Solutions

Use MARS to write and simulate a MIPS assembly language program to implement the strlen function....
Use MARS to write and simulate a MIPS assembly language program to implement the strlen function. The program should ask for a user's input and print the length of the user's input. Write the main function to call strlen. The main function should: - Prompt the user for input - Pass that input to strlen using registers $a0. - Preserve (i.e. push onto the stack) any T registers that the main function uses. The strlen function should: - Preserve any...
Write a MIPS assembly program to implement each of the following operations. 1. DM[6892] = DM[6884]...
Write a MIPS assembly program to implement each of the following operations. 1. DM[6892] = DM[6884] (Copy the 32-bit data at data memory address 6884 to data memory address 6892.) 2. DM[7968] = DM[6796] + DM[6800] (Add the 32-bit data at data memory address 6792 and the 32-bit data at data memory address 6800, then store the sum into data memory address 7968.)
Write a MIPS assembly program to implement each of the following operations. 1. DM[6892] = DM[6884]...
Write a MIPS assembly program to implement each of the following operations. 1. DM[6892] = DM[6884] (Copy the 32-bit data at data memory address 6884 to data memory address 6892.) 2. DM[7968] = DM[6796] + DM[6800] (Add the 32-bit data at data memory address 6792 and the 32-bit data at data memory address 6800, then store the sum into data memory address 7968.)
In MIPS assembly language, write a function that will display the max and min value in...
In MIPS assembly language, write a function that will display the max and min value in an array. Then write a function to calculate and display the average of all values in an array. This must be done in MIPS assembly language.
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the...
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the following declaration: void towers(int n, char source, char dest, char spare); The function outputs a message describing each move. The source, destination, and spare poles are indicated with a character identifier of your choosing ('A', 'B', 'C' are common). Write a MIPS assembly language program that demonstrates the Towers of Hanoi procedure. Your program should ask the user for the number of disks. The...
MIPS ASSEMBLY : 1) Code in MIPS a function that allows to store the text entered...
MIPS ASSEMBLY : 1) Code in MIPS a function that allows to store the text entered by the user in a 300-byte buffer. The function must return the actual size of the entered text in number of bytes. The text could contain lines, that is to say that the symbol '\ n' could be present. The end of reading is defined by both consecutive '\ n' '\ n' symbols. 2) Code in MIPS a function that helps identify if the...
This is to be done with MIPS assembly language. Write MIPS code which is equivalent to...
This is to be done with MIPS assembly language. Write MIPS code which is equivalent to the follow java program: int day = (int)(Math.random() * 7); switch (day) { case 1: System.out.println(“Monday”); break case 2: System.out.println(“Tuesday”); break case 3: System.out.println(“Wednesday”); break case 4: System.out.println(“Thursday”); break case 5: System.out.println(“Friday”); break case 6: System.out.println(“Saturday”); break case 0: System.out.println(“Sunday”); break }
Write a program in MIPS assembly language to perform the calculation of the following equation and...
Write a program in MIPS assembly language to perform the calculation of the following equation and save the result accordingly:    f = 5x + 3y + z Assumptions: - Registers can be used to represent variables x, y, z, and f - Initialize x, y, and z to values of your choice. f can be initialized to zero. - Use comments to specify your register usage and explain your logic
Translate the following C code to MIPS assembly. The main function and subfunction are translated to...
Translate the following C code to MIPS assembly. The main function and subfunction are translated to two separate .asm files. Finish the assembly code segment for the above requirement. int main() { int x=2; int y=1; int z=0; z=Subfunc(x,y); printf(“Value of z is: %d”, z); } int Subfunc(int x, int y) { int t1=0; t1=x+y+100; return t1;} File 1: .data str: .asciiz "The value of z:" .text #.globl main main: addi $s0, $0,2 #x addi $s1, $0,1 #y addi $s2,...
Write a MIPS Assembly language program to request a file name from the user, open the...
Write a MIPS Assembly language program to request a file name from the user, open the file, read the contents, and write out the contents to the console. This is what I have so far: .data    fileName:   .space 100    prompt1:   .asciiz "Enter the file name: "    prompt2:    .asciiz ""    prompt3:   .asciiz "\n"    buffer:    .space 4096 .text    main:        #        li $v0, 4        la $a0, prompt1       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT