230 likes | 419 Vues
Computer Science 210 Computer Organization. Machine Language Instructions: Control. Control Structures: Sequence. Statement-1. begin statement-1 . . statement- n end. Statement- n. Control Structures: Selection. if condition then consequent sequence else
E N D
Computer Science 210Computer Organization Machine Language Instructions: Control
Control Structures: Sequence Statement-1 begin statement-1 . . statement-n end . . Statement-n
Control Structures: Selection if condition then consequent sequence else alternative sequence Boolean expression False Alternative True Consequent
Control Structures: Loops (Entry Control) while condition do loop body sequence Boolean expression False True Loop body
Control Structures: Loops (Exit Control) do loop body sequence until condition Loop body False Boolean expression True
LC-3 Control Instructions • Conditional branch (BR) • Absolute branch (JMP) • Procedure call (JSR, JSRR, RET, RTI) • System call (TRAP)
Condition Codes • 3 single-bit registers named N, Z, and P • Exactly one will be set at all times • Set by any instructions that write data to a register (ADD, AND, NOT, LD, LDR, LDI, LEA) 0 1 0 N Z P
Example: Subtract 1 from R3 0 1 0 When R3 = 0 add R3, R3, -1 N Z P 0 0 1 When R3 > 0 Circuitry sets condition codes after add executes N Z P 1 0 0 When R3 < 0 N Z P
Conditional Branch (BR) • Alters a sequence of instructions by changing the PC • Branch is taken if the condition is true • Signed offset is added to PC if condition is true; otherwise, PC not changed
Conditional Branch 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 0 0 0 N Z PPC offset 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 0 0 0 0 1 0 0 1 1 0 1 1 0 0 1 BR n z p x0D9 Offset is sign-extended and added to the incremented PC Destination must be no more than +256 or -255 from the BR itself
Example: An Unconditional Branch 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 0 0 0 N Z PPC offset 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 0 0 0 1 1 1 0 1 1 0 1 1 0 0 1 BR n z p x0D9 At least one condition code is guaranteed to match the codes in this instruction
Example: Sum 10..1 sum = 0 number = 10 while number > 0: sum += number number -= 1
Example: Sum 10..1 sum <- sum & 0 number = 10 while number > 0: sum += number number -= 1
Example: Sum 10..1 sum <- sum & 0 number <- sum + 10 while number > 0: sum += number number -= 1
Example: Sum 10..1 sum <- sum & 0 number <- sum + 10 while number > 0: sum <- sum + number number -= 1
Example: Sum 10..1 sum <- sum & 0 number <- sum + 10 while number > 0: sum <- sum + number number <- number + (-1)
Example: Sum 10..1 sum <- sum & 0 number <- sum + 10 while: sum <- sum + number number <- number + (-1) BR p while while is a label for the address of an instruction
Example: Sum 10..1 R2 <- R2 & 0 R1 <- R2 + 10 while: R2 <- R2 + R1 R1 <- R1 + (-1) BR p while Replace variables with registers R2 = sum, R1 = number
Example: Sum 1..10 sum = 0 number = 1 limit = 10 while number <= limit: sum += number number += 1
Example: Sum 1..10 sum <- sum & 0 number <- sum + 1 limit <- sum + 10 while: diff <- limit – number BR n endwhile sum <- sum + number number <- number + 1 BR nzp while endwhile:
Jump Instruction (JMP) 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 1 1 0 0 Base 0 0 0 0 0 0 0 0 0 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 Contents of the base register are copied to the PC Can go anywhere in memory!
Trap Instruction (TRAP) 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 1 1 1 1 0 0 0 0 Trap vector (8 bits) OS service routine Operation coded in trap vector x23 = character input, x21 = character output, x25 = halt program R0 used for input and output After completion, PC is set to instruction following the TRAP