Solving MIPS Programming: Enter 5 Ints & Reverse Order

In summary: The only value I could find that was 0 was $v0, which is the syscall code for exit. I'm not sure why you're storing a value there. Did you mean to use $t1 instead of $t0?In summary, the code provided is attempting to teach mips by writing a simple program, but it is not functioning properly due to strings declared in .data having an impact on one of the pointers. The program is supposed to allow the user to enter 5 integers, store them in an array, and then display them in reverse order. However, the code is not properly keeping track of the input and is causing an exception at a specific line. The code could benefit from more comments and breaking up
  • #1
missinglink
1
0
I am trying to teach mips to my self and wrote a fairly simple program and it isn't working and found out that the strings I declared in .data are somehow affecting one of my pointers.

Here is the code:

#Write a program that allows the user to enter 5 ints and store these ints in an array and display them in reverse order

.data


msg: .asciiz "Enter your number: "
msg1: .asciiz "Here are your numbers: "
msg2: .asciiz " "
array: .data 20


.text

main:

la $s0, array

add $t0, $s0, $zero

li $t1, 0
li $t2, 5

loop:

li $v0, 4

la $a0,msg

syscall

li $v0, 5

syscall

add $t2, $v0, $zero

sw $t2, 0($t0) # This is the line causing errors

addi $t0, $t0, 4
addi $t1, $t1, 1

slt $t3, $t1, $t2

bne $t3, $zero, loop

li $v0, 4

la $a0, msg1

syscall

li $t5, 0

loop1:

add $t0, $t0, -4

lw $t4, 0($t0)

li $v0, 4

add $a0, $t4, $zero

syscall

li $v0, 4

la $a0, msg2

syscall

addi $t1, $t1, -1

slt $t2, $t5, $t1

bne $t2, $zero, loop1

li $v0, 10

syscall


Can anyone help please?
 
Technology news on Phys.org
  • #2
When you post code, it's good manners to put [ code ] and [ /code ] tags around it (without the extra spaces). I have done that below.
missinglink said:
I am trying to teach mips to my self and wrote a fairly simple program and it isn't working and found out that the strings I declared in .data are somehow affecting one of my pointers.

Here is the code:
Code:
#Write a program that allows the user to enter 5 ints and store these ints in an array and display them in reverse order

.data 


msg: .asciiz "Enter your number: "
msg1: .asciiz "Here are your numbers: "
msg2: .asciiz " "
array: .data 20


.text

main:

la $s0, array

add $t0, $s0, $zero

li $t1, 0
li $t2, 5

loop:

li $v0, 4

la $a0,msg

syscall

li $v0, 5

syscall

add $t2, $v0, $zero

[COLOR="Red"]sw $t2, 0($t0)[/COLOR] # This is the line causing errors

addi $t0, $t0, 4
addi $t1, $t1, 1

slt $t3, $t1, $t2

bne $t3, $zero, loop

li $v0, 4

la $a0, msg1

syscall

li $t5, 0

loop1:

add $t0, $t0, -4

lw $t4, 0($t0)

li $v0, 4

add $a0, $t4, $zero

syscall

li $v0, 4

la $a0, msg2

syscall

addi $t1, $t1, -1

slt $t2, $t5, $t1

bne $t2, $zero, loop1

li $v0, 10

syscall

Can anyone help please?

I took a stab at running your code, and hit the same exception at the line you indicated. Since there essentially no comments in your code, it's difficult to tell what you are trying to do. Comments are even more important in assembly code, to help a reader (including yourself) understand what, and more importantly, why you are doing something.

It would be a good idea for you to break up your code into sections, using comments, where each section has a specific purpose.

The first block of code should ask the user to input the five numbers. When I ran your code, it continued asking for numbers well past the fifth. Your loop should keep track of how many times it has run, so that it asks for only five numbers and then moves on.

The final block should print the numbers. The simplest way to do this would be to merely print the numbers in reverse order, starting at the last number in the array (the highest memory address), and working your way to the beginning. I am assuming you don't actually need to reverse the numbers in the array itself.

With regard to the exception you're getting, this code:
Code:
sw $t2, 0($t0)

stores the value at $t2 (R10) at location $t0 + 0 (R8 + 0). When I ran the program, the address in R8 was 0, which meant that the program was trying to store a value in location 0.
 
Last edited:

1. What is MIPS programming?

MIPS (Microprocessor without Interlocked Pipeline Stages) is a reduced instruction set computer (RISC) instruction set architecture (ISA) developed by MIPS Technologies. It is commonly used in embedded systems, gaming consoles, and other electronic devices.

2. What is the purpose of solving MIPS programming?

Solving MIPS programming allows for efficient and optimized development of software for MIPS-based devices. It involves creating and implementing algorithms and code in the MIPS assembly language.

3. What is the process for solving MIPS programming?

The process for solving MIPS programming involves understanding the problem, designing an algorithm to solve it, writing the code in MIPS assembly language, and testing and debugging the code to ensure it functions correctly.

4. What are the steps for entering 5 integers and reversing their order in MIPS programming?

The steps for entering 5 integers and reversing their order in MIPS programming are:

  • Declare and initialize variables to store the integers
  • Use a loop to input the integers from the user
  • Store the integers in an array
  • Use another loop to print the integers in reverse order

5. How can I improve my skills in solving MIPS programming?

The best way to improve your skills in solving MIPS programming is to practice regularly and familiarize yourself with the MIPS assembly language. You can also study and analyze existing MIPS programs, participate in online communities and forums, and take courses or tutorials to learn more about MIPS programming techniques and best practices.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Programming and Computer Science
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
8K
  • Programming and Computer Science
Replies
5
Views
9K
  • Programming and Computer Science
Replies
1
Views
5K
  • Programming and Computer Science
Replies
1
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
Back
Top