Practice Final
| Type | Assessment |
|---|
Question 1
What are the resulting types of the following expressions? Write them in English
using among other things, the phrases “pointer to”, “array of”, and “function returning”.
Note, these are expressions, not type declarations. The first two are done for you.
char *a = “Last”;
struct c {
int c1;
float c2;
char *c3;
} c;
struct c *d;
int **f(int a, int b);
float *(*g)[];
int h[10][5][3];| Expression | Resulting Type |
|---|---|
| a | Pointer to char |
| *a | char |
| a[0] | char |
| c.c3 | Pointer to char |
| *f(3,4) | Pointer to int |
| f(3,4) | Pointer to pointer to int |
| (*d).c3[1] | char |
| d->c2 | float |
| g | Pointer to array of pointer to float |
| *g | Array of pointer to float |
| h[3] | Array 5 of array 3 of int |
| h[3][4][2] | int |
Question 2
Presume the following variables are all outside a function definition and are assigned
to the memory addresses listed.

| Expression | Value |
|---|---|
| a+1 | a + 1 = a + 1*sizeof(24/6) = 0x20000 + #4 = 0x20004 |
| &a[0] | &a[0] = &(*(a+0)) = a + 0*sizeof(4) = 0x20000 |
| a[0] | a[0] = *(a+0) = *(a+0*sizeof(4)) = *a = *(0x20000) = 1 |
| b | b = &b[0] = address of ‘s’ |
| b[2] | b[2] = *(b+2) = *(b+2*sizeof(1)) = *(b+2) = ‘r’ |
| cx | cx = &cx[0] = &(*(cx+0)) = cx+0*sizeof(16) = 0x2001c |
| &cx[2] | &cx[2] = &(*(cx+2)) = cx + 2*sizeof(16) = 0x2001c + #32 = 0x2003c |
| &cx[3].c2 | &cx[3].c2 = &(*(cx + 3*sizeof(struct c) + sizeof(c1)) = cx + 3*sizeof(16) + #8 = 0x2001c + #48 + #8 = 0x20054 |
| cx + 2 | cx + 2 = &cx[0] + 2 = &(*(cx+0)) + 2 = cx+0*sizeof(16) + 2*sizeof(16) = cx + #32 = 0x2001c + #32 = 0x2003C |
| cp + 1 | cp + 1 = &cx[0] + 1 = &(*(cx + 0*sizeof(struct c) + 1*sizeof(struct c))) = cx + 1*sizeof(16) = cx + #16 = 0x2001c + #16 = 0x2002c |
| &cp[1] | &cp[1] = &(*(cx + 1) = cx + 1*sizeof(struct c) = cx + #16 = 0x2001c + #16 = 0x2002c |
Question 3
Trace the values of the variables in the following code by writing in the values that
change when each line of code is executed. The first line of the table shows the value of
each variable when the declarations are finished.
int a = 12, b = 9;
double c = 3.5, d = -2.7;
char e[] = “badly”;
char f = ‘w’;
int *pa = &a, *pb = &b;
double *pc = &c, *pd = &d, **ppc = &pc, **ppd = &pd;
char *pf = &f, **ppf =&pf;| Property | a | b | c | d | e | f | pa | pb | pc | pd | ppc | ppd | pf | ppf |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Untitled | 12 | 9 | 3.5 | -2.7 | badly | w | &a | &b | &c | &d | &pc | &pd | &f | &pf |
| *pa = 5 | 5 | 0 | 0 | 0 | ||||||||||
| *pb = *pa +2 | 0 | 7 | 0 | 0 | ||||||||||
| pa = pb | 0 | 0 | 0 | 0 | &b | |||||||||
| *pa = 1 | 0 | 1 | 0 | 0 | ||||||||||
| pd = pc | 0 | 0 | 0 | 0 | &c | |||||||||
| **ppd = 4.0 | 0 | 0 | 4 | 0 | ||||||||||
| **ppc = 1.0 | 0 | 0 | 1 | 0 | ||||||||||
| **ppf = ‘X” | 0 | 0 | 0 | 0 | X | |||||||||
| pf = e | 0 | 0 | 0 | 0 | &e[0] | |||||||||
| (*ppf)[3] = 0 | 0 | 0 | 0 | 0 | bad\0y\0 = bad\0 = bad |
Question 7

| Assembly Instruction | R1 | R2 | R3 | 0x30f4 | 0x3102 |
|---|---|---|---|---|---|
| LEA R1, #-3 | x30F7-x0003 = x30F4 | 0 | 0 | ||
| ADD R2, R1, #14 | x30F4 + x000E = x3012 | 0 | 0 | ||
| ST R2, #-5 | 0 | x3012 | 0 | ||
| AND R2, R2, #0 | 0 | 0 | 0 | ||
| ADD R2, R2, #5 | 5 | 0 | 0 | ||
| STR R2, R1, #14 | 0 | 5 | |||
| LDI R3, #-9 | 5 | 0 |
Question 8

- z = a + b
LDR R1, R5, #4
LDR R2, R5. #5
ADD R3, R1, R2
STR R3, R5, #-2
- y = c * 4 + z
LDR R1, R5, #6
LDR R2, R5, #-2
ADD R1, R1, R1
ADD R1, R1, R1
ADD R3, R2, R1
STR R3, R5, #-1
- x = DIV(a, y)
; Load a and y
LDR R1, R5, #4
LDR R2, R5, #-1
; Push a and y parameters for DIV
ADD R6, R6, #-1
STR R2, R6, #1
ADD R6, R6, #-1
STR R1, R6, #0
; Call DIV subroutine and store return value
JSR DIV
LDR R3, R6, #0
ADD R6, R6, #3
STR R3, R5, #0
- The memory location labeled RV stores the return value of the function. R6 should point to the RV after execution returns to the caller.
- R0, R4, and R5’s values must not be changed while the body of MATH executes. R5 is our frame pointer and should only be changed during stack buildup/teardown. R0 and R4 are general purpose registers not saved on the stack and hence should not be used.
- When MATH executes its final RET instruction, RV, a, b, and c will be left on the stack.
Question 9
Write the assembly code instructions to perform the following actions. You may
use R4 as a temporary register.
- Convert the number in R3 to its twos-complement.
NOT R3, R3
ADD R3, R3, #1- Compute the inclusive-OR of R1 and R2 with result in R0. Remember that a|b is the same as ~((~a)&(~b)).
NOT R1, R1
NOT R2, R2
AND R0, R1, R2
NOT R0, R0- Place the value 1 in R0 if the number in R1 is even; 0 otherwise
AND R0, R0, #0- Multiply the number in R3 by 4.
ADD R3, R3, R3
ADD R3, R3, R3- Compute the NAND of R1 and R2 with result in R0
AND R0, R1, R2
NOT R0, R0- Compute the absolute value of R1 in R0
ADD R0, R1, #0
BRzp END
NOT R0, R0
ADD R0, R0, #1
END- Subtract R1 from R0
NOT R1, R1
ADD R1, R1, #1
ADD R0, R0, R1Question 10
(a) For what purpose are multiple sign-extenders (SEXT) connected between IR
and ADDR2MUX in the datapath of the LC-3. (See the reference document for the
datapath diagram and instruction set table.)
- The multiple sign-extenders (SEXT) connected between IR and ADDR2MUX in the datapath of the LC-3 are to allow for an offset to be applied to the IR value. An offset helps when computing addresses for instructions before or after the current instruction address.
(b) Why is there a need for three of them to be connected to ADDR2MUX?
- Three sign-extenders are connected to ADDR2MUX to allow for a 6 bit offset (offset6), 9 bit offset (PCoffset9), or 11 bit offset (PCoffset11) to the value in ADDR1MUX.
Question 11
| Title | A | B | C | D | S |
|---|---|---|---|---|---|
| Untitled | 0 | 0 | 0 | 0 | 1 |
| Untitled | 0 | 0 | 0 | 1 | 0 |
| Untitled | 0 | 0 | 1 | 0 | 1 |
| Untitled | 0 | 0 | 1 | 1 | 0 |
| Untitled | 0 | 1 | 0 | 0 | 0 |
| Untitled | 0 | 1 | 0 | 1 | 1 |
| Untitled | 0 | 1 | 1 | 0 | 1 |
| Untitled | 0 | 1 | 1 | 1 | 1 |
| Untitled | 1 | 0 | 0 | 0 | 0 |
| Untitled | 1 | 0 | 0 | 1 | 1 |
| Untitled | 1 | 0 | 1 | 0 | 1 |
| Untitled | 1 | 0 | 1 | 1 | 0 |
| Untitled | 1 | 1 | 0 | 0 | 0 |
| Untitled | 1 | 1 | 0 | 1 | 0 |
| Untitled | 1 | 1 | 1 | 0 | 0 |
| Untitled | 1 | 1 | 1 | 1 | 1 |
Question 12
char Title[200];
extern int errorstatus;
static double stats[100][100];
double calculate(int a, int b) {
double result;
static int count = 0;
result = (double)(a + count) / b;
if (result > 0)
count++;
return result;
}| Property | Statically addressed | Stack | Heap | Visible to all | Visible within C file | Visible within function |
|---|---|---|---|---|---|---|
| Title | X | X | ||||
| errorstatus | X | X | ||||
| stats | X | X | ||||
| a | X | X | ||||
| b | X | X | ||||
| result | X | X | ||||
| count | X | X |
Question 13
_____ (a) The BR and JMP LC-3 instructions use the same addressing mode
_____ (b) The instruction TRAP x27 loads hexadecimal 27 into the PC
_____ (c) Representing the number -81 in twos-complement requires at least 8 bits
_____ (d) An 8-bit twos-complement integer can represent numbers in the range -
127 to +127
_____ (e) A twos-complement integer can be multiplied by two by shifting it one bit
to the left, filling the low-order bit with zero
_____ (f) Any Boolean function can be calculated by a proper combination of NOR
gates
_____ (g) In C, computing (14 & 19) gives the result of 2
_____ (h) The hexadecimal number 3F07 can be represented in octal as 37407
_____ (i) Sign-extending the 8-bit twos-complement integer 0xA7 to 32 bits yields
0xFFFFFFA6
_____ (j) The twos-complement of 0x0000 is 0xFFFF
- False; BR uses PCoffset9 while JMP uses the base register with no offset
- False; TRAP codes such as TRAP x27 are loaded into the trap vector table
- True; the 2’s complement range for 8 bits is -128 to 127 and -81 is within this range
- False; the range for an 8 bit 2’s complement integer is -128 to 127
- True
- True; NOR gates can be used to construct AND, OR, and NOT gates. With those three gates you can construct any boolean function
- True; 14 = 01110 and 19 = 10101. 01110 & 10011 = 00010 = 2
- True; hex 3F07 and octal 37407 are both equivalent to 16135 in decimal
- False; sign extending 0xA7 would fill the left 24 bits with 1s which translates to 6 leading Fs in hex. A sign extension would not impact the rightmost 8 bits and hence changing A7 to A6 is what makes this false.
- False; 0x0000 is equivalent to just 0 and two’s complement does not have a negative representation of 0, hence this question is conceptually invalid
Verified