Question
Having trouble with my code.... USING MIPS.... 1) Write a program that prompt the user to input n numbers (0 array. Then it finds the
Having trouble with my code....
USING MIPS....
1) Write a program that prompt the user to input n numbers (0 array. Then it finds the median of the array, assuming the elements are in ascending order. How many number you like to enter: 9 =============================== Enter number 1: 10 Enter number 2: 20 Enter number 3: 30 Enter number 4: 40 Enter number 5: 50 Enter number 6: 60 Enter number 7: 70 Enter number 8: 80 Enter number 9: 90 =============================== The median is: 50 ===============================
Another example would be:
How many number you like to enter: 6
===============================
Enter number 1: 11
Enter number 2: 15
Enter number 3: 16
Enter number 4: 18
Enter number 5: 212
Enter number 6: 220
===============================
The median is: 17
===============================
this is what I have so far, please try not to change my code too much because this is a basic course so we're learned the basic stuff......
.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