Computer Science 281
Hour Exam #2
Name
____________________________
Friday, March 26
95 pts. (will be adjusted to 100 pts in the gradebook)
1. (10 pts.) Write assembly language instructions
To set bit 3 of %g5
To clear bit 3 of %g5
To tst bit 3 of %g5
2. (5 pts) What is the difference between a srl instruction and a sra instruction?
4. (10 pts.) Write a small routine (call it double) which takes the address of its argument and doubles its value. For example, if
int x = 10;
double(&x);
printf(“The value of x is now %d\n”, x);
appears in an assembly language program, the value of x will be 20 upon returning to the main program. Note that the address of x is being passed to the
subroutine.
5. (10 pts.) Write a small assembly language routine which accepts the address of a ten-element integer array in %i0 and sets each array element as follows:
for (i = 0; i < 10; ++i)
a[i] = 2 * i + 1;
6. (10 pts.) The address of a 3 by 5 integer array A is in %g1. Write the assembly language code to place the value of A[2,3] in %g2.
7. (10 pts.) %g1 contains the address of a 3 by 3 integer array A. Write the assembly language code to zero out A[k, k] for k = 0, 1, 2 (but zero out only
A[k][k]. The corresponding C code is something like
for (int k = 0; k < 3; ++k)
A[k][k] = 0;
8. (20 pts.). %g1 contains the address of a struct with the following form:
struct SimpleRec {
int X;
int Y;
int SUM;
struct SimpleRec *next;
};
a. For the struct pointed to by %g1, add the X and Y fields and place the sum in the SUM field. Notice that all four fields are 4 bytes each.
b. The struct pointed to by %g1 in turn points to a second struct (via next). Place the value of the field SUM found in that second struct into %g2.
9. (20 pts.) The first thing that we do when we enter a subroutine is to execute a SAVE command , for example
save %sp, -96, %sp
What happens when we do this? Include a discussion of the registers as seen by the calling program and the called program, and the change to the stack. Remembering that %o6 is the %sp, and %o7 holds an instruction address, how are these registers used?