created 07/08/2003

Chapter 33 Programming Exercises


NOTE: If you have read the chapters on subroutine linkage, write each exercise as a main program calling a subroutine. Use either the stack-based linkage or the frame-based linkage convention.

In the Settings menu of SPIM set Bare Machine OFF, Allow Pseudo Instructions ON, Load Trap File ON, Delayed Branches OFF, Delayed Loads OFF, Mapped IO ON, Quiet OFF.

Run the programs by clicking SimulatorGo and then OK in the pop-up panel.


Exercise 1 — Date

Consider the following structure:

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

Write a program that allocates memory for such a structure and fills it in with values such as 9/7/2003.

Write out the values in the structure.

Click here to go back to the main menu.


Exercise 2 — Array of Structures

Consider the following structure, the same as for Exercise 1:

struct
{
  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 structure pointer to the first address of the allocated space. Then add the size of a structure to it for each execution of the loop.

Finally, write out the contents of all the structures.

Click here to go back to the main menu.


Exercise 3 — Array of Structure Pointers

Consider the following structure, the same as for the above exercises:

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

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          

Not all of the potential addresses will be used. 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.)

Click here to go back to the main menu.


Exercise 4 — Image Allocation

Write a program that asks the user for the size of an image: R rows and C columns. Then allocate space for R*C bytes.

Fill row 1 with 1s, fill row 2 with 2s, and so on. Examine the data section of the simulator to see if you did this correctly.

Click here to go back to the main menu.


End of Exercises