0% found this document useful (0 votes)
25 views37 pages

COA Module 2 Notes

The document discusses various addressing modes used in computer organization and architecture, including register, absolute, immediate, indirect, index, and relative addressing modes. It explains how these modes are utilized for accessing operands and managing variables and constants in assembly language programming. Additionally, it covers concepts like pointers, indexing, and the structure of assembly language as a readable representation of machine language instructions.

Uploaded by

Gurushankar S J
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)
25 views37 pages

COA Module 2 Notes

The document discusses various addressing modes used in computer organization and architecture, including register, absolute, immediate, indirect, index, and relative addressing modes. It explains how these modes are utilized for accessing operands and managing variables and constants in assembly language programming. Additionally, it covers concepts like pointers, indexing, and the structure of assembly language as a readable representation of machine language instructions.

Uploaded by

Gurushankar S J
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/ 37

COMPUTER ORGANIZATION & ARCHITECTURE

COMPUTER ORGANIZATION AND ARCHITECTURE

MODULE 2 Part 2

Addressing Modes

• Programmers use organizations called data structures to represent the data used in computations.
These include lists, linked lists, arrays, queues, and so on.
• The different ways in which the location of an operand is specified in an instruction are referred
to as addressing modes.
• Note: Operands are nothing but data or information on which operation has to be performed.

• Effective Address (EA) – It is the address of the operand/data. It is calculated depending upon the
different types of addressing modes.

Implementation of Variables and Constants

SAVITA M, DEPT OF E&CE 1


COMPUTER ORGANIZATION & ARCHITECTURE

• Variables and 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 a memory location to
hold its value.
• Thus, the value can be changed as needed using appropriate instructions.
• Two of the addressing modes that can be used to access variables are:
• Register mode
• Absolute mode
• Register Mode– The operand is the contents of a processor register; the name (address) of the
register is given in the instruction.
• Absolute Mode– The operand is in a memory location; the address of this location is given
explicitly in the instruction. This mode is also called Direct mode.
• The instruction
Move LOC,R2
uses these two modes.
Here LOC represents a memory location (absolute mode) and R2 is a register (register mode).
• Processor registers are used as temporary storage locations where the data in a register are accessed
using the Register mode.
• The Absolute mode can represent global variables in a program.
• A declaration such as
Integer A,B
in a HLL program will cause the compiler to allocate a memory location to each of the variables
A and B. Whenever they are referenced later in the program, the compiler can use the Absolute
mode to access these variables.

• Immediate Mode – The operand is given explicitly in the instruction.


• Address and data constants can be represented using Immediate mode.
• For example, consider the instruction
Move 200immediate,R0
where the value 200 is placed in the register R0.

SAVITA M, DEPT OF E&CE 2


COMPUTER ORGANIZATION & ARCHITECTURE

• But using a subscript to denote Immediate mode is not appropriate in assembly languages.
• Instead sharp sign (#) is used in front of the value to indicate that this value is to be used as an
immediate operand.
• Hence we write the instruction above in the form
Move #200,R0

• Constant values are used frequently in HLL programs.


• For example, A= B+6
contains the constant 6.

• This statement can be implemented using Immediate mode and Absolute mode as follows:
Move B,R1
Add #6,R1
Move R1,A
• Constants are also used in assembly language to increment a counter, test for some bit pattern and
so on.

Indirection and Pointers


• Indirect Mode – The effective address of the operand is the contents of register or memory
location whose address appears in the instruction.
• We denote indirection by placing the name of register or memory address given in the instruction
in parenthesis as shown in figure 2.11 and table 2.1.

SAVITA M, DEPT OF E&CE 3


COMPUTER ORGANIZATION & ARCHITECTURE

• In the figure 2.11a, the contents of R0 is to be added with the contents of memory location B
whose address is present in R1.
• The operand is present in main memory location B which is to be added with R1.
• A general-purpose register R1 is used to store the address of memory location B.
• Since the operand in memory location B is being accessed indirectly through the general-purpose
register R1, this type of addressing is called Indirect addressing mode (through general purpose
register).
• Instead of using general-purpose register, a memory location can also be used to store the address
of the memory location holding the operand (in this case B) as shown in figure 2.11b.
• Here the register or memory location that contains the address of an operand is called a pointer.
• In the above example, register R1 (fig 2.11a) and memory location A (fig 2.11b) are acting as
pointers.
• By changing the contents of a pointer, which points to the address of the operand, another operand,
can be accessed by keeping the same add instruction. (Treasure hunt analogy)
• Consider the figure below-

SAVITA M, DEPT OF E&CE 4


COMPUTER ORGANIZATION & ARCHITECTURE

• The program uses indirect addressing to add the list of numbers starting from the memory location
NUM1.
• Register R2 is used as a pointer to the numbers in the list, and the operands are accessed indirectly
through R2.
• Counter value n is loaded from the memory location N into R1.
• The address value of the first number in the list is NUM1. Here immediate addressing mode is
used to place this address into R2.
• Then R0 is cleared to zero.
• The address of the next instruction Add (R2),R0 is given the name LOOP.
• Here the operand at the location NUM1 is fetched and added to R0.
• The second Add instruction adds 4 to the contents of pointer R2, so that it will contain the address
value of NUM2.

SAVITA M, DEPT OF E&CE 5


COMPUTER ORGANIZATION & ARCHITECTURE

• Let us consider an example to explain the program in the figure 2.12.

Name of the Address of Contents /


memory the memory operand
location location values
NUM1 2000 1

NUM2 2004 3

NUM3 2008 4

NUM4 2012 6

NUM5 2016 8

• Let N=5. Let us assume that the memory locations NUM1 to NUM5 has the addresses and contents
as shown in above table.
• Step 1: The counter value 5 present in N is copied to R1.
• Step 2: The value of the address of NUM1 which is 2000 is copied to R2.
• Step 3: R0 is cleared to zero.
• Step 4: Add (R2),R0 i.e the contents of R2=2000 which is the address of memory location
NUM1 where operand value 1 is stored and this value is added to the contents of R0 which is 0
[1+0=1]. This result is stored in R0.
• Step 5: Add #4,R2 → this instruction adds a value of 4 to the contents of R2 which is presently
2000 [2000+4=2004]. Therefore, the contents of R2 is now 2004 which is the address of NUM2.
• Step 6: R1 is decremented by the value of one, i.e R1=5-1=4.
• Step 7: Since the count value is not equal to zero, the execution jumps back to LOOP.
• Step 8: Add (R2),R0 i.e the contents of R2=2004 which is the address of memory location
NUM2 where operand value 3 is stored and this value is added to the contents of R0 which is 1
[1+3=4]. This result is stored in R0.

SAVITA M, DEPT OF E&CE 6


COMPUTER ORGANIZATION & ARCHITECTURE

• Step 9: Add #4,R2 → this instruction adds a value of 4 to the contents of R2 which is presently
2004 [2004+4=2008]. Therefore, the contents of R2 is now 2008 which is the address of NUM3.
• Step 10: R1 is decremented by the value of one, i.e R1=4-1=3.
• Step 11: Since the count value is not equal to zero, the execution jumps back to LOOP.
• This process is repeated until the count value becomes zero.
• Step 12: The final sum present in R0 is copied to the memory location named SUM.

Indexing and Arrays


• Index Mode – The effective address of the operand is generated by adding a constant value to
the contents of a register.
• This type of addressing mode is useful in dealing with lists and arrays.
• The register used may be either a special register provided for this purpose or any one of a set of
general purpose registers in the processor. In either case,the register is referred to as index register.
• The Index mode is symbolically indicated as X(Ri) , where X denotes the constant value contained
in the instruction and Riis the name of the register involved.
• The effective address of the operand is given by EA=X+[Ri] and the contents of the index register
are not changed in generating the effective address, X can be either a symbolic name representing
a number or an explicit number.
• Since X is a signed integer, it must be sign-extended to the register length before being added to
the contents of the register.
• The following figure shows two ways of using Index mode.

SAVITA M, DEPT OF E&CE 7


COMPUTER ORGANIZATION & ARCHITECTURE

• In figure a, the index register, R1, contains the address of a memory location, and the value X
defines an offset (or displacement) from this address to the location where the operand is found.
• The contents of index register R1 is 1000, which is an address and to this address the offset value
20 is added which gives the value 1020.
• The address 1020 is the address at which the operand, which is to be added with the contents of
R2, is present.
• In figure b, the constant X corresponds to a memory address, and the contents of the index register
define the offset of the operand.
• In this method, the memory address value 1000 is added to the index register value i.e 20 to get
the value 1020, which is the address at which the operand to be added with R2 is present.
• 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.
• To see the usefulness of the indexed addressing, consider a simple example involving a list of test

SAVITA M, DEPT OF E&CE 8


COMPUTER ORGANIZATION & ARCHITECTURE

scores for students taking a given course.


• Assume that the list of scores, beginning at location LIST, structured as shown in figure below.

• A four-word memory block comprises a record that scores the relevant information for each
student.
• Each record consists of the student’s 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.
• The addresses given in the figure for the student IDs and test scores assume that the memory is
byte addressable and that the word length is 32 bits.
• The above figure can be considered as a two-dimensional array having n rows and four columns,
where each row contains the entries for one student and the columns give the IDs and test scores.
• Suppose that we wish to compute the sum of all scores obtained on each of the tests and store these
three sums in memory locations SUM1, SUM2 and SUM3.
• A possible program for this task is given in the figure below.

SAVITA M, DEPT OF E&CE 9


COMPUTER ORGANIZATION & ARCHITECTURE

• In the body of the loop, the program uses the Index addressing mode in the manner depicted in
figure a, to access each of the three scores in a student’s records.
• Register R0 is used as the index register.
• Before entering the loop, R0 is set to point to the ID location of the first student record; thus it
contains the address LIST.
• On the first pass through the loop, test scores of the first student are added to the running sums
held in the registers R1, R2 and R3, which are initially cleared to 0.
• These 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 students records have been accessed, and the loop terminates.
• Until then the conditional branch instruction transfers control back to the start of the loop to process
the next record.
• The last three instructions transfer the accumulated sums from registers R1, R2 and R3, into the
memory locations SUM1, SUM2 and SUM3 respectively.

• The other methods of implementing indexed addressing mode is:

SAVITA M, DEPT OF E&CE 10


COMPUTER ORGANIZATION & ARCHITECTURE

• (Ri,Rj) → here the effective address is the sum of the contents of the registers Ri and
Rj. The second register (i.e Rj) is usually called the base register. This form of indexed
addressing provides more flexibility in accessing operands, because both components
of the effective address can be changed.

• X(Ri,Rj) → here the effective address is the sum of the constant X and the contents of
registers Ri and Rj. This added flexibility is useful in accessing multiple components
inside each item in a record i.e this mode implements a three-dimensional array.

Relative Addressing
• Instead of using a general-purpose register, we can also use PC as an index register.
• Then, X(PC) can be used to address memory location that is X bytes away from the location
presently pointed to by the program counter.
• Relative Mode – The effective address is determined by the index mode using PC in place of
general-purpose register Ri.
• The most common use of this addressing mode is to access specific target address, in case of
branch instructions.
Branch>0 LOOP
this causes the program execution to go to the branch target location identified by the name LOOP
if branch condition is satisfied.
• This location, i.e the location of the branch name LOOP, can be computed by specifying it as an
offset from the current value of the PC.
• Since the branch target may be either before or after the branch instruction, the offset is given as a
signed number.
Additional Modes
• There are two modes which can access data items in successive locations in memory.
• Autoincrement Mode – The effective address of the operand is the contents of the register
specified in the instruction. After accessing the operands, the contents of this register are
automatically incremented to point to the next item in a list.

SAVITA M, DEPT OF E&CE 11


COMPUTER ORGANIZATION & ARCHITECTURE

• Autoincrement mode is written as → (Ri)+


• The plus sign indicates that the contents of the register Ri are incremented by one after the operand
is accessed.
• In byte addressable memory, the increment is 1 for byte sized operands, 2 for16 bit operands and
4 for 32 it operands.
• Autodecrement Mode – The contents of register specified in the instruction are first
automatically decremented and are then used as the effective address of the operand.
• Autodecrement mode is written as → -(Ri)
• In this mode, operands are accessed in descending address order.

Assembly Language
• Machine instructions are represented by patterns of 0s and 1s.
• In programs, we use symbolic names to represent this patterns (ex. Move, Add) which are
acronyms for the symbolic names called Mnemonics.
• A set of mnemonics and the rules for their use constitute a programing language referred to as an
assembly language.
• The assembly language is a readable representation of the machine language for the processor.
• Assembler coverts the assembly language instructions into machine language instructions.
• Assembler program is one of a collection of utility programs that are part of system software.
• The user program in its original alphanumeric text format is called source program.
• The assembled machine language program is called object program.
• Consider a Move instruction as → MOVE R0,SUM

SAVITA M, DEPT OF E&CE 12


COMPUTER ORGANIZATION & ARCHITECTURE

• Here the mnemonic MOVE represents the binary pattern, or OP Code, which indicates the
operation performed by the instruction.
• The OP-code mnemonic is followed by at least one blank space character. Then the information
that specifies the operands is given.
• Since there are several possible addressing modes for specifying the operand locations, the
assembly language must indicate which mode is being used.

• For example,
ADD #5,R3 → Absolute Addressing mode
ADDI 5,R3 → Immediate Addressing mode
MOVE #5,(R2) or MOVEI 5,(R2) → Indirect Addressing mode

Assembler Directives
• The Assembler Directives give directions to the assembler during the assembly process but are not
translated into machine instructions.
• The directives control the generation of machine code and organization of the program.
• For example, SUM EQU 200
• When the object program is run, the above instruction is not executed and it is simply informs the
assembler that the name SUM should be replaced by the value 200, whenever SUM appears in the
program. This instruction does not appear in the object program.
• The assembly program first gets translated into object program and is stored in the main memory.
• The assembler does not know the memory address where the machine instructions and the required
data items are to be stored after the program is loaded for execution.
• If the memory address to store the instructions and data are not specified in the program, they will
be stored in randomly in any memory locations.
• For an assembler to store the object program in a specified location, it 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 are used to provide all the above information to the assembler.

SAVITA M, DEPT OF E&CE 13


COMPUTER ORGANIZATION & ARCHITECTURE

• Some assembler directive examples:


• EQU – This is used to assign a label to a value or a symbol. In the above example, for the
value 200, the label SUM is assigned. It is used to reduce the recurrence of numerical values
or constants in a program. The recurring value is assigned with the label and that label is
used in place of the numerical value throughout the program. Whenever the assembler
comes across the label it substitutes the numerical value for that label and finds out the
equivalent code.

• ORIGIN – It directs the assembler to start the memory allotment for the particular segment,
block or code from the declared address in ORIGIN instruction. For example, the instruction
‘ORIGIN 204’ in the figure indicates that the instructions written after this line (next 2 lines
in this particular program) are saved from the address 204 in the memory. Then the
instruction ‘ORIGIN 100’ indicates that the lines following this instruction will be saved
from the address 100 in the memory.
• DATAWORD – It indicates that the a data value of 100 is to be placed in the memory word
at the address 204 with the label N.
• RESERVE – It declares that a particular block of memory is to be reserved for data. In the
program, 400 bytes of memory will be reserved under the label NUM1.
• RETURN – It indicates the point at which execution of the program should be terminated.
It causes the assembler to insert an appropriate machine instruction that returns control to
the operating system of the computer.
• END – It marks the end of an assembly language program text and it ignores the source
lines available later on.

SAVITA M, DEPT OF E&CE 14


COMPUTER ORGANIZATION & ARCHITECTURE

SAVITA M, DEPT OF E&CE 15


COMPUTER ORGANIZATION & ARCHITECTURE

Most assembly languages require statements in a source program to be written in the form
Label Operation Operands Comment
• These four fields are separated by an appropriate delimiter, typically one or more lank characters.
• Label- is an optional name associated with memory address where the machine language
instruction produced from the statement willbe loaded.Labels may be associated with the data
items.Such labels in the figure are SUM,N,NUM1,START and LOOP.
• Operation Field-This field contains OP code mnemonic of the desired instruction or assembler
directive. It contains addressing information for accessing one or more operands,depending on the
type of instruction.
• Comment Field-It is ignored by the assembler and is used for documentation purpose to make the
program easier to understand.
➢ Note: Assembly languages differ in detail and complexity from one computer to another.

SAVITA M, DEPT OF E&CE 16


COMPUTER ORGANIZATION & ARCHITECTURE

Assembly and Execution of Programs


• A Source program is written in assembly language must be assembled into a machine language
object program before it can be executed which is done by the assembler program, that replaces
all symbols denoting operations and addressing modes with binary codes used in machine
instructions, and replaces all names and labels with their actual values.
• Branch instruction is usually implemented in a machine code by specifying the branch target using
relative addressing mode and the assembler computes the branch offset which is the distance to
the target and puts it into the machine instruction.
• As the assembler scans through the source program, it keeps track of all the names and numerical
values that correspond to them in a symbol table.Thus, when the name appears a second time, it is
placed with its value from the table.
• In programs involving branch instructions, the assembler will not be able to form the complete
symbol table in one pass. Hence the assembler 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.
• The assembler stores the object program on a magnetic disk. Loader is a utility program which
loads the object program into the memory of computer before it is executed.
• Debugger program helps the user to find programming errors other than logical and syntax errors.
_________________________________________________________________________
Number Notation
• The numerical value are convenient to be represented in decimal notation. But they are stored in
computer as binary numbers.
• Most assemblers allow numerical values to be specified in different ways,using conventions
defined by the assembly language syntax. Different representations as following are used-
a) 8 bit binary number representation n Ex:93 as 01011101
b) Hexadecimal number representation Ex:93 as 5D (after 9 i.e,1001 remaining six 4 bit patterns
1010,1011,….1111 represented by letters A,B,C,D,E,F
When used in instruction as-
1. Immediate operand can be given as decimal number ADD #93, R1
2. Binary number identified by prefix symbol percentage sign ADD %01011101, R1

SAVITA M, DEPT OF E&CE 17


COMPUTER ORGANIZATION & ARCHITECTURE

3. Hex number identified by prefix symbol dollar sign ADD $5D, R1

Basic Input/Output Operations


• Input/Output (I/O) operations are essential, and the way they are performed can have a significant
effect on the performance of the computer.
• Consider a task that reads in character input from a keyboard and produces character output on
display screen.
• A simple way of performing such I/O tasks is to use a method known as program-controlled I/O.
• The rate of data transfer from keyboard to a computer is limited by the typing speed of the user,
which is unlikely to exceed a few characters per second.
• The rate of output transfers from the computer to the display is much higher.
• It is determined by the rate at which characters can be transmitted over the link between the
computer and the display device, typically several thousand characters per second.
• However, this is still much slower than the speed of a processor that can execute many millions of
instructions per second.
• The difference in speed between the processor and I/O devices creates the need for mechanisms to
synchronize the transfer of data between them.
• A solution to this problem is as follows: On output, the processor sends the first character and then
waits for a signal from the display that the character has been received. It then sends the second
character, and so on.
• Input is sent from the keyboard in a 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. Then the processor proceeds to read that code.
• Consider the figure below.

SAVITA M, DEPT OF E&CE 18


COMPUTER ORGANIZATION & ARCHITECTURE

• In the above figure, the keyboard and the display are separate devices.
• The action of striking a key on the keyboard does not automatically cause the corresponding
character to be displayed on the screen.
• One block of instructions in the I/O program transfers the character into the processor, and another
associated block of instructions causes the character to be displayed.
• Consider the problem of moving a character code from the keyboard to the processor.
• Striking a key stores the corresponding character code in a 8-bit buffer register associated with the
keyboard and let it be called DATAIN, as shown in the above figure.
• To inform the processor that a valid character is in DATAIN, a status control flag, 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 is entered at the keyboard, SIN is again set to 1 and the process repeats.
• A similar process takes place when characters are transferred from the processor to the display.
• A buffer register, DATAOUT, and a status control flag, SOUT, are used for this transfer.
• When SOUT equals to 1, the display is ready to receive a character.
• The processor monitors SOUT, and when SOUT is set to 1, the processor transfers a character
code to DATAOUT.
• The transfer of a character to DATAOUT 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 part of
circuitry commonly known as device interface.

SAVITA M, DEPT OF E&CE 19


COMPUTER ORGANIZATION & ARCHITECTURE

• The circuitry for each device is connected to the processor via a bus as shown in the previous
figure.
• In order to perform I/O transfers, we need machine instructions that can check the state of the
status flag and transfer data between the processor and the I/O devices.

READWAIT Branch to READWAIT if SIN = 0


Input from DATAIN to R1

• The first instruction tests the status flag and the second performs the branch.
• The processor monitors the status flag by executing a short wait loop and proceeds to transfer the
input data from DATAIN to the processor register R1, when SIN is set to 1 as a result of key being
struck.
• The Input operation resets SIN to 0.

WRITEWAIT Branch to WRITEWAIT if SOUT = 0


Output from R1 to DATAOUT
• The wait loop is executed repeatedly until the status flag SOUT is set to 1 by the display device
when it is free to receive a character.
• The Output operation transfers a character from R1 to DATAOUT to be displayed, and it clears
SOUT to 0.
• Note: We assume that the initial state of SIN is 0 and the initial state of SOUT is 1.
• Many computers use an arrangement called memory-mapped I/O in which some memory address
values are used to refer to peripheral device buffer registers, such as DATAIN and DATAOUT.
• Thus, no special instructions are needed to access the contents of these registers; data can be
transferred between these registers and the processor using instructions as shown below.
MoveByte DATAIN,R1
where the contents of the keyboard character buffer DATAIN is transferred to register R1 in the
processor.

MoveByte R1,DATAOUT

SAVITA M, DEPT OF E&CE 20


COMPUTER ORGANIZATION & ARCHITECTURE

where the contents of register R1 is transferred to DATAOUT.


• The status flags SIN and SOUT are automatically cleared when the buffer registers DATAIN and
DATAOUT are referenced, respectively.
• The MoveByte operation code signifies that the operand size is a byte, to distinguish it from the
operation code Move that has been used for word operands.

• It is a common practice to include SIN and SOUT in device status registers, one for each of the
two devices.
• Let us assume that bit b3 in registers INSTATUS and OUTSTATUS corresponds to SIN and
SOUT, respectively.
• The read operation may be now implemented as
READWAIT Testbit #3,INSTATUS
Branch=0 READWAIT
MoveByte DATAIN,R1

• The write operation may be implemented as


WRITEWAIT Testbit #3,OUTSTATUS
Branch=0 WRITEWAIT
MoveByte R1,DATAOUT

• The Testbit instruction tests the state of one bit in the destination location, where the bit position
to be tested is indicated by the first operand.
• If the bit tested is equal to 0, then the condition of the branch is true, and a branch is made to the
beginning of the wait loop.
• When the device is ready, that is, when the bit tested becomes equal to 1, the data are read from
the input buffer or written onto the output buffer.
• Program-controlled I/O requires continuous involvement of the processor in the I/O activities.
• It is desirable to avoid situations like wait loop to avoid wasting of processor execution time.
• Other I/O techniques, based on the use of interrupts, may be used to improve the utilization of the
processor.

SAVITA M, DEPT OF E&CE 21


COMPUTER ORGANIZATION & ARCHITECTURE

Stacks and Queues


Stacks
• A computer program often needs to perform a particular subtask using the familiar subroutine
structure.
• In order to organize the control and information linkage between the main program and the
subroutine, a data structure called stack is used.
• 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.
• In last-in-first-out (LIFO) stack, the last data item placed on the stack is the first one removed
when retrieval begins.
• Placing a new item on the stack is called push operation.
• Removing the top item from the stack is called pop operation.
• Data stored in the memory of a computer can be organized as a stack, with successive elements
occupying successive memory locations.
• Assume that the first element is placed in location BOTTOM, and when new elements are pushed
on to the stack, they are placed in successively lower address locations. That is we use a stack that
grows in the direction of decreasing memory address as is a common practice.

SAVITA M, DEPT OF E&CE 22


COMPUTER ORGANIZATION & ARCHITECTURE

• The above figure 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.
• A processor register called stack pointer (SP) is used to keep track of the address of the element
of the stack that is at the top at any given time.
• The stack pointer could be any general-purpose register or a register dedicated to this function.
• If we assume a byte addressable memory with 32-bit word length, the push operation can be
implemented as
Subtract #4,SP
Move NEWITEM,(SP)
where the Subtract instruction subtracts the source operand by 4 from the destination operand
contained in SP and places the result in SP.
• These two instructions move the word from location NEWITEM onto the top of the stack,
decrementing the stack pointer by 4 before the move.
• The pop operation can be implemented as
Move (SP),ITEM
Add #4,SP
• These two instructions move the top value from the stack into the location ITEM and then
increment the stack pointer by 4 so that it points to the new top element.
• The figure below shows the effect of push and pop operations on stack.

SAVITA M, DEPT OF E&CE 23


COMPUTER ORGANIZATION & ARCHITECTURE

• If the processor has the Autoincrement and Autodecrement addressing modes, then the push
operation can be performed by the single instruction
Move NEWITEM,-(SP)
and the pop operation can be performed by
Move (SP)+,ITEM
• When a stack is used in a program, it is usually allocated a fixed amount of space in the memory.
• In this case, we must avoid pushing an item onto the stack when the stack has reached its maximum
size.
• Also, we must avoid attempting to pop an item off an empty stack, which could result in
programming error.
• Suppose that a stack runs from location 2000 (BOTTOM) down no further than location 1500.
• The stack pointer is loaded initially with the address value 2004.
• Recall that SP is decremented by 4 before new data are loaded on the stack
• Hence, an initial value of 2004 means that the first item pushed onto the stack will be at location
2000.
• To prevent either pushing an item on a full stack or popping an item off an empty stack, the single-
instruction push and pop operations can be replaced by the instruction sequences as shown in the
figure below.

SAVITA M, DEPT OF E&CE 24


COMPUTER ORGANIZATION & ARCHITECTURE

• The Compare instruction


Compare src,dst
performs the operation
[dst] - [src]
and sets the condition code flags according to the result. It does not change the value of either
operands.
Queues
• Queue is another useful data structure that is similar to stack.
• Data are stored in and retrieved from a queue on a first-in-first-out (FIFO) basis.
• Thus the queue grows in the direction of increasing addresses in the memory, new data are added
at the back (high address end) and retrieved from the front (low address end) of the queue.

SAVITA M, DEPT OF E&CE 25


COMPUTER ORGANIZATION & ARCHITECTURE

• There are two important differences between how a stack and a queue are implemented.
• One end of the stack i.e bottom is fixed, while the other end rises and falls as data are pushed and
popped.
• Also a single pointer is needed to point to the top of the stack at any given time.
• 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.
• Another difference between stack and a queue is that, 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 the memory is to use a circular buffer.
• Let us assume that the memory addresses from BEGINNING to END are assigned to the queue.
• The first entry in the queue is entered into the location BEGINNING and successive entries are
appended to the queue by entering them at successively higher addresses.
• By the time the back of the queue reaches END, space will have been created at the beginning if
some items have been removed from the queue.
• Hence the back pointer is reset to the value BEGINNING and the process continues.
• Care must be taken to detect when the region assigned to the data structure is either completely
full or completely empty, just like a stack.

Subroutines
• Subroutine is a subtask (in the form of block of instructions) which is performed many times on
different data values.
• To saves space only one copy of the instructions that constitute subroutine is placed in the memory
and any program that require the use of subroutine simply reaches to its starting location.
• When a program branches to a subroutine we say that it is calling the subroutine.The instruction
that performs this branch operation is named a call instruction.
• After a subroutine has been executed, the calling program must resume execution, continuing
immediately after the instruction that called the subroutine. The subroutine is said to return to the
program that called it y executing Return instruction.
• PC is used to point to the appropriate location when the subroutine is called and again to point

SAVITA M, DEPT OF E&CE 26


COMPUTER ORGANIZATION & ARCHITECTURE

back to the same location where the program execution has been left.
• Hence the contents of the PC must be saved by the call instruction to enable correct return to the
calling program.
• 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 is specific location,which
may be register dedicated to this function and such register is called the link register.

• When the subroutine completes its task, the Return instruction to the calling program by branching
indirectly through the link register.
The Call instruction is just a special branch instruction that performs the following operations-
• 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 contained in the link register
Branch to the address contained in the link register.

Subroutine Nesting and The Processor Stack


• A common programming practice, called subroutine nesting,is to have one subroutine call another.
• 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, return address of the first subroutine will be lost.
• Subroutine nesting can be carried out to any depth.
• 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 last-in–first-out order.(LIFO)
• This suggests that the return addresses associated with subroutine calls should be pushed on to the
processor stack.
• Many processors do this automatically as one of the operations performed by the Call instruction.
• A particular register called stack pointer, SP is used that points to processor stack.

SAVITA M, DEPT OF E&CE 27


COMPUTER ORGANIZATION & ARCHITECTURE

• Call instruction Pushes the contents of the PC on to the processor stack and loads the subroutine
address into the PC.
• The Return instruction pops the return address from the processor stack in to the PC.
Parameters 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.
• This exchange of information between a calling program and subroutine is referred to as parameter
passing.

• Parameter or in memory locations, where they can be accessed by the subroutine. Alternatively,
the parameters may be placed on the processor stack.
• Passing parameters through processor registers is straight forward and efficient.

• Subroutine uses three registers. They contain valid data that belong to the calling program, their
contents should be saved by pushing them on to the stack.
• We have used a single instruction, Move multiple, to store the contents of register R0 through R2
on the stack.
• Many processor have such instruction. The top of the stack is now at level3.

SAVITA M, DEPT OF E&CE 28


COMPUTER ORGANIZATION & ARCHITECTURE

• The subroutine accesses the parameters n andNUM1 from the stack using indexed addressing.
Note that it does not change the pointer because valid data items are still at the top of the stack.
• The value is loaded into R1as the initial value of the count, and the address NUM1 is loaded into
R2, which is used as a pointer to scan the list entries.
• At the end of the computation, register R0 contains the sum.
• Before the subroutine returns to the calling program, the contents of R0 are placed on the stack,
replacing the parameter NUM1, which is no longer needed.
• Then the contents of three registers used by the subroutine are restored from the stack.
• Now the top item on the stack is the return address at level2.
• After the subroutine returns, the calling program stores the result in location SUM and lowers the
top of the stack to its original level by incrementing the SP by 8.

SAVITA M, DEPT OF E&CE 29


COMPUTER ORGANIZATION & ARCHITECTURE

SAVITA M, DEPT OF E&CE 30


COMPUTER ORGANIZATION & ARCHITECTURE

Parameter Passing by Value and by Reference


• The nature of the parameters NUM1 and n, passed to the subroutines in figure 2.25 where the
purpose of the subroutine is to add a list of numbers.
• Instead of passing the actual entities, the calling program passes the address of the first number in
the list. This technique is called Passing by reference.
• If the acual value that is, the actual number of entries nis passed to the subroutine then it is called
Passing by Value.

Additional Instructions
Logical Instructions
• Logic operations such as AND, OR, and NOT, applied to individual bits, are the basic building
blocks of digital circuits.
• It is also useful to be able to perform logic operations in software, which is done using instructions
that apply these operations to all bits of a word or byte independently and in parallel.
• For example, the instruction
Not dst
compliments all bits contained destination operand.

Not R0
Add #1,R0
• The above instructions help us to find the 2’s compliment of a number stored in R0. For example
if +3 (0011) is initially stored in R0, the first line NOT R0 finds the 1’s compliment of +3 which
is 1100 and it is again stored in R0. The next line adds a value 1 to the contents of R0 to get 1101
(-3). This gives us the 2’s compliment of +3.
• In some computers, a single instruction can perform 2’s compliment directly as given below.
Negate R0
• Logical instruction And performs the bit-wise AND operation on the source and destination

SAVITA M, DEPT OF E&CE 31


COMPUTER ORGANIZATION & ARCHITECTURE

operands.
• The And instruction is often used in practical programming tasks where all the bits of an operand
except for some specified field are to be cleared to 0.
• For example, let R0=1010011. Let’s say, the three left most bits are to be retained and all other
remaining 4 bits are to be cleared to 0 to get 1010000. This can be done using the And instruction
as
And #1110000,R0
Here the contents of R0 are bitwise ANDed with the vaule 1110000 to get the value 1010000. The
left most three bits ‘101’ are ANDed with 1’s in the number 1110000 and hence they are retained.
The remaining bits are ANDed with 0 and hence they are cleared to 0. Finally R0=1010000.
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 positions.
• For general operands, we use logical shift and 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.
• The general form of logical left shift operation is
LShiftL count,dst
• The count operand may be given as an immediate operand, or it may be contained in a processor
register.
• Vacated positions in the right are filled with zeros, and the bits shifted out from the left side are
passed through the carry flag, C, and then dropped.
• Involving the C flag in shift operations is useful in performing arithmetic operations on large
numbers that occupy more than one word.
• The figure a below shows an example of shifting the contents of register R0 left by two bit
positions.

SAVITA M, DEPT OF E&CE 32


COMPUTER ORGANIZATION & ARCHITECTURE

• The logical shift right instruction, LShiftR, works in the same manner except that I shifts to the
right as shown in figure b.

Digit-Packing Example
• Consider two decimal digits 13 (0001 0011) and 25 (0010 0101) located at memory byte locations
LOC and LOC+1. Here these numbers are said to be unpacked format.
• The task is to represent each of these digits in the 4-bit BCD code and store both of them in a single
byte location PACKED (35→0011 0101).
• This result is said to be in packed-BCD format.
• This can be done using the program below.
• Let the memory location LOC has its address 1000 where the value 13 is stored and LOC+1 has
its address as 1004 and 25 is stored in it.

SAVITA M, DEPT OF E&CE 33


COMPUTER ORGANIZATION & ARCHITECTURE


Label Address Value
LOC 1000 13
LOC+1 1004 25

Instructions Contents

1 Move #LOC,R0 LOC→R0 = 1000


2 MoveByte (R0)+,R1 (LOC)→R1 = 13, R0=1004
3 LShiftL #4,R1 After shift, R1 = 0011 0000(30)
4 MoveByte (R0),R2 R2 = 25
5 And #0F,R2 R2=05
6 Or R1,R2 (30)OR(05)= 35= R2
7 MoveByte R2,PACKED PACKED=35

Arithmetic Shifts
• Shifting a number by 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.
• Of course, overflow might occur on shifting left and the remainder is lost in shifting left.
• Another important observation is that on a right shift the sign bit must be repeated as the fill-in bit
for the vacated position.
• This requirement on right shifting distinguishes arithmetic shifts from logical shifts in which the
fill-in bit is always 0.
• Otherwise, the two shifts are very similar in their operation.
• AShiftR, arithmetic right shift, shifts the number by specified number of positions to the right and
fiils in the vacant positions with the sign bit as shown in figure c.

SAVITA M, DEPT OF E&CE 34


COMPUTER ORGANIZATION & ARCHITECTURE

• The arithmetic left shift is exactly same as the logical left shift.
Rotate Operations
• In shift operations, the bits that are 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 versions of both the left and right rotate instructions are usually provided.
• In one version, the bits of the operand are simply rotated and in the other version, the rotation
includes the C flag.
• The figure below shows the left and right operations with and without the C flag being included in
the rotation.
• Note that when the C flag is not included in the rotation, it still retains the last bit shifted out of the
end of the register.
• RotateL – rotate left without carry
• RotateLC – rotate left with carry
• RotateR – rotate right without carry
• RotateRC – rotate right with carry
• The main use for the rotate instructions is in algorithms for arithmetic operations other than
addition and subtraction.

SAVITA M, DEPT OF E&CE 35


COMPUTER ORGANIZATION & ARCHITECTURE

Multiplication and Division


• Two signed integers can be multiplied or divided by machine instructions with the same format.
• The instruction
Multiply Ri,Rj

SAVITA M, DEPT OF E&CE 36


COMPUTER ORGANIZATION & ARCHITECTURE

performs the operation


Rj  [Ri] x [Rj]
• The product of two n-bit numbers can be as large as 2n bits.
• Therefore, the answer will not necessarily fit in to register Rj.
• A number of instruction set have a Multiply instruction that computes a lower order n bits of the
product and places it in register Rj.
• This is sufficient if it is known that all products in some particular task will fit in to n bits.
• To accommodate the general 2n bit product case, some processors produce the product in two
registers. Usually adjacent registers Rj and R(j+1), with the higher order half being placed in
register R(j+1).
• Instruction that provide signed integer division is
Divide Ri,Rj
performs the operation
Rj  [Ri] / [Rj]
placing the quotient in Rj and remainder may be placed in R(j+1).

Glossary

Register: A small unit of very fast memory that is directly accessible to the CPU, and is mostly
used to store inputs, outputs or intermediate results of computation.

Accumulator: A register in a computer used for holding the intermediate results of a


computation or data transfer.

Offset: The difference between a target memory address and a base address.

Stack: A linear data structure in which the last data item is the first retrieved; a LIFO queue.

SAVITA M, DEPT OF E&CE 37

You might also like