You are on page 1of 47

COMSATS Institute of
Information Technology
CCS221– COMPUTER ORGANIZATION AND ASSEMBLY
LANGUAGE
Lecture # 4

Quote of the day
If you are not a part of the solution,
you are a part of the problem.
― Eldridge Cleaver
4/17/16

CCS221– COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE

3

Quick recap

4/17/16

CCS221– COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE

4

General Concepts • Basic Microcomputer Design 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 5 .

OR.Basic Microcomputer Design • The central processor unit (CPU). a control unit. and NOT 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 6 . a high-frequency clock. contains a limited number of storage locations named registers. and an arithmetic logic unit: • The clock synchronizes the internal operations of the CPU with other system components • The control unit (CU) coordinates the sequencing of steps involved in executing machine instructions • The arithmetic logic unit (ALU) performs arithmetic operations such as addition and subtraction and logical operations such as AND. where calculations and logical operations take place.

so programs residing in memory must be copied into the CPU before they can execute • Individual program instructions can be copied into the CPU one at a time. transfers data from RAM to the CPU. or groups of instructions can be copied together 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 7 .Basic Microcomputer Design • The memory storage unit is where instructions and data are held while a computer program is running • The storage unit receives requests for data from the CPU. and transfers data from the CPU into memory • All processing of data takes place within the CPU.

Basic Microcomputer Design • A bus is a group of parallel wires that transfer data from one part of the computer to another • A computer system contains four bus types: • The data bus transfers instructions and data between the CPU and memory • The I/O bus transfers data between the CPU and the system input/output devices • The control bus uses binary signals to synchronize actions of all devices attached to the system bus • The address bus holds the addresses of instructions and data when the currently executing instruction transfers data between the CPU and memory 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 8 .

Clock • Each operation involving the CPU and the system bus is synchronized by an internal clock pulsing at a constant rate • The basic unit of time for machine instructions is a machine cycle (or clock cycle) • The length of a clock cycle is the time required for one complete clock pulse • In the following figure. a clock cycle is depicted as the time between one falling edge and the next: 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 9 .

Finally. this involves address calculations 4. the CPU decodes the instruction by looking at its binary bit pattern.Instruction Execution Cycle • CPU has to go through a predefined sequence of steps to execute a machine instruction. it increments the instruction pointer 2. Sometimes. if an output operand was part of the instruction. and Overflow 5. such as Zero. Next. Carry. This bit pattern might reveal that the instruction has operands 3. called the instruction execution cycle: 1. It also updates a few status flags. using any operand values it fetched during the earlier step. Right after doing this. the CPU executes the instruction. the CPU stores the result of its execution in the operand 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 10 . First. Next. the CPU has to fetch the instruction from an area of memory called the instruction queue. If operands are involved. the CPU fetches the operands from registers and memory.

Instruction Execution Cycle 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 11 .

Reading from Memory • Place the address of the value you want to read on the address bus • Change the value of the processor’s RD (read) pin • Wait one clock cycle for the memory chips to respond • Copy the data from the data bus into the destination operand 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 12 .

Reading from Memory • Cycle 1: The address bits of the memory operand are placed on the address bus. During this cycle. the memory controller places the operand on the data bus (DATA) Cycle 1 Cycle 2 CLK ADDR Address RD Data DATA • Cycle 4: The read line goes to 1. showing that some bits equal 1 and others equal 0 • Cycle 2: The read line is set 0 to notify memory that a value is to be read • Cycle 3: The CPU waits one cycle to give memory time to respond. The address lines in the diagram cross. signaling the CPU to read the data on the data bus 4/17/16 Cycle 3 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 13 Cycle 4 .

the operating system must point the CPU to the program’s entry point. it searches a predetermined list of directories for the filename • If the OS fails to find the program filename. it issues an error message 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 14 .Loading and Executing a Program • Before a program can run. which is the address at which the program is to begin execution • The following steps break this process down in more detail: • The operating system searches for the program’s filename in the current disk directory • If it cannot find the name there. it must be loaded into memory by a utility known as a program loader • After loading.

It is the OS’s job to track the execution of the process and to respond to requests for system resources • When 4/17/16 CCS221– COMPUTER the process ends. it is called a process • The OS assigns the process an identification number (process ID). it is removed from ORGANIZATION memory AND ASSEMBLY LANGUAGE 15 . including the file size and its physical location on the disk drive • The OS determines the next available location in memory and loads the program file into memory • It allocates a block of memory to the program and enters information about the program’s size and location into a descriptor table • The OS may adjust the values of pointers within the program so they contain addresses of program data • The OS begins execution of the program’s first machine instruction • As soon as the program begins running. the OS retrieves basic information about the program’s file from the disk directory.Loading and Executing a Program • If the program file is found. which is used to keep track of it while running • The process runs by itself.

Loading and Executing a Program 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 16 .

Cache • Computer memory creates a speed bottleneck because most programs have to access variables • To reduce the amount of time spent reading and writing memory—they store the most recently used instructions and data in high-speed memory called cache • When the processor is able to find its data in cache memory. we call that a cache miss 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 17 . we call that a cache hit • On the other hand.

STRUCTURE AND FUNCTION • Structure: The way in which the components are interrelated • Function: The operation of each individual component as part of the structure 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 18 .

there are only four: • Data processing • Data storage • Data movement • Control 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 19 .Basic functions of a Computer In general terms.

When data are moved over longer distances /remote device the process is known as data communications 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 20 .Function • Process data: The data may take a wide variety of forms. When data are received from or delivered to a device that is directly connected to the computer. and the range of processing requirements is broad • Store data: The computer must temporarily store at least those pieces of data that are being worked on at any given moment • Move data: between itself and the outside world. the process is known as input–output.

there must be control of these three functions • Ultimately. a control unit manages the computer’s resources and orchestrates the performance of its functional parts in response to those instructions 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 21 .Function • Finally. this control is exercised by the individual(s) who provides the computer with instructions • Within the computer.

Possible Operations • The computer can function as a data movement device 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 22 .

Possible Operations • It can function as a data storage device 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 23 .

Possible Operations 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 24 .

Possible Operations 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 25 .

all of its linkages to the external environment can be classified as peripheral devices or communication lines • We will have something to say about both types of linkages 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 26 .Structure • The computer interacts in some fashion with its external environment • In general.

system bus. consisting of a number of conducting wires to which all the other components attach 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 27 . and I/O • Example . main memory.Structure There are four main structural components: • Central processing unit (CPU): Controls the operation of the computer and performs its data processing functions. often simply referred to as processor • Main memory: Stores data • I/O: Moves data between the computer and its external environment • System interconnection: Some mechanism that provides for communication among CPU.

CPU Structural Components 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 28 .

CPU Structural Components • Control unit: Controls the operation of the CPU and hence the computer • Arithmetic and logic unit (ALU): Performs the computer’s data processing functions • Registers: Provides storage internal to the CPU • CPU interconnection: Some mechanism that provides for communication among 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 29 .

A TOP-LEVEL VIEW OF COMPUTER FUNCTION AND INTERCONNECTION .

COMPUTER COMPONENTS 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 31 .

Instruction Processing • Instruction processing consists of two steps: The processor reads instructions from memory one at a time and executes each instruction • Program execution consists of repeating the process of instruction fetch and instruction execution • The instruction execution may involve several operations and depends on the nature of the instruction • The processing required for a single instruction is called an instruction cycle 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 32 .

Instruction Fetch and Execute 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 33 .

the processor fetches an instruction from memory • A register called the program counter (PC) holds the address of the instruction to be fetched next • Unless told otherwise.Fetch • At the beginning of each instruction cycle. the processor always increments the PC after each instruction fetch so that it will fetch the next instruction in sequence 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 34 .

Execute • The fetched instruction is loaded into a register in the processor known as the instruction register (IR) • The instruction contains bits that specify the action the processor is to take • The processor interprets the instruction and performs the required action. the processor may fetch an instruction from location 149. which specifies that the next instruction be from location 182. • An instruction’s execution may involve a combination of these actions 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 35 . these actions fall into four categories: • Processor-memory: Data may be transferred from processor to memory or from memory to processor • Processor-I/O: Data may be transferred to or from a peripheral device by transferring between the processor and an I/O module • Data processing: The processor may perform some arithmetic or logic operation on data • Control: An instruction may specify that the sequence of execution be altered. on the next fetch cycle. The processor will remember this fact by setting the program counter to 182. the instruction will be fetched from location 182 rather than 150. In general. For example. Thus.

referred to as machine instructions or computer instructions • The collection of different instructions that the processor can execute is referred to as the processor’s instruction set 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 36 .MACHINE INSTRUCTION CHARACTERISTICS • The operation of the processor is determined by the instructions it executes.

The operation is specified by a binary code.Elements of a Machine Instruction • Each instruction must contain the information required by the processor for execution. known as the operation code. ADD.. that is. operands that are inputs for the operation • Result operand reference: The operation may produce a result • Next instruction reference: This tells the processor where to fetch the next instruction after the execution of this instruction is complete 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 37 . or opcode • Source operand reference: The operation may involve one or more source operands.g. These elements are as follows: • Operation code: Specifies the operation to be performed (e. I/O).

Common examples include • • • • • • ADD Add SUB Subtract MUL Multiply DIV Divide LOAD Load data from memory STOR Store data to memory • Operands are also represented symbolically. For example.Instruction Representation • Opcodes are represented by abbreviations. called mnemonics. that indicate the operation. Y 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 38 . the instruction ADD R.

Instruction Types • Data processing: Arithmetic and logic instructions • Data storage: Movement of data into or out of register and or memory locations • Data movement: I/O instructions • Control: Test and branch instructions 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 39 .

Example • The processor contains a single data register called an accumulator (AC) • Both instructions and data are 16 bits long • The instruction format provides 4 bits for the opcode. so that there can be as many as 16 different opcodes • And up to 4096 (4K) words of memory can be directly addressed 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 40 .

Example 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 41 .

Example 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 42 .

which can be described as three fetch and three execute cycles. are required: • The PC contains 300.Example • Figure illustrates a partial program execution. the address of the first instruction • This instruction is loaded into the instruction register IR. and the PC is incremented • The first 4 bits in the IR indicate that the AC is to be loaded • The remaining 12 bits specify the address (940) from which data are to be loaded 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 43 . showing the relevant portions of memory and processor registers • The program fragment shown adds the contents of the memory word at address 940 to the contents of the memory word at address 941 and stores the result in the latter location • Three instructions.

Example • The next instruction (5941) is fetched from location 301. and the PC is incremented • The old contents of the AC and the contents of location 941 are added. and the PC is incremented • The contents of the AC are stored in location 941 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 44 . and the result is stored in the AC • The next instruction (2941) is fetched from location 302.

NINTH EDITION By William Stallings 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 45 . 3 & 12 COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE. IRVINE • Chapter # 1. Seventh Edition By KIP R. 2.Reading Material • Chapter # 2 Assembly Language for x86 Processors.