Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
22 views41 pages

Module 2 Addressing Modes

Uploaded by

leens1806
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views41 pages

Module 2 Addressing Modes

Uploaded by

leens1806
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Module 2

Addressing Modes
Dr. Anupama H
Assistant Professor
Dept. of ECE

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Addressing modes
• The different ways in which the location of an operand is specified in
an instruction

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Implementation of Variables
• Register mode — The operand is the contents of a processor register; the name (address) of the register is
given in the instruction.
Add R1, R2
• Absolute or Direct mode — The operand is in a memory location; the address of this location is given
explicitly in the instruction.
Load LOC
Integer A, B; represent global variables
• This High level pgm instruction causes the compiler to allocate a memory location to each of the variables A
and B.
Move LOC,R2
• uses these two modes. Processor registers are used as temporary storage locations where the data in a
register are accessed using the Register mode.

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Representation of constants
• Immediate mode — The operand is given explicitly in the instruction.
• Address and data constants can be represented in assembly language using the Immediate mode
• used to specify the value of a source operand.
• Move #200,R0 ; places the value 200 in register R0
• HLP,
A=B+6
Assuming that A and B have been declared as variables and using the Absolute mode, this
statement may be compiled as follows:
Move B,R1
Add #6,R1
Move R1,A
• Constants are used in assembly language to increment a counter, test for some bit pattern, and so
on.

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Indirection and Pointers
• The instruction does not give the operand or its address explicitly.
• Instead, it provides information from which the memory address of the operand can be
determined. This address is known as the effective address (EA) of the operand.
• Indirect mode - The effective address of the operand is the contents of a register or memory
location whose address appears in the instruction.
• For the Add instruction, the processor uses the value B, which is in register R1, as the effective
address of the operand.
• It requests a read operation from the memory to read the contents of location B. The value read
is the desired operand, which the processor adds to the contents of register R0.
• Indirect addressing through a memory location is also possible. In this case, the processor first
reads the contents of memory location A, then requests a second read operation using the value
B as an address to obtain the operand.
• The register or memory location that contains the address of an operand is called a pointer

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Add (R2),R0
• fetches the operand at location NUM1 and adds it to R0.
• The second Add instruction adds 4 to the contents of the pointer R2, so that it will contain the
address value NUM2 when the above instruction is executed in the second pass through the loop.
• Consider the C-language statement
A = *B;
where B is a pointer variable. This statement may be compiled into
Move B,R1
Move (R1),A

• Using indirect addressing through memory, the same action can be achieved with
Move (B),A

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Indexing and Arrays
• useful in dealing with lists and arrays
• Index mode — The effective address of the operand is generated by adding a
constant value to the contents of a register
• Register used is referred to as an index register
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, the constant X may be given either as an explicit number or
as a symbolic name representing a numerical value

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
EA (Effective Address) Calculation - Problem
Register R1 and R2 of a computer contain the values 1200 and 4600. What is the EA
of the memory operand in each of the following.
a) Load 20(R1), R5
EA = 20 + [R1] = 20 + 1200= 1400
b) Store R5, 30( R1, R2)
EA = 30 + [R1] + [R2] = 30 + 1200 + 4600 = 5830
c) Add –(R2), R5
EA= 4596
d) Subtract (R1)+, R5
EA = 1200

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Two ways of using the Index mode
• The index Register, RI, contains the address of a memory location, and
the value X defines an offset (also called a displacement) from this
address to the location where the operand is found
• The constant X corresponds to a memory address, and the contents
of the index register define the offset to the operand
• In either case, the effective address is the sum of two values; one is
given explicitly in the instruction, and the other is stored in a register.

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Simple example involving a list of test scores
for students taking a given course
• Assume LIST- list of scores, a four-word memory block comprises a record that stores the relevant
information for each student.
• Each record consists of the student’s identification number (ID), followed by the scores the
student earned on three tests.
• There are n students in the class, and the value n is stored in location N immediately in front of
the list, the word length is 32 bits.
• List represents a two-dimensional array having n rows and four columns. Each row contains the
entries for one student, and the columns give the IDs and test scores.

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Program - Compute the sum of all scores obtained on each of the tests and store
these three sums in memory locations SUM1, SUM2, and SUM3

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
• On the first iteration through the loop, test scores of the first student are added to the running
sums held in registers Rl, R2, and R3
• The scores are accessed using the Index addressing modes 4(R0), 8(R0), and 12(R0). The index
register R0 is then incremented by 16 to point to the ID location of the second student.
• Register R4, initialized to contain the value n, is decremented by 1 at the end of each pass
through the loop. When the contents of R4 reach 0, all student records have been accessed, and
the loop terminates.
• The last three instructions transfer the accumulated sums from registers Rl, R2, and R3, into
memory locations SUM1, SUM2, and SUM3, respectively.
• Index mode facilitates access to an operand whose location is defined relative to a reference
point within the data structure in which the operand appears
• ID locations of successive student records are the reference points, and the test scores are the
operands accessed by the Index addressing mode.

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
• second register may be used to contain the offset X,
(Ri,Rj)
• The effective address is the sum of the contents of registers Ri and Rj. The second register is
called the base register.
• Index mode uses two registers plus a constant, denoted as
X(Ri,Rj)
• The effective address is the sum of the constant X and the contents of registers Ri and Rj.
• implements three-dimensional array

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Relative Addressing
• The program counter, PC, is used instead of a general- purpose register.
• X(PC) can be used to address a memory location that is X bytes away from the location presently pointed to by the
program counter.
• Addressed location is identified “relative” to the program counter, which always identifies the current execution point in a
program, the name Relative mode is associated with this type of addressing.
• Relative mode — The effective address is determined by the Index mode using the program counter in place of the
general-purpose register Ri.
• This mode can be used to access data operands. But, its most common use is to specify the target address in branch
instructions.
Branch>0 LOOP
• causes program execution to go to the branch target location identified by the name LOOP if the branch condition is
satisfied.
• This location can be computed by specifying it as an offset from the current value of the program counter.
• Since the branch target may be either before or after the branch instruction, the offset is given as a signed number.
• Assume that the four instructions of the loop body, starting at LOOP, are located at memory locations 1000, 1004, 1008,
and 1012. Hence, the updated contents of the PC at the time the branch target address is generated will be 1016. To
branch to location LOOP (1000), the offset value needed is X = -16. i.e -16(PC)
Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
• 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.
(Ri)+
• 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.
-(Ri)

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Assembly Language
• A complete set of symbolic names and rules for their use constitute a programming language
• The set of rules for using the mnemonics in the specification of complete instructions and programs is called the syntax of
the language.
• Programs written in an assembly language can be automatically translated into a sequence of machine instructions by a
program called an assembler
• The user program in its original alphanumeric text format is called a source program, and the assembled machine
language program is called an object program.
MOVE R0,SUM
• mnemonic MOVE represents the binary pattern, or OP code.
• The assembler translates this mnemonic into the binary OP code that the computer understands.
Ex: ADD #5,R3
ADDI 5,R3
• The suffix I in the mnemonic ADDI states that the source operand is given in the Immediate addressing mode
Ex: MOVE #5,(R2) MOVEI 5,(R2)

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Assembler Directives
• Assembler directives (or commands) are instructions that tell the assembler how to handle a program and its data, and
how to arrange them in memory. They are not translated into machine code and do not represent instructions
• Commands to the assembler that direct the assembly process
Ex: SUM EQU 200
• not an instruction that will be executed when the object program is run, doesn’t even appear in the object program
• Informs the assembler that the name SUM should be replaced by the value 200 wherever it appears in the program.
• EQU – assigns a label with a value or symbol
• ORIGIN- -tells the assembler program where in the memory to place the data block that follows i.e address 204
• DATAWORD – reserves the number of memory words
• The RESERVE directive declares that a memory block of 400 bytes is to be reserved for data
• The second ORIGIN directive specifies that the instructions of the object program are to be loaded in the memory starting
at address 100.
• END - the end of the source program text
• START- is the address of the location at which execution of the program is to begin.
• RETURN - identifies the point at which execution of the program should be terminated.
Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
• Assembly languages statements in a source program written in the form
Label Operation Operand(s) Comment
• These four fields are separated by an appropriate delimiter, typically one or more blank characters.
• The Label is an optional name associated with the memory address. Labels may also be associated with
addresses of data items. In Figure there are five labels- SUM, N, NUM1, START, and LOOP.
• The Operation contains the OP-code mnemonic of the desired instruction or assembler directive.
• The Operand contains addressing information for accessing one or more operands, depending on the type of
instruction.
• The Comment is ignored by the assembler program. It is used for documentation purposes to make the
program easier to understand.

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Assembly and Execution of Programs
• A source program written in an assembly language must be assembled into a machine language object
program before it can be executed.
• Done by the assembler program, replaces all symbols denoting operations and addressing modes with the
binary codes used in machine instructions, and replaces all names and labels with their actual values.
• The assembler assigns addresses to instructions and data blocks, starting at the address given in the ORIGIN
assembler directives.
• also inserts constants given in DATAWORD commands and reserves memory space as requested by RESERVE
commands.
• Replace the names -
• 1 : EQU - value of a name is specified
• 2. name defined in the Label field - the value represented by the name is determined by the location of this
instruction in the assembled object program. For example, the names START and LOOP will be assigned the
values 100 and 112, respectively
• 3, Branch instruction - The assembler computes the branch offset, which is the distance to the target, and
puts it into the machine instruction using relative addressing mode

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Two-pass assembler
• As the assembler scans through a source program, it keeps track of all names and the numerical values that
correspond to them in a symbol table. Thus, when a name appears a second time, it is replaced with its
value from the table
• During the first pass - creates a complete symbol table. At the end of this pass, all names will have been
assigned numerical values.
• When name appears as an operand before its is given forward branch, the assembler then goes through the
source program a second time and substitutes values for all names from the symbol table. Such an
assembler is called a two-pass assembler
• Loader program - performs a sequence of input operations needed to transfer the machine language
program from the disk into a specified place in the memory
• Debugger program

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Number Notation
• Decimal number:
ADD #93,R1
• Binary number: percent sign
ADD #%01011101 ,R1

• Hexadecimal : Dollar symbol


ADD #$5D,R1

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Basic Input/Output Operations
• The difference in speed between the processor and I/O devices
creates the need for mechanisms to synchronize the transfer of data
between them

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Program-controlled I/O & Memory mapped
I/O
• Both are ways to control input/output (I/O) devices
• Programmed I/O - the CPU periodically checks the status of I/O devices.
• requires continuous involvement of the processor in the I/O activities
• Waste of the CPU's time,
• especially when waiting for slower I/O operations to complete
• Memory-mapped I/O (MMIO) - This method uses memory read and write instructions to control
I/O devices, and assigns special memory addresses to them. This allows the CPU to communicate
directly with devices without needing extra I/O instructions.
• MMIO has several advantages over other I/O methods, including:
• doesn't require a system call
• has almost zero overhead for data in memory
• provides faster and more direct access to I/O device

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Using Programmed I/O
• Consider moving a character code from the keyboard to the processor. Striking a key stores the corresponding character
code in an 8-bit buffer register DATAIN. Status control flag SIN is set to 1 after data is stored.
• When SIN is set to 1, the processor reads the contents of DATAIN. When the character is transferred to the processor, SIN
is automatically cleared to 0. If a second character is entered at the keyboard, SIN is again set to 1 and the process repeats.
• Similarly, when the display is ready to receive a character, SOUT equals 1. The processor monitors SOUT, and when
SOUT=1, the processor transfers a character code to DATAOUT. This transfer clears SOUT to 0; when the display device is
ready to receive a second character, SOUT is again set to 1.
• The buffer registers DATAIN and DATAOUT and the status flags SIN and SOUT are known as a device interface. The circuitry
for each device is connected to the processor via a bus
• To perform I/O transfers, machine instructions have to check the state of the status flags and transfer data between the
processor and the I/O device. The processor monitors the keyboard status flag SIN and transfer a character from DATAIN to
register R1 and for transferring output to the display
READWAIT Branch to READWAIT if SIN = 0
Input from DATAIN to R1
WRITEWAIT Branch to WRITEWAIT if SOUT = 0
Output from R1 to DATAOUT
Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Memory Mapped I/O
• Data can be transferred between the buffer registers and the processor
using instructions Move, Load, or Store.
• The contents of the keyboard character buffer DATAIN can be transferred to
register R1 and from R1 to DATAOUT in the processor by the instruction
MoveByte DATAIN,R1
MoveByte R1,DATAOUT
• Addressing buffer registers as two memory locations
• Assume that bit b3 in registers INSTATUS and OUTSTATUS (Device status
registers) corresponds to SIN and SOUT, respectively.

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
MIMO Read and Write operation
• Read a line of characters typed at a keyboard and send them out to a display device. The program
finishes when the carriage return character, CR, is read, stored, and sent to the display
READWAIT Testbit #3,INSTATUS
Branch=0 READWAIT
MoveByte DATAIN,R1

WRITEWAIT Testbit #3,OUTSTATUS


Branch=0 WRITEWAIT
MoveByte R1,DATAOUT

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Stacks and Queues
• A stack is a linear data structure that follows the Last In, First Out
(LIFO) principle.
• Elements can be added or removed at one end, this end is called the
top of the stack, and the other end is called the bottom
• Push: Adds an element to the top of the stack.
• Pop: Removes and returns the top element of the stack.
• Stack Pointer points to top of stack , a register
• A queue is a linear data structure that follows the First In, First Out
(FIFO) principle
Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Subtract #4,SP
Move NEWITEM,(SP)

Move (SP),ITEM
Add #4,SP
• Using the Autoincrement and Autodecrement addressing modes, then the push operation can be performed
by the single instruction
Move NEWITEM,-(SP)
Move (SP)+,ITEM
Queues –
• Both ends of a queue move to higher addresses as data are added at the back and removed from the front.
• two pointers needed to keep track of the two ends of the queue.
• continuously move through the memory in the direction of higher addresses.
• One way to limit the queue to a fixed region in memory is to use a circular buffer.
• The memory addresses from BEGINNING to END are assigned to the queue. The first entry in the queue is
entered into location BEGINNING, and successive entries are appended till the time the back of the queue
reaches END
• Space will have been created at the beginning if some items have been removed.
• The back pointer is reset to the value BEGINNING and the process continues.
Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Subroutines
• perform a particular subtask many times on different data values
• Block of instructions
• Ex: evaluate the sine function, sort a list of values into increasing or decreasing order
• only one copy of the instructions that constitute the subroutine is placed in the memory,
• Program that requires use of the subroutine simply branches to its starting location – calling the
subroutine.
• Subroutine linkage method - The way in which a computer makes it possible to call and return
from subroutines.
• Link register - saves the return address.
• Call instruction, a special branch instruction performs
• Store the contents of the PC in the link register
• Branch to the target address specified by the instruction
• The Return instruction is a special branch instruction that performs:
• Branch to the address contained in the link register

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Subroutine Nesting and the Processor Stack
• One subroutine call another
• Return address of the second call is also stored in the link register, destroying its previous
contents.
• Essential to save the contents of the link register in some other location before calling another
subroutine.
• Else the return address of the first subroutine will be lost.
• Solution - return addresses associated with subroutine calls should be pushed onto a stack
• The stack pointer points to the processor stack
• The Call instruction pushes the contents of the PC onto the processor stack and loads the
subroutine address into the PC.
• The Return instruction pops the return address from the processor stack into the PC.

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Parameter Passing
• Exchange of information between a calling program and a subroutine
• program must provide the parameters to the subroutine - the operands or their addresses to be used in the computation.
• Subroutine returns the results of the computation.
• Parameter passing may be accomplished in several ways –
• The parameters may be placed in registers or in memory locations,
• The parameters may be placed on the processor stack used for saving the return address.

• Adding a list of numbers can be implemented as a subroutine, with the parameters passed through registers.
• n- The size of the list contained in memory location N,
• NUM1 - address of the first number are passed through registers R1 and R2.
• The sum computed by the subroutine is passed back to the calling program through register R0.
• The first two instructions load n and NUM1 into R1 and R2.
• The Call instruction branches to the subroutine starting at location LISTADD.
• This instruction also pushes the return address onto the processor stack.
• The subroutine computes the sum and places it in R0.
• After the return operation is performed by the subroutine, the sum is stored in memory location SUM by the calling
program. Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT
Parameter Passing by Value and by Reference

Dr. Anupama H
Assistant Professor, Dept. of ECE, BIT

You might also like