3 Replies - 441 Views - Last Post: 20 February 2019 - 11:11 AM

#1 code2017   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-September 18

Mips code error with array loop

Posted 13 February 2019 - 10:46 PM

I've tried a few things but I still haven't figured out why my program fails when compiling, error occurs after the sys call, program accepts the number of elements user wants to add to array and then asks user to enter numbers for array to find the median at the end, yet my code keeps going in a loop and doesnt stop.
.data

Array: .word 0,0,0,0,0,0,0,0,0,0

str0: .asciiz "\nHow many numbers would you like to add: "

str1: .asciiz "\nEnter number: "

str2: .asciiz "\nThe median is: "

content0: .asciiz "===========================================\n"

content1: .asciiz "\n==========================================="

content2: .asciiz "\n===========================================\n"

error0: .asciiz "\nError--- Must be between 1 and 10: \n"

.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

This post has been edited by andrewsw: 14 February 2019 - 01:45 AM
Reason for edit:: fixed failed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Mips code error with array loop

#2 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6813
  • View blog
  • Posts: 28,189
  • Joined: 12-December 12

Re: Mips code error with array loop

Posted 14 February 2019 - 01:46 AM

What errors occur?
Was This Post Helpful? 0
  • +
  • -

#3 code2017   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-September 18

Re: Mips code error with array loop

Posted 14 February 2019 - 10:14 AM

After user types the desired number of elements that are going to be used in the array, the prompt enter a number is displayed for user to continue typing numbers yet it doesn't stop. For example if user wants to input 5 numbers, the code simply doesnt stop asking for numbers after fifth number.
Was This Post Helpful? 0
  • +
  • -

#4 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,112
  • Joined: 03-April 12

Re: Mips code error with array loop

Posted 20 February 2019 - 11:11 AM

Have you tried single stepping?
Or maybe start with adding integer print after integer read to see what you got.
(And maybe fix the comments.)

li $v0, 5 # User input
syscall
sw $v0, 0($s0) # Address of first element in the array
move $a0, $v0
li $v0, 1 # print integer
syscall


This post has been edited by turboscrew: 20 February 2019 - 11:30 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1