Chapter 5 The LC-3
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)
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, LD, LDR, LDI, LEA) Exactly one will be set at all times Based on the last instruction that altered a register
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.
BR (PC-Relative) What happens if bits [11:9] are all zero? All one? If all zero, no CC is tested, so branch is never taken. (See Appendix B.) If all one, then all are tested. Since at least one of the CC bits is set to one after each operate/load instruction, then branch is always taken. (Assumes some instruction has set CC before branch instruction, otherwise undefined.) What happens if bits [11:9] are all zero? All one?
Example from last class: Multiply value stored in R2 by 15 ; initialize 0101 000 000 1 00000 ; R0 <- R0 AND 0 0101 001 001 1 00000 ; R1 <- R1 AND 0 ; code to repeat 0001 001 001 000 010 ; R1 <- R1 + R2 0001 000 000 1 00001 ; R0 <- R0 + 1 ; set condition codes 0001 011 000 1 10001 ; R3 <- R0 – 15 ; branch if R3 < 0 because count < 15 0000 100 111111100 ; PC<- PC- 4 if n==1 R0 <- R0 AND 0 R1 <- R1 AND 0 R1 <- R1 + R2 R0 <- R0 + 1 R3 <- R0 -15 true R3 < 0? n==1 false
Refined Example: a better way to count ; initialize 0101 000 000 1 00000 ; R0 <- R0 AND 0 0101 001 001 1 00000 ; R1 <- R1 AND 0 0001 000 000 1 01111 ; R0 <- R0 + 15 ; code to repeat 0001 001 001 000 010 ; R1 <- R1 + R2 0001 000 000 1 11111 ; R0 <- R0 - 1 ; set condition codes 0001 011 000 1 10001 ; R3 <- R0 – 15 ; branch if R0 >0 because count < 15 0000 001 111111101 ; PC<- PC- 3 if p==1 R0 <- R0 AND 0 R1 <- R1 AND 0 R0 <- R0 + 15 R1 <- R1 + R2 R0 <- R0 -1 true R0 > 0? p==1 false
Iterative Do-While Loop While Loop true false
Multiply Using the While Loop Structure R0 <- R0 AND 0 ; initialize 0101 000 000 1 00000 ; R0 <- R0 AND 0 0101 001 001 1 00000 ; R1 <- R1 AND 0 0001 000 000 1 01111 ; R0 <- R0 + 15 ; branch if R0== 0 because count == 15 0000 010 000000011 ; PC<- PC+3 if z==1 ; code to repeat 0001 001 001 000 010 ; R1 <- R1 + R2 0001 000 000 1 11111 ; R0 <- R0 - 1 ; branch unconditionally 0000 111 111111100 ; PC<- PC-4 R1 <- R1 AND 0 R0 <- R0 + 15 true R0 == 0? z==1 false R1 <- R1 + R2 R0 <- R0 -1 always true true false
Unconditional branch to retest condition Code for Iteration PC offset to address C Exact bits depend on condition being tested true false Unconditional branch to retest condition PC offset to address A Assumes all addresses are close enough that PC-relative branch can be used.
Conditional If If-Else "hammock" "diamond"
WRONG??? If conditional Problem statement: Increment R0 if R0 < R1 R0 <- NOT R0 ; form 2s complement of R0 1001 000 000 111111 ; R0 <- NOT R0 0001 000 000 1 00001 ; R0 <- R0 + 1 ; R3 <- R1 + complement of R0 0001 011 000 000 001 ; R3 <- R0 + R1 ; branch if R3 is neg or 0 0000 110 000000001 ; PC<- PC+1 if z==1 or n==1 ; increment R0 1111000000100101 ; halt R0 <- R0 + 1 R3 <- R0 + R1 true z==1 R3 <= 0? n==1 false R0 <- R0 +1 false
Analyze the Situation Goal: Increment R0 if R0 < R1 R0 and R1 can each be positive or negative So increment R0 if (R1-R0) is positive and branch to skip that step if (R1-R0) is zero or negative when we don’t want to increment -> we do want to branch Assume R0 >= R1 result of (R1-R0) R0 is pos & R1 is pos zero or neg R0 is pos & R1 is neg neg R0 is neg & R1 is pos NA pos R0 is neg & R1 is neg zero, neg
If conditional (CORRECTED) Problem statement: Increment R0 if R0 < R1 ; form 2s complement of R0 & store in R4 1001 100 000 111111 ; R4 <- NOT R0 0001 100 100 1 00001 ; R4 <- R4 + 1 ; R3 <- R1 + complement of R0 (stored in R4) 0001 011 100 000 001 ; R3 <- R4 + R1 ; branch if R3 is neg or 0 0000 110 000000001 ; PC<- PC+1 if z==1 or n==1 ; increment R0 0001 000 000 1 00001 ; R0 <- R0 + 1 1111000000100101 ; halt R0 <- NOT R0 R0 <- R0 + 1 R3 <- R0 + R1 true R3 <= 0? z==1 n==1 false R0 <- R0 +1 false
Unconditional branch to Next Subtask Code for Conditional PC offset to address C Exact bits depend on condition being tested Unconditional branch to Next Subtask PC offset to address D Assumes all addresses are close enough that PC-relative branch can be used.
JMP (Register) Jump is an unconditional branch -- always taken. Target address is the contents of a register. Allows any target address.
TRAP Calls a service routine, identified by 8-bit “trap vector.” When routine is done, PC is set to the instruction following TRAP. (We’ll talk about how this works later.) vector routine x23 input a character from the keyboard (IN) x21 output a character to the monitor (OUT) x25 halt the program (HALT) Warning: TRAP changes R7.