Machine Instructions Operations 1 ITCS 3181 Logic and Computer Systems 2014 B. Wilkinson Slides4-1.ppt Modification date: Oct 22, 2014.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

Goal: Write Programs in Assembly
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Branches Two branch instructions:
Chapter 8: Central Processing Unit
Machine Instructions Operations
ISA Issues; Performance Considerations. Testing / System Verilog: ECE385.
Data Dependencies Describes the normal situation that the data that instructions use depend upon the data created by other instructions, or data is stored.
INSTRUCTION SET ARCHITECTURES
Chapter 10- Instruction set architectures
Machine Instructions Operations 1 ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-1.ppt Modification date: March 18, 2015.
Machine Instructions Control Flow 1 ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-1B.ppt Modification date: March 24, 2015.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
1 ITCS 3181 Logic and Computer Systems B. Wilkinson Slides9.ppt Modification date: March 30, 2015 Processor Design.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson slides3.ppt Modification date: March 16, Addressing Modes The methods used in machine instructions.
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
1 ITCS 3181 Logic and Computer Systems 2014 B. Wilkinson Slides8.ppt Modification date: Nov 3, 2014 Random Logic Approach The approach described so far.
TK 2633 Microprocessor & Interfacing
TK 2633 Microprocessor & Interfacing Lecture 3: Introduction to 8085 Assembly Language Programming (2) 1 Prepared By: Associate Prof. Dr Masri Ayob.
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
Memory - Registers Instruction Sets
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
Chapter 4 Processor Technology and Architecture. Chapter goals Describe CPU instruction and execution cycles Explain how primitive CPU instructions are.
Topics covered: ARM Instruction Set Architecture CSE 243: Introduction to Computer Architecture and Hardware/Software Interface.
Specifying the Actions Internal Architecture of a Simple Processor
Implementation of a Stored Program Computer
ITCS 3181 Logic and Computer Systems 2014 B. Wilkinson Slides6.ppt Modification date: Oct 30, Processor Design Specifying the Actions Internal Architecture.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 1 Flow Control.
Instruction Set Design by Kip R. Irvine (c) Kip Irvine, All rights reserved. You may modify and copy this slide show for your personal use,
Machine Instruction Characteristics
Implementation of a Stored Program Computer ITCS 3181 Logic and Computer Systems 2014 B. Wilkinson Slides2.ppt Modification date: Oct 16,
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
Execution of an instruction
Module : Algorithmic state machines. Machine language Machine language is built up from discrete statements or instructions. On the processing architecture,
Computer Architecture and Organization
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Chapter 2 Decision-Making Instructions (Instructions: Language of the Computer Part V)
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
1 The Instruction Set Architecture September 27 th, 2007 By: Corbin Johnson CS 146.
What is a program? A sequence of steps
Elements of Datapath for the fetch and increment The first element we need: a memory unit to store the instructions of a program and supply instructions.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Instruction Sets. Instruction set It is a list of all instructions that a processor can execute. It is a list of all instructions that a processor can.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
MIPS Processor.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Computer Architecture. Instruction Set “The collection of different instructions that the processor can execute it”. Usually represented by assembly codes,
Deeper Assembly: Addressing, Conditions, Branching, and Loops
Instruction sets : Addressing modes and Formats
CS2100 Computer Organisation
Control Unit Lecture 6.
Assembly Language Programming of 8085
ARM Registers Register – internal CPU hardware device that stores binary data; can be accessed much more rapidly than a location in RAM ARM has.
Processor Instructions set. Learning Objectives
Lecture 4: MIPS Instruction Set
CS170 Computer Organization and Architecture I
CSC 3210 Computer Organization and Programming
MIPS Processor.
ECE232: Hardware Organization and Design
Chapter 8 Central Processing Unit
Instruction encoding We’ve already seen some important aspects of processor design. A datapath contains an ALU, registers and memory. Programmers and compilers.
The University of Adelaide, School of Computer Science
Computer Instructions
3.
COMS 361 Computer Organization
COMS 361 Computer Organization
ECE 352 Digital System Fundamentals
MIPS Assembly.
An Introduction to the ARM CORTEX M0+ Instructions
Presentation transcript:

Machine Instructions Operations 1 ITCS 3181 Logic and Computer Systems 2014 B. Wilkinson Slides4-1.ppt Modification date: Oct 22, 2014

2 Instructions We will use a simple instruction formats of a so-called reduced instruction set computer (RISC), which has the characteristics: Simple, fixed length instruction format A few addressing modes Limited number of operations Designed to achieve high speed of execution It was recognized in the 1980’s that such processors would actually execute programs faster that the prevalent CISCs (complex instruction set computers).

3 Instruction format Our instruction format is quite similar to the G4/G5 PowerPC and SUN Sparc processors and also similar but not identical to that used in the assembly language simulator in the labs. Intel 64/IA32 processor family -- uses a very complex and archaic instruction format, based upon the early 8086 processor (which was themselves loosely based upon even earlier Intel processors). However Intel processors now convert this complex instruction format (CISC) internally to simpler RISC formats for performance reasons.

4 1.Thirty-two 32-bit integer registers called R0 to R31 2. One register, R0, holds zero permanently 3 All arithmetic done only between registers: - Three-register format op-code, destination register, source register 1, source register 2 or - Immediate addressing op-code destination, source register, constant 4. Memory operations limited to: load register, and store register using register indirect addressing plus offset only. Some Key Features of Processor Used R0 R1 R2 R31 R30 R3 R29 R4 R … 0000 R31 holds return address for procedures. R29 is a stack pointer see later.

5 Data Transfer Instructions that copy the contents of one location to another location. Examples MOV R1,R2;R1 = R2 LD R3,100[R2];Contents of memory whose address is ;given by R2 copied to R3. ST [R5],R4;Contents of R4 copied to memory loc. ;whose address held in R5. (Offset = 0) Note LD and ST cause 32-bit transfers. Use LB and SB to cause 8-bit transfers. InstructionComments

6 Arithmetic Instructions Performs an arithmetic operation such as addition, subtraction, multiplication or division. Examples ADD R1,R2,R3;R1 = R2 + R3 SUB R5,R4,3;R5 = R4 – 3 For literals (immediate addressing) Differences in assembly language notation. SUB R5,R4,3;R5 = R4 - 3 might be written as: SUB R5,R4,#3;R5 = R4 - 3 or SBI R5,R4,3;R5 = R4 - 3 depending upon assembly language.

7 Logical Instructions Performs bit-wise logical operation such as AND, OR, exclusive- OR, or NOT. AND, OR, exclusive-OR operate upon pairs of bits of two operands. Bit-wise AND, OR, Ex-OR, and NOT are available in C and Java (although you probably did not come across them!): C/Java Language Examples y = y & 1;bit-wise AND y with the number 1 z = z | 2;bit-wise OR z with the number 2

8 Machine Instruction Examples AND R1,R2,R3;R1 = R2 “AND” R3 if R2 = R3 = then R1 = OR R1,R2,R3;R1 = R2 “OR” R3 if R2 = R3 = then R1 =

9 Shift Instructions Moves the bits of a location one or more places left or right. Again available in C/Java (although you probably did not come across them!): C/Java language Examples y = y << 1;shift y 1 place left 1 z = z >> 2;shift z 2 places right

10 Machine Instruction Examples Examples SHL R1,R1,1;Shift R1 left one place SHR R1,R1,1;Shift R1 right one place X = 0 or 1 see next slide

11 Arithmetical and Logical Shifts Two types of shift usually provided: “Logical” shift (SHL, SHR) Fill free end with 0, i.e. X = 0. “Arithmetic” shift (SAL, SAR) Arithmetic shifts multiple/divide by 2. Arithmetic shift right maintains value of sign bit, i.e. X = value of original sign bit.

12 Example Starting with a number Shift arithmetic left one place. Get Shift arithmetic right two places. Get i.e. lost the 0.5.

13 Question What is the difference, if any, between arithmetic shift left and logical shift left, i.e. what is the difference, if any, between: SHL R1, R1, 1 and SAL R1, R1, 1 Answer

14 Arithmetic shift left same as logical shift left (except arithmetic overflow may be detected). Note: Java has logical shift right - called unsigned right shift, >>>. Example x = x >>> 2;

15 Question What is the effect of the sequence? SAL R1, R1, 1 SAR R1, R1, 1 Answer

16 Rotate Instructions Moves bits of location one or more places left of right in a circular fashion. Examples ROL R1,R1,1;Rotate R1 left one place ROR R1,R1,1;Rotate R1 right one place Version of rotate exists where the rotate passes thro a Carry flag within the condition code register, see later about the CCR

17 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.

18 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.

19 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

20 There is several ways of implementing the IF statement. if (x relation y) goto L1; Here we will consider two ways: 1.Using one branch instruction. 2Using two instructions, one to determine whether relationship is true, and another to branch to L1 if true.

21 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.

22 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!=

23 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.

24 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

25 Specifying “target” location L1 Several ways L1 could be specified in instruction: Absolute (direct) addressingThe address of L1 held in instruction. (PC) Relative addressingThe distance from branch instruction to labeled instruction stored in instruction. Program counter, see later

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

27 (Program counter) Relative Addressing Specify target location as number of instructions from branch instruction. (The location of the instruction being fetched from memory is given by an instruction pointer called the program counter historically) 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.)

28 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.

29 (PC) Relative Addressing BE R1,R2,+30;location 90 say. L1:;location 120 say Offset/displacement PC = 90 (Note: PC incremented by the size of instruction, 4 here, after instruction fetched in most implementations)

30 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

31 2. Condition Code Register Approach 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 <negative >positive and not zero >=positive or zero <=negative or zero ==zero !=not zero

32 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, … Usually a form of subtract operation is first performed, although technically other arithmetic and logical operations can affect the condition code register.

33 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

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

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

36 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

37 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, which is similar to a subtract instruction except the result is not stored anywhere, only the CCR flags are set according to the result. Example CMP R1, R2;R1 - R2, sets CCR flags

38 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

39 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:

40 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:

41 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:

42 Question A processor uses a condition code register approach for branch instructions. Write an assembly language sequence for the C/Java code: if ((a 0)) b = a; assuming that the variables a and b are assigned to registers R1 and R2 respectively. Provide comments Use any reasonable hypothetical instructions. (Note && is the symbol for the logical AND operation.)

43 Answer

44 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.

45 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

46 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

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

48 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:...

49 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.

50 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. CCR approach requires two sequential instructions with no instructions allowed in between that affect the CCR. (All arithmetic/logic instruction affect CCR.) Alternative -- use a general purpose register in place of CCR to hold “conditions.”

51 One solution -- 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.

52 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

53 One version of branch instruction used in MIPS processor 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, i.e.: BEQ Rs1, Rs2, L1;goto L1 if Rs1 = Rs2 and BNE Rs1, Rs2, L1;goto L1 if Rs1 != Rs2

54 Using MIPS Branch Instruction To do if (x < y) goto L1; where x and y are stored in R2 and R3 respectively, we might write: SLT R1,R2,R3;R1 = 1 if R2 < R3 BNE R1,R0,L1;goto L1 if R1 != 0

55 It is much more common to use a condition code register. Pentium uses condition code register for historical reasons. However, the condition code register approach makes it much more difficult to design a high-performance processor. In our design subsequently, we will use: Bcond Rs1, L1

56 Questions