Sowmya K P Asst. Professor Dept of Ise Citech Subject Code 15Cs34
Sowmya K P Asst. Professor Dept of Ise Citech Subject Code 15Cs34
Subject
MODULE 1: Basic structure of computer &
Machine instructions and programs
Subject
Functional Units
Arithmetic
Input and
logic
Memory
Output Control
I/O Processor
MAR MDR
Control
PC R0
R1
Processor
IR
ALU
Rn - 1
n general purpose
registers
• Its output is available for the control circuits which generates the
timing signals that control the various processing elements in one
execution of instruction.
• Single-bus
Performance
• The most important measure of a computer is how quickly it can
execute programs.
• Three factors affect performance:
• Hardware design
• Instruction set
• Compiler
• The total time required to execute the program is elapsed time is a
measure of the performance of the entire computer system. It is
affected by the speed of the processor, the disk and the printer.
• The time needed to execute a instruction is called the processor
time.
• Processor time to execute a program depends on the hardware
involved in the execution of individual machine instructions
Performance
• The figure which includes the cache memory as part of the processor
unit
Main Cache
memory memory Processor
Bus
• R – clock rate
N S
T
R
How to improve T?
Clock Rate
• These are two possibilities for increasing the clock rate
‘R’.
1. Improving the IC technology makes logical circuit
faster, which reduces the time of execution of basic
steps. This allows the clock period P, to be reduced and
the clock rate R to be increased.
2. Reducing the amount of processing done in one
basic step also makes it possible to reduce the clock
period P. however if the actions that have to be
performed by an instructions remain the same, the
number of basic steps needed may increase.
Performance Measurement
• The performance measure is the time taken by the computer to execute a
given bench mark. Initially some attempts were made to create artificial
programs that could be used as bench mark programs
i 1
This is repeated for all the programs in the SPEC suit, and the
geometric mean of the result is computed.
Machine instructions and programs : Objectives
b 31 b 30 b1 b0
•
•
•
Sign bit: b 31= 0 for positive numbers
b 31= 1 for negative numbers
Word
address Byte address Byte address
0 0 1 2 3 0 3 2 1 0
4 4 5 6 7 4 7 6 5 4
• •
• •
• •
k k k k k k k k k k
2 -4 2 -4 2 -3 2 - 2 2 - 1 2 - 4 2 - 1 2 - 2 2 -3 2 -4
• Single Accumulator
– Result usually goes to the Accumulator
– Accumulator has to be saved to memory quite
often
• General Register
– Registers hold operands thus reduce memory
traffic
– Register bookkeeping
• Stack
– Operands and result are always in the stack
Instruction Formats
• Three-Address Instructions
– ADD R1, R2, R3 R1 ← R2 + R3
• Two-Address Instructions
– ADD R1, R2 R1 ← R1 + R2
• One-Address Instructions
– ADD M AC ← AC + M[AR]
• Zero-Address Instructions
– ADD TOS ← TOS + (TOS – 1)
• RISC Instructions
– Lots of registers. Memory is restricted to Load & Store
Address Contents
i
Assumptions:
Begin execution here Move A,R0
3-instruction - One memory operand
i+4 Add B,R0 program
segment per instruction
i+8 Move R0,C
- 32-bit word length
- Memory is byte
addressable
A - Full memory address
can be directly specified
in a single-word instruction
B Data for
the program
Two-phase procedure
-Instruction fetch
-Instruction execute
C
Page 43
•
•
•
i + 4n - 4 Add NUM n,R0
i + 4n Move R0,SUM
•
•
•
SUM
NUM1
NUM2
•
•
•
NUM n
Branching LOOP
Clear R0
Determine address of
"Next" number and add
Program "Next" number to R0
loop
Decrement R1
Branch>0 LOOP
Branch target
Move R0,SUM
Conditional branch
•
•
•
SUM
N n
NUM1
•
•
•
NUM n
Condition Codes
• Condition code flags
• Condition code register / status register
• N (negative)
• Z (zero)
• V (overflow)
• C (carry)
• Different instructions affect different flags
Conditional Branch Instructions
A: 11110000
• Example:
– A: 1 1 1 1 0 0 0 0 +(−B): 1 1 1 0 1 1 0 0
– B: 0 0 0 1 0 1 0 0 11011100
C=1 Z=0
S=1
V=0
Status Bits
Cn-1
A B
Cn ALU
F
V Z S C
Fn-1
Zero Check
Addressing Modes
Opcode Mode
...
CHAPTER 2: Machine instructions and programs
Subject
Addressing Modes
• Register mode :-The operand is the content of a processor
register; the name(address) is given in the processor.
Ex:- Move R1,R2
• Absolute mode:-The operand is in a memory location; the
address of this location is given explicitly in the instruction.
100
101 0 1 0 4
102
103
104 1 1 0 A
Addressing Modes
• Index mode:- The effective address of the operand is
generated by adding a constant value to the content of a
register
Useful with XR = 2
“Autoincrement” or
“Autodecrement”
+
100
AR = 100
101
Could be Positive or
Negative 102 1 1 0 A
(2’s Complement) 103
104
Addressing Modes
• Relative Address
0
– EA = PC + Relative Addr 1
PC = 2 2
100
AR = 100
101
102 1 1 0 A
Could be Positive or 103
Negative 104
(2’s Complement)
Additional Modes
• Auto increment mode :-The effective address of
the operand is the content of a register specified
in the instruction determined. After accessing
the operand, the contents of this registers are
automatically incremented to point to the next
item in a list.
• Ex:- (Ri)+
Move N,R1
Move #NUM1,R2 Initialization
Clear R0
LOOP Add (R2)+,R0
Decrement R1
Branch>0 LOOP
Move R0,SUM
Figure 2.16. The Autoincrement addressing mode used in the program of Figure 2.12.
Addressing Modes
Name Assem bler syn tax Addressing function
Indirect (R i ) EA = [R i ]
(LOC) EA = [LOC]
Index X(R i) EA = [R i ] + X
Autoincremen t (R i )+ EA = [R i ] ;
Incremen t R i
Autodecrement (R i ) Decremen t R i ;
EA = [R i]
Assembly Language
• Machine instructions are represented by patterns of 0’s
and 1’s –awkward to deal
• So we use symbolic names to represent the patterns. so
far we have used normal words, such as Move, Add,
and Branch, for the instruction operation to represent
the corresponding binary code patterns.
• When writing programs for a specific computer, such
words are normally replaced by acronyms called
mnemonics, such as MOV,ADD,BR .similarly we use the
notation R3 to refer to register 3 and LOC to refer to a
memory location.
• A complete set of names and rules for their use
constitutes a programming language, generally referred
to as an assembly language
Assembly Language
• The set of rules for using the mnemonics in
the specification of complete instructions and
programs is called the syntax of the language.
Assembly Language : Types of Instructions
• Data Transfer Instructions
Data value is not
modified
Name Mnemonic
Load LD
Store ST
Move MOV
Exchange XCH
Input IN
Output OUT
Push PUSH
Pop POP
Assembler DIrectives
• In addition to provide a mechanism for representing
instructions in a program.
• The assembly language allows the programmer to specify
other information needed to translate the source program into
object program.
• Suppose that the name SUM is used to represent the value
200. This can be conveyed to the assembler through a
statement as
SUM EQU 200
This statement simply informs the assembler that the SUM
should be replaced by the value 200 where ever it appears in
the program.
- Registers
- Flags
- Device interface
Program-Controlled I/O Example
The keyboard and the display are separate device as shown in fig. the
action of striking a key on the keyboard does not automatically cause
the corresponding character to be displayed on the screen.
The open end is called top of the stack(TOS),and the fixed end is
bottom. We use the term Push and POP to denote the operations on
the stack.
When elements are pushed into the stack they are placed in
successively lower address locations. Thus the stack grows in the
direction of decreasing memory address
Stack : Stack Organization
Current
Top of Stack
• LIFO TOS 0
Last In First Out 1
2
3
4
5
SP 6 0 1 2 3
7 0 0 5 5
FULL EMPTY 8 0 0 0 8
9 0 0 2 5
Stack Bottom 10 0 0 1 5
Stack
Stack Organization
Current 1 6 9 0
Top of Stack
• PUSH TOS 0
SP ← SP – 1 1
2
M[SP] ← DR 3
If (SP = 0) then (FULL ← 1) 4
5 1 6 9 0
EMPTY ← 0
SP 6 0 1 2 3
7 0 0 5 5
FULL EMPTY 8 0 0 0 8
9 0 0 2 5
Stack Bottom 10 0 0 1 5
Stack
Stack Organization
Current
Top of Stack
• POP TOS 0
DR ← M[SP] 1
2
SP ← SP + 1 3
If (SP = 11) then (EMPTY ← 1) 4
5 1 6 9 0
FULL ← 0
SP 6 0 1 2 3
7 0 0 5 5
FULL EMPTY 8 0 0 0 8
9 0 0 2 5
Stack Bottom 10 0 0 1 5
Stack
Stack Organization
• Memory Stack
0
– PUSH PC
1
SP ← SP – 1 2
M[SP] ← DR
– POP AR 100
101
DR ← M[SP] 102
SP ← SP + 1
200
SP 201
202
Stack organization
• Assume a byte addressable memory and a word length of a
data to be 32-bits
• The push operation places a data item above the current
top of the stack. i.e. the SP is to be decremented before
data can be placed.
• So to transfer a data from a memory location NUM to the
have the following instructions.
• Subtract #4,SP
• Move NUM,(SP)
• To remove a data
• Move (SP),NUM
• Add #4,SP
Reverse Polish Notation
• Infix Notation
A+B
• Prefix or Polish Notation
+AB
• Postfix or Reverse Polish Notation (RPN)
AB+
(2) (4) (3) (3) +
RPN (8) (3) (3) +
AB+CD ABCD+
(8) (9) +
17
Reverse Polish Notation
• Example
(A + B) [C (D + E) + F]
(A B +) (D E +) C F +
Reverse Polish Notation
• Stack Operation
(3) (4) (5) (6) +
PUSH 3
PUSH 4
6
MULT
PUSH 5 5 4 30
PUSH 6
12 3 42
MULT
ADD
STACK:-POP and PUSH
• Suppose that a stack runs from location 2000(BOTTOM) down no
further location 1500. The stack pointer is loaded initially with the
address value 2004.
• SP is decremented by 4 before new data are stored onto the stack,
hence a initial value of 2004 means that the first item pushed onto
the stack will be at location 2000.
• To prevent either pushing an item on full stack or popping an item
off an empty stack, the single-instruction push and pop operations
can be replaced by the instruction sequences
• The compare instruction
• Move (SP)+,ITEM.
• Move NEWITEM,-(SP).
Subroutines
• In a given program, it is often necessary to perform
a particular subtask many times on different data-
values. Such a subtask is usually called a
subroutine.
For example,
• a subroutine may evaluate the sine function or sort
a list of values into increasing or decreasing order.
• It is possible to include the block of instructions
that constitute a subroutine at every place where it
is needed in the program. However, to save space,
only one copy of the instructions that constitute
the subroutine is placed in the memory, and any
program that requires the use of the subroutine
simply branches to its starting location..
Subroutines
• When a program branches to a subroutine we say that it
is calling the subroutine. The instruction that performs
this branch operation is named a Call instruction
• Call ARRAYADD
• Move R0,SUM
• ….….
• END
• ARRAYADD Add (R2)+,R0 Subroutine
• Decrement R1
• Branch>0 ARRAYADD
• Return
Stack Frame
During execution of the subroutine, six
locations at the top of the stack contain entries
that are needed by the subroutine.
These locations constitute a private workspace
for the subroutine, created at the time the
subroutine is entered and freed up when the
subroutine returns control to the calling program.
Such space is called a stack frame .
Stack Frame
•
• Move #NUM1,-(SP)
• Move N,-(SP)
Calling (main)
• Call ARRAYADD Program
• Move 4(SP),SUM
• Add #8,SP
• ….….
• END
Stack Frame
• ARRAYADD Move Multiple R0-R2,-(SP)
• Move 16(SP),R1
• Move 20(SP),R2
•
• Clear R0
Subroutine
• BACK Add (R2)+,R0
• Decrement R1
• Branch>0 LOOP
• Move R0,20(SP)
• Move Multiple (SP)+,R0-R2
• Return
Stack Frame
• In addition to the stack pointer SP, it is useful to have
another pointer register, called the Frame pointer (FP), for
convenient access to the parameters passed to the
subroutine and to the local memory variables used by the
subroutine
• These local variables are only used within the subroutine,
so it is appropriate to allocate space for them in the stack
frame associated with the subroutine. We assume that four
parameters are passed to the subroutine, three local
variables are used within the subroutine, and registers R0
and R1 need to be saved because they will also be used
within the subroutine.
• The pointers SP and FP are manipulated as the stack frame
is built, used, and dismantled for a particular of the
subroutine
Stack Frame
• We begin by assuming that SP point to the old top-of-stack
(TOS) element in fig b. Before the subroutine is called, the
calling program pushes the four parameters onto the stack.
before: 0 0 1 1 1 0 . . . 0 1 1
after: 1 1 1 0 . . . 0 1 1 0 0
0 R0 C
before: 0 1 1 1 0 . . . 0 1 1 0
after: 0 0 0 1 1 1 0 . . . 0 1
R0 C
before: 1 0 0 1 1 . . . 0 1 0 0
after: 1 1 1 0 0 1 1 . . . 0 1
c Ro
before 1 1 0 1 0 1 1
after 0 1 1 0 1 1 0
AshiftL #1,R0
Rotate Instructions
• In the shift operations, the bits shifted out of the operand are lost,
except for the last bit shifted out which is retained in the Carry flag C.
• To preserve all bits, a set of rotate instructions can be used. They move
the bits that are shifted out of one end of the operand back into the
other end.
• Two versions of both the left and right rotate are usually provided. In
one version, the bits of the operand are simply rotated. In the other
version, the rotation includes the C flag instructions.
• When carry flag is not involved in the rotation it contains the last bit
shifted out of the register.
• All four possibilities of rotate instructions are as shown
C R0
before: 0 0 1 1 1 0 . . . 0 1 1
after: 1 1 1 0 . . . 0 1 1 0 1
C R0
before: 0 0 1 1 1 0 . . . 0 1 1
after: 1 1 1 0 . . . 0 1 1 0 0
R0 C
before: 0 1 1 1 0 . . . 0 1 1 0
after: 1 1 0 1 1 1 0 . . . 0 1
R0 C
before: 0 1 1 1 0 . . . 0 1 1 0
after: 1 0 0 1 1 1 0 . . . 0 1