assignment 3 - solution - comp228

Page 1

Brief Solution to Assignment 3 1(a)

(b)

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 Only the entries where changes occur are shown in the following table. Step initial Fetch

Get Operand Execute Fetch

Execute

RTN MAR <= PC IR <= M[MAR] PC <= PC + 1 decode IR[15-12] MAR <= IR[11-0] MBR <= M[MAR] AC <= AC + MBR MAR <= PC IR <= M[MAR] decode IR[15-12] MBR <= AC M[MAR] <= MBR

PC 100

IR

AC 1234

MAR

MBR

M[200] 0234

100 3200 101

200 0234 1468 101 2200 1468 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. 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). 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 LOAD Y 008 100C | 009 6000 | OUTPUT 00A 7000 | HALT 00B 0000 | X Dec 10

(ii)

(iii)


00C 0000 |

(iv)

Dec 0

| | SYMBOL TABLE -------------------------------------------------Symbol | Defined | References --------+--------+-----------------------------R | 003 | 001, 007 S1 | 000 | 002 X | 00B | 005 Y | 00C | 004, 008 ------------------------------------------------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) S,

T,

One, Ten, (b) S,

T,

X, One, Ten, (c)

Y

Sub,

Load One Store X Load X Subt Ten Skipcond 400 Jump T Halt Load X Add One Store X Jump S Dec 1 Dec 10 Clear Store Sum Load X Subt Ten Skipcond 800 Jump T Halt Load Sum Add X Store Sum Load X Add One Store X Jump S Dec 1 Dec 1 Dec 10 Dec Load Subt Store JumpI

0 X Y Z Sub


3(a)

(b)

4

MAR <= PC PC <= PC + 1 IR <= M[MAR] MAR <= 0 MBR <= M[MAR] MAR <= MBR MBR <= M[MAR] MAR <= MAR - 1 AC <= MBR MBR <= M[MAR] AC <= AC + MBR MBR <= MAR – 1 MAR <= 0 M[MAR] <= MBR Clear AddI Store Load Subt Store Load AddI Store Load Subt Store Load

0 Temp 0 One 0 Temp 0 Temp 0 One 0 Temp

//MAR points to top of stack //MBR contains top of stack data //AC buffers top of stack data //MBR contains next stack data //sums the two //MBR now contains the stack pointer //updates stack pointer at location 0

//Temp is a memory location reserved for sum

//Updates stack pointer at location 0 //Now AC contains the sum

Sample code (untested): S1,

Input Subt Thirty Store Temp Skipcond 000 Jump S2 Halt S2, Subt Ten Skipcond 000 Halt Load Temp Add Sum Store Sum Jump S1 Thirty, Dec 48 Ten, Dec 10 Sum, Dec 0

//input an ASCII digit //check if < hex 30

//check if < hex 3A


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.