MODULE-2 - Addressing Modes
MODULE-2 - Addressing Modes
and
Architecture
Carl Hamacher, Zvonko Vranesic, Safwat
Zaky,
Computer Organization, 5th
Edition,
Tata McGraw Hill, 2002.
Module-2
Addressing Modes
Generating Memory
Addresses
How to specify the address of branch
target?
Can we give the memory operand
symbolically as X(Ri)
X(Ri): EA = [Ri]+X
Addressing Modes..
The value X defines an offset (also called
a displacement) from this address to
the location where the operand is
found.
Move 20(R0),R1
Add 1000(R1),R2
The constant X may be given either
as an explicit number or as a
symbolic name representing a
numerical value.
If X is shorter than a word, sign-
extension is needed.
Addressing Modes..
Addressing Modes..
Consider a simple example involving a
list of test scores for students taking a
given course.
A four-word memory block comprises a
Add (R1,R2),R0
Addressing Modes..
Base with Index and Offset mode
The effective address is the sum of the
constant X and the contents of registers
specified in the instruction.
X(Ri,Rj): EA = [Ri]+[Rj]+X
Move 20(R0,R1),R2
Add 1000(R1,R2),R0
Addressing Modes..
Relative mode
The effective address is determined by the
Index mode using the program counter in
place of the general-purpose register Ri.
Branch>0 LOOP
X(PC) : EA = [PC] + X
– note that X is a signed number
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.
Addressing Modes..
Autoincrement mode
The 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 item in a
list.
Add (R2)+,R0
The increment is 1 for byte-sized
operands, 2 for 16-bit operands, and 4
Addressing Modes..
Go to Figure
2.25
Addressing Modes..
Autodecrement mode
The contents of a register specified in the
instruction are first automatically
decremented and are then used as the
effective address of the operand.
Add -(R2),R0
The decrement is 1 for byte-sized operands, 2
for 16- bit operands, and 4 for 32-bit
operands.
Assembly Language
Assembly Language
Machine instructions are
represented by patterns of 0s and
1s.
Awkward to deal with when discussing or
preparing programs.
We use symbolic names
Assembly language – A complete set of
symbolic names and rules for their use
Syntax - The set of rules for using the
destination operand
Assembly Language..
The assembly language must indicate
which mode is being used.
For example
A numerical value or a name used by itself,
such as SUM in the preceding instruction,
may be used to denote the Absolute mode.
The sharp sign usually denotes an
immediate operand.
ADD #5,R3
Assembly Language..
In some assembly languages, the
intended addressing mode is indicated
in the OP-code mnemonic.
In this case, a given instruction has
different OP- code mnemonics for
different addressing modes.
ADDI 5,R3
Indirect addressing is usually specified by
putting parentheses around the name or
symbol denoting the pointer to the
operand.
MOVE #5,(R2)
Assembler Directives
Theseare commands used by the
assembler while it translates a source
program into an object program.
SUM EQU 200
Assembler Directives..
Assembler Directives..
Toproduce an object program, an
assembler has to know
How to interpret the names
Where to place the instructions in the
memory
Where to place the data operands in the
memory
Assembler Directives..
Assembler Directives..
EQU (equate) - informs the assembler
that the variable name should be
replaced by the value specified.
In this case, SUM should be replaced by
the value 200 wherever it appears in
the program
ORIGIN - tells the assembler program
where in the memory to place the
data block that follows.
In this case, the location specified
has the address 204
Assembler Directives..
DATAWORD - informs the assembler
that the data value specified is to be
placed in the memory word
In this case, the data value 100 is to be
placed in the memory word at address
204.
Assembler Directives..
Any statement that results in
instructions or data being placed in a
memory location may be given a
memory address label.
The label is assigned a value equal
similar way.
The processor waits for a signal indicating
that a character key has been struck and
that its code is available in some buffer
register associated with the keyboard.
Program-Controlled I/O
Example..
Bus
Processor
DATAIN DATAOUT
SIN SOUT
- Registers
- Flags K eyboard Display
- Device
interface
Figure 2.19 Bus connection for processor, keyboard, and
display.
Program-Controlled I/O
Example..
The keyboard and the display are
separate devices as shown in Figure
2.19.
The action of striking a key on the
MoveByte DATAlN,R1
MoveByte R1,DATAOUT
Program-Controlled I/O
Example..
Let us assume that bit b3 in registers
INSTATUS and OUTSTATUS corresponds to
SIN and SOUT, respectively.
The read operation may be implemented
READWAIT
MoveByte
DATAIN, R1
Program-Controlled I/O
Example..
Program-Controlled I/O
Example..
Assumption – the initial state of SIN is 0
and the initial state of SOUT is 1.
Any drawback of this mechanism in
terms of efficiency?
Two wait loopsprocessor execution time
is wasted
Alternate solution?
Interrupt
Stacks and Queues
Stacks
A stack is a list of data elements, usually words or
bytes, with the accessing restriction that
elements can be added or removed at one end
of the list only.
This end is called the top of the stack, and the other end
is called the bottom.
The structure is sometimes referred to as a
pushdown stack.
Last-In-First-Out (LIFO) stack - the last data item
placed on the stack is the first one removed
when retrieval begins.
The terms push and pop are used to describe
placing a new item on the stack and removing
Stacks..
Assume that the first element is placed in
location BOTTOM, and when new
elements are pushed onto the stack,
they are placed in successively lower
address locations.
Figure 2.21 shows a stack of word data
Subtract #4,SP
Move NEWITEM,(SP)
Or using autodecrement mode as
Move NEWITEM,-(SP)
Stacks..
Assuming 32-bit word length, the POP
operation will copy the content from the
stack to the destination and increment
the stack pointer by 4.
Can be implemented as
Move
(SP),ITEM Add
#4,SP
Or using autoincrement
mode as
Stacks..
Stacks..
Stack is usually allocated a fixed
amount of space in the memory.
We must avoid pushing an item on a full
operand.
Queues
A queue is a list of data elements that
works on first-in-first-out (FIFO) basis.
Data are stored in and retrieved from a
subroutine.
For example, a subroutine may
evaluate the sine function or sort a
list of values into increasing or
decreasing order.
Subroutines..
To save space, only one copy of the
instructions of the subroutine is placed in
the memory, and any program that
requires the use of the subroutine simply
branches to its starting location.
When a program branches to a
subroutine, we say that it is calling the
subroutine.
The instruction that performs this
Refer Figure
2.16
Parameter Passing..
Figure 2.25 shows how the program in
Figure
2.16 for adding a list of numbers
can be implemented as a
subroutine, with the parameters
passed through registers.
The size of the list, n, contained in
highly flexible
A stack can handle a large number of
parameters.
Parameter Passing..
Parameter Passing by
Value and by Reference
Note the nature of the two parameters,
NUM1 and n, passed to the subroutines
in previous examples.
Instead of passing the actual list entries,
to be made.
The ASCII code for Z is 01011010,
Instructions
Logical Shift
Arithmetic Shift
Rotate
Instructions
Logical Shifts
Logical shift
Logical shift left (LShiftL)
Logical shift right (LShiftR)
These instructions shift an operand
over a number of bit positions
specified in a count operand
contained in the instruction.
General form
LShiftL
count,dst
LShiftR
count,dst
The count operand may be an
Logical Shifts..
Arithmetic Shifts
Retains the sign of the
number
Arithmetic Shifts
Note:
Shifting a number one bit position to the
left is equivalent to multiplying it by 2;
and shifting it to the right is equivalent
to dividing it by 2.
Rotate Operations
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
product
case: high-
order half in
R(j+1)
Divide Ri,