Reputation: 8144
Okay guys, basically the problem I'm having is this.
I've been assigned to write a MIPS program that stores a struct dynamically.
Basically, it stores an ID, a Year, a Title, and a Description It's to be stored using a binary search tree.
If you've ever coded a stack in C++ you know what I'm talking about. I've successfully stored the ID and Titles in memory dynamically, but I'm having trouble storing the user-entered strings.
This is a complex question and there's not a lot of info that I've been able to find online so props if you can help me out with this :)
Here is my memory setup:
$s5 - Stores Root Node
$s7 - Stores Size of Tree (Not Necessary)
Each New Item Contains a chunk of 344 bytes
The bytes are setup as such:
8 Bytes - [ID]
8 Bytes - [Year]
64 Bytes - [Title]
256 Bytes - [Description]
8 Bytes - [LastNodeAddress]
8 Bytes - [NextNodeAddress]
Here's the code and you may see the issue:
li $v0, 9 #allocate memory for new record
li $a0, 344 #enough memory for 2 addresses and all the data
syscall
move $s0, $v0 #hang onto the initial address of all our info
li $v0, 4 #prompt for ID
la $a0, addid
syscall
li $v0, 5 #enter integer
syscall
sw $v0, 0($s0) #store our ID into memory Offset: 0
li $v0, 4 #prompt for add year
la $a0, addyear
syscall
li $v0, 5 #enter integer
syscall
sw $v0, 4($s0) #store year into our memory Offset: 4
li $v0, 4 #prompt for add title
la $a0, addtitle
syscall
li $v0, 8 #read title into titlebuffer
la $a0, titlebuffer
li $a1, 64
syscall
sw $a0, 8($s0) #store title into our memory Offset: 8
li $v0, 4 #prompt for add description
la $a0, adddescription
syscall
li $v0, 8 #read from input into descriptionbuffer
la $a0, descriptionbuffer
li $a1, 256
syscall
sw $a0, 72($s0) #store description into our memory Offset: 72
bne $s7, 0, setlocations #if this isn't root node let's set the locations
add $s7, $s7, 1 #add 1 to the size of the records
move $s5, $s0 #store this address as root node for now
The problem is that all that's being stored is the address of the buffers. The buffers are defined in my data section like this:
.data
titlebuffer: .space 64
descriptionbuffer: .space 256
What I end up with is just the addresses stored in the memory I allocated, and I have no idea how to store strings into allocated memory.
Any help would be greatly appreciated! :)
Upvotes: 1
Views: 17451
Reputation: 8144
Don't bother defining your memory at the start of your program like I showed in the original question.
Instead, allocate it and read the values into the correct offsets of your dynamic memory.
Instead of la $a0, descriptionbuffer
Instead of la $a0, titlebuffer
Use:
la $a0, 8($s0)
la $a0, 72($s0)
Here I move the memory address into $s0
using move $s0, $v0
and read the values into the correct offsets.
to print you do the same thing!
Here is the working code:
li $v0, 9 #allocate memory for new record
li $a0, 344 #enough memory for 2 addresses and all the data
syscall
move $s0, $v0 #hang onto the initial address of all our info
li $v0, 8 #read our title into the allocated space
la $a0, 8($s0) #Offset: 8
li $a1, 64
syscall
li $v0, 8 #read our description into the allocated space
la $a0, 72($s0) #Offset: 72
li $a1, 256
syscall
In addition, you can find the final solution here: https://stackoverflow.com/a/9953839/1274820
Edit: Well, after 10k views, I decided to add more info here so you don't have to search through later code 😉
Here's the full code to store 4 different pieces of data in memory:
li $v0, 9 #allocate memory for new record
li $a0, 344 #[334 = how much memory - in bytes]
syscall
move $s0, $v0 #store the address of our allocated memory in $s0
li $v0, 5 #enter integer
syscall
sw $v0, 0($s0) #store our ID into memory Offset: 0
li $v0, 5 #enter integer
syscall
sw $v0, 4($s0) #store year into our memory Offset: 4
li $v0, 8 #read our title into the allocated space
la $a0, 8($s0) #Offset: 8
li $a1, 64
syscall
li $v0, 8 #read our description into the allocated space
la $a0, 72($s0) #Offset: 72
li $a1, 256
syscall
That will store ID, Year, Title, and Description - the offset is where we place the data in the dynamic memory.
Imagine that I have a block of 334 bytes (like above):
[ 334 ]
We store the ID
an integer (4 bytes of data) at offset 0 like this:
[(ID) 330 ]
Then, we store the year next to it (at offset 4).
[(ID)(YR) 326 ]
And so on ...
To print it, do it like this:
li $v0, 1 #Print the ID stored at $s0 [Offset: 0]
lw $a0, 0($s0)
syscall
li $v0, 1 #Print the Year stored at $s0 [Offset: 4]
lw $a0, 4($s0)
syscall
li $v0, 4 #Print the Title stored at $s0 [Offset: 8]
la $a0, 8($s0)
syscall
li $v0, 4 #Print descript stored at $s0 [Offset: 72]
la $a0, 72($s0)
syscall
Upvotes: 6