Solved

# MIPS - Memory allocation(Array of Structure Pointers)

Posted on 2006-05-09
2,298 Views
Consider the following C-style struct to represent a date:

struct DATE
{
int day;
int month;
int year;
};

Use the same date structure as the preceding exercises.

In the static data section of your program declare space for an array of up to 25 addresses:

.data
size:      .word      0
dates:      .space      25*4

You might not use all of the potential addresses.  The field size starts at zero because to start, none of the addresses are used.

Write a program that asks the user for how many dates are needed. Set size to that amount. Now a counting loop executes for that many times. Each execution of the loop allocates space for one data, puts the address of that date in the array of addresses, asks the user for values for that data, and finally, fills the date with those values.

At the end of the program, write out the values in the dates. Do this by scanning through the array of addresses to access each date. (Probably the dates will be in order in a contiguous block of memory, but a real-world program can not count on this being true.)
0
Question by:cyrus_bu

LVL 16

Expert Comment

Hi cyrus_bu,

As this is homework, please post what you've done so far or explain what it is you are finding difficulty with. We can help with homework but we can 'do' your homework.

Paul
0

Author Comment

As this problem, I don't have any idea to do that. Do you have any idea to do this problem?
0

LVL 3

Accepted Solution

Hi cyrus_bu,

Here is one of solutions.

.text
.global __start

__start:

li      \$v0, 4                        #print How many dates (1-25)?
syscall

li      \$v0, 5                        #read num of dates
syscall

bgt      \$v0, 25, __start      #ask again if num > 25
blt      \$v0,  1, __start      #also if num < 1

sw      \$v0, size                  #size = num of dates

li      \$t0, 1                        #input date count
lw      \$t1, size                  #total date count
la      \$s1, dates                  #base addr of dates array

get_nxtdate:

bgt      \$t0, \$t1, prt_dates

li      \$a0, 12                        #12 bytes to be allocated
li      \$v0, 9                        #alloc ram
syscall
sw      \$v0,0(s1)                  #store ram addr in array

subu      \$sp, \$sp, 12                  #save \$t0,\$t1,\$s1 in stack
sw            \$t0, 8(\$sp)            #before call input()
sw            \$t1, 4(\$sp)
sw            \$s1, 0(\$sp)

move      \$a0,\$v0                        #pass alloc ram addr
jal            input
nop

lw            \$t0, 8(\$sp)            #restore registers from
lw            \$t1, 4(\$sp)            #stack
lw            \$s1, 0(\$sp)
addu      \$sp, \$sp, 12                  #balance stack pointer

add            \$t0, \$t0, 1            #bump input date count
add            \$s1, \$s1, 4            #points to next array slot
j            get_nxtdate            #go get next date

prt_dates:

li      \$t0, 1                        #input date count
lw      \$t1, size                  #total date count
la      \$s1, dates                  #base addr of dates array

prt_nxtdate:

bgt      \$t0, \$t1, done

subu      \$sp, \$sp, 12                  #save \$t0,\$t1,\$s1 in stack
sw            \$t0, 8(\$sp)            #before call input()
sw            \$t1, 4(\$sp)
sw            \$s1, 0(\$sp)

lw            \$a0,0(\$s1)            #pass next allocated ram addr
jal            output
nop

lw            \$t0, 8(\$sp)            #restore registers from
lw            \$t1, 4(\$sp)            #stack
lw            \$s1, 0(\$sp)
addu      \$sp, \$sp, 12                  #balance stack pointer

add            \$t0, \$t0, 1            #bump input date count
add            \$s1, \$s1, 4            #points to next array slot
j            prt_nxtdate            #go get next date

done:

li            \$v0, 10                  #exit program
syscall

.data
ask_num:      asciiz      "\nEnter number of dates (max. 25):"
size            .word      0
dates            .space      25*4      #store 25 allocated ram address

#############################################################################
This program is very similar with your last one. The last program you allocated 12 bytes of RAM for DATE structure
and saved its address in the variable, ptr. This program will call such allocation up to 25 times (based on an
user's input) and stored those pointers/addresses in the array of addresses, which is the variable, dates.
Remember, the dates had reserved 25 slots, one for each RAM address.

With the above __start routine, you need to copy and paste your input and output routines in order to make a
complete program.

0

Author Comment

Hi PeterdLo:

Thanks for your help. You guys are the best programmer.. Before your post, i was figured out the program, but i will follow your code to check with my program. Thanks!!
0

## Featured Post

### Suggested Solutions

Show semaphore name in Pascal 12 623
False Theorem 27 904
How to make a Bootable Program without an OS 4 516
#if defined in C Source File ??? 12 441
We are happy to announce a brand new addition to our line of acclaimed email signature management products – CodeTwo Email Signatures for Office 365.
David Varnum recently wrote up his impressions of PRTG, based on a presentation by my colleague Christian at Tech Field Day at VMworld in Barcelona. Thanks David, for your detailed and honest evaluation!
Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…