The LC-3 – Chapter 5 COMP 2620 Dr. James Money COMP
Data Movement Instructions Recall that there are three types of instructions: – Operate instructions – Data movement instructions – Control instructions We consider the control instructions today
Control Instructions Recall that these types of instruction affect the flow of execution That is, they directly change the PC value depending on the result of the instruction We first consider a condition branch instruction
Conditional Branches Recall the LC-3 has three condition codes: – N – negative – Z – zero – P – positive These codes are set when an instruction write to a register Only one of these are set at a time
Conditional Branches The instructions that set these flags are – ADD – AND – NOT – LD – LDR – LDI – LEA
Conditional Branches The condition branch has opcode 0000 Bits [11:9] correspond to the N, Z, and P condition codes Bits [8:0] is the PC offset address for the branch Range of offset is -256…255 Target must be 256 bytes of the PC
Conditional Branches If the particular condition code is set in the processing unit(N,Z, or P) and it is selected by the instruction, then the jump if executed If the condition code is false, the jump do not occur This corresponds to an if (condition) ->jump else do nothing;
BR (PC-Relative) What happens if bits [11:9] are all zero? All one?
Task: Compute sum of 12 integers. Assume the numbers start at location 0x3100 Assume the program code starts at location 0x3000 Using Branch Instructions
Pseudocode: – addr= 0x3100 – valsleft = 12 – sum=0 – If (valsleft==0) end – Load value from memory[addr] – Sum=sum+value – valsleft=valsleft-1 – addr=addr+1
Using Branch Instructions R1 x3100 R3 0 R2 12 R2=0? R4 M[R1] R3 R3+R4 R1 R1+1 R2 R2-1 NO YES
AddressInstructionComments x R1 0x3100 (PC+0xFF) x R3 0 x R2 0 x R2 12 x If Z, goto 0x300A (PC+5) x Load next value to R4 x Add to R3 x Increment R1 (pointer) X Decrement R2 (counter) x Goto 0x3004 (PC-6)