程序代写代做代考 assembler mips Tutorial Week 9 (Solutions)

Tutorial Week 9 (Solutions)

Task 1. Assume you are dealing with a 32 bit microprocessor that accesses memory in words (i.e. 32 bits) at a time, while memory is addressed bytewise. You should store strings in memory in an aligned way. You should use as little memory as possibel without becoming unaligned.
• Store the string “S” starting at an address above or equal to 2012 (decimal). ——
• 2012 S
• 2013
• 2014
• 2015
• ——
• 

• Store the string “S” starting at an address above or equal to 2013 (decimal). ——
• 2012
• 2013
• 2014
• 2015
• ——
• 2016 S
• 2017
• 2018
• 2019
• ——

• 

• Store the strings “Sunshine”, “Rain”, “Joy”, “Pain” starting at an address above or equal to 2012 (decimal). ——
• 2012 S
• 2013 u
• 2014 n
• 2015 s
• ——
• 2016 h
• 2017 i
• 2018 n
• 2019 e
• ——
• 2020 R
• 2021 a
• 2022 i
• 2023 n
• ——
• 2024 J
• 2025 o
• 2026 y
• 2027
• ——
• 2028 P
• 2029 a
• 2030 i
• 2031 n
• ——
• 

• Store the strings “Sunshine”, “Rain”, “Joy”, “Pain” starting at an address above or equal to 2013 (decimal). ——
• 2012
• 2013
• 2014
• 2015
• ——
• 2016 S
• 2017 u
• 2018 n
• 2019 s
• —-
• 2020 h
• 2021 i
• 2022 n
• 2023 e
• —-
• 2024 R
• 2025 a
• 2026 i
• 2027 n
• —-
• 2028 J
• 2029 o
• 2030 y
• 2031
• —-
• 2032 P
• 2033 a
• 2034 i
• 2035 n
• ——
• 

Task 4. Write the following programs in MIPS assembler language. Recall that register $zero is hardwired to 0. For each subtask we give one possible solution — there are many other ways to implement each subtask.
• A program that counts the content of register $t1 down to 0 and then terminates. li $t1 25
• loop:
• beq $zero $t1 exit
• addi $t1 $t1 -1
• b loop
• exit:
• 

• A program that counts the content of a memory cell down to 0 and then terminates. .data
• L1: .word 25
• .text
• .globl loop
• loop:
• lw $t1 L1 ( $zero )
• beq $zero $t1 exit
• addi $t1 $t1 -1
• sw $t1 L1 ( $zero )
• b loop
• exit:
• 

• A program that copies 100 Bytes from one region of memory to another. .data
• L1: .ascii
• “1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890”
• # 100 bytes, formatted, so you can watch data being copied over.
• L2: .space 100

• .text
• .globl main
• main:
• li $t1, 0
• li $t2, 0
• li $t3, 100
• loop:
• beq $t1, $t3, exit
• lw $t8, L1($t1)
• sw $t8, L2($t2)
• addiu $t1, $t1, 4
• addiu $t2, $t2, 4
• b loop
• exit:
• li $v0, 10
• syscall

• 

• A program that copies N words from the memory region starting at A to the memory region starting at B. Note that the source and target regions may overlap, or incrementing a register may lead to wrapping of the register back to 0. Is that a problem for your program? The solution to the previous problem is sufficient here, where we use L1 as A, L2 as B, and $t3 for N. 

• Write a program that computes (2+3)*4 and leaves the result on top of the stack (remember that the stack pointer should always point to the first free memory cell that’s above the top of the stack). li $t1 2
• li $t2 3
• add $t1 $t1 $t2
• li $t2 4
• mul $t1 $t1 $t2
• sw $t1 0 ( $sp )
• addiu $sp $sp -4