🏪

Practice Final

TypeAssessment

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];

ExpressionResulting Type
aPointer to char
*achar
a[0]char
c.c3Pointer to char
*f(3,4)Pointer to int
f(3,4)Pointer to pointer to int
(*d).c3[1]char
d->c2float
gPointer to array of pointer to float
*gArray 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.

ExpressionValue
a+1a + 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
bb = &b[0] = address of ‘s’
b[2]b[2] = *(b+2) = *(b+2*sizeof(1)) = *(b+2) = ‘r’
cxcx = &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 + 2cx + 2 = &cx[0] + 2 = &(*(cx+0)) + 2 = cx+0*sizeof(16) + 2*sizeof(16) = cx + #32 = 0x2001c + #32 = 0x2003C
cp + 1cp + 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;

Propertyabcdefpapbpcpdppcppdpfppf
Untitled1293.5-2.7badlyw&a&b&c&d&pc&pd&f&pf
*pa = 55000
*pb = *pa +20700
pa = pb0000&b
*pa = 10100
pd = pc0000&c
**ppd = 4.00040
**ppc = 1.00010
**ppf = ‘X”0000X
pf = e0000&e[0]
(*ppf)[3] = 00000bad\0y\0 = bad\0 = bad

Question 7

Assembly InstructionR1R2R30x30f40x3102
LEA R1, #-3x30F7-x0003 = x30F400
ADD R2, R1, #14x30F4 + x000E = x301200
ST R2, #-50x30120
AND R2, R2, #0000
ADD R2, R2, #5500
STR R2, R1, #1405
LDI R3, #-950

Question 8

  1. z = a + b

LDR R1, R5, #4

LDR R2, R5. #5

ADD R3, R1, R2

STR R3, R5, #-2

  1. 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

  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

  1. The memory location labeled RV stores the return value of the function. R6 should point to the RV after execution returns to the caller.
  1. 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.
  1. 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.

  1. Convert the number in R3 to its twos-complement.
NOT R3, R3

ADD R3, R3, #1
  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
  1. Place the value 1 in R0 if the number in R1 is even; 0 otherwise
AND R0, R0, #0
  1. Multiply the number in R3 by 4.
ADD R3, R3, R3

ADD R3, R3, R3
  1. Compute the NAND of R1 and R2 with result in R0
AND R0, R1, R2

NOT R0, R0
  1. Compute the absolute value of R1 in R0
ADD R0, R1, #0

BRzp END

NOT R0, R0

ADD R0, R0, #1

END
  1. Subtract R1 from R0
NOT R1, R1

ADD R1, R1, #1

ADD R0, R0, R1

Question 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.)

  1. 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?

  1. 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

TitleABCDS
Untitled00001
Untitled00010
Untitled00101
Untitled00110
Untitled01000
Untitled01011
Untitled01101
Untitled01111
Untitled10000
Untitled10011
Untitled10101
Untitled10110
Untitled11000
Untitled11010
Untitled11100
Untitled11111

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; 
}

PropertyStatically addressedStackHeapVisible to allVisible within C fileVisible within function
TitleXX
errorstatusXX
statsXX
aXX
bXX
resultXX
countXX

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

  1. False; BR uses PCoffset9 while JMP uses the base register with no offset
  1. False; TRAP codes such as TRAP x27 are loaded into the trap vector table
  1. True; the 2’s complement range for 8 bits is -128 to 127 and -81 is within this range
  1. False; the range for an 8 bit 2’s complement integer is -128 to 127
  1. True
  1. True; NOR gates can be used to construct AND, OR, and NOT gates. With those three gates you can construct any boolean function
  1. True; 14 = 01110 and 19 = 10101. 01110 & 10011 = 00010 = 2
  1. True; hex 3F07 and octal 37407 are both equivalent to 16135 in decimal
  1. 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.
  1. 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