This assignment is designed to introduce you to the MIPS assembly language, usin
ID: 3878053 • Letter: T
Question
This assignment is designed to introduce you to the MIPS assembly language, using registers and memory, input/output syscalls and the MIPS simulator.
VERY IMPORTANT: You are restricted to the core instructions for this assignment. DO NOT use any pseudo or extended instructions. Turn off the option on MARS under settings that allows the use of these instructions, This means you must use the standard formats and no use of li or la instructions.
a. Open the text editor and type in the following. Then save your work as assign1.asm
.data
val1: .word 0 val2: .word 0 val3: .word 0 .text
.globl main main:
b. Add the following zero terminated ascii strings to the data segment after the three words.
1. Your name
2. “Enter a number “ 3. A new line (“ ”)
c. Add the MIPS assembly language instructions after the main for the following steps. You must show each step in the order given. Each step may require multiple instructions. Note that the variables val1, val2, and val3 represent addresses or locations in memory in the data segment. Use other registers as needed.
Steps:
1. Initialize the register $s0 to 19
2. Prompt the user to enter an integer and then read the integer into register $s1 3. Prompt the user to enter an integer and read the number into register $s2
4. Store the value in $s1 into the memory labeled “val1”
5. Store the value in $s2 into the memory labeled “val2”
6. Calculate the value of $s0 - $s1 + $s2 and store the result in the memory labeled “val3” 7. Print the following output each on its own line
a. Your name
b. The value in val1 c. The value in val2 d. The value in val3
8. Exchange or swap the values in $s1 and $s2 9. Set the value in $s0 to –$s0
10. Print the values finally in $s0, $s1 and $2
Specifics:
Use only the instructions covered to date.
Stop the program by using a syscall with the command 10.
Test, test, test!!!!!
Documentation:
Comment the beginning of your programs with your name, class ID, and assignment number.
Comment every instruction.
Explanation / Answer
Code:
.data
val1: .word 0
val2: .word 0
val3: .word 0
name: .asciiz "Your name"
string: .asciiz "Enter a number "
newline: .asciiz " "
.text
.globl main
main:
li $s0, 19 #initialise $s0 to 19
li $v0, 4 # set v0 to 4, for string output
la $a0, string #set a0 to address of string
syscall #perform system call
#read integer from user
li $v0, 5
syscall
move $s1, $v0 #store it in $s1
#prompt user for integer
li $v0, 4
la $a0, string
syscall
#read from user
li $v0, 5
syscall
move $s2, $v0 #store in $s2
sw $s1, val1 #store s1 in val1
sw $s2, val2 #store s2 in val2
#calculate s0-s1+s2
sub $t0,$s0,$s1
add $t0,$t0,$s2
sw $t0, val3 #store result in val3
#print name and newline
li $v0, 4
la $a0, name
syscall
li $v0, 4
la $a0, newline
syscall
#print val1 and newline
li $v0, 1
lw $a0, val1
syscall
li $v0, 4
la $a0, newline
syscall
#print val2 and newline
li $v0, 1
lw $a0, val2
syscall
li $v0, 4
la $a0, newline
syscall
#print val3 and newline
li $v0, 1
lw $a0, val3
syscall
li $v0, 4
la $a0, newline
syscall
#swap s1 and s2
move $t0, $s1
move $s1, $s2
move $s2, $t0
#negate s0
sub $s0,$0,$s0
#print s0 and newline
li $v0, 1
move $a0, $s0
syscall
li $v0, 4
la $a0, newline
syscall
#print s1 and newline
li $v0, 1
move $a0, $s1
syscall
li $v0, 4
la $a0, newline
syscall
#print s3 and newline
li $v0, 1
move $a0, $s2
syscall
li $v0, 4
la $a0, newline
syscall
#exit with syscall
li $v0, 10
syscall
Output:
Enter a number 5
Enter a number 10
Your name
5
10
24
-19
10
5
-- program is finished running --
Hope this helps. Do Upvote! :)