Overview Review Trap Instruction Program in LC3 machine

  • Slides: 16
Download presentation
Overview Review Trap Instruction Program in LC-3 machine language Use LC-3 Simulator

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. ” vector

TRAP • Calls a service routine, identified by 8 -bit “trap vector. ” vector routine x 23 input a character from the keyboard x 21 output a character to the monitor x 25 halt the program • When routine is done, PC is set to the instruction following TRAP.

LC-3 Editor / Simulator Go to Authors Web page (http: //www. mhhe. com/patt 2

LC-3 Editor / Simulator Go to Authors Web page (http: //www. mhhe. com/patt 2 ) Download LC-3 (LC 301. exe) LC-3 Edit LC-3 Simulate Review “Guide to Using the Windows Version of the LC-3 Simulator and LC-3 Edit”

Example Address Opcode Instruction Comments x 30 F 6 1 1 1 0 0

Example Address Opcode Instruction Comments x 30 F 6 1 1 1 0 0 0 1 1 1 1 0 1 R 1 PC – 3 = x 30 F 4 x 30 F 7 0 0 0 1 1 1 0 R 2 R 1 + 14 = x 3102 x 30 F 8 0 0 1 1 1 0 1 1 M[PC - 5] R 2 M[x 30 F 4] x 3102 x 30 F 9 0 1 0 1 0 1 0 0 0 R 2 0 x 30 FA 0 0 0 1 0 1 0 0 1 R 2 + 5 = 5 x 30 FB 0 1 1 1 0 0 0 1 1 1 0 M[R 1+14] R 2 M[x 3102] 5 1 0 0 1 1 1 1 0 1 1 1 R 3 M[M[x 30 F 4]] R 3 M[x 3102] R 3 5 x 30 FC

Example 1: Multiply • This program multiplies two unsigned integers in R 4 and

Example 1: Multiply • This program multiplies two unsigned integers in R 4 and R 5. clear R 2 add R 4 to R 2 decrement R 5 No R 5 = 0? Yes HALT Write program

Program to multiply [R 4] x [R 5] and place result in R 2

Program to multiply [R 4] x [R 5] and place result in R 2 x 3200 Clear Accumulator (R 2) R 2 <- 0 x 3201 Add [R 4] to Accumulator R 2 <- R 2 + R 4 x 3202 Dec R 5 <- R 5 – 1 x 3203 Do again if [R 5] > 0 BR p x 3201 x 3204 Stop HALT

Program to multiply [R 4] x [R 5] and place result in R 2

Program to multiply [R 4] x [R 5] and place result in R 2 Write program

Program to multiply [R 4] x [R 5] and place result in R 2

Program to multiply [R 4] x [R 5] and place result in R 2 x 3200 Clear Accumulator (R 2) R 2 <- 0 0101 010 1 00000 54 A 0 x 3201 Add [R 4] to Accumulator R 2 <- R 2 + R 4 0001 010 0 00 1484 x 3202 Dec R 5 <- R 5 – 1 0001 101 1 11111 1 B 7 F x 3203 Do again if [R 5] > 0 BR p x 3201 0000 011 111111101 07 FD x 3204 Stop HALT 1111 0000 00100101 F 025 Test on Simulator

Example 2 • Compute sum of 12 integers. Numbers start at location x 3100.

Example 2 • Compute sum of 12 integers. Numbers start at location x 3100. Program starts at location x 3000. R 1 x 3100 R 3 0 (Sum) R 2 12(count) R 2=0? NO R 4 R 3 R 1 R 2 YES Write program M[R 1] R 3+R 4 R 1+1 R 2 -1

Example 2 – Sum integers form x 3100 – x 310 B Address Instruction

Example 2 – Sum integers form x 3100 – x 310 B Address Instruction Comments x 3000 1 1 1 0 0 0 R 1 x 3100 x 3001 0 1 0 1 1 1 0 0 0 R 3 0 x 3002 0 1 0 1 0 1 0 0 0 R 2 0 x 3003 0 0 0 1 1 1 0 0 R 2 12 x 3004 0 0 0 0 0 1 If Z, goto x 3009 x 3005 0 1 1 0 0 0 0 1 0 0 0 Load next value to R 4 x 3006 0 0 0 1 1 0 0 0 1 Add to R 3 x 3007 0 0 0 1 1 0 0 1 Increment R 1 (pointer) X 3008 0 0 0 1 0 1 1 1 1 Decrement R 2 (counter) x 3009 0 0 1 1 1 0 0 0 1 0 0 Goto x 3004

Sample program 4 Write a program to place the absolute value of the [R

Sample program 4 Write a program to place the absolute value of the [R 2] in R 2

Example 3 - # Occurrences of Inputted Char

Example 3 - # Occurrences of Inputted Char

Example 3 - # Occurrences of Inputted Char Program (1 of 2) Address Instruction

Example 3 - # Occurrences of Inputted Char Program (1 of 2) Address Instruction Comments x 3000 0 1 0 1 0 1 0 0 0 R 2 0 (counter) x 3001 0 0 1 0 0 1 0 R 3 M[x 3102] (ptr) x 3002 1 1 0 0 0 1 1 Input to R 0 (TRAP x 23) x 3003 0 1 1 0 0 0 R 1 M[R 3] x 3004 0 0 0 1 1 1 0 0 R 4 R 1 – 4 (EOT) x 3005 0 0 0 1 1 1 0 If Z, goto x 300 E x 3006 1 0 0 1 1 1 1 R 1 NOT R 1 x 3007 0 0 0 1 1 0 0 1 R 1 + 1 X 3008 0 0 0 1 0 0 0 R 1 + R 0 x 3009 0 0 1 0 1 1 If N or P, goto x 300 B

Example 3 - # Occurrences of Inputted Char Program (2 of 2) Address Instruction

Example 3 - # Occurrences of Inputted Char Program (2 of 2) Address Instruction Comments x 300 A 0 0 0 1 0 1 0 0 1 R 2 + 1 x 300 B 0 0 0 1 1 1 0 0 1 R 3 + 1 x 300 C 0 1 1 0 0 0 R 1 M[R 3] x 300 D 0 0 1 1 1 0 0 0 1 0 0 Goto x 3004 x 300 E 0 0 1 0 0 1 1 R 0 M[x 3013] x 300 F 0 0 0 1 0 0 0 0 0 1 0 R 0 + R 2 x 3010 1 1 0 0 0 1 Print R 0 (TRAP x 21) x 3011 1 1 0 0 0 1 HALT (TRAP x 25) X 3012 Starting Address of File x 3013 0 0 0 0 0 1 1 0 0 ASCII x 30 (‘ 0’)

Program to display absolute value of number in R 2 (-99 >= [R 2]

Program to display absolute value of number in R 2 (-99 >= [R 2] >= 99) Write program

Program to display absolute value of number in R 2 (-99 >= [R 2]

Program to display absolute value of number in R 2 (-99 >= [R 2] >= 99) • • • Find absolute value ADD R 2, #0 BRzp +2 NOT R 2, R 2 ADD R 2, #1 • • • R 5 will contain x 30 (#48), the offset for ASCII numbers AND R 5, #0 ADD R 5, #15 ADD R 5, #3 5 B 60 1 B 6 F 1 B 63 • • Check for [R 2] >9 AND R 4, #0 ; init high digit holder to zero ADD R 2, #-10 BRn +2 ADD R 4, #1 BR -4 ADD R 3, R 2, #10 ; save low digit 5920 14 B 6 0802 1921 0 FFC 16 AA • • • If high digit = 0, don't display it ADD R 4, #0 ; set CC BRz +2 Display high digit ADD R 0, R 4, R 5 TRAP x 21 Display low digit Add R 0, R 3, R 5 TRAP x 21 Halt TRAP x 25 ; set cc 14 A 0 0602 94 BF 14 A 1 1920 0402 1105 F 021 10 C 5 F 021 F 025