Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

15-513 Introduction to Computer Systems

Written Assignment 3
June 22, 2021

1 Question 1
Question: What are caller- and callee-saved registers? Briefly compare the
similarities and differences between the two.

1.1 Similarities
Caller- and callee-saved registers are registers, standing at the highest level
(L0) of the memory hierarchy. CPU can access registers in a single clock
cycle.

1.2 Differences
The distinction between caller- and callee-saved registers can be observe lit-
erally from their names. Caller represents the procedure that calls another
procedure. Callee represents the procedure that is being called by another
procedure.
A caller-saved register can be modified by any function, meaning that the
caller function must first save the data of the caller-saved register before it
makes the call, i.e., caller has the responsibility to save the data of caller-
saved registers. On the other hand, callee-saved registers must preserve the
values before and after the callee function, i.e., callee has the responsibility
to preserve the data of caller-saved registers. This can be done by either not
modifying the register at all or by pushing the original value on the stack,
altering it, and then popping the old value form the stack before returning
[1].

1
2 Question 2
Question: Think about following structure
1 struct a {
2 char c [ 3 ] ;
3 size t y;
4 int z ;
5 };
Using proper alignment, how much space does this struct take up in mem-
ory? Describe a way to save space with this struct and state how much
space is saved. How would accessing values differ with or without padding
in data structures like structs and arrays? Why is the padding used in these
cases important?

2.1 Space usage using proper alignment


24 bytes. In general, char occupies 1 bytes, size t occupies 8 bytes, and int
occupies 4 bytes in the memory. Also, alignment restrictions (proper align-
ment) on allowable addresses for the primitive data types are implemented
in many computer systems. With or without proper alignment has an ef-
fect on the data allocation of struct. The following figure demonstrates the
difference of data allocation of struct with and without proper alignment.
Without proper alignment:

c[3] y z (1)
|{z} |{z} |{z}
3 bytes 8 bytes 4 bytes

With proper alignment:

c[3] internal padding y z external padding (2)


|{z} | {z } |{z} |{z} | {z }
3 bytes 5 bytes 8 bytes 4 bytes 4 bytes

2.2 Saving space


We can put the large data types first to save space. For example:
1 struct a {
2 size t y;
3 int z ;
4 char c [ 3 ] ;
5 };

2
In this case, the struct only occupies 16 bytes. Through changing the order
of data fields in struct, 8 bytes are saved.

y z c[3] external padding (3)


|{z} |{z} |{z} | {z }
8 bytes 4 bytes 3 bytes 1 bytes

2.3 Accessing values with or without padding in data


structures
Miss Alignment (without padding) might save memory space compare to
proper alignment (with padding). However, alignment ensures that the ad-
dress of the primitive data types must be multiple of its require bytes. For
example, the memory location of double isn’t going to span across bytes,
meaning that we can access double data with merely 1 operations rather
than multiple operations.

2.4 Why is padding important?


The advantages of applying alignment restrictions (padding) on primitive
data types have two aspects. First, the hardware design which forms the
interface between the processor and the memory system can be simplified.
Second, the performance of memory accessing can be improved [1].
For example, if we guarantee that any double will be aligned to have
its address be a multiple of 8 bytes, then we can design a processor which
always fetches 8 bytes (a simplified hardware design) from memory with
an address that must be a multiple of 8. In this case, we can access the
double value with a single memory operation rather than multiple memory
operations [1]. (performance has been improved)

References
[1] Randal E Bryant, O’Hallaron David Richard, and O’Hallaron David
Richard. Computer systems: a programmer’s perspective. Vol. 2. Prentice
Hall Upper Saddle River, 2003.

You might also like