Question
Program the code using MARS simulator. How to calculate the median, it's giving me zero for the median? Don't use mtc1 and div.s to calculate
Program the code using MARS simulator.
How to calculate the median, it's giving me zero for the median?
Don't use mtc1 and div.s to calculate the median.
.data
Array: .word 0,0,0,0,0,0,0,0,0,0
str0: .asciiz " How many numbers would you like to add: "
str1: .asciiz " Enter number: "
str2: .asciiz " The median is: "
content0: .asciiz "=========================================== "
content1: .asciiz " ==========================================="
content2: .asciiz " =========================================== "
error0: .asciiz " Error--- Must be between 1 and 10: "
.text
main:
la $s0, Array # Load $s0 to array
li $v0, 4 # Display prompt for user input
la $a0, str0 # Load the string str
syscall
li $v0, 5 # User input
syscall
move $s1, $v0 # store $v0 to $s0
li $t0, 0 # Hard code to 0
li $t1, 11 # Hard code to 11
ble $s1, $t0, error # Check to see if input is less than 0
bge $s1, $t1, error # Check to see if input is greater than 11
li $v0, 4 # ========================
la $a0, content0
syscall
loop:
beq $t0, $s1, output # User input equals 0s
li $v0, 4 # Display prompt for user input
la $a0, str1 # Load the string str1
syscall
li $v0, 5 # User input
syscall
sw $v0, 0($s0) # Address of first element in the array
addi $t0, $t0, 1 # increment by 1
addi $s0, $s0, 4 # Add 4 to the next section
j loop # Jump to output
li $t6, 2 # Hard code to 2
div $s1, $t6 # divide by 2
mfhi $t3 # Remainder
mflo $t4 # Quotient
li $t5, 4 # Hard code to 4
mult $t4, $t5 # multiply $lo by 4
odd:
lw $t7, 0($s0)
mult $s0, $t5
j output
even:
lw $t8, 0($s0)
mult $t8, $t5
#mflo
j output
output:
li $v0, 4 # ==================
la $a0, content1
syscall
la $a0, str2 # Load string str2/ Display prompt
syscall
li $v0, 1 # Load integer
move $a0, $t7 # Display integer
syscall
li $v0, 4 # ==================
la $a0, content2
syscall
j exit # Jump to exit
error:
li $v0, 4 # Display error message
la $a0, error0 # if not between 0 and 11
syscall
j main # Jump back to main program
exit:
li $v0, 10 # Terminate program
syscall
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started