SEN2201 · Week 9 · Review for the Midterm Exam

Midterm review: numbers, logic, memory, von Neumann, LC-3

A one-page summary of weeks 1-8, the exam recipe for each topic and mixed practice questions. Start here the evening before the exam.

2's complementoverflowsign extensionIEEE 754bit maskmintermmaxtermDeMorganNANDR-S latchaddress spaceaddressabilityinstruction cyclePC-relativecondition codes

This page is a compressed version of the first eight weeks. We collected the recipe of each topic in a table, then gave the solutions of mixed questions. This is a procedure course: most of the points come from questions solved with the same steps every time. First read the table, then solve the questions against the clock, and go back to the page of the topic you got wrong.

The scope of the midterm may change from semester to semester (in some semesters weeks 1-6, in some the LC-3 is included). Decide which block to emphasize according to the announcement on the course page.

Topic → question type → recipe

TopicTypical questionRecipe
Conversionsdec ↔ bin ↔ hexsubtract powers of 2; for hex group in fours from the right
Four readingsthe 4 values of an 8-bit patternif MSB = 0 they are all the same; 2’s = unsigned - 256
2’s complementwrite -Xinvert + 1
Addition/subtractionA - B, overflowA + (-B), discard the 9th bit, write the sign rule
Sign extension5 bits → 8/16 bitscopy the MSB to the left
IEEE 754encode/decode1.xxx × 2^e, E = e + 127, do not write the hidden 1
Bit maskset/clear/test/toggleOR / AND (mask with 0s) / AND / XOR
ASCIIletter and digit conversionsubtract x30; bit 5 = x20
Circuit readingdiagram → table → expressionname the intermediate outputs, fill in row by row
Simplificationminimal SOPpairs that differ in one variable, X + X’ = 1
ComplementF’DeMorgan, then multiply out and simplify
NAND-onlyonly NANDdouble-complement the SOP
Σm / ΠMcanonical forms1 rows / 0 rows
Decoder/mux/FAshort answerinput = the selected number
LatchQ tracingNAND: 11 holds, the letter that is 0 does the job
Memoryaddress bits, size2k2^k locations × m bits
FSMtrace, number of flip-flopstable row; ⌈log⁡2s⌉\lceil\log_2 s\rceil
von Neumann5 parts, registersPC address, IR instruction, MAR address, MDR data
Instruction cyclephases, FETCHMAR ← PC, PC ← PC+1, MDR ← M[MAR], IR ← MDR
I/OpollingLDI KBSR, BRzp, LDI KBDR
LC-3 encodingencode/decodeopcode table, fields, incremented PC
LC-3 tracingregister/CC/memoryline-by-line trace table

Five reminders for exam day

  1. Write the representation. An expression like “8-bit 2’s complement” should be at the start of your answer.
  2. Do the check. In every conversion, go back to decimal and verify. Five seconds, one point.
  3. For overflow, write the rule sentence: “Overflow occurs only if both operands have the same sign and the result has the opposite sign.”
  4. In simplification, write the name of the rule: distributive, complement, DeMorgan…
  5. In the LC-3, add the offset to the incremented PC. The most commonly lost point.

Mixed examples

Numbers: the four readings of 0x9C and sign extension

0x9C = 1001 1100
unsigned  128+16+8+4          = 156
SM        minus, 001 1100 = 28 = -28
1's       flip 0110 0011      = -99
2's       156 - 256           = -100
sign extension to 16 bits: 1111 1111 1001 1100 = 0xFF9C (still -100)

IEEE: encode 19.5

19 = 10011, 0.5 = .1 → 10011.1 = 1.00111 × 2^4
S = 0, E = 4 + 127 = 131 = 1000 0011, F = 00111 0...
0 10000011 00111000000000000000000 = 0x419C0000

Logic: Σm(2, 3, 6, 7)

m2 = A'BC'  m3 = A'BC  m6 = ABC'  m7 = ABC
A'B(C' + C) + AB(C' + C) = A'B + AB = B(A' + A) = B

Four minterms were reduced to a single letter: every row where B = 1.

Memory and FSM

2142^{14} locations × 32 bits = 214×42^{14} \times 4 bytes = 64 KB. An FSM with 12 states: 23=8<12≤162^3 = 8 < 12 \le 16 → 4 flip-flops.

von Neumann

FETCH when PC = x4A3F: MAR ← x4A3F, PC ← x4A40, MDR ← M[x4A3F], IR ← MDR. The increment of the PC in hex: F + 1 = 10, the carry goes left.

LC-3: encode, decode, trace

LDR R5, R1, #-2 → 0110 101 001 111110 = x6A7E.

xA9FE at x3000 → 1010 100 111111110 → LDI R4, offset -2 → pointer address x3001 - 2 = x2FFF; R4 ← M[M[x2FFF]].

        AND  R1, R1, #0     R1 = 0
        ADD  R2, R1, #3     R2 = 3
AGAIN   ADD  R1, R1, R2     |  round R1   R2
        ADD  R1, R1, R1     |   1     6    2
        ADD  R2, R2, #-1    |   2    16    1
        BRp  AGAIN          |   3    34    0  (Z, fall through)
        HALT

Overflow: three additions, three decisions

Addition (8 bits)ResultDecimalCarry-outOverflowWhy
01100100 + 0011110010100000100 + 60 → -960yes(+) + (+) = (−)
11000000 + 1011111101111111-64 + (-65) → 1271yes(−) + (−) = (+)
11110110 + 0000110000000010-10 + 12 → 21nothe signs differ

The third row is the trap the exam likes: there is a carry-out but the result is correct. In the second row the true sum is -129, which went past the lower limit of 8 bits, -128.

Circuit: a NOR and an AND

Gate 1 NOR(A, B) = X, gate 2 AND(B, C) = Y, gate 3 OR(X, Y) = F. When you fill in the table with the intermediate columns, you get F = Σm(0, 1, 3, 7) = A’B’ + BC (week 3, the circuit reading section). In the exam, give a name to every intermediate output; partial credit comes from there.

I/O: a three-line wait loop

POLL    LDI  R1, KBSRP   ; read KBSR
        BRzp POLL        ; bit 15 = 0 → wait
        LDI  R0, KBDRP   ; the character is in R0

When ready, KBSR = x8000, that is negative. That is why BRzp. KBSRP is a .FILL xFE00; LD cannot reach that far, LDI is needed.

Instruction cycle: the phases of LDR

LDR R4, R2, #-3, R2 = x4005: FETCH (MAR ← PC, PC ← PC + 1, IR ← instruction), DECODE (opcode 0110), EVALUATE ADDRESS (x4005 - 3 = x4002), FETCH OPERANDS (MDR ← M[x4002]), no EXECUTE, STORE RESULT (R4 ← MDR, the CC is set). Memory was accessed twice.

Quick checklist

Do you know?Answer
6-bit 2’s complement range-32..31
The 16-bit hex of -10xFFFF
The IEEE code of 1.00x3F800000
‘A’ and ‘a’x41, x61
When does an n-MOS conductgate = 1
(A+B)′(A+B)'A′B′A'B'
3-to-8 decoder, input 011D3
16-to-1 mux selects4
FA 1+1+1S = 1, Cout = 1
D latch WE = 0holds
LC-3 memory size128 KB
The phase ADD skipsEVALUATE ADDRESS
KBSR addressxFE00
RETJMP R7 = xC1C0

Common mistakes

Wrong ideaThe truth
Carry-out = overflowLook at the sign rule
In IEEE, the hidden 1 goes into the fractionIt is not written
m5 = A’BC’AB’C
In a NAND latch S = 1 setsS = 0 sets
The PC points to the current instructionTo the next one
LD address = instruction address + offset(instruction address + 1) + offset
BRzp and BRp are the sameBRzp also branches on zero
ST changes the CCIt does not

Test yourself

Exam-style questions. Solve them on paper first, then reveal the answer.

Question 1easy

Give the decimal value of 0xB7 as unsigned, sign-magnitude, 1's complement and 2's complement.

Show answer
183, -55, -72, -73

0xB7 = 1011 0111. Unsigned: 128+32+16+4+2+1 = 183.
SM: minus, 011 0111 = 55 → -55. 1's: flip 0100 1000 = 72 → -72. 2's: -128 + 55 = -73 (or 183 - 256).

Question 2medium

In 8-bit 2's complement, A = 01001110 and B = 10111000. Compute A - B and state whether overflow occurred.

Show answer
10010110, overflow

A = 78, B = -72. -B = 01001000 (+72). A + (-B) = 01001110 + 01001000 = 10010110 (MSB 1, read as -106).
The true result is 150 > 127. The two numbers added (A, -B) are positive and the result is negative → overflow.

Question 3easy

What is the minimum number of bits needed to represent -129 in 2's complement?

Show answer
9

8 bits go down to -128 at most. 9 bits: −28=−256≤−129-2^8 = -256 \le -129. The answer is 9.

Question 4medium

Encode -7.375 in IEEE 754 single precision (hex).

Show answer
0xC0EC0000

7 = 111, 0.375 = .011 → 111.011 = 1.11011 × 2^2. S = 1, exponent = 129 = 1000 0001, fraction = 11011 and zeros.
1 10000001 11011000... → 1100 0000 1110 1100 0000 ... = 0xC0EC0000.

Question 5medium

Decode the IEEE 754 single-precision value 0x3E200000.

Show answer
0.15625

0011 1110 0010 0000 ... → S = 0, exponent = 0111 1100 = 124 → e = -3, fraction = 01.
1.01 × 2^-3 = 0.00101 = 1/8 + 1/32 = 0.15625.

Question 6easy

The ASCII code of 'd' is 0x64. Which operation and mask give 'D'?

Show answer
AND 0xDF → 0x44

The difference between upper and lower case is bit 5 (0x20). Clear bit 5: AND 1101 1111 = 0xDF. 0x64 AND 0xDF = 0x44 = 'D'.

Question 7medium

F(A,B,C) = A'B'C + A'BC + ABC. Simplify F and write it as ΠM(…).

Show answer
F = A'C + BC = ΠM(0, 2, 4, 5, 6)

A'B'C + A'BC = A'C(B' + B) = A'C. A'BC + ABC = BC(A' + A) = BC (A'BC was used twice).
F = Σm(1, 3, 7), the rows where F = 0 are 0, 2, 4, 5, 6 → ΠM(0, 2, 4, 5, 6).

Question 8hard

Using DeMorgan, find F' for F = A'C + BC as a sum of products.

Show answer
AB' + C'

F' = (A'C)'·(BC)' = (A + C')(B' + C') = AB' + AC' + B'C' + C'C' = AB' + C'(A + B' + 1) = AB' + C'.
Check: the rows where F' = 1 are 0, 2, 4, 5, 6; its union with the set {1, 3, 7} of F is {0..7}.

Question 9easy

A NAND R-S latch starts with Q = 0. (S, R) = 01 → 11 → 10 → 11 → 01. Give Q after each step.

Show answer
1, 1, 0, 0, 1

Active-low: 01 set → 1, 11 hold → 1, 10 reset → 0, 11 hold → 0, 01 set → 1.

Question 10medium

A memory has 13 address bits and 16-bit addressability. What is its size in KB? How many flip-flops does a 7-state FSM need?

Show answer
16 KB; 3

213×162^{13} \times 16 bits = 8192 × 2 bytes = 16384 bytes = 16 KB. 7 states: 22=4<7≤82^2 = 4 < 7 \le 8 → 3 flip-flops.

Question 11medium

Decode the LC-3 instructions x6A7E and x0BF8 (the second one is at address x3010).

Show answer
LDR R5, R1, #-2 and BRnp x3009

x6A7E = 0110 101 001 111110: LDR, DR = R5, BaseR = R1, offset6 = -2.
x0BF8 = 0000 101 111111000: n = 1, z = 0, p = 1 → BRnp, offset = -8, target = x3011 - 8 = x3009.

Question 12hard

What are R1, R2 and the condition codes when this program halts?

        AND  R1, R1, #0
        ADD  R2, R1, #3
AGAIN   ADD  R1, R1, R2
        ADD  R1, R1, R1
        ADD  R2, R2, #-1
        BRp  AGAIN
        HALT
Show answer
R1 = 34, R2 = 0, Z

R1 = 0, R2 = 3. Round 1: R1 = (0+3)·2 = 6, R2 = 2. Round 2: R1 = (6+2)·2 = 16, R2 = 1. Round 3: R1 = (16+1)·2 = 34, R2 = 0 → Z, BRp falls through.
The last CC was set by ADD R2: Z.

Verification: Every answer was recomputed with node: numbers at the bit level, IEEE 754 with DataView, Boolean expressions with all input combinations, the latch at the gate level, the LC-3 instructions and the program on our own assembler + simulator (verify/SEN2201/09.mjs, lc3.mjs).

Review card

2's: MSB = -2^(k-1) | -X = invert + 1 | 8 bits -128..127 | sign ext: copy the MSB OVERFLOW: two numbers with the same sign + a result with the opposite sign. In A-B look at -B. Carry-out != overflow IEEE: S | E=e+127 (8) | F (23), the hidden 1 is not written. 1.0 = 0x3F800000 Mask: AND clear/test, OR set, XOR toggle | ASCII '0'=x30 'A'=x41 'a'=x61, difference x20 Σm = the 1 rows of F, ΠM = the 0 rows | minterm 1->plain, maxterm 1->complemented DeMorgan (XY)'=X'+Y', (X+Y)'=X'Y' | NAND-only: double-complement the SOP Decoder n->2^n a single 1 | MUX 2^n inputs n selects | FA S=A⊕B⊕C, Cout=majority NAND RS: 11 hold 01 set 10 reset 00 forbidden | D latch WE=1 -> Q=D Memory 2^k × m bits | FSM s states -> ceil(log2 s) FF von Neumann: Memory, Processing, Input, Output, Control | PC address, IR instruction, MAR address, MDR data FETCH: MAR<-PC, PC<-PC+1, MDR<-M[MAR], IR<-MDR | 6 phases: F D EA FO EX SR LC-3: address = (PC+1) + SEXT(off) | imm5 -16..15 | off6 -32..31 | off9 -256..255 CC: ADD AND NOT LD LDI LDR write it | BR: target-(address+1)