4) Write code to finish all the functions listed in project1.asm. In project1.as
ID: 3885319 • Letter: 4
Question
4) Write code to finish all the functions listed in project1.asm. In project1.asm, an array A of 11 integers are given at the beginning (make sure do not change these integers):
43, -5, 11, -12, 64, -7, 14, 71, 103, 13, -27
The finished project1.asm should read in two indices i and j (the range of each index is from 0 to 10) 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~10]:
1
Index j [0~10]:
2
A[i]=-5
A[j]=11
A[i]+A[j]=6
A[i]-A[j]=-16
--------------------------------------------------------------------
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.
here is the code:
.data
baseadd: .word 43, -5, 11, -12, 64, -7, 14, 71, 103, 13, -27
string1: .asciiz "Index i [0~10]: "
string2: .asciiz "Index j [0~10]: "
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
Explanation / Answer
Here is your complete code,
.data
baseadd: .word 43, -5, 11, -12, 64, -7, 14, 71, 103, 13, -27
string1: .asciiz "Index i [0~10]: "
string2: .asciiz "Index j [0~10]: "
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
addi $v0, $zero, 5 # code for reading integer is 5
syscall # call operating system
add $s2, $v0, $zero # j in $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] to $s4
#Print A[j] from $s4
addi $v0, $zero, 4 # code for printing string is 4
la $a1, string4 # load address of string to be printed into $a1
syscall # call operating system
add $a1, $s4, $zero
addi $v0,$zero,1 # prints integer
syscall
# Write code here to compute and print A[i]+A[j]
la $a0, string5 # load address of string to be printed into $a0
syscall # call operating system
addi $v0, $zero, $s3 # code for printing string is A[i]
ADD $s4,$v0,$zero #add the values in $s3 and $s4
add $a0,$s6,$zero
addi $vo,$zero,1 #prints addition
syscall
# Write code here to compute and print A[i]-A[j]
la $a0, string6 # load address of string to be printed into $a0
syscall # call operating system
addi $v0, $zero, $s3 # code for printing string is A[i]
SUB $s4,$v0,$zero #subtract the values in $s3 and $s4
add $a0,$s7,$zero
addi $vo,$zero,1 #prints subtraction
syscall
# exit
addi $v0, $zero, 10
syscall
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.