assignment 3 - solution - comp228

3
Brief Solution to Assignment 3 1(a) Instruction Affected Registers Load X PC, AC Subt X PC, AC Input PC, InREG Halt PC Skipcond PC Jump X PC Jns X PC AddI X PC, AC (b) Only the entries where changes occur are shown in the following table. Step RTN PC IR AC MAR MBR M[200] initial 100 1234 0234 Fetch MAR <= PC 100 IR <= M[MAR] 3200 PC <= PC + 1 101 decode IR[15-12] Get Operand MAR <= IR[11-0] 200 MBR <= M[MAR] 0234 Execute AC <= AC + MBR 1468 Fetch MAR <= PC 101 IR <= M[MAR] 2200 decode IR[15-12] Execute MBR <= AC 1468 M[MAR] <= MBR 1468 (c) 12-bit address 2 12 locations = 4K words maximum. Instruction and data word are 2 bytes each (hence each memory word is 2 bytes). Maximum program size also equals maximum memory size. Largest 2’s complement integer is 2 11 – 1. Maximum size of opcode field is 16 – 12 = 4 bits. Hence maximum number of opcodes is 16. (d)(i) <Y, 00C, 004, 008> says: symbol Y is defined on line 12 (i.e. C), and referenced in lines 4 and 8. (ii) R, X and Y are symbolic locations that contain program data (initialized to 0 via the Dec 0 assembler directive). R corresponds to memory location 3, X corresponds to memory location 11 (i.e. B) and Y as in (i). (iii) 000 5000 | S1 INPUT 001 0003 | JNS R 002 9000 | JUMP S1 003 0000 | R DEC 0 004 200C | STORE Y 005 400B | SUBT X 006 8400 | SKIPCOND 400 007 C003 | JUMPI R 008 100C | LOAD Y 009 6000 | OUTPUT 00A 7000 | HALT 00B 0000 | X Dec 10

description

Step RTN PC IR AC MAR MBR M[200] initial 100 1234 0234 Fetch MAR

Transcript of assignment 3 - solution - comp228

Page 1: assignment 3 - solution - comp228

Brief Solution to Assignment 3 1(a) Instruction Affected Registers Load X PC, AC Subt X PC, AC Input PC, InREG Halt PC Skipcond PC Jump X PC Jns X PC AddI X PC, AC (b) Only the entries where changes occur are shown in the following table.

Step RTN PC IR AC MAR MBR M[200]initial 100 1234 0234 Fetch MAR <= PC 100

IR <= M[MAR] 3200 PC <= PC + 1 101 decode IR[15-12]

Get Operand MAR <= IR[11-0] 200

MBR <= M[MAR] 0234 Execute AC <= AC + MBR 1468 Fetch MAR <= PC 101

IR <= M[MAR] 2200 decode IR[15-12]

Execute MBR <= AC 1468 M[MAR] <= MBR 1468

(c) 12-bit address 212 locations = 4K words maximum. Instruction and data word are 2 bytes each (hence each memory word is 2 bytes). Maximum program size also equals maximum memory size. Largest 2’s complement integer is 211 – 1.

Maximum size of opcode field is 16 – 12 = 4 bits. Hence maximum number of opcodes is 16.

(d)(i) <Y, 00C, 004, 008> says: symbol Y is defined on line 12 (i.e. C), and referenced in lines

4 and 8. (ii) R, X and Y are symbolic locations that contain program data (initialized to 0 via the Dec 0

assembler directive). R corresponds to memory location 3, X corresponds to memory location 11 (i.e. B) and Y as in (i).

(iii) 000 5000 | S1 INPUT 001 0003 | JNS R 002 9000 | JUMP S1 003 0000 | R DEC 0 004 200C | STORE Y 005 400B | SUBT X 006 8400 | SKIPCOND 400 007 C003 | JUMPI R 008 100C | LOAD Y 009 6000 | OUTPUT 00A 7000 | HALT 00B 0000 | X Dec 10

Page 2: assignment 3 - solution - comp228

00C 0000 | Y Dec 0

| | SYMBOL TABLE -------------------------------------------------- Symbol | Defined | References --------+--------+------------------------------ R | 003 | 001, 007 S1 | 000 | 002 X | 00B | 005

Y | 00C | 004, 008 ------------------------------------------------- (iv) The program terminates when the current input character is ‘LF’

corresponding to the ASCII code Dec 10, whereupon it outputs the same character before terminating.

The program loop (hence S1) will be executed at many times as there are input characters, the last one being a linefeed.

2(a) Load One Store X S, Load X Subt Ten Skipcond 400 Jump T Halt T, Load X Add One Store X Jump S One, Dec 1 Ten, Dec 10 (b) Clear Store Sum S, Load X Subt Ten Skipcond 800 Jump T Halt T, Load Sum Add X Store Sum Load X Add One Store X Jump S X, Dec 1 One, Dec 1 Ten, Dec 10 (c) Sub, Dec 0 Load X Subt Y Store Z JumpI Sub

Page 3: assignment 3 - solution - comp228

3(a) MAR <= PC PC <= PC + 1 IR <= M[MAR] MAR <= 0 MBR <= M[MAR] MAR <= MBR //MAR points to top of stack MBR <= M[MAR] //MBR contains top of stack data MAR <= MAR - 1 AC <= MBR //AC buffers top of stack data MBR <= M[MAR] //MBR contains next stack data AC <= AC + MBR //sums the two MBR <= MAR – 1 //MBR now contains the stack pointer MAR <= 0 M[MAR] <= MBR //updates stack pointer at location 0 (b) Clear AddI 0 Store Temp //Temp is a memory location reserved for sum Load 0 Subt One Store 0 Load Temp AddI 0 Store Temp Load 0 Subt One //Updates stack pointer at location 0 Store 0 Load Temp //Now AC contains the sum 4 Sample code (untested): S1, Input //input an ASCII digit Subt Thirty //check if < hex 30 Store Temp Skipcond 000 Jump S2 Halt S2, Subt Ten //check if < hex 3A Skipcond 000 Halt Load Temp Add Sum Store Sum Jump S1 Thirty, Dec 48 Ten, Dec 10

Sum, Dec 0