module 2
module 2
• The different ways in which the location of an operand is specified in an instruction are referred to as
1) Register Mode
2) Absolute Mode
Register Mode
• Registers are used as temporary storage locations where the data in a register are accessed.
Move LOC, R2; Copy content of memory-location LOC into register R2.
Immediate Mode
• Clearly, the immediate mode is only used to specify the value of a source-operand.
• To execute the Add instruction in fig 2.11 (a), the processor uses the value which is in register R1,
• 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 as shown in fig 2.11(b). In this case, the
processor first reads the contents of memory-location A, then requests a second read operation using the
• Fig(a) 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 EA of operand: Eg: Add 20(R1), R2
EA=>1000+20=1020
• An alternative use is illustrated in fig(b). Here, 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.
• Another version of the Index mode uses 2 registers which can be denoted as (Ri, Rj)
• This form of indexed addressing provides more flexibility in accessing operands because both components
of the effective-address can be changed.
• Another version of the Index mode uses 2 registers plus a constant, which can be denoted as 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. In other words, this mode
implements a 3-dimensional array.
RELATIVE MODE
The effective-address is determined using the PC in place of the general purpose register Ri.
• 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.
• An instruction such as
Branch > 0 LOOP ; Causes program execution to go to the branch target location identified
After accessing the operand, the contents of this register are automatically incremented to point to the
next item in a list.
The contents of a register specified in the instruction are first automatically decremented and are then
used as the effective-address of the operand.
These 2 modes can be used together to implement an important data structure called a stack.
ASSEMBLER DIRECTIVES
• Directives are the assembler commands to the assembler concerning the program being assembled.
• These commands are not translated into machine opcode in the object-program.
• EQU informs the assembler about the value of an identifier (Figure: 2.18).
Ex: SUM EQU 200 ;Informs assembler that the name SUM should be
replaced by the value 200.
COMPUTER ORGANIZATION | DEPT. OF ELECTRONICS & COMMUNICATION ENGG. 37
18EC35 COMPUTER ORGANIZATION| MODULE 2: ADDRESSING MODES
• ORIGIN tells the assembler about the starting-address of memory-area to place the data block.
Ex: ORIGIN 204 ;Instructs assembler to initiate data-block at
memory-locations starting from 204.
• DATAWORD directive tells the assembler to load a value into the location.
Ex: N DATAWORD 100 ;Informs the assembler to load data 100 into the
memory-location N(204).
• RESERVE directive is used to reserve a block of memory.
Ex: NUM1 RESERVE 400 ;declares a memory-block of 400 bytes
is to be reserved for data.
• 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.
• Most assembly languages require statements in a source program to be written in the form:
1) Label is an optional name associated with the memory-address where the machine
2) Operation Field contains the OP-code mnemonic of the desired instruction or assembler.
3) Operand Field contains addressing information for accessing one or more operands,
4) Comment Field is used for documentation purposes to make program easier to understand.
• Consider the problem of moving a character-code from the keyboard to the processor (Figure: 2.19).
For this transfer, buffer-register DATAIN & a status control flags(SIN) are used.
• When a key is pressed, the corresponding ASCII code is stored in a DATAIN register
• An analogous process takes place when characters are transferred from the processor
• The buffer registers DATAIN and DATAOUT and the status flags SIN and SOUT
MEMORY-MAPPED I/O
• Some address values are used to refer to peripheral device buffer-registers such as DATAIN
& DATAOUT.
• No special instructions are needed to access the contents of the registers; data can be
transferred between these registers and the processor using instructions such
• For example, contents of the keyboard character buffer DATAIN can be transferred
MoveByte DATAIN,R1
• The MoveByte operation code signifies that the operand size is a byte.
• The Testbit instruction tests the state of one bit in the destination, where the bit position
2.4.1 STACKS
• A stack is a special type of data structure where elements are inserted from one end
and elements are deleted from the same end. This end is called the top of the stack
(Figure: 2.14).
1) Insert: An element is inserted from top end. Insertion operation is called push operation.
2) Delete: An element is deleted from top end. Deletion operation is called pop operation.
• 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 Stack Pointer (SP).
2.4.2 QUEUE
1) One end of the stack is fixed while the other end rises and falls as data are pushed and popped.
2) In stack, a single pointer is needed to keep track of top of the stack at any given time.
In queue, two pointers are needed to keep track of both the front and end for removal
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
2.4.3 SUBROUTINES
• A subtask consisting of a set of instructions which is executed many times is called a Subroutine.
• Program resumes execution at the instruction immediately following the subroutine call
• The way in which a computer makes it possible to call and return from subroutines
• 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
• The Call Instruction is a special branch instruction that performs the following operations:
• The Return Instruction is a special branch instruction that performs the operation:
• In this case, the return-address of the second call is also stored in the link-register,
• 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.
Return instruction pops the return-address from the processor-stack into the PC.
PARAMETER PASSING
• 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.
• The work-space is
→ freed up when the subroutine returns control to the calling-program (Figure: 2.26).
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.
• The contents of FP remains fixed throughout the execution of the subroutine, unlike stack-
pointer SP, which must always point to the current top element in the stack.
Operation on Stack Frame
• Initially SP is pointing to the address of oldTOS.
• 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.
• Now, FP is to be initialized and its old contents have to be 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 return-
address, so the Return instruction can be executed, transferring control back to the calling-program.
• 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.
• Main program pushes the 2 parameters param2 and param1 onto the stack and then calls SUB1.
• 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 memory-location
• Logic operations such as AND, OR, and NOT applied to individual bits.
• 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.
Not dst
• 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
For a number, we use an arithmetic shift, which preserves the sign of the number.
LOGICAL SHIFTS
• These instructions shift an operand over a number of bit positions specified in a count operand
• In 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.
• 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 instructions are usually provided. In one version, the bits of the
operand is simply rotated.
• 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.
Add R1, R2 ;Has to specify the registers R1 and R2, in addition to the OP code. 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 some bits to
express that the source operand uses the Index addressing mode and that the index value is 24.
• 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
• 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
• 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
• In RISC type machine, the memory references are limited to only Load/Store operations.
COMPUTER ORGANIZATION | DEPT. OF ELECTRONICS & COMMUNICATION ENGG.
52
COMPUTER ORGANIZATION| MODULE 2: ADDRESSING MODES 18EC35
Problem 1:
Write a program that can evaluate the expression A*B+C*D In a single-accumulator processor.
Assume that the processor has Load, Store, Multiply, and Add instructions and that all values fit in the
accumulator
Solution:
Store RESULT
Problem 2:
Registers R1 and R2 of a computer contains the decimal values 1200 and 4600. What is the
Solution:
COMPUTER ORGANIZATION | DEPT. OF ELECTRONICS & COMMUNICATION ENGG. 53
18EC35 COMPUTER ORGANIZATION| MODULE 2: ADDRESSING MODES
(b) EA = 3000
Problem 3:
Registers R1 and R2 of a computer contains the decimal values 2900 and 3300. What is the
(e) Subtract-(R2),R5
Solution:
So, 95+R1+R2=95+2900+3300=6255.
d) Add (R1)+,R5 This is Autoincrement mode. Contents of R1 are the EA so, 2900 is the EA.
Problem 4:
Solution:
Problem 5:
Both of the following statements cause the value 300 to be stored in location 1000,
but at different times.
ORIGIN 1000
DATAWORD 300
And
Move #300,1000
(b) Copy the fifth item from the top into register R3.
Solution:
(a) Move (R5)+,R0
Add (R5)+,R0 Move R0,-(R5)
(b) Move 16(R5),R3
(c) Add #40,R5
Problem 7:
Consider the following possibilities for saving the return address of a subroutine:
(a) In the processor register.
(b) In a memory-location associated with the call, so that a different location is used when
the subroutine is called from different places.
(c) On a stack.
Which of these possibilities supports subroutine nesting and which supports subroutine recursion(that is, a
subroutine that calls itself)?
Solution:
(a) Neither nesting nor recursion is supported.
(b) Nesting is supported, because different Call instructions will save the return address at
different memory-locations. Recursion is not supported.
(c) Both nesting and recursion are supported.