程序代写代做代考 Java assembly mips Tutorial Week 10 (Solutions)

Tutorial Week 10 (Solutions)

Task 1. Solutions are tested in MARS 4.5.
Solution for task 1, part 2:
.data

# Data to encrypt
data: .ascii “this is 16 bytes”

# Encryption key
key: .ascii “the key is here!”

# Destination for encrypted data
dest: .space 16

.text
.globl main

main:
la $a0, data # Store address of the first byte of data in $a0
la $a2, key # Store address of the first byte of key in $a2
la $a3, dest # Store address of the first byte of dest in $a3
li $a1, 16 # Store the length of the data in $a1
add $t0, $a0, $a1 # Store the address of the first byte after data in
# $t0, which can be used as a stop condition for
# our loop

loop:
ble $t0, $a0, end # Check we haven’t reached the end of the data
lw $t1, ($a0)
lw $t2, ($a2)
xor $t3, $t1, $t2 # Xor the current word of data with the current word
# of key
sw $t3, ($a3)
addi $a0, $a0, 4 # Increment the data segment pointers
addi $a2, $a2, 4
addi $a3, $a3, 4
b loop # Go back to the loop condition check

end:
li $v0, 10 # Make an exit system call
syscall

Solution for task 1, part 2 (with a temporary register):
add $a2, $a1, $zero # Equivalent to ‘move $a2, $a1’.
add $a1, $a0, $zero
add $a0, $a2, $zero

Solution for task 1, part 2 (without a temporary register):
add $a1, $a1, $a0 # Add them together, overwriting $a1
sub $a0, $a0, $a1 # Subtract the sum of both from $a0, leaving -(initial value of $a1) in $a0
neg $a0, $a0 # Negate $a0 to obtain the initial value of $a1
sub $a1, $a1, $a0 # Subtract $a0 from $a1, leaving the initial value of $a0 in $a1

Solution for task 1, part 3:
.text
.globl main

main:
li $a0, 43110
li $v0, 1 # 1 is the print integer syscall code
syscall
b exit

exit:
li $v0, 10 # 10 is the exit syscall code
syscall

Solution for task 1, part 4:
.text
.globl main

main:
jal print # ‘Jump and link’ – jump to the label, and put the address
# of the next command that would otherwise be executed in
# $ra (return address) so we can jump back and resume
move $a0, $a1
jal print
move $a0, $a2
jal print
move $a0, $a3
jal print
move $a0, $t0
jal print
move $a0, $t1
jal print
move $a0, $t2
jal print
move $a0, $t3
jal print
move $a0, $t4
jal print
move $a0, $t5
jal print
move $a0, $t6
jal print
move $a0, $t7
jal print
b exit

print:
li $v0, 1 # 1 is the print integer syscall code
syscall
jr $ra # Jump back to the caller

exit:
li $v0, 10 # 10 is the exit syscall code
syscall

Task 2. For MARS 4.5 you can use the following.
java -jar Mars_4_5.jar dec ae777 filename a0

Here Mars_4_5.jar is contains the MARS code, and the file containing the MIPS assembly is in filename.
Task 3. Here are the programs.
• Program 1 is the factorial program.
• Program 2 computes the Fibonacci numbers.
• Program 3 computes the Ackermann function.