Load the following program in PennSim and answer the following questions.

.ORIG x3000
LEA R1, STRZ
AND R2, R2, #0
LD R4, CHAR
LOOP LDR R3, R1, #0
BRz DONE Branch if null
ADD R3, R3, R4
BRnp SKIP Branch if not ' '
ADD R2, R2, #1
SKIP ADD R1, R1, #1
BR LOOP Always branch
DONE ST R2, COUNT
HALT
CHAR .FILL xFFE0
COUNT .FILL x0000
STRZ .STRINGZ "Hello World as always!"
.END

- (1 point) How many times is the instruction at label "LOOP" executed?
It will branch to LOOP until it loads a null character in R3. The length of the string is 22 characters + 1 null character, so 23 times.

- (1 point) How many times is the instruction at label "SKIP" executed?
Skip is executed for each character except null, so 22

- (2 points) Describe what this program does in 1-2 sentences.
It counts the number of space character in the string at STRZ and stores that value in COUNT

### Problem 2 (4 Points)

Show the symbol table created by the assembler for the following program.

.ORIG x3000
LD R0, LENGTH
AND R1, R1, #0
LEA R2, ARRAY
L00P LDR R3, R2, #0
ADD R1, R1, R0
ADD R2, R2, #1
ADD R0, R0, #-1
BRp L00P
ADD R3, R3, R0
BRz DONE
ST R2, SUM
DONE HALT
LENGTH .FILL x05
ARRAY .BLKW 5
PRSTR .STRINGZ "Done!"
SUM .FILL x0000
.END

Symbol | Address |

LOOP | 0x3003 |

DONE | 0x300B |

LENGTH | 0x300C |

ARRAY | 0x300D |

PRSTR | 0x3012 |

SUM | 0x3018 |

### Problem 3 (2 points)

The following program is supposed to store the result 4 * R0 at memory address 0x9000. There are two errors in the code. Identify which instructions have an error, and the corresponding type of error.

.ORIG 0x3000
FOURX AND R1, R1, #0
AND R3, R3, #0
ADD R3, R3, #4
LOOP ADD R1, R1, R0
ADD R3, R3, #-1
BRzp LOOP ; Logical error: Loop 5 times instead of 4
ST R1, x9000 ; Syntax error: x9000 is out of range
HALT
.END

### Problem 4 (4 Points)

Recall the state machine from homework 4:

The subroutine "FSM" implements the above state machine. The input is stored at the label "INPUT" and the resulting output is stored at the label "OUTPUT". The input bit sequence is read from the most significant bit (MSB) to the least significant bit (LSB). For example, if the content of INPUT was "1011 0011 0110 1000", then the first input bit is 1 and the last input bit is 0. The first output bit is stored as the MSB of OUTPUT, and the last output bit is stored as the LSB. The labels S0, S1, S2, S3 corresponds to states 00, 01, 10, and 11, respectively. Fill in the missing instructions to complete the subroutine.

.ORIG x3000
LEA R0, PROMPT
PUTS
JSR FSM
HALT
FSM ST R0, TMP_R0
ST R1, TMP_R1
ST R2, TMP_R2
ST R3, TMP_R3
LD R0, INPUT
AND R1, R1, 0 ; R1 = output
LEA R2, S0 ; R2 = address of current state
ADD R3, R1, 15
ADD R3, R3, 1 ; R3 = 16
; Loop over all bits
LOOP JMP R2 ; Determines which state to jump into
S0 ; Output = 0
ADD R0, R0, 0 ; For checking next state
BRzp ST_END ; if MSB(R0) == 0, stay in S0, else S1
LEA R2, S1
BRnzp ST_END
S1 ; Output = 1
ADD R1, R1, 1
ADD R0, R0, 0 ; For checking next state
BRzp S1_S3 ; if MSB(R0) == 0, go to S3, else S0
LEA R2, S0
BRnzp ST_END
S1_S3 LEA R2, S3
BRnzp ST_END
S2 ; Output = 0
ADD R0, R0, 0 ; For checking next state
BRzp S2_S0 ; if MSB(R0) == 0, go to S0, else S1
LEA R2, S1
BRnzp ST_END
S2_S0 LEA R2, S0
BRnzp ST_END
S3 ; Output = 1
ADD R1, R1, 1
ADD R0, R0, 0 ; For checking next state
BRzp S3_S2 ; if MSB(R0) == 0, go to S2, else S0
LEA R2, S0
BRnzp ST_END
S3_S2 LEA R2, S2
ST_END
ADD R1, R1, R1 ; Left shift R1, the temporary result
ADD R0, R0, R0 ; Left shift R0
ADD R3, R3, -1
BRp LOOP
ST R1, OUTPUT
LD R3, TMP_R3
LD R2, TMP_R2
LD R1, TMP_R1
LD R0, TMP_R0
RET
INPUT .FILL 0
OUTPUT .FILL 0
TMP_R0 .FILL 0
TMP_R1 .FILL 0
TMP_R2 .FILL 0
TMP_R3 .FILL 0
PROMPT .STRINGZ "Modify the value at label INPUT to test the FSM.\n"
.END

### Problem 5 (4 points)

**Solution:** hw7_p5_sol.txt

### Grading:

Correct result for all test cases: 4 points

Otherwise:

- 1 point for loop that terminates
- 1 point for correct call to RSH
- 1 point for correct implementation of if statement

### Problem 6 (12 points)

**Solution:** hw7_p6_sol.txt

### Grading:

CHECKINPUTVALID

- Correct output for all test cases: 2 points
- Otherwise 1 point per test case
- Case 1: correct output on valid input
- Case 2: correct output on invalid input

CHECKCOLUMNVALID

- Correct output for all test cases: 2 points
- Otherwise 1 point per test case
- Case 1: correct output on valid input
- Case 2: correct output on invalid input

CHECKEND

- Correct output for all test cases: 8 points
- Otherwise 2 points per test case
- Case 1: correct output on continuing game (game hasn't ended)
- Case 2: correct output on non-full board win
- Case 3: correct output on full board win
- Case 4: correct output on tie