Chapter 5 The LC3 Copyright The Mc GrawHill

  • Slides: 18
Download presentation
Chapter 5 The LC-3

Chapter 5 The LC-3

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. 4

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. 4 -2

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Control

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Control Instructions Used to alter the sequence of instructions (by changing the Program Counter) Conditional Branch • branch is taken if a specified condition is true Ø signed offset is added to PC to yield new PC • else, the branch is not taken Ø PC is not changed, points to the next sequential instruction Unconditional Branch (or Jump) • always changes the PC TRAP • changes PC to the address of an OS “service routine” • routine will return control to the next instruction (after TRAP) 5 -3

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Condition

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Condition Codes LC-3 has three condition code registers: N -- negative Z -- zero P -- positive (greater than zero) Set by any instruction that writes a value to a register (ADD, AND, NOT, LDR, LDI, LEA) Exactly one will be set at all times • Based on the last instruction that altered a register 5 -4

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Branch

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Branch Instruction Branch specifies one or more condition codes. If the set bit is specified, the branch is taken. • PC-relative addressing: target address is made by adding signed offset (IR[8: 0]) to current PC. • Note: PC has already been incremented by FETCH stage. • Note: Target must be within 256 words of BR instruction. If the branch is not taken, the next sequential instruction is executed. 5 -5

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. BR

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. BR (PC-Relative) What happens if bits [11: 9] are all zero? All one? 5 -6

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Example

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Example from last class: Multiply value stored in R 2 by 15 ; initialize 0101 00000 ; R 0 <- R 0 AND 0 0101 001 1 00000 ; R 1 <- R 1 AND 0 ; code to repeat 0001 001 000 010 ; R 1 <- R 1 + R 2 0001 00001 ; R 0 <- R 0 + 1 ; set condition codes 0001 011 000 1 10001 ; R 3 <- R 0 – 15 R 0 <- R 0 AND 0 R 1 <- R 1 + R 2 R 0 <- R 0 + 1 R 3 <- R 0 -15 R 3 < 0? ; branch if R 3 < 0 because count < 15 0000 111111100 ; PC<- PC- 4 if n==1 true n==1 false 5 -7

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Refined

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Refined Example: a better way to count ; initialize 0101 00000 ; R 0 <- R 0 AND 0 0101 001 1 00000 ; R 1 <- R 1 AND 0 0001 000 1 01111 ; R 0 <- R 0 + 15 ; code to repeat 0001 001 000 010 ; R 1 <- R 1 + R 2 0001 000 1 11111 ; R 0 <- R 0 - 1 ; set condition codes 0001 011 000 1 10001 ; R 3 <- R 0 – 15 ; branch if R 0 >0 because count < 15 0000 001 111111101 ; PC<- PC- 3 if p==1 R 0 <- R 0 AND 0 R 1 <- R 1 AND 0 R 0 <- R 0 + 15 R 1 <- R 1 + R 2 R 0 <- R 0 -1 R 0 > 0? true p==1 false 5 -8

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Iterative

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Iterative Do-While Loop true false 5 -9

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Multiply

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Multiply Using the While Loop Structure R 0 <- R 0 AND 0 ; initialize 0101 00000 ; R 0 <- R 0 AND 0 0101 001 1 00000 ; R 1 <- R 1 AND 0 0001 000 1 01111 ; R 0 <- R 0 + 15 ; branch if R 0== 0 because count == 15 0000 010 000000011 ; PC<- PC+3 if z==1 ; code to repeat 0001 001 000 010 ; R 1 <- R 1 + R 2 0001 000 1 11111 ; R 0 <- R 0 - 1 ; branch unconditionally 0000 111111100 ; PC<- PC-4 R 1 <- R 1 AND 0 R 0 <- R 0 + 15 true z==1 R 0 == 0? false R 1 <- R 1 + R 2 R 0 <- R 0 -1 always true false true 5 -10

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Code

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Code for Iteration true Exact bits depend on condition being tested PC offset to address C false Unconditional branch to retest condition Assumes all addresses are close enough that PC-relative branch can be used. PC offset to address A 6 -11

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Conditional

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Conditional If "hammock" If-Else "diamond" 5 -12

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. If

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. If conditional Problem statement: Increment R 0 if R 0 < R 1 WRONG? ? ? R 0 <- NOT R 0 ; form 2 s complement of R 0 1001 000 111111 ; R 0 <- NOT R 0 0001 00001 ; R 0 <- R 0 + 1 ; R 3 <- R 1 + complement of R 0 0001 011 000 001 ; R 3 <- R 0 + R 1 ; branch if R 3 is neg or 0 0000 110 00001 ; PC<- PC+1 if z==1 or n==1 ; increment R 0 0001 00001 ; R 0 <- R 0 + 1 1111000000100101 ; halt R 0 <- R 0 + 1 R 3 <- R 0 + R 1 true z==1 n==1 R 3 <= 0? false R 0 <- R 0 +1 false 5 -13

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Analyze

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Analyze the Situation Goal: Increment R 0 if R 0 < R 1 R 0 and R 1 can each be positive or negative when we don’t want to increment -> we do want to branch Assume R 0 >= R 1 result of (R 1 -R 0) R 0 is pos & R 1 is pos zero or neg R 0 is pos & R 1 is neg R 0 is neg & R 1 is pos NA pos R 0 is neg & R 1 is neg zero, neg So increment R 0 if (R 1 -R 0) is positive and branch to skip that step if (R 1 -R 0) is zero or negative 5 -14

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. If

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. If conditional (CORRECTED) Problem statement: Increment R 0 if R 0 < R 1 ; form 2 s complement of R 0 & store in R 4 1001 100 000 111111 ; R 4 <- NOT R 0 0001 100 1 00001 ; R 4 <- R 4 + 1 ; R 3 <- R 1 + complement of R 0 (stored in R 4) 0001 011 100 001 ; R 3 <- R 4 + R 1 ; branch if R 3 is neg or 0 0000 110 00001 ; PC<- PC+1 if z==1 or n==1 ; increment R 0 0001 00001 ; R 0 <- R 0 + 1 1111000000100101 R 0 <- NOT R 0 <- R 0 + 1 R 3 <- R 0 + R 1 true z==1 n==1 R 3 <= 0? false R 0 <- R 0 +1 ; halt false 5 -15

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Code

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Code for Conditional Exact bits depend on condition being tested Unconditional branch to Next Subtask Assumes all addresses are close enough that PC-relative branch can be used. PC offset to address C PC offset to address D 6 -16

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. JMP

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. JMP (Register) Jump is an unconditional branch -- always taken. • Target address is the contents of a register. • Allows any target address. 5 -17

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. TRAP

Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. TRAP Calls a service routine, identified by 8 -bit “trap vector. ” vector routine x 23 input a character from the keyboard (IN) x 21 output a character to the monitor (OUT) x 25 halt the program (HALT) When routine is done, PC is set to the instruction following TRAP. (We’ll talk about how this works later. ) Warning: TRAP changes R 7. 5 -18