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

MIPS - Memory allocation program.

Consider the following C-style struct to represent a date: (Use array of structures)
struct DATE
{
  int day;
  int month;
  int year;
};

Write a program that asks the user for how many dates are needed and then allocates space (all at one time) for that many structs.

Then, in a loop, ask the user for the values to put in each structure. Do this by initializing a struct pointer to the base address of the allocated space. Then add the size of a struct to it for each execution of the loop.

Finally, print out the contents of all the structures.

You can use the same subroutine to print.  Modify the load data subroutine to prompt for the date fields instead.
0
cyrus_bu
Asked:
cyrus_bu
  • 2
  • 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:
Here is my code:

.text
      .globl __start

__start:
      la      $a0,str1       # input a number
      li      $v0,4
      syscall
      li      $v0,5
      syscall
      sw      $v0,size       # store the date
      nop

      move      $t0,$v0
      li      $t1,1       # init count

      mul      $a0,$v0,12       # allocate the RAM
      li      $v0,9
      syscall
      move      $s1,$v0       # copy $v0 to $s1
      sw      $s1,ptr
      nop

      lw      $s1,ptr       # load address of str
      lw      $s2,size
      
lo1:      bgt      $t1,$t0,done  # if t1 > size
      nop
      
      sw      $s1,0($s2)        # store str address to size
      nop

      move      $a0,$s1
      jal      input       # input the dates
      nop

      add      $t1,$t1,1       # count++
      add      $s1,$s1,4       # push array


      j      lo1
      nop

done:      li      $t1,1       # reinit count
      la      $s2,size

lo2:      bgt      $t1,$t0,exit       # if t1>size
      nop

      lw      $s1,0($s2)       # store str address to size
      nop

      move      $a0,$s1
      jal      output       # output the dates
      nop

      add      $t1,$t1,1       # count++
      add      $s1,$s1,4       # push array
      add      $s2,$s2,4
      
      j      lo2
      nop

exit:      li      $v0,10
      syscall

      .data
str1:      .asciiz "Input a number:"
size:      .word 0
ptr:      .word 0

      .text
      .globl input

input:      
      sub      $sp,$sp,4
      sw      $s0,($sp)

      move      $s0,$a0      #copy to $s0

      li      $v0,4      # input a day
      la      $a0,dd
      syscall
      li      $v0,5      
      syscall
      sw      $v0,0($s0) # in day
      nop

      li      $v0,4      # input a month
      la      $a0,mm
      syscall
      li      $v0,5      
      syscall
      sw      $v0,4($s0) # in month
      nop

      li      $v0,4      # input a year
      la      $a0,yy
      syscall
      li      $v0,5      
      syscall
      sw      $v0,8($s0)
      nop

      li      $v0,4
      la      $a0,ent
      syscall
      lw      $s0,($sp)
      nop
      add      $sp,$sp,4
      jr      $ra
      nop

      .data
dd:      .asciiz "\nInput a day:"
mm:      .asciiz "\nInput a month:"
yy:      .asciiz "\nInput a year:"
ent:      .asciiz "\n"
      .text
      .globl output
output:
      sub      $sp,$sp,4
      sw      $s0,($sp)
      move      $s0,$a0

      la      $a0,day # print day
      li      $v0,4
      syscall
      lw      $a0,0($s0)
      li      $v0,1
      syscall

      la      $a0,month # print month
      li      $v0,4
      syscall
      lw      $a0,4($s0)
      li      $v0,1
      syscall

      la      $a0,year # print year
      li      $v0,4
      syscall
      lw      $a0,8($s0)
      li      $v0,1
      syscall

      la      $a0,ent1
      li      $v0,4
      syscall
      
      lw      $s0,($sp)
      add      $sp,$sp,4
      jr      $ra
      nop

      .data
day:      .asciiz "The day is:"
month:      .asciiz "\nThe month is:"
year:      .asciiz "\nThe year is:"
ent1:      .asciiz "\n"

I got a worng answer in this program..but i don't know why is the problem. please help my code...
0
 
PeterdLoCommented:
Hi cyrus_bu,

Your program was almost there. You need to correct few lines before it will work.
1) In your lo1: loop, change "add  $s1,$s1,4"  to "add $1,$1,12".
2) In the lo2: loop, change "add $s1,$s1,4" to   "add  $1,$1,12"    and delete the line contains "add  $s2,$s2,4".

Hopfully, this will work for you. Have a good day.
0
 
PeterdLoCommented:
Hi cyrus_bu,

Sorry, I had missed 3 changes. They are as follows.

1) In the loop of lo1:, delete "sw  $s1,0($s2)" line.

2) After the label done:, replace the line "la  $s2,size" by "lw  $1,ptr".

3) In the loop of lo2:, delete the line "lw  $s1,0($s2)".

Basically, we are not using the variable "size" to store the buffer pointer in the variable "ptr".

Good Luck!
0
 
cyrus_buAuthor Commented:
Thank you very much!!! It comes out the correct answers. Thanks for your help.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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