Computer Organization & Architecture: Chapter 2 (Lecture 2)
Computer Organization & Architecture: Chapter 2 (Lecture 2)
Computer Organization & Architecture: Chapter 2 (Lecture 2)
Architecture
Chapter 2 (Lecture 2)
Instruction Sets
1
Introduction
• Instructions
—Specify operations to be performed by a computer
—Words of a computer’s language
• Instruction set
—Collection of the instructions of a computer
—Vocabulary of a computer’s language
• Elements of an instruction
Introduction…cntd
• Operation code (opcode)
—Specifies the operation to be performed (e.g. ADD, SUB, MOVE,I/O).
—The operation is specified by a binary code, known as the operation
code, or opcode.
• Addresses (operands)
—Provide more information about the operation
—May include:
– Source operands: The operation may involve one or more source
operands, that is , operands that inputs for the operation.
– Destination operands: specify where results go
– Next instruction reference: tells the CPU where to fetch the next
instruction after the execution of the current instruction is
complete.
Introduction…cntd
• Instructions to be read by a computer contain strings of
1s and 0s (They are numbers) (Machine instructions)
• Symbolic representations of machine instructions are
used for convenience (assembly language)
• Even more convenient (High-level languages)
void main()
{ Compiler
main: Assembler
int a,b,c; 0000 0101 0110 0111
ADD c,a,b
c = a+b;
}
Instruction Format
• Defines the layout of the bits of an instruction in
terms of its constituent fields (What does each
field represent and how many bits is it?)
• Common formats:
Instruction Format…cntd
Instruction Format…cntd
• E.g. Assume a CPU has 80 different operations. Each
operand specifies one of 8 CPU registers. How many bits
(minimum) for each field?
How long should an instruction be?
• Opcode: 7 bits
• Reg.1,Reg. 2,Reg.3: 3 bits each(9 bits total)
• Instruction length: 16 bits
Number of Addresses (a)
• 3 addresses
—Operand 1, Operand 2, Result
—May be a forth - next instruction (usually
implicit)
—Not common
—Example ADD X,A,B
– Where X,A,B are memory locations or particular
registers
– This instruction adds the contents of A and B and
puts the result in location X
Number of Addresses (b)
• 2 addresses
—One address doubles as operand and result
—Reduces length of instruction
—Requires some extra work
– Temporary storage to hold some results
—Example ADD A,B
– Where A and B are memory locations or particular
registers
– The instruction Adds the content of A and B and put
the result in location A
Number of Addresses (c)
• 1 address
—Implicit second address
—Usually a register (accumulator)
—Common on early machines
—Example
– LOAD D, this instruction loads the content of D into
the accumulator register (AC)
– STOR Y, this instruction stores the content of AC into
memory location Y
Number of Addresses (d)
• 0 (zero) addresses
—All addresses implicit
—Uses a stack
—e.g. push a
push b
add
pop c
c=a+b
How Many Addresses
• More addresses
—More complex instructions
—More registers
—Fewer instructions per program
• Fewer addresses
—Less complex instructions
—More instructions per program
—Faster fetch/execution of instructions
Three-Address Instructions
a. X = (A + B) * (C + D)
b. Y= (A-B) / (C+D * E).
____________________________________
_
a. X = (A + B) * (C + D)
Instruction Comment
1.ADD R1, A, B R1 A + B
2. ADD R2, C, D R2 C + D
3.MUL X, R1, R2 X R1 * R2
Instruction Comment
• LOAD A AC A
• ADD B AC AC + B
• STORE T T AC T is address of tempo memory
location
• LOAD C AC C
• ADD D AC AC + D
• MUL T AC AC * T
• STORE X X AC
• Use an implied AC register for all data manipulation. All operations are done between
the AC register and memory operand
One-Address Instructions…Cntd
b. Y= (A-B) / (C+D * E).
Instruction Comment
• LOAD D AC D AC( Accumulator)
• MUL E AC AC*E
• ADD C AC AC+C
• STOR Y Y AC
• LOAD A AC A
• SUB B AC AC-B
• DIV Y AC AC÷Y
• STOR Y Y AC
By using Zero address scheme, write a set of
instruction that would perform the above
Arithmetic Operations
Zero-Address Instructions
a. X = (A + B) * (C + D)
Instruction Comment
PUSH A TOS A
PUSH B TOS B
ADD TOS A + B
PUSH C TOS C
PUSH D TOS D
ADD TOS C + D
MUL TOS (C + D) * (A + B)
POP X X TOS
• Common types:
—Data transfer(Data movement)
—Arithmetic
—Logical
—Input/output
—System control
Data Transfer
• Copy values from one location to another
(e.g. MOV, PUSH/POP)
MOV destination, source
Destination: can be register or memory location
Source: can be register, memory location or an
immediate number
place the value 20 in CX register
e.g. MOV CX, 20
(CX←20)
MOV CX, [20]
copy value at memory location 20 to CX
Data Transfer…cntd
• PUSH source
• Used to transfer data to stack
Source: can be register or memory location
e.g. PUSH CX copy CX to stack
Data Transfer…cntd
• POP destination
• Used to retrieve data from stack
Destination: can be register or memory location
e.g. POP BX copy data on top of stack to register BX
Arithmetic
• (e.g. ADD, INC, SUB, DEC,MUL,DIV)
ADD destination, source
Destination: can be register or memory location
Source: can be register, memory location or an
immediate number
e.g. ADD CX, BX (CX←CX+BX)
Arithmetic…cntd
• DEC destination
Destination: can be register or memory location
e.g. DEC CX (CX←CX-1)
• MUL source
Source: can be register or memory location
Destination is an accumulator register, AX
e.g. MUL BL (AX←AL x BL)
Logical
• Operate on a bit-by-bit basis
(e.g. AND, OR, XOR, NOT)
e.g. AND CX, BX (CX←CX AND BX)
Input/output
• Instructions to read data from an input module
and to write data to an output module
(e.g. IN, OUT)
IN accumulator, port OUT port, accumulator
Port: address of the I/O module (8-bits for 8086)
e.g. IN AL, 60H (read keyboard port)
Transfer of control
• Instructions discussed so far execute sequentially
• Transfer of control instructions change the
sequence of execution (update value of the
program counter (PC))
• Common transfer of control instructions
— Branch (Jump) instructions
— Procedure call instruction
— Skip instructions
Transfer of control…cntd
• Branch (Jump) Instruction
—Has address of next instruction as an operand
– Conditional branch: branch is made if a certain condition
is met
Transfer of control…cntd
• Unconditional branch: branch is made without
any condition
e.g. Jmp target (Jump to target address)
Transfer of control…cntd
• Procedure call Instructions
—Instruct the processor to go and execute an entire
procedure and return
—Procedure:
– Small, self-contained program within a larger program
(like function in high-level languages)
– Allows same piece of code to be used many times
e.g. CALL subroutine …..RET
Transfer of control…cntd
Addressing Modes
• Addressing mode refers to the specification of the location of
data required by an operation
• Consider the following instruction format
Advantage
• Does not require extra memory reference to fetch
the operand
Drawback
• Only a constant can be supplied
• The number of values is limited by the size of the
operand field (e.g. 0 to 255 if the field is 8 bits)
Immediate Addressing Diagram
Direct Addressing
• A memory address is specified in the address field of the
instruction
• Advantage
• Requires only one memory reference
Drawback
• Can address a limited number of memory locations
(relatively smaller address space) (e.g. If the field is 8 bits
the instruction can only access 256 memory locations)
Direct Addressing Diagram
Register Addressing
• Register address is specified in the address field of the instruction
• A n-bit field can specify any one of 2n registers.
• Mostly Operands are located in registers
• Advantage
• Can address larger number of memory locations compared with
direct addressing
Q. If a CPU has 32 registers (16 bits each), how many memory
locations can be addressed using register indirect addressing?
Register Indirect Addressing…Cntd
• EA = [R2]
• Operand is in memory cell pointed to by
contents of register R2
• Large address space ((2n )=(memory
locations)),n= address bits
Register Indirect Addressing Diagram
Displacement Addressing
• Combines direct addressing and register indirect
addressing
• Main memory address is added with a displacement
value to get the effective address in memory
Displacement Addressing…cntd
Known by a variety of names:
Base-register addressing: Address field of instruction
contains displacement value
Displacement Addressing Diagram
Stack Addressing
• An implied addressing that refers to the top of
a stack
• It is implied that the address is contained in
side a stack pointer register
X86 addressing modes