• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2793
  • Last Modified:

MIPS - Memory allocation(Array of Structure Pointers)

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
cyrus_bu
Asked:
cyrus_bu
  • 2
1 Solution
 
PaulCaswellCommented:
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
 
cyrus_buAuthor Commented:
As this problem, I don't have any idea to do that. Do you have any idea to do this problem?
0
 
PeterdLoCommented:
Hi cyrus_bu,

Here is one of solutions.

      .text
      .global __start

__start:

      la      $a0, ask_num
      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.

Hopfully, this will help you to understand your program better.

      
      
      
      
0
 
cyrus_buAuthor Commented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now