MIPS programming - store a list of numbers

In summary, the programmer is having difficulty coming up with a way to store a list of input numbers in memory, and has considered a number of different techniques. One possibility is to have the user enter the numbers into a fixed-size memory area, and to keep track of the number of entries in the area using a counter. If the user enters a number that causes the counter to overflow, the program should exit gracefully.
  • #1
ACLerok
194
0
Hi, I am a completely newb to the MIPS assembly language and I'm having a bit of trouble with this one procedure. Basically, i want to have the user enter in a list of numbers. The user can keep entering in numbers but when -999 is entered, the user cannot enter in anymore numbers. This list of numbres is saved and then printed out in the orderthey were entered. I'm having trouble figuring out how to implement this. Can anyone help me out or have some code to put me in the right direction? Thank you in advance.
 
Technology news on Phys.org
  • #2
ACLerok said:
Hi, I am a completely newb to the MIPS assembly language and I'm having a bit of trouble with this one procedure. Basically, i want to have the user enter in a list of numbers. The user can keep entering in numbers but when -999 is entered, the user cannot enter in anymore numbers. This list of numbres is saved and then printed out in the orderthey were entered. I'm having trouble figuring out how to implement this. Can anyone help me out or have some code to put me in the right direction? Thank you in advance.

This is an interesting question, if only because an unknown of input numbers have to be stored. Because how many numbers are to be entered isn't known, you can't just set aside a fixed amount of memory in the user data segment. Pushing the numbers onto the stack would be a possibility, but when you popped them off the stack, the numbers would be in the reverse order.

The only remaining technique that I can think of would be a linked list on the heap, with each node containing one input number and the address of the next node. After getting the first input value, save its address for later use, and then keep filling nodes with the input values and the value returned by the sbrk system service, which allocates memory from the heap.

I'll try to flesh this out in a later post.
 
  • Like
Likes berkeman
  • #3
ACLerok said:
The user can keep entering in numbers but when -999 is entered, the user cannot enter in anymore numbers.
Mark44 said:
Because how many numbers are to be entered isn't known, you can't just set aside a fixed amount of memory in the user data segment.
And good programming practice is to have some limit on the maximum number anyway, even if you are allocating memory as you go. The platform you are progamming on usually won't have an infinite amount of memory available, so you need to decide what upper limit you want to put on that number. You may still dynamically allocate memory, but you should use the upper-limit check to keep from overflowing memory areas or getting failed memory allocation requests from the computer, IMO.

One of the hardest problems to debug in real-world applications is if you haven't been careful to put limitation checks on memory allocations. We once went round and round for days trying to figure out the problem with a new embedded radio program. Turns out we didn't put a hard upper limit on the size of an incoming packet, because we knew how big the packets were going to be. So we just allocated plenty of room for any incoming packet, and went on our merry way. Turns out that certain noise patterns could mimic the start of a valid packet, and once that happens, you are off to the races storing noise as a packet, looking for a packet termination character. Boom! o0)
 
  • #4
berkeman said:
The platform you are progamming on usually won't have an infinite amount of memory available, so you need to decide what upper limit you want to put on that number. You may still dynamically allocate memory, but you should use the upper-limit check to keep from overflowing memory areas or getting failed memory allocation requests from the computer, IMO.
That's a good point.
I wasn't thinking in terms of establishing a limit, since the input was going to come from user entry, and the user would likely get tired of entering numbers long before the heap memory was exhausted. Nevertheless, if the program was later modified to take input from, say, a large disk file of several GB, it would be crucial to have the program exit gracefully rather than fail due to lack of memory.
 

What is MIPS programming?

MIPS (Microprocessor without Interlocked Pipeline Stages) is a type of computer architecture that uses a reduced instruction set computing (RISC) design. It is commonly used for high-performance computing applications and is often found in embedded systems.

What is meant by storing a list of numbers in MIPS programming?

Storing a list of numbers in MIPS programming involves using the data storage capabilities of the MIPS architecture to store a series of numerical values in a specific location in memory. This allows for easy access and manipulation of the numbers by the processor.

How do you store a list of numbers in MIPS programming?

To store a list of numbers in MIPS programming, you must first allocate space in the memory by using the .data directive. Then, you can use the .word directive to define the numbers in the list. Finally, you can use the sw (store word) instruction to store the numbers in the allocated memory locations.

What are the advantages of storing a list of numbers in MIPS programming?

Storing a list of numbers in MIPS programming allows for efficient data access and manipulation by the processor. It also allows for a compact and organized way of storing large sets of data in memory.

Can you provide an example of storing a list of numbers in MIPS programming?

Yes, here is an example of storing the numbers 5, 10, and 15 in MIPS programming:.datanumbers: .word 5, 10, 15.textmain:la $t0, numbers # load address of numbers into $t0sw $t1, 0($t0) # store 5 in the first memory locationsw $t2, 4($t0) # store 10 in the second memory locationsw $t3, 8($t0) # store 15 in the third memory location

Similar threads

  • Programming and Computer Science
Replies
4
Views
378
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
2
Replies
65
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
11
Views
994
  • Programming and Computer Science
Replies
20
Views
5K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
Back
Top