DDCO CO 01 Part 2
DDCO CO 01 Part 2
Programs
Objectives
Machine instructions and program execution,
including branching and subroutine call and return
operations.
Addressing methods for accessing register and
memory operands.
Assembly language for representing machine
instructions, data, and programs.
Program-controlled Input/Output operations.
Memory Locations,
Addresses, and Operations
Memory Location, Addresses, and Operation
n bits
first word
Memory consists of second word
many millions of
storage cells, each of •
•
which can store 1 bit •
Data is usually i th word
accessed in n-bit
groups •
•
n is called word •
. Memory words.
Memory Example
Memory Location, Addresses, and Operation
32-bit word length example
32 bits
b 31 b 30 b1 b0
•
•
•
Sign bit: b 31= 0 for positive numbers
b 31= 1 for negative numbers
K bit Address
2k combinations
Address locations 0 to 2k-1
Memory Location, Addresses, and Operation
It is impractical to assign distinct addresses to
individual bit locations in the memory.
The most practical assignment is to have successive
addresses refer to successive byte locations in the
memory – byte-addressable memory.
Byte locations have addresses 0, 1, 2, …
If word length is 16 bits, they successive words are
located at addresses 0, 2, 4,…
If word length is 32 bits, they successive words are
located at addresses 0, 4, 8,…
Byte and word address
Address Data Address Data Address Data
0 0 0
1 2 4
2 4 8
3 6
4 Word length: 4 bytes
2 43 J 74
3 45 N 78
C 43
4
E 45
5
6
7
Byte and word address
Word Word
Address Data Address Data Character ASCII
0 78 74 0 74 78 J 74
Word length 2 45 43 2 43 45 N 78
2 bytes C 43
4 4
E 45
0 74 (J) 0 78 (N)
Word length 1 78 (N) 1 74 (J)
2 bytes NJ
JN 2 43 (C) 2 45 (E) EE
CE 3 45 (E) 3 43 (C)
4 4
Big-Endian and Little-Endian
Assignments
Two ways
byte
address
are
assigned
across
words
Big-Endian and Little-Endian
Assignments
Big-Endian: lower byte addresses are used for the most significant bytes of the word
Little-Endian: opposite ordering. lower byte addresses are used for the less significant bytes of the word
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
2k- 4 2k- 4 2k- 3 2k- 2 2k- 1 2 - 4 2- 1 2 - 2 2 -3 2 -4
General format:
Operation operand(s)
Three address instruction
Memory
A 40
C=A+B B 30
C
C ← [A]+[B]
10
Format: A
Format:
A 40
B 30
Add A,C B
C
30
10
Instruction:
Load A
Add B Example
Accumulator : 100
Store C
CPU Organization
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
Stack
Operands and result are always in the stack
Restrictions on operations
Arithmetic operations allowed only in Registers
Instructions:
Move A, Ri
Move B, Rj
Add Ri, Rj
Store Rj,C
Restrictions on operations
Oneoperand can be in the memory another
operand in the register
Move A, Ri
Add B, Ri
Store Ri,C
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[ADRS]
Zero-Address Instructions
ADD ;TOS ← TOS + (TOS – 1)
B Data for
the program
Two-phase procedure
Instruction fetch
Instruction execute
C
Branching i+4
i+8
Add NUM2,R0
Add NUM3,R0
•
•
i + 4n- 4 Add •
NUMn, Ro
i + 4n Move R0,SUM
•
•
SUM •
NUM1
NUM2
•
•
NUMn •
repeat
sum= sum + NUM(i);
counter=counter-1; Decrement R 1
until counter>0
printf(“%d”,sum);
Move N,R1
Clear R0
Branching LOOP
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
NUM2
•
•
•
NUMn
Figure: Using a loop to add n numbers.
Condition Codes
The processor keeps track of information about
the results of various operations for use by
subsequent conditional branch instructions
Z (zero): set to 1 if the result is 0; otherwise, cleared to 0
V (overflow): set to 1 if arithmetic overflow occurs;
otherwise, cleared to 0
C (carry): set to 1 if a carry-out results from the operation
otherwise, cleared to 0
N and Z flags caused by an arithmetic or a logic operation,
V and C flags caused by an arithmetic operation
Status Bits
Condition Codes - Example
Example: A: 11110000
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
Z=0
C=1
S=1
V=0
A Program for C [A]+
[B]
Addressing Modes
Generating Memory Addresses
Can we give the memory operand address directly
in a single Add instruction in the loop?
The source
operand is data
register R0
25 R0
R1
The Move R0, R1
instruction uses data
registers for both source
and destination operands
Register Addressing
Mo v e R0,R1
25 R0
R1
Mo v e R0,R1
25 R0
25 R1
Also called as “Direct mode” in some assembly
languages
Register and absolute modes can be used to represent
variables
Absolute addressing
Memory
This instruction has a direct
Mo v e LOC ,R2
source operand
LOC 42
R2
Memory
Mo v e LOC ,R2
LOC 42
42 R2
Uses # sign to indicate immediate operand
Move #200, R0
Can be used to represent constants.
Immediate mode
Mo v e #4,R0
R0
R0
Immediate mode
Mo v e #4,R0
R0
Mo v e #4,R0
4 R0
C 50
50
44 R1
Addressing modes (contd..)
Register,Absolute and Immediate modes
contained either the address of the operand or the
operand itself.
Some instructions provide information from which
the memory address of the operand can be
determined
That is, they provide the “Effective Address” of the
operand.
They do not provide the operand or the address of the
operand explicitly.
Effective address (EA)
Indirection and Pointers
Indirectmode: the effective address of the operand
is the contents of a register or memory location
whose address appears in the instruction
Main
memory
B Operand A B
R1 B Register B Operand
•Register R1 contains Address B •Address A contains Address B
•Address B has the operand •Address B has the operand
R1 and A are called as “pointers”
Main
memory
B Operand R1 Operand
R1 B Register B
•Register R1 contains Address B
•Address B has the operand
Main
memory
A B A Operand
B Operand
Move (A),R1
100
101 0104
102
103
104 70
Using Indirect Addressing in a Program
Using Indirect Addressing in a
Program
Effective address (EA)
Indexing and Arrays
Index mode: the effective address of the operand is
generated by adding a constant value to the contents of a
register
The register used may be either a special register
provided for this purpose, or, more commonly, it may be
any one of a set of general purpose registers in the
processor.
It is referred to as an index register
Indexing and Arrays
The index mode is useful in dealing with lists and arrays
Assembly syntax is X(Ri), where X denotes the constant
value contained in the instruction and Ri is the name of
the register involved.
The effective address of the operand is given by EA=X+
[Ri]
The contents of the index register are not changed in the
process of generating the effective address
Addressing modes (contd..)
Effective Address of the operand is generated by adding a constant value to the contents of the
register
R1 1000
100
R1 = 100
101
Could be Positive or
Negative 102 110A
(2’s Complement) 103
104
Indexed Addressing
Indexed Addressing
An Example for Indexed Addressing
LIST+16+4
LIST+16+8
LIST+16+12
LIST+32
Variations of Indexed
Addressing Mode
Another version of the Index mode use two registers plus a constant,
which can be denoted as X(Ri,Rj)
The effective address is the sum of the constant X and the contents of
registers Ri and Rj
This mode implements a three-dimensional array
Effective address
Addressing Modes
(contd..)
Autoincrement mode:
Effective address of the operand is the contents of a
register specified in the instruction.
After accessing the operand, the contents of this
register are automatically incremented to point to the
next consecutive memory location.
Ex. Add (R2)+, R0
Before execution: R2=1000, [1000]=30, R0=10
After execution: R2=1004, [1000]=30, R0= 40
Using Indirect Addressing in a Program
Using Indirect Addressing in a
Program
An Example of Autoincrement
Addressing
Addressing Modes
(contd..)
Autodecrement mode
Effective address of the operand is the contents of a register specified
in the instruction.
Before accessing the operand, the contents of this register are
automatically decremented to point to the previous consecutive
memory location.
Ex. Add -(R2), R0
Before execution: R2=1000, [0996]=30, R0=10
After execution: R2=0996, [0996]=30, R0= 40
•Variation of the Indexing Mode, where the index register is the PC instead of a
general purpose register.
•When the instruction is being executed, the PC holds the address of the next
instruction in the program.
92
Addressing Modes
Relative Address
0
EA = X + [PC] 1
PC = 2 2
100
X = 100
101
102 110A
Could be Positive or 103
Negative 104
(2’s Complement)
Relative Addressing
X(PC) – note that X is a signed number
Branch>0 LOOP
This location is computed by specifying it as an offset from the
current value of PC.
Branch target may be either before or after the branch instruction,
the offset is given as a singed num.
Branch>0 LOOP
-16(PC)
1000
1012
1016
Effective address (EA)
Implicitly the increment and
decrement amounts are 1.
This would allow us to access individual
bytes in a byte addressable memory.
I/O
operations are essential, the way they are
performed can have a significant effect on the
performance of the computer.
Program-Controlled I/O
Example
Read in character input from a keyboard and produce character
output on a display screen.
Bus
Processor
DATAIN DATAOUT
DATAOUT
Alternate solution?
Interrupt