Download presentation
Presentation is loading. Please wait.
Published byDominick Mosley Modified over 6 years ago
1
The University of Adelaide, School of Computer Science
23 June 2018 Chapter 2 Instructions: Language of the Computer Chapter 2 — Instructions: Language of the Computer
2
The University of Adelaide, School of Computer Science
23 June 2018 Shift Operations opcode Rm shamt Rn Rd 11 bits 5 bits 6 bits shamt: how many positions to shift Shift left logical Shift left and fill with 0 bits LSL by i bits multiplies by 2i Shift right logical Shift right and fill with 0 bits LSR by i bits divides by 2i (unsigned only) 2 Chapter 2 — Instructions: Language of the Computer
3
The University of Adelaide, School of Computer Science
23 June 2018 AND Operations Useful to mask bits in a word Select some bits, clear others to 0 AND X9,X10,X11 X10 X11 X9 3 Chapter 2 — Instructions: Language of the Computer
4
The University of Adelaide, School of Computer Science
23 June 2018 OR Operations Useful to include bits in a word Set some bits to 1, leave others unchanged OR X9,X10,X11 X10 X11 X9 4 Chapter 2 — Instructions: Language of the Computer
5
The University of Adelaide, School of Computer Science
23 June 2018 EOR Operations Differencing operation create a 0 when bits are the same and a 1 if they are different the equivalent to NOT is an EOR 111…111 EOR X9,X10,X12 // NOT operation X10 X12 X9 5 Chapter 2 — Instructions: Language of the Computer
6
Conditional Operations
The University of Adelaide, School of Computer Science 23 June 2018 Conditional Operations Branch to a labeled instruction if a condition is true Otherwise, continue sequentially CBZ register, L1 if (register == 0) branch to instruction labeled L1; Compare and branch if zero CBNZ register, L1 if (register != 0) branch to instruction labeled L1; Compare and branch if not zero B L1 branch unconditionally to instruction labeled L1; 6 Chapter 2 — Instructions: Language of the Computer
7
Compiling If Statements
The University of Adelaide, School of Computer Science 23 June 2018 Compiling If Statements C code: if (i==j) f = g+h; else f = g-h; f, g, … in X22, X23, … Compiled LEGv8 code: SUB X9,X22,X23 // X9 = i − j CBNZ X9,Else // go to Else if i ≠ j (X9 ≠ 0) ADD X19,X20,X21 // f = g + h (skipped if i ≠ j) B Exit // go to Exit Else: SUB X9,X22,x23 // f = g −h Exit: … Assembler calculates addresses 7 Chapter 2 — Instructions: Language of the Computer
8
Compiling Loop Statements
The University of Adelaide, School of Computer Science 23 June 2018 Compiling Loop Statements C code: while (save[i] == k) i += 1; i in x22, k in x24, address of save in x25 Compiled LEGv8 code: Loop: LSL X10,X22,#3 // Temp reg X10 = i * ADD X10,X10,X25 // X10 = address of save[i] LDUR X9,[X10,#0] // Temp reg X9 = save[i] SUB X11,X9,X24 // X11 = save[i] − k CBNZ X11,Exit // go to Exit if save[i] ≠ k(X11 ≠ 0) ADDI X22,X22,#1 // i = i B Loop // go to Loop Exit: … 8 Chapter 2 — Instructions: Language of the Computer
9
The University of Adelaide, School of Computer Science
23 June 2018 Basic Blocks A basic block is a sequence of instructions with No embedded branches (except at end) No branch targets (except at beginning) A compiler identifies basic blocks for optimization An advanced processor can accelerate execution of basic blocks 9 Chapter 2 — Instructions: Language of the Computer
10
More Conditional Operations
The University of Adelaide, School of Computer Science 23 June 2018 More Conditional Operations Condition codes, set from arithmetic instruction with S-suffix (ADDS, ADDIS, ANDS, ANDIS, SUBS, SUBIS) negative (N): result had 1 in MSB zero (Z): result was 0 overlow (V): result overflowed carry (C): result had carryout from MSB Use subtract (A-B) to set flags, then conditionally branch: 10 Chapter 2 — Instructions: Language of the Computer
11
Conditional branches LEGv8 includes four branches to complete the testing of the individual condition code bits: Branch on minus (B.MI): N=1; Branch on plus (B.PL): N=0; Branch on overflow set (B.VS): V=1; Branch on overflow clear (B.VC): V=0. An alternative to condition codes: have instructions that compare two registers and then branch based on the result. compare two registers and set a third register to a result indicating the success of the comparison, which a subsequent conditional branch instruction then tests to see if register is non-zero (condition is true) or zero (condition is false). Conditional branches in MIPS follow the latter approach 11
12
Conditional Example if (a > b) a += 1; a in X22, b in X23
SUBS X9,X22,X23 // use subtract to make comparison B.LTE Exit // conditional branch ADDI X22,X22,#1 Exit: 12
13
The University of Adelaide, School of Computer Science
23 June 2018 Signed vs. Unsigned Signed comparison Unsigned comparison Example X22 = X23 = X22 < X23 # signed –1 < +1 X22 > X23 # unsigned +4,294,967,295 > +1 13 Chapter 2 — Instructions: Language of the Computer
14
The University of Adelaide, School of Computer Science
23 June 2018 Procedure Calling Steps required Place parameters in registers X0 to X7 Transfer control to procedure Acquire storage for procedure Perform procedure’s operations Place result in register for caller Return to place of call (address in X30) §2.8 Supporting Procedures in Computer Hardware 14 Chapter 2 — Instructions: Language of the Computer
15
Procedure Call Instructions
The University of Adelaide, School of Computer Science 23 June 2018 Procedure Call Instructions Procedure call: branch-and-link instruction BL ProcedureLabel Address of following instruction put in LR (X30) Jumps to target address Procedure return: branch register instruction BR LR Copies LR (X30) to program counter Can also be used for computed jumps e.g., for case/switch statements Program Counter (PC): register containing the address of the current instruction BL saves PC+4 in register LR 15 Chapter 2 — Instructions: Language of the Computer
16
Leaf Procedure Example
The University of Adelaide, School of Computer Science 23 June 2018 Leaf Procedure Example C code: long long int leaf_example (long long int g, long long int h, long long int i, long long int j) { long long int f; f = (g + h) - (i + j); return f; } Arguments g, …, j in X0, …, X3 f in X19 (hence, need to save $s0 on stack) 16 Chapter 2 — Instructions: Language of the Computer
17
Local Data on the Stack 17
18
Leaf Procedure Example
The University of Adelaide, School of Computer Science 23 June 2018 Leaf Procedure Example LEGv8 code: leaf_example: SUBI SP,SP,#24 STUR X10,[SP,#16] STUR X9,[SP,#8] STUR X19,[SP,#0] ADD X9,X0,X1 ADD X10,X2,X3 SUB X19,X9,X10 ADD X0,X19,XZR LDUR X10,[SP,#16] LDUR X9,[SP,#8] LDUR X19,[SP,#0] ADDI SP,SP,#24 BR LR Save X10, X9, X19 on stack X9 = g + h X10 = i + j f = X9 – X10 copy f to return register Resore X10, X9, X19 from stack Return to caller 18 Chapter 2 — Instructions: Language of the Computer
19
Register Usage X9 to X17: temporary registers
Not preserved by the callee X19 to X28: saved registers If used, the callee saves and restores them Two stores and two loads can be dropped from the code. Still must save and restore X19 19
20
The University of Adelaide, School of Computer Science
23 June 2018 Non-Leaf Procedures Procedures that call other procedures For nested call, caller needs to save on the stack: Its return address Any arguments and temporaries needed after the call Restore from the stack after the call 20 Chapter 2 — Instructions: Language of the Computer
21
Non-Leaf Procedure Example
The University of Adelaide, School of Computer Science 23 June 2018 Non-Leaf Procedure Example C code: int fact (int n) { if (n < 1) return f; else return n * fact(n - 1); } Argument n in X0 Result in X1 21 Chapter 2 — Instructions: Language of the Computer
22
Leaf Procedure Example
The University of Adelaide, School of Computer Science 23 June 2018 Leaf Procedure Example LEGv8 code: fact: SUBI SP,SP,#16 STUR LR,[SP,#8] // save the return address STUR X0,[SP,#0] // save the argument n SUBIS XZR,X0,#1 B.GE L1 ADDI X1,XZR,#1 ADDI SP,SP,#16 BR LR L1: SUBI X0,X0,#1 BL fact LDUR X0,[SP,#0] LDUR LR,[SP,#8] MUL X1,X0,X1 Save return address and n on stack test for n < 1 if n >= 1, go to L1 Else, set return value to 1 Pop stack, don’t bother restoring values Return n = n - 1 call fact(n-1) Restore caller’s n Restore caller’s return address Pop stack return n * fact(n-1) return 22 Chapter 2 — Instructions: Language of the Computer
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.