What is a program A sequence of steps

































- Slides: 33
What is a program? ¡ ¡ ¡ A sequence of steps For each step, an arithmetic or logical operation is done (e. g. ADD, LOAD) For each operation a unique code is provided A hardware segment accepts the code and issues the control signals For each operation, a different set of control signals is needed
Example of an Instruction Format
Instruction Cycle State Diagram
What is an Instruction Set? ¡ ¡ ¡ The complete collection of instructions that are understood (can be executed) by a CPU Machine Code (each instruction is represented by a sequence of bits) Usually represented by symbolic representation (mnemonics) l ADD R 1, R 2 R 1 + R 2
Elements of an Instruction ¡ Operation code (Op code) l ¡ Source Operand reference l ¡ The operation may involve one or more source operands) inputs for the operation( Result Operand reference l ¡ Specifies the operation to be performed The operation may produce a result (put the result here) Next Instruction Reference l tells the processor where to fetch the next instruction (mostly implicit)
Simple Instruction Format The instruction is divided into fields, corresponding to the constituent elements of the instruction. With most instruction sets, more than one format is used.
Source and result operands ¡ ¡ ¡ Main or virtual memory Processor register Immediate: l ¡ The value of the operand is contained in a field in the instruction being executed. I/O device
Instruction Types ¡ ¡ Data processing (arithmetic and logic instructions) Data storage (data transfer into or out of main memory and or registers) Data movement (I/O instructions) Program flow control (test and branch instructions)
Instruction set Design ¡ Operation repertoire l ¡ ¡ ¡ Data types Instruction formats: length, fields, size of each field, and so on Registers l ¡ How many operations to provide? What can they do? How complex are they? Number of CPU registers and their use by instructions Addressing modes (later) RISC VS CISC
Number of Addresses 1 ¡ Three-address instructions l l l Operand 1, Operand 2, Result a = b + c; May be a forth - next instruction (usually implicit) Not common Needs very long words to hold everything
Number of Addresses 2 ¡ Two-address instructions l l One address doubles as operand result a=a+b Reduces length of instruction Requires some extra work ¡ Temporary storage to hold some results
Number of Addresses 3 ¡ One-address instructions l l Implicit second address Usually a register (accumulator) Common on early machines instructions are more primitive, requiring a less complex processor
Number of Addresses 4 ¡ Zero-address instructions l l ¡ All addresses implicit Uses a stack e. g. c = a + b l l push a push b add pop c
Stack Organization
Stack Example
Number of Addresses 5
Number of Addresses 6 Try Y = (A - B) / (C + D * E) l l Three-address instructions Two-address instructions One-address instructions Zero-address instructions Conclusion
How Many Addresses ? ¡ More addresses l l More complex instructions More registers ¡ l ¡ Inter-register operations are quicker Fewer instructions per program Fewer addresses l l l Less complex instructions More instructions per program Faster fetch/execution of instructions
Types of Operand ¡ ¡ Addresses (unsigned integers) Numbers (limited) l l ¡ Characters l ¡ Integer/floating point Binary/Decimal ASCII, … etc. Logical Data l Bits or flags Note: some machines define special data types or data structures
Types of Operation ¡ ¡ ¡ ¡ Data Transfer Arithmetic Logical Conversion (e. g. Binary to Decimal) I/O System Control (for operating systems use) Transfer of Control See Table 10. 3 and 10. 4 for example
Data Transfer ¡ Specify l l l ¡ ¡ Source (memory, register, or the stack) Destination (memory, register, or the stack) Amount of data May be different instructions for different movements Or one instruction and different addresses
Arithmetic ¡ ¡ ¡ Add, Subtract, Multiply, Divide Signed Integer Floating point Decimal (BCD) May include l l Increment (a++) Decrement (a--) Negate (-a) Absolute
Logical ¡ ¡ Bitwise operations (Boolean operations) such as: AND, OR, XOR, NOT Shift and rotate operations l l l Logical shift Arithmetic shift Cyclic shift (rotate)
Shift and Rotate Operations
Shift and Rotate Operations (Exercise)
Examples of Shift and Rotate Operations
Input/Output ¡ ¡ ¡ May be specific instructions May be done using data movement instructions (memory mapped I/O) May be done by a separate controller (DMA)
Transfer of Control ¡ Branch (jump) l l ¡ e. g. branch to x if result is zero e. g. branch to x Procedure call l l e. g. call x Storing the return address (stack, register or start of called procedure)
Branch Instruction
Nested Procedure Calls
Byte (bit) Order ¡ ¡ ¡ ¡ What order do we read numbers that occupy more than one byte 12345678 h can be stored in 4 bytes locations as follows Address Value (1) Value(2) 184 12 78 185 34 56 186 56 34 186 78 12
Byte Order Names ¡ ¡ The problem is called Endian (endianness) The system on the left has the most significant byte in the lowest numerical byte address (big-endian) l ¡ Memory is viewed as left to right, top to bottom The system on the right has the least significant byte in the lowest numerical byte address (little-endian) l Memory is viewed as right to left, top to bottom
RQ: 10. 1, 10. 2, 10. 3, 10. 4, 10. 5, 10. 7, 10. 8, 10. 11, 10. 14 P: 10. 2, 10. 6, 10. 8, 10. 24