Overview

16
Overview Review Trap Instruction Program in LC-3 machine language Use LC-3 Simulator

description

Overview. Review Trap Instruction Program in LC-3 machine language Use LC-3 Simulator. TRAP. Calls a service routine , identified by 8-bit “trap vector.” When routine is done, PC is set to the instruction following TRAP. LC-3 Editor / Simulator. - PowerPoint PPT Presentation

Transcript of Overview

Page 1: Overview

Overview

Review Trap Instruction

Program in LC-3 machine language

Use LC-3 Simulator

Page 2: Overview

TRAP

• Calls a service routine, identified by 8-bit “trap vector.”

• When routine is done, PC is set to the instruction following TRAP.

vector routine

x23 input a character from the keyboard

x21 output a character to the monitor

x25 halt the program

Page 3: Overview

LC-3 Editor / Simulator

Go to Authors Web page (http://www.mhhe.com/patt2 )

Download LC-3 (LC301.exe)

LC-3 Edit

LC-3 Simulate

Review “Guide to Using the Windows Version of the LC-3 Simulator and LC-3 Edit”

Page 4: Overview

Example

Address Instruction Comments

x30F6 1 1 1 0 0 0 1 1 1 1 1 1 1 1 0 1 R1 PC – 3 = x30F4

x30F7 0 0 0 1 0 1 0 0 0 1 1 0 1 1 1 0 R2 R1 + 14 = x3102

x30F8 0 0 1 1 0 1 0 1 1 1 1 1 1 0 1 1M[PC - 5] R2

M[x30F4] x3102

x30F9 0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 R2 0

x30FA 0 0 0 1 0 1 0 0 1 0 1 0 0 1 0 1 R2 R2 + 5 = 5

x30FB 0 1 1 1 0 1 0 0 0 1 0 0 1 1 1 0M[R1+14] R2M[x3102] 5

x30FC 1 0 1 0 0 1 1 1 1 1 1 1 0 1 1 1R3 M[M[x30F4]]

R3 M[x3102]

R3 5

Opcode

Page 5: Overview

Example 1: Multiply

• This program multiplies two unsigned integers in R4 and R5.

clear R2

add R4 to R2

decrement R5

R5 = 0?

HALT

No

Yes

Write program

Page 6: Overview

x3200 Clear Accumulator (R2) R2 <- 0

x3201 Add [R4] to Accumulator R2 <- R2 + R4

x3202 Dec R5 R5 <- R5 – 1

x3203 Do again if [R5] > 0 BR p x3201

x3204 Stop HALT

Program to multiply [R4] x [R5] and place result in R2

Page 7: Overview

Write program

Program to multiply [R4] x [R5] and place result in R2

Page 8: Overview

x3200 Clear Accumulator (R2) R2 <- 0 0101 010 010 1 00000 54A0

x3201 Add [R4] to Accumulator R2 <- R2 + R4 0001 010 010 0 00 100 1484

x3202 Dec R5 R5 <- R5 – 1 0001 101 101 1 11111 1B7F

x3203 Do again if [R5] > 0 BR p x3201 0000 011 111111101 07FD

x3204 Stop HALT 1111 0000 00100101 F025

Program to multiply [R4] x [R5] and place result in R2

Test on Simulator

Page 9: Overview

Example 2

• Compute sum of 12 integers.Numbers start at location x3100. Program starts at location x3000.

R1 x3100R3 0 (Sum)R2 12(count)

R2=0?

R4 M[R1]R3 R3+R4R1 R1+1R2 R2-1

NO

YES

Write program

Page 10: Overview

Example 2 – Sum integers form x3100 – x310B

Address Instruction Comments

x3000 1 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 R1 x3100

x3001 0 1 0 1 0 1 1 0 1 1 1 0 0 0 0 0 R3 0

x3002 0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 R2 0

x3003 0 0 0 1 0 1 0 0 1 1 1 0 1 1 0 0 R2 12

x3004 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 If Z, goto x3009

x3005 0 1 1 0 1 0 0 0 0 1 0 0 0 0 0 0 Load next value to R4

x3006 0 0 0 1 0 1 1 0 1 1 0 0 0 0 0 1 Add to R3

x3007 0 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 Increment R1 (pointer)

X3008 0 0 0 1 0 1 0 0 1 0 1 1 1 1 1 1 Decrement R2 (counter)

x3009 0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 0 Goto x3004

Page 11: Overview

Write a program to place the absolute value of the [R2] in R2

Sample program 4

Page 12: Overview

Example 3 - # Occurrences of Inputted Char

Count = 0(R2 = 0)

Ptr = 1st file character(R3 = M[x3012])

Input charfrom keybd

(TRAP x23)

Done?(R1 ?= EOT)

Load char from file(R1 = M[R3])

Match?(R1 ?= R0)

Incr Count(R2 = R2 + 1)

Load next char from file(R3 = R3 + 1, R1 = M[R3])

Convert count toASCII character

(R0 = x30, R0 = R2 + R0)

Print count(TRAP x21)

HALT(TRAP x25)

NO

NO

YES

YES

Page 13: Overview

Example 3 - # Occurrences of Inputted CharProgram (1 of 2)

Address Instruction Comments

x3000 0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 R2 0 (counter)

x3001 0 0 1 0 0 1 1 0 0 0 0 1 0 0 1 0 R3 M[x3102] (ptr)

x3002 1 1 1 1 0 0 0 0 0 0 1 0 0 0 1 1 Input to R0 (TRAP x23)

x3003 0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 R1 M[R3]

x3004 0 0 0 1 1 0 0 0 0 1 1 1 1 1 0 0 R4 R1 – 4 (EOT)

x3005 0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 0 If Z, goto x300E

x3006 1 0 0 1 0 0 1 0 0 1 1 1 1 1 1 1 R1 NOT R1

x3007 0 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 R1 R1 + 1

X3008 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 R1 R1 + R0

x3009 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1 If N or P, goto x300B

Page 14: Overview

Example 3 - # Occurrences of Inputted CharProgram (2 of 2)

Address Instruction Comments

x300A 0 0 0 1 0 1 0 0 1 0 1 0 0 0 0 1 R2 R2 + 1

x300B 0 0 0 1 0 1 1 0 1 1 1 0 0 0 0 1 R3 R3 + 1

x300C 0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 R1 M[R3]

x300D 0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 0 Goto x3004

x300E 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 R0 M[x3013]

x300F 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 R0 R0 + R2

x3010 1 1 1 1 0 0 0 0 0 0 1 0 0 0 0 1 Print R0 (TRAP x21)

x3011 1 1 1 1 0 0 0 0 0 0 1 0 0 1 0 1 HALT (TRAP x25)

X3012 Starting Address of File

x3013 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 ASCII x30 (‘0’)

Page 15: Overview

Program to display absolute value of number in R2 (-99 >= [R2] >= 99)

Write program

Page 16: Overview

Program to display absolute value of number in R2 (-99 >= [R2] >= 99)

• Find absolute value• ADD R2, R2, #0 ; set cc

14A0• BRzp +2 0602• NOT R2, R2 94BF• ADD R2, R2, #1 14A1

• R5 will contain x30 (#48), the offset for ASCII numbers• AND R5, R5, #0 5B60• ADD R5, R5, #15 1B6F• ADD R5, R5, #15 1B6F• ADD R5, R5, #15 1B6F• ADD R5, R5, #3 1B63

• Check for [R2] >9• AND R4, R4, #0 ; init high digit holder to zero 5920• ADD R2, R2, #-10 14B6• BRn +2 0802• ADD R4, R4, #1 1921• BR -4 0FFC• ADD R3, R2, #10 ; save low digit 16AA

• If high digit = 0, don't display it• ADD R4, R4, #0 ; set CC 1920• BRz +2 0402• Display high digit• ADD R0, R4, R5 1105• TRAP x21 F021• Display low digit• Add R0, R3, R5 10C5• TRAP x21 F021• Halt• TRAP x25 F025