In this project for Intro to Computer Architecture you are a
In this project for Intro to Computer Architecture, you are asked to do following:
1) Download Mars -- a MIPS simulator - to your laptop or desktop and learn to use it, including its editor, MIPS code compilation and execution. Mars website: http://courses.missouristate.edu/KenVollmar/MARS/.
2) Download the file project1.asm. Read it and make sure it can be compiled and executed in Mars.
3) Learn how to read an input integer and print out an integer in the console. Learn how to print out a string in the console.
4) Write code to finish all the functions listed in project1.asm. In project1.asm, an array A of 10 integers are given at the beginning (make sure do not change these integers):
34, 5, 11, -12, 60, -2, 14, 71, 13, -27
The finished project1.asm should read in two indices i and j (the range of each index is from 0 to 9) from the console, then load/compute and print out A[i], A[j], A[i]+A[j] and A[i]-A[j]. Here is an example:
-----------------------------------------------------
Index i [0~9]:
7
Index j [0~9]:
1
A[i]=71
A[j]=5
A[i]+A[j]=76
A[i]-A[j]=66
--------------------------------------------------------------------
I have provided some code in project1.asm. All the needed strings have been defined at the beginning of project1.asm. Please read the comments carefully before you work on this project.
Please submit your completed project1.asm with the added code. Make sure the submitted MIPS code is fully tested in Mars and can be compiled and run.
Please copy and paste the code I provide in the answer along with where the answers are supposed to be placed. Do not copy and paste from other questions beacuse they are not correct.
project1.asm reads as:
.data
baseadd: .word 34, 5, 11, -12, 60, -2, 14, 71, 13, -27
string1: .asciiz \"Index i [0~9]:\ \"
string2: .asciiz \"Index j [0~9]:\ \"
string3: .asciiz \"\ A[i]=\"
string4: .asciiz \"\ A[j]=\"
string5: .asciiz \"\ A[i]+A[j]=\"
string6: .asciiz \"\ A[i]-A[j]=\"
.text
main:
# Read input i to $s1
addi $v0, $zero, 4 # code for printing string is 4
la $a0, string1 # load address of string to be printed into $a0
syscall # call operating system
addi $v0, $zero, 5 # code for reading integer is 5
syscall # call operating system
add $s1, $v0, $zero # i in $s1
# Read input j to $s2
addi $v0, $zero, 4 # code for printing string is 4
la $a0, string2 # load address of string to be printed into $a0
syscall # call operating system
# Write code here to read input j to $s2
#baseadd of the array to $s5
la $s5, baseadd
# Write code here to load A[i] to $s3
# Print A[i] from $s3
addi $v0, $zero, 4 # code for printing string is 4
la $a0, string3 # load address of string to be printed into $a0
syscall # call operating system
add $a0, $s3, $zero
addi $v0,$zero,1 # prints integer
syscall
# Write code here to load and print A[j]
# Write code here to compute and print A[i]+A[j]
# Write code here to compute and print A[i]-A[j]
# exit
addi $v0, $zero, 10
syscall
Solution
.data
baseadd: .word 34, 5, 11, -12, 60, -2, 14, 71, 13, -27
string1: .asciiz \"Index i [0~9]:\ \"
string2: .asciiz \"Index j [0~9]:\ \"
string3: .asciiz \"\ A[i]=\"
string4: .asciiz \"\ A[j]=\"
string5: .asciiz \"\ A[i]+A[j]=\"
string6: .asciiz \"\ A[i]-A[j]=\"
.text
main:
# Read input i to $s1
addi $v0, $zero, 4 # code for printing string is 4
la $a0, string1 # load address of string to be printed into $a0
syscall # call operating system
addi $v0, $zero, 5 # code for reading integer is 5
syscall # call operating system
add $s1, $v0, $zero # i in $s1
# Read input j to $s2
addi $v0, $zero, 4 # code for printing string is 4
la $a0, string2 # load address of string to be printed into $a0
syscall # call operating system
# Write code here to read input j to $s2
#baseadd of the array to $s5
la $s5, baseadd
# Write code here to load A[i] to $s3
# Print A[i] from $s3
addi $v0, $zero, 4 # code for printing string is 4
la $a0, string3 # load address of string to be printed into $a0
syscall # call operating system
add $a0, $s3, $zero
addi $v0,$zero,1 # prints integer
syscall
# Write code here to load and print A[j]
# Write code here to compute and print A[i]+A[j]
# Write code here to compute and print A[i]-A[j]
# exit
addi $v0, $zero, 10
syscall


