Download presentation

Presentation is loading. Please wait.

Published byConor Aston Modified about 1 year ago

1
Machine Instructions Control Flow 1 ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-1B.ppt Modification date: March 24, 2015

2
2 Control Flow Compilers must translate statements such as: if ((x != y) && (z < 0)) { a = b + 5; b = b + 1; } into machine instructions. Unreasonable to try to provide a unique machine instruction for this IF statement because of the vast number of possible IF statements. Need to extract essential primitive operations for machine instructions.

3
3 Decompose into simple IF statements of the form: if (x relation y) goto L1; where: relation is any of usual relations allowed in high level languages (, >=, <=, ==, !=) L1 is a label prefixed to an instruction to identify it.

4
4 i.e. translate if ((x != y) && (z < 0)) { a = b + 5; b = b + 1; }... into if (x == y) goto L1; if (z >= 0) goto L1; a = b + 5; b = b + 1; L1:... Label

5
5 Several ways of implementing the IF statement. if (x relation y) goto L1; Here we will consider two ways: 1.Using one branch instruction Used in our design and lab. 2Using two instructions, one to determine whether relationship is true, and another to branch to L1 if true. Used by Intel and based upon a very old instruction set, so we have to know about it.

6
6 1. Using one branch instruction Single “branch” machine instruction compares two registers and goes to the labeled location if the condition is true, i.e. Bcond Rs1, Rs2, L1 changes the execution sequence to the instruction labeled L1 if Rs1 cond Rs2 is true, where cond can be any of six possible conditions, and Rs1 and Rs2 can be any of the 32 registers.

7
7 Conditional Branch Instruction op-codes Bcond BcondCondition High level language notation BLBranch if less than< BGBranch if greater than> BGEBranch if greater or equal to>= BLEBranch if less or equal to<= BEBranch if equal== BNEBranch if not equal!=

8
8 Example To implement if (x == y) goto L1;. L1: by a single machine instruction, we get: BE R1,R2,L1. L1: where the compiler allocates R1 for x and R2 for y.

9
9 Machine Instruction Encoding The instruction Bcond Rs1,Rs2,L1 requires an op-code (Bcond), the two source registers Rs1 and Rs2, and L1 to be specified. Note: Bcond is either BL, BG, BGE, BLE, or BNE

10
10 Specifying “target” location L1 Several ways L1 could be specified in instruction: (a) Absolute (direct) addressing Address of L1 held in instruction. (b) (PC) Relative addressingDistance from branch instruction to labeled instruction stored in instruction. Program counter, PC, holds the location of the instruction to be fetched from memory next, see later.

11
11 (a) Absolute (direct) addressing BE R1,R2,120. L1:;location 120 say Note: Absolute addressing will not used in our design.

12
12 (b) (Program counter) Relative Addressing Specify target location as number of instructions from branch instruction. Reasoning: Mostly, conditional branch instructions used to implement small changes in sequences or program loops of relatively short length, so distance from branch to target (label) quite small compared to full address of the labeled instruction. Also good programming practice to limit sequence changes to short distance from current location to avoid difficult to read code. Also makes code relocatable. (i.e. code can be loaded anywhere in memory without altering branch instructions.)

13
13 PC-Relative Addressing The number of locations to the target is held in the instruction as an offset. Offset is added to the program counter to obtain the “effective address” of the target location.

14
14 (PC) Relative Addressing BE R1,R2,+30; location 90 say. L1:; location 120 ( ) Offset/displacement We will deal with how to implement this later. Note: PC incremented by the size of instruction, 4 here, after instruction fetched from memory in most implementations. 30

15
15 Question How would one code: if (x > y) x = 10; with machine instructions where x and y are stored in R2 and R3 respectively? Answer BLE R2, R3, L1 MOV R2, 10 L1: Corrected

16
16 2. Using two instructions, one to determine whether relationship is true, and another to branch to L1 if true. In this approach, we determine whether the Boolean condition in if (x relation y) goto L1; is true by subtracting y from x and recognizing whether the result is positive or negative, zero, or not zero: relationx - y

17
17 Condition Code Register (CCR) Contains a set of flags (single bits) used to be able to recognize the different possible relationships (, >=, <=, ==, !=) after the previous arithmetic operation. Flags in condition code register indicate a particular aspect of the result of the last arithmetic instruction, i.e. positive or negative, zero or not zero, …

18
18 Sign Flag (S or N flag) (positive or negative) Indicates whether previous arithmetic result is negative or positive. S = 1 for negative result S = 0 for positive result. S is actually the most significant (sign) bit of the result of the previous arithmetic operation

19
19 Zero Flag Zero flag, Z: Z = 1 for result = 0 Z = 0 for result != 0 (Note zero is a positive number.)

20
20 Condition Code Register Condition Code register normally closely linked to the ALU (Arithmetic and Logic Unit):

21
21 Using Condition Code Register Decompose IF statement such as: if (x relation y) goto L1 into two sequential operations: 1.Subtract y from x which sets condition code register according to result 2.Read condition code register and “branch” to L1 if specific condition indicated

22
22 Step 1 Subtract and Set Condition Code Register All arithmetic instructions set condition code register according to the result of the arithmetic operation, but a compare instruction specifically provided, similar to a subtract instruction except result is not stored anywhere, only the CCR flags set according to the result. Example CMP R1, R2;R1 - R2, sets CCR flags

23
23 Step 2 Reading Condition Code Register and Branching A conditional branch instruction used to examine the values stored in the condition code register to determine whether the specific condition exists and to branch if it does. All six conditions usually available: BLBranch if less than BGBranch if greater than BGEBranch if greater or equal to BLEBranch if less or equal to BE (or BZ) Branch if equal BNE (or BNZ)Branch if not equal

24
24 Example Suppose x held in R1 and y held in R2. The if statement: if (x == y) goto L1; could be implemented by sequence of two instructions, CMP and BE: CMP R1,R2;Compare contents of R1, R2 (x, y) BE L1;Go to L1 if equal L1:

25
25 Example Suppose x held in R1 and y held in R2. The if statement: if (x == y) goto L1; could be implemented by sequence of two instructions, CMP and BE: CMP R1,R2;Compare contents of R1, R2 (x, y) BE L1;Go to L1 if equal L1:

26
26 Example Suppose x held in R1 and y held in R2. The if statement: if (x == y) goto L1; could be implemented by sequence of two instructions, CMP and BE: CMP R1,R2;Compare contents of R1, R2 (x, y) BE L1;Go to L1 if equal L1:

27
27 More complex constructions if - then - else Suppose we had to implement: if (a < b) c = a; else a = c;... assuming that the variables a, b, and c are assigned to registers R1, R2, and R3 respectively.

28
28 Leads to: CMP R1,R2;Compare R1, R2 (x, y) BL L1;Go to L1 if a less than b MOV R1, R3;a = c L1:MOV R3,R1;c = a; L2:... We need to alter the instruction sequence unconditionally. With the instructions we know about so far, it could be done with: CMP R1, R1 BE L2;if (x == x) goto L2; but there is a special instruction called a jump instruction to do it. Skip over c = a, unconditionally

29
29 JUMP Instructions Causes an unconditional change of execution sequence to a new location. Necessary to implement more complicated IF constructs, FOR, and WHILE loops. Using J as the opcode mnemonic, the jump instruction is: J L1;goto to L1

30
With jump instruction: CMP R1,R2;Compare R1, R2 (x, y) BL L1;Go to L1 if a less than b MOV R1, R3;a = c J L2 L1:MOV R3,R1;c = a; L2: Go to L2

31
31 For loops Suppose we had to implement the C/Java code sequence: for (i = 1; i < 10; i++) a = a * i;... Let us assume i is held in register R1, and x is held in register R2. Possible solution MOV R1, 1;i = 1 L2:CMP R1,10;Compare R1 with 10 BGE L1;Go to L1 if end of loop MUL R2,R2,R1;a = a * i ADD R1,R1,1;increment i J L2 L1:...

32
32 Jump Instruction with Register-Indirect Addressing An “address register” specified in instruction holds the address of the location holding the next instruction to be executed. Examples J [R1];jump to location whose address in R1 J 100[R1];jump to location whose address given ;by R Target address specified as absolute address, rather than relative address. Used to implement high level language SWITCH statements. Also return from procedures see next section.

33
33 Avoiding Condition Code Register It turns out that the CCR approach has disadvantages when one tries to implement a high performance processor, and does not really suit RISC designs. Makes it much more difficult to design a high-performance processor. CCR approach requires two sequential instructions with no instructions allowed in between that affect the CCR. (All arithmetic/logic instruction affect CCR.) For the most part we will use single instruction in our designs. An alternative to the CCR is to use a general purpose register in place of CCR to hold “conditions.”

34
34 Simplified version of branch instruction Bcond Rs1, L1 where Rs1 is compared against zero rather than Rs2. Do subtract operation previous to this instruction, placing result in Rs1.

35
35 Question How would one code: if (x > y) goto L1; where x and y are stored in R2 and R3 respectively using previous simplified version of branch instruction? Answer SUB R4, R2, R3 BG R4, L1

36
36 One version of branch instruction in MIPS processor (Lab) Use a general purpose register set to 1 of 0 if one register is less than another. SLT Rs1,Rs2,Rs3;Rs1 = 1 if Rs2 < Rs3 together with a simple branch instruction that only tests condition equal or not equal.

37
37 Questions

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google