Computer Organization
Unit-2
2.5 ADDRESSING MODE
The different ways in which the location of an operand is specified in an instruction is called as
Addressing mode.
Generic Addressing Modes:
Implementation of Variables and Constants: Variables:
Variables & constants are the simplest data-types and are found in almost every computer
program.
In assembly language, a variable is represented by allocating a register (or memory-location)
to hold its value.
Thus, the value can be changed as needed using appropriate instructions.
The value can be changed as needed using the appropriate instructions. There are 2 accessing
modes to access the variables. They are
Register Mode
Absolute Mode
1.Register Mode:
The operand is the contents of the processor register.
The name (address) of the register is given in the instruction.
Registers are used as temporary storage locations where the data in a register are accessed.
Priyanka H V
Page 1
Computer Organization
For example, the instruction,
Move R1, R2
Copy content of register R1 into register R2
2. Absolute Mode (Direct Mode):
The operand is in a memory location.
The address of this location is given explicitly in the instruction.
For example, the instruction,
Move LOC, R2
Copy content of memory-location LOC into register R2
3. Immediate Addressing Mode:
The operand is specified in the instruction itself.
Move 200immediate, R0
Move #200, R0
The immediate mode is only used to specify the value of a source-operand.
4. INDIRECTION AND POINTERS
It provides information from which the memory-address of the operand can be determined.
this address is refered as the effective address(EA) of the operand.
Indirect Mode
The EA of the operand is the contents of a register (or memory-location) whose address
appears in the instruction.
The register (or memory-location) that contains the address of an operand is called a pointer.
{The indirection is denoted by ( ) sign around the register or memory-location}.
Eg: Add (R1),R0
The operand is in memory. Register R1 gives the effective-address (B) of the operand. T
he data is read from location B and added to contents of register R0
To execute the Add instruction in fig 2.11(a), the processor uses the value which is in register
R1, as the EA 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.
Priyanka H V
Page 2
Computer Organization
Indirect addressing through a memory location is also possible as shown in fig 2.11(b).
In this, 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.
In above program, Register R2 is used as a pointer to the numbers in the list, and the
operands are accessed indirectly through R2.
The initialization-section of the program loads the counter-value n from memory-location N
into R1.
It uses the immediate addressing-mode to place the address value NUM1, which is the
address of the first number in the list, into R2. Then it clears R0 to 0.
Priyanka H V
Page 3
Computer Organization
The first two instructions in the loop implement the unspecified instruction block starting at
LOOP.
The first time through the loop, the instruction 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.
5. Indexing and Arrays:
Index Mode:
The effective address of an operand is generated by adding a constant value to the contents of
a register. The constant value uses either special purpose or general purpose register.
X (RI)
Where X denotes the constant value contained in the instruction, Ri name of the register
involved.
The Effective Address of the operand EA=X + [Ri]
The index register R1 contains the address of a new location and the value of X defines an
offset (also called a displacement).
Fig 2.13 illustrates two ways of using the Index mode.
In fig(a), the index register, R1, 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.
To find operand first go to Reg R1 (using address)-read the content from R1 i.e. 1000
Priyanka H V
Page 4
Computer Organization
Add the content 1000 with offset 20 to get the result.
The sum of two values is given explicitly in the instruction and the other is stored in register.
Add 20(R1), R2
EA=1000+20=1020
In fig(b),the constant X corresponds to a memory address, and the contents of the index
register define the offset to the operand.
To find operand first go to Reg R1 (using address)-read the content from R1 i.e. 20
Add the content 20 offset is added with 1000 to get the result.
Add 1000(R1), R2
EA=1000+20=1020
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
6. Base with Index Mode
Another version of the Index mode uses 2 registers which can be denoted as
(Ri, Rj)
Here, a second register may be used to contain the offset X. The second register is usually
called the base register.
The effective-address of the operand is given by EA= [Ri] + [Rj].
This form of indexed addressing provides more flexibility in accessing operands, because
both components of the effective address can be changed.
Priyanka H V
Page 5
Computer Organization
7. Base with Index & Offset Mode
Another version of the Index mode uses 2 registers plus a constant, which can be denoted as
X (Ri, Rj)
The effective-address of the operand is given by EA=X+[Ri]+[Rj].
This added flexibility is useful in accessing multiple components inside each item in a
record, where the beginning of an item is specified by the (Ri, Rj) part of the addressing-
mode.
This mode implements a 3-dimensional array.
.
8. Relative Addressing:
This is similar to index-mode.The effective address is determined using the PC in place of
the general purpose register Ri.
The operation is indicated as X(PC).
X(PC) denotes an effective-address of the operand which is X locations above or below the
current contents of PC.
Since the addressed-location is identified "relative" to the PC, the name Relative mode is
associated with this type of addressing.
This mode is used commonly in conditional branch instructions.
An instruction such as
Branch > 0 LOOP
Causes program execution to go to the branch target location identified by name LOOP if branch
condition is satisfied
9. Additional Modes:
There are two additional modes. They are
Auto-increment mode
Auto-decrement mode
9.1 Auto-increment mode:
The Effective Address of the operand is the contents of a register in the instruction.
After accessing the operand, the contents of this register is automatically incremented to
point to the next item in the list.
This mode is denoted as
(Ri)+
Priyanka H V
where Ri=pointer register
Page 6
Computer Organization
9.2 Auto-decrement mode:
The Effective Address of the operand is the contents of a register in the instruction.
After accessing the operand, the contents of this register is automatically decremented to
point to the next item in the list.
This mode is denoted as
-(Ri)
where Ri=pointer register
These 2 modes can be used together to implement an important data structure called a stack.
2.6 Assembly Language
A complete set of symbolic names and rules for their use constitute an 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.
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 formal is called a source program, and the
assembled machine language program is called an object program.
LOAD
- To load operand from memory
STORE - To store operand to memory
MOVE - To transfer data from one location to another location/Register
Move instruction is written
MOVE R0,SUM
The mnemonic MOVE represents the binary pattern, or OP code, for the
operation performed by the instruction.
The instruction
Priyanka H V
Page 7
Computer Organization
ADD #5,R3
Adds the number 5 to the contents of register R3 and puts the result back
into register R3.
Assembler Directives:
Directives are the assembler commands. These commands are neither translated into machine
opcode nor assigned any memory location in the object program.
EQU informs the assembler about the value of an identifier (Figure: 2.18).
Ex: SUM EQU 200
This statement informs the assembler that the name SUM should be breplaced by the
value 200 wherever it appears in the program.
ORIGIN tells the assembler about the starting-address of memory-area to place the data
block.
DATAWORD directive tells the assembler to load a value (say 100) into the location (say
204).
Ex: N DATAWORD 100
Priyanka H V
Page 8
Computer Organization
RESERVE directive declares that a memory-block of 400 bytes is to be reserved for data and
that the name NUM1 is to be associated with address 208.
Ex: NUM1 RESERVE 400
END directive tells the assembler that this is the end of the source-program text.
RETURN directive identifies the point at which execution of the program should be
terminated.
Any statement that makes instructions or data being placed in a memory-location may be
given a label.The label(say N or NUM1) is assigned a value equal to the address of that
location.
GENERAL FORMAT OF A STATEMENT
Most assembly languages require statements in a source program to be written in the form:
Label Operation Operands Comment
Label is an optional name associated with the memory-address where the machine language
instruction produced from the statement will be loaded.
The Operation field contains the OP-code mnemonic of the desired instruction or assembler
The Operand field contains addressing information for accessing one or more operands,
depending on the type of instruction.
The Comment field is used for documentation purposes to make the program easier to
understand.
ASSEMBLY AND EXECUTION OF PRGRAMS
Programs written in an assembly language are automatically translated into a sequence of
machine instructions by the assembler.
Assembler program
replaces all symbols denoting operations & addressing-modes with binary-codes used in
machine instructions.
replaces all names and labels with their actual values.
assigns addresses to instructions and data blocks, starting at the address given in the ORIGIN
directive.
inserts constants that may be given in DATAWORD directives.
reserves memory-space as requested by RESERVE directives.
Priyanka H V
Page 9
Computer Organization
As the assembler scans through a source-program, it keeps track of all names of numericalvalues 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. Hence, such an assembler is called a two-pass
assembler.
Two Pass Assembler:
First pass
Work out all the addresses of labels
Second pass
Generate machine code, substituting values for the labels
The assembler stores the object-program on a magnetic-disk. The object-program must be
loaded into the memory of the computer before it is executed. For this, a loader program is
used.
Debugger program is used to help the user find the programming errors.
Debugger program enables the user
To stop execution of the object-program at some points of interest and
To examine the contents of various processor registers and memory-location
NUMBER NOTATION:
Most assemblers allow numerical values to be specified in different ways, using conventions
that are defined by the assembly language syntax.
Consider, for example, the number 93, which is represented by the 8-bit binary number
01011101. If this value is to be used an immediate operand, it can be given as a decimal
number, as in the instructions.
ADD #93, R1
A binary number identified by a prefix symbol such as a percent sign, as in
In hexadecimal representation, the decimal value 93 becomes 5D. In assembly language, a
hex representation is often identified by a dollar sign prefix. Thus, we would write
ADD #$5D, R1
2.7 BASIC INPUT/OUTPUT OPERATIONS
I/O operation is the mean by which data are transferred between the i/p device to precess and
between the processor and the outside world.
The simple way to perform I/O task is to use a method known as Program-Controlled I/O.
For Input data transfer, buffer-register (DATAIN) & a status control flags (SIN) are used.
Striking key stores the corresponding character-code in an 8-bit buffer-register (DATAIN)
Priyanka H V
Page 10
Computer Organization
associated with the keyboard (Figure: 2.19).
To inform the processor that a valid character is in DATAIN, a SIN is set to 1.
A program monitors SIN, and 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.
An analogous process takes place when characters are transferred from the processor to the
display.
Figure 2.19: Bus Connection for processor, keyboard and display
A buffer register, DATAOUT, and a status control flag, SOUT are used for output data transfer.
When SOUT=1, the display is ready to receive a character.
The transfer of a character to DATAOUT clears SOUT to 0.
The buffer registers DATAIN and DATAOUT and the status flags SIN and SOUT are part of circuitr
commonly known as a device interface.
The below instructions explains how the data is moved between input device and the processor
register(R1).
READWAIT
Branch to READWAIT if SIN=0
INPUT from DATAIN to R1
The below instructions explains how the data is moved between the processor register(R1) and
output device.
WRITEWAIT
Branch to WRITEWAIT if SOUT=0
OUTPUT from R1 to DATAOUT
Read operation can be implemented as shown below:
READWAIT
Priyanka H V
Test bit #3, INSTATUS
Page 11
Computer Organization
Branch=0 READWAIT
Move Byte DATAIN, R1
Write operation can be implemented as shown below:
WRITEWAIT
Test bit #3, OUTSTATUS
Branch=0 WRITEWAIT
Move Byte R1, DATAOUT
Following is a program to read a line of characters and display it:
Move
#LOC,R0
Initialize pointer register R0 to point to
the address of the first location in memory
where the characters are to be stored.
READ TestBit
#3,INSTATUS
Branch=0
READ
MoveByte
DATAIN,(R0)
Wait for a character to be entered
in the keyboard buffer DATAIN.
Transfer the character from DATAIN into
the memory (this clears SIN to 0)
ECHO TestBit #3,
OUTSTATUS
Branch=0
ECHO
MoveByte
(R0),DATAOUT
Wait for the display to become ready.
Move the character just read to the display
buffer register (this clears SOUT to 0).
Compare
#CR,(R0)+
Check if the character just read is CR
(carriage return). If it is not CR, then
Branch0
READ
branch back and read another character.
Also, increment the pointer to store the
next character.
STACKS and QUEUES
A stack is a list of data elements 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 (Figure: 2.21).
Fig b shows a stack of word data items in the memory of a computer. It contains numerical
values, with 43 at the bottom and -28 at the top.
Priyanka H V
Page 12
Computer Organization
The terms push and pop are used to describe placing a new item on the stack and removing
the top item from the stack, respectively.
A processor-register is used to keep track of the address of the element of the stack that is at
the top at any given time. This register is called the SP (Stack Pointer).
Figure 14: A stack of words in the memory
If we assume a byte-addressable memory with a 32-bit word length,
The push operation can be implemented as
Subtract #4,SR
Move NEWITEM,(SP)
With auto decrement
Move NEWITEM,-(SP)
The pop operation can be implemented as
Move (SP), ITEM
Add #4, SP
With auto increment
Move (SP) +, ITEM
(Stack example refer text book)
Routine for a safe pop and push operation as follows:
Priyanka H V
Page 13
Computer Organization
Queue:
First-in-first-out
Unlike a stack, need to keep track of both the front and end for removal and insertion
respectively
Need two pointers to keep track of both ends
Assuming it moves through memory in direction of higher addresses, as it is used, it
walks through memory towards higher addresses.
Circular buffers:
Start at BEGINNING and entries appended until it reaches END after which it wraps
back around to BEGINNING
Need to deal with cases when it is completely full and completely empty
Difference between stack and queue:
1. One end of the stack is fixed (the bottom), while the other end rises and falls as data are
pushed and popped.
On the other hand, both ends of a queue move to higher addresses as data are added at the
back and removed from the front. So two pointers are needed to keep track of the two
ends of the queue.
Priyanka H V
Page 14
Computer Organization
2. A single pointer is needed to point to the top of the stack at any given time.
On the other hand, two pointers are needed to keep track of the two ends of the queue.
3 Without further control, a queue would continuously move through the memory of a
computer in the direction of higher addresses. One way to limit the queue to a fixed
region in memory is to use a circular buffer.
2.9 SUBROUTINES
A subtask which consists of a set of instructions which are executed many times, such
subtasks are called as subroutine.
The program branches to a subroutine with a Call instruction (Figure: 2.24).
Once the subroutine is executed, the calling-program must resume execution starting from
the instruction immediately following the Call instructions i.e. control is to be transferred
back to the calling-program. This is done by executing a Return instruction at the end of the
subroutine.
The way in which a computer makes it possible to call and return from subroutines is referred
to as its subroutine linkage method.
The simplest subroutine linkage method is to save the return-address in a specific location,
which may be a register dedicated to this function. Such a register is called the link register.
When the subroutine completes its task, the Return instruction returns to the calling-program
by branching indirectly through the link-register.
The Call instruction is a special branch instruction that performs the following operations:
Store the contents of PC into link-register.
Branch to the target-address specified by the instruction.
The Return instruction is a special branch instruction that performs the operation:
Branch to the address contained in the link-register.
Priyanka H V
Page 15
Computer Organization
SUBROUTINE NESTING AND THE PROCESSOR STACK
Subroutine nesting means one subroutine calls another subroutine.
In this case, the return-address of the second call is also stored in the link-register, destroying
its previous contents.
Hence, it is essential to save the contents of the link-register in some other location before
calling another subroutine. Otherwise, the return-address of the first subroutine will be lost.
Subroutine nesting can be carried out to any depth. Eventually, the last subroutine called
completes its computations and returns to the subroutine that called it.
The return-address needed for this first return is the last one generated in the nested call
sequence. That is, return-addresses are generated and used in a LIFO order.
This suggests that the return-addresses associated with subroutine calls should be pushed
onto a stack. A particular register is designated as the SP(Stack Pointer) to be used in this
operation.
SP is used to point to the processor-stack.
Call instruction pushes the contents of the PC onto the processor-stack.
Return instruction pops the return-address from the processor-stack into the PC.
PARAMETER PASSING
When calling a subroutine, a program must provide to the subroutine the parameters, that is,
the operands or their addresses, to be used in the computation.
Later, the subroutine returns other parameters, in this case, the results of the computation.
Priyanka H V
Page 16
Computer Organization
This exchange of information between a calling-program and a subroutine is referred to as
parameter passing. (Figure: 2.25).
The parameters may be placed in registers or in memory-location, where they can be
accessed by the subroutine.
Alternatively, parameters may be placed on the processor-stack used for saving the return-
address
Following is a program for adding a list of numbers using subroutine with the parameters
passed through registers.
STACK FRAME
Stack frame refers to locations that constitute a private work-space for the
subroutine(Figure:2.26).
The work-space is
created at the time the subroutine is entered &
freed up when the subroutine returns control to the calling-program.
Following is a program for adding a list of numbers using subroutine with the parameters
passed to stack
Fig: 2.27 show an example of a commonly used layout for information in a stack-frame.
Frame pointer(FP) is used to access the parameters passed
to the subroutine &
to the local memory-variables
The contents of FP remains fixed throughout the execution of the subroutine, unlike stackpointer SP, which must always point to the current top element in the stack.
Priyanka H V
Page 17
Computer Organization
Operation on Stack Frame
Initially SP is pointing to the address of oldTOS.
Priyanka H V
Page 18
Computer Organization
The calling-program saves 4 parameters on the stack (Figure 2.27).
The Call instruction is now executed, pushing the return-address onto the stack.
Now, SP points to this return-address, and the first instruction of the subroutine is executed.
FP is initialized and its old contents are stored. Hence, the first 2 instructions in the
subroutine are:
Move FP,-(SP)
Move SP,FP
The FP is initialized to the value of SP i.e. both FP and SP point to the saved FP address.
The 3 local variables may now be pushed onto the stack. Space for local variables is
allocated by executing the instruction
Subtract #12,SP
Finally, the contents of processor-registers R0 and R1 are saved in the stack. At this point, the
stack-frame has been set up as shown in the fig 2.27.
The subroutine now executes its task. When the task is completed, the subroutine pops the
saved values of R1 and R0 back into those registers, removes the local variables from the
stack frame by executing the instruction.
Add #12, SP
And subroutine pops saved old value of FP back into FP. At this point, SP points to returnaddress, so the Return instruction can be executed, transferring control back to the callingprogram.
STACK FRAMES FOR NESTED SUBROUTINES
Stack is very useful data structure for holding return-addresses when subroutines are nested.
When nested subroutines are used; the stack-frames are built up in the processor-stack.
Consider the following program to illustrate stack frames for nested subroutines
Priyanka H V
Page 19
Computer Organization
The Flow of Execution is as follows:
Main program pushes the 2 parameters param2 and param1 onto the stack and then calls
SUB1.
Priyanka H V
Page 20
Computer Organization
SUB1 has to perform an operation & send result to the main-program on the stack (Fig: 2.28
& 29).
During the process, SUB1 calls the second subroutine SUB2 (in order to perform some
subtask).
After SUB2 executes its Return instruction; the result is stored in register R2 by SUB1.
SUB1 then continues its computations & eventually passes required answer back to main-
program on the stack.
When SUB1 executes return statement, the main-program stores this answers in memorylocation RESULT and continues its execution.
2.10 LOGIC INSTRUCTIONS
Logic operations such as AND, OR, and NOT applied to individual bits.
These are the basic building blocks of digital-circuits.
This is also useful to be able to perform logic operations is software, which is done using
instructions that apply these operations to all bits of a word or byte independently and in
parallel.
The NOT instruction
Not dst
Example: Set of instruction to perform 2s complement
Not
R0
Add
#1, R0
The AND instruction
AND #$FF000000,R0
SHIFT AND ROTATE INSTRUCTIONS
There are many applications that require the bits of an operand to be shifted right or left some
specified number of bit positions.
The details of how the shifts are performed depend on whether the operand is a signed
number or some more general binary-coded information.
For general operands, we use a logical shift.
For a number, we use an arithmetic shift, which preserves the sign of the number.
LOGICAL SHIFTS
Two logical shift instructions are needed, one for shifting left(LShiftL) and another for
shifting right(LShiftR).
These instructions shift an operand over a number of bit positions specified in a count
operand contained in the instruction.
Priyanka H V
Page 21
Computer Organization
The general format for a logical left shift instruction is
LShiftL count, dst
The general format for logical right shift instruction is
LShiftR count, dst
The general format for a Arithmetic right shift instruction is
AShiftR count, dst
Note: Refer class notes for more examples and arithmetic shift left.
Priyanka H V
Page 22
Computer Organization
A routine to pack two BCD digits.
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 instructions can be used. They move the bits that are
shifted out of one end of the operand back into the other end.
Two methods of both the left and right rotate instructions are usually provided.
In one method, the bits of the operand are simply rotated.
In the second method, the rotation includes the C flag.
Priyanka H V
Page 23
Computer Organization
Priyanka H V
Page 24
Computer Organization
A routine that packs two BCD digits using AND and LshiftL instruction.
2.11 ENCODING OF MACHINE INSTRUCTIONS
To be executed in a processor, an instruction must be encoded in a binary-pattern. Such
encoded instructions are referred to as machine instructions.
The instructions that use symbolic names and acronyms are called assembly language
instructions.
We have seen instructions that perform operations such as add, subtract, move, shift, rotate,
and branch. These instructions may use operands of different sizes, such as 32-bit and 8-bit
numbers.
Let us examine some typical cases. The instruction
Add R1, R2
If the processor has 16 registers, then four bits are needed to identify each register. Additional
bits are needed to indicate that the Register addressing-mode is used for each operand.
The instruction
Move 24(R0), R5
Requires 16 bits to denote the OP code and the two registers, and 6 bits for addressing mode
and remaining for the index value (24).
The shift instruction
LShiftR #2, R0
And the move instruction
Move #$3A, R1
Priyanka H V
Page 25
Computer Organization
Have to indicate the immediate values 2 and #$3A, respectively, in addition to the 18 bits used to
specify the OP code, the addressing modes, and the register. This limits the size of the immediate
operand to what is expressible in 14 bits.
Consider next the branch instruction
Branch >0
LOOP
Again, 8 bits are used for the OP code, leaving 24 bits to specify the branch offset.
In all these examples, the instructions can be encoded in a 32-bit word (Fig 2.39).
The OP code for given instruction refers to type of operation that is to be performed.
Source and destination field refers to source and destination operand respectively.
The "Other info" field allows us to specify the additional information that may be needed
such as an index value or an immediate operand.
Consider the instructions:
Move R2, LOC
And #$FF000000. R2
In above cases, the instruction doesnt fit into 32 bit so extra word is included.
Using multiple words, we can implement complex instructions, closely resembling
operations in high-level programming languages. The term complex instruction set
computers (CISC) refers to processors that use CISC approach results in instructions of
variable length, dependent on the number of operands and the type of addressing modes used.
In RISC (reduced instruction set computers), any instruction occupies only one word.
The RISC approach introduced other restrictions such as that all manipulation of data must
be done on operands that are already in processor registers.
Ex: Add
R1,R2,R3
In RISC type machine, the memory references are limited to only Load/Store operations.
Priyanka H V
Page 26
Computer Organization
Priyanka H V
Page 27