Solving MIPS Programming: Enter 5 Ints & Reverse Order

  • Thread starter Thread starter missinglink
  • Start date Start date
  • Tags Tags
    Mips Programming
AI Thread Summary
The discussion centers on troubleshooting a MIPS assembly program that is intended to prompt the user for five integers, store them in an array, and display them in reverse order. The user reports an issue where a line of code, specifically `sw $t2, 0($t0)`, is causing errors due to the pointer being incorrectly set, leading to attempts to write to address 0. Key points raised include the need for better code organization through comments, which would clarify the purpose of each section and help prevent logical errors. It is suggested that the loop for input should correctly track the number of entries to ensure it only prompts for five integers. Additionally, the output section should print the numbers in reverse order without needing to reverse the array itself. The discussion emphasizes the importance of understanding memory addresses and ensuring that pointers are initialized correctly to avoid runtime exceptions.
missinglink
Messages
1
Reaction score
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
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

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?

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:
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top