0% found this document useful (0 votes)
4 views31 pages

COA Module2 Notes

This document covers addressing modes, basic input/output operations, stacks, queues, and subroutines in computer organization. It explains how operands are specified in instructions, the use of program-controlled I/O, and the structure and operations of stacks and queues. Additionally, it discusses subroutine nesting and parameter passing, emphasizing the importance of managing return addresses and register contents during subroutine calls.

Uploaded by

Hemanth B
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)
4 views31 pages

COA Module2 Notes

This document covers addressing modes, basic input/output operations, stacks, queues, and subroutines in computer organization. It explains how operands are specified in instructions, the use of program-controlled I/O, and the structure and operations of stacks and queues. Additionally, it discusses subroutine nesting and parameter passing, emphasizing the importance of managing return addresses and register contents during subroutine calls.

Uploaded by

Hemanth B
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

Module 2 Computer Organization

ADDRESSING MODES AND BASIC INPUT AND OUTPUT OPERATIONS

Addressing Modes: The different ways in which the location of an operand


is specified in an instruction is referred to as addressing modes.

Some common addressing modes used are shown in the table below.

1
Module 2 Computer Organization

 A declaration such as
Integer A, B;
in high level language will cause the compiler to allocate memory locations to
variables A and B. Whenever they are later referenced in the program then
Absolute Mode is used to access the variables.

 Consider the following statement in high level language


A=B+6
Assuming A and B are earlier declared as variables, this statement may be
compiled as
Move B,R1
Add #6,R1
Move R1,A

2
Module 2 Computer Organization

3
Module 2 Computer Organization

 Consider a C language statement


A = *B;
where B is a pointer variable. This statement may be compiled as
Move B,R1
Move (R1),A
 Using Indirect addressing through memory, the same action can be achieved
with
Move (B),A

4
Module 2 Computer Organization

 The constant X is usually represented by fewer bits than the word length of the
computer.
 Since X is a signed integer, it is sign extended to the register length before being
added to the contents of the register.

5
Module 2 Computer Organization

Let us see the usefulness of Index Addressing


 Consider an example involving a list of test scores for students taking a given
course.
 Assume that the list of scores beginning at location LIST is structured as shown in
the fig. 2.14

 A four word memory block comprises a record that stores the relevant information
for each student (Identification number, three test scores).
 There are n students in the class
 Suppose we wish to compute the sum of all scores obtained on each of the tests
and store in locations SUM1, SUM2, SUM3.

6
Module 2 Computer Organization

 A program for this is shown below

 R0 is set to the ID location of the first student.


 Registers R1, R2, R3 are cleared.
 Index addressing is used in the LOOP to access the locations of the 3 test marks.
 The running sums is held in these three registers.
 The memory location N contains the number of students n which is stored in
register R4.
 The register R0 is incremented by 16 to point to the ID of the next student.
 Register R4 is decremented.
 The LOOP executes until R4 becomes 0.
 The sum in R1, R2, R3 are stored in memory locations SUM1, SUM2, SUM3.

7
Module 2 Computer Organization

 Let us again consider the program in fig. 2.12, Assume that the four instructions
of the loop body starting at LOOP, are located at memory locations 1000, 1004,
1008, 1012. Hence the updated contents of the PC at the time the branch target
address is generated is 1016. To branch to location LOOP (1000), the offset
value needed is X = -16.

8
Module 2 Computer Organization

 The increment is depending on the size of the operand.


 Since the size of the operand is specified as part of the operation code of an
instruction, the above indication is sufficient.

 These symbolic names are known as mnemonics

9
Module 2 Computer Organization

 Assembler directives are used by assembler while it translates a source program


into an object program.

Consider the example of a program in figure 2.17.

10
Module 2 Computer Organization

It shows the memory addresses where the machine instructions and required data items
are to be found after program is loaded for execution.
If the assembler is to produce an object program according to this arrangement it has to
know

To provide this information the source program may be written as shown in fig 2.18

11
Module 2 Computer Organization

 A key part of the assembly process is determining the values that replace the
names.
 In some instructions, the name is defined in the Label field of a given instruction,
the value represented by the name is determined by the location of this
instruction in the assembled object program.

12
Module 2 Computer Organization

 Hence assembler must keep track of addresses as it generates the machine


code for successive instructions. For example, START and LOOP will be
assigned the values 100 and 112 respectively.
 In case of Branch instructions this method is not employed. A Branch instruction
is usually implemented in machine code by specifying the branch target using
Relative addressing mode. The assembler computes the branch offset, which is
the distance to the target and puts it into the machine instruction.
 As assembler scans through a source program, it keeps track of all names and
numerical values that correspond to them in a symbol table.
 Thus when the name appears for the second time, it is replaces with its value
from the table.
 A problem arises when a name appears as an operand before it is given a value
(forward branching instructions).
 So assembler scans through the source program twice, first pass to create the
complete symbol table and second pass to substitute value of all names from
symbol table. Such an assembler is called two pass assembler.

 Executing the loader performs a sequence of input operations needed to


transfer machine language program from disk into specified place in memory.
 The loader must know the length of the program and address where it has to
be stored.
 The assembler places this information in a header preceding the object code.
 The loader starts execution of the object program by branching to the first
instruction to be executed.

13
Module 2 Computer Organization

Number Notation:

 Most assemblers allow numerical values to be specified in different


ways.
 For example, Consider a number 93 is used as an immediate operand
 It can be given in following ways:
o Decimal: ADD #93,R1
o Binary: ADD #%01011101,R1
o Hexadecimal: ADD #$5D,R1

Basic Input/Output Operations:

 Consider a task that reads a character input from keyboard and


produces a character output on display screen.
 A method called program-controlled I/O is used for performing such
tasks.
 The rate of data transfer from keyboard to computer may be few
characters per second but the rate of data transfer from computer to
the display may be thousands of characters per second.
 A solution to this problem is as follows: On output, the processor
sends the first character and waits for a signal from the display that
the character has been received. Similarly on the input side, the
processor waits for a signal that a character key has been struck. Then
the processor proceeds to read operation. This operation can be
represented in the figure below.

14
Module 2 Computer Organization

 The following sequence of operations can be used for transferring a


character from DATAIN to register R1 as well as for transferring a
character from DATAOUT to display

 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 these registers. Data
can be transferred between these registers and the processor using
instructions such as Move, Load or Store.
 For example, the contents of keyboard character buffer DATAIN can be
transferred to register R1 in the processor by the instruction
MoveByte DATAIN,R1
MoveByte operation code signifies that the operand size is a byte as
compared to Move instruction where operand size is a word.
 Similarly contents of register R1 can be transferred to DATAOUT by
instruction
MoveByte R1,DATAOUT

15
Module 2 Computer Organization

 However, it is most common to include SIN and SOUT in device status


registers where bit b3 in registers INSTATUS and OUTSTATUS
corresponds to SIN and SOUT respectively.
 The read instruction may now be implemented as

 The write operation may be implemented as

 The Testbit instruction tests the state of one bit. If the bit tested is
equal to 0 then the condition of branch instruction is true and the
branch is made to the beginning of the wait loop. When it is 1 then the
data is read from input buffer or written into output buffer.
 The program below reads a line of character and displays it

16
Module 2 Computer Organization

 The program finishes when the carriage return character is read,


stored and sent to the display.
Disadvantages of Program Controlled I/O: It requires continuous
involvement of the processor. All execution time of the program is spent by
the processor waiting for a character to be struck or the display to become
available. This waste of time can be avoided by using interrupts.

STACKS

 A stack is a list of data elements that 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 insertion operation is called push operation and deletion operation
is called pop operation.
 This structure is also referred to as pushdown stack or Last In First Out
stack.
 Stack Pointer is a register used to keep track of address of elements of
the stack that is at the top at any given time.
 The figure below shows a stack

17
Module 2 Computer Organization

 If we consider 32 bit word length, then push operation can be


implemented as

 These two instructions move a 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

 These two instructions move the top value from the stack into location
ITEM and increment the SP by 4 so that it points at the new top
element.
 Push and Pop operations can be implemented using single instructions
using Autoincrement and Autodecrement addressing modes

 The figure below shows the result of Push and Pop operations

18
Module 2 Computer Organization

 We must avoid pushing an item into the stack when it has reached its
maximum size and also avoid attempting to pop an item from an
empty stack.
 The routine shown below checks for safe pushing and popping

QUEUES

 Here data are stored and retrieved from First In First Out basis.
 New data are added at the back and retrieved from the front of the
queue.

19
Module 2 Computer Organization

Differences between stack and queue

Stack Queue
One end is fixed while other Both ends of queues move to higher
end rises and falls as data addresses as data is pushed and
are pushed and popped. popped.
A single pointer is needed to Two pointers are needed to keep track
point to the top of the stack of two ends of the queue.
In stack care must be taken Queue would continuously move
to see to it that nothing is through the memory of the computer in
pushed into a stack which is the direction of higher addresses. This
full and nothing is popped can be limited by using a circular
out from the stack which is buffer. Let us assume that memory
empty. addresses from BEGINNING to END are
assigned to the queue. The first entry
in the queue is entered into
BEGINNING and successive entries are
appended at higher addresses. By the
time queue reaches END, space would
be created in the beginning. So back
pointer is reset to BEGINNING and the
process continues.

SUBROUTINES

 A subroutine is a particular subtask which performs many times on


different data values.
 It helps in saving space as only one copy of subroutine is saved in
memory and called many times.
 A Call instruction causes a branch to the subroutine.
 A subroutine is said to return to the calling program by executing a
Return instruction.
 The contents of the Program Counter must be saved by the Call
Instruction to enable correct return to the calling program.
 This is possible by subroutine-linkage method.
 In this method a register called link register is used. When a
subroutine is called, the address in the PC is stored in link register
before branching. The Return instruction then returns to the calling
program by branching indirectly through link register.
 The Call instruction performs the following operations

20
Module 2 Computer Organization

o Store the contents on PC in the link register.


o Branch to target address specified by the instruction.
 The Return instruction performs the following operations
o Branch to the address contained in the link register.
 The figure below illustrates this procedure

SUBROUTINE NESTING AND THE PROCESSOR STACK

 Subroutine Nesting is a process in which one subroutine calls another


subroutine.
 In this case the return address of second call is also stored in link
register destroying the previous contents.

21
Module 2 Computer Organization

 Hence it is essential to store the contents of the link register in some


other place before replacing, otherwise the return address for the first
call will be lost.

PARAMETER PASSING

22
Module 2 Computer Organization

 The first four instructions constitute the relevant part of the calling
program.
 The first two instructions load n and NUM1 into R1 and R2.
 The Call instruction branches to subroutine at location LISTADD. This
instruction also pushes the return address onto the processor stack.
 The subroutine computes the sum and places it in R0.
 After the return operation, the sum is then stored in memory location
SUM by the calling program.

Figure 2.26

 If many parameters are involved then a stack can be used.


 In the fig 2.26a, the parameters passed to the subroutine are the
address of first number in the list and the number of entries.
 The subroutine computes the addition and returns the sum.
 The parameters are pushed on to the processor stack pointed to by
register SP.

23
Module 2 Computer Organization

 Assume that before the subroutine is called, the top of the stack is at
level 1 in fig 2.26b.
 The calling program pushes the address NUM1 and the value n onto
the stack and calls the subroutine LISTADD.
 The Call instruction also pushes the return address onto the stack. The
top of the stack is now at level 2.
 The subroutine uses three registers. Their contents have to be saved
by pushing them onto the stack.
 MoveMultiple is a single instruction to store contents of register R0 to
R2 onto the stack.
 The top of the stack is now at level 3.
 The subroutine accesses the parameters n and NUM1 from the stack
using index addressing.
 Then n is loaded into R1 and address NUM1 into R2.
 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 NUM1 which is no longer needed.
 The contents of three registers used by the subroutine are restored
from the stack.
 Now the top item on stack is return address at level 2.
 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.
 In fig 2.25, the calling program passes the address of the first element
in the list. This is known as passing by reference.
 In fig 2.26, actual number of entries is passed to the subroutine. This
is known as passing by value.

24
Module 2 Computer Organization

25
Module 2 Computer Organization

ADDITIONAL INSTRUCTIONS

1. Logic Instructions:
Some of the important logic operations are AND, NOT, OR.
NOT dst
instruction complements all bits contained in the destination operand.

instructions can be used to determine the 2's complement of a


number. Same operation can be carried out by other instruction
Negate R0
Let us take an example where the program must branch when the
leftmost character is 'Z' in the 32 bit register R0. The following set of
instructions can be used to carry out this function.

In these instructions the rightmost 3 characters are reset by using


AND operation and then the result is compared with character Z whose
ASCII code is 01011010 (5A). If the comparison is true then branching
is done.

2. Shift and Rotate Instructions:


There are two types of shifts. Logical and Arithmetic.
a. Logical Shift:

26
Module 2 Computer Organization

Consider the following routine that packs two BCD digits

Suppose that two decimal digits represented in ASCII code are located
in memory at byte locations LOC and LOC+1. Each of these digits have
to be represented in 4 bit BCD code and stored in a single byte
location PACKED. The result is said to be in packed-bcd format.

b. Arithmetic Shifts:

In arithmetic shift the vacant positions are replaced by the sign bit
which the most significant bit whereas in logical shift, the vacant

27
Module 2 Computer Organization

positions are replaced by '0'. This is necessary because sometimes


overflow may occur in shifting left and remainder may be lost in shifting
right.
3. Rotate Operation
In shift operations, the bits shifted are lost except for the last bit which
is retained in the carry flag (C). All these bits are preserved by rotate
operations. There are left and right rotate instructions. There are two
versions of both left and right rotate instructions. In one version, the
bits of operand are simply rotated. In another version, rotation
includes the carry flag C. The figure below shows both left and right
rotate operations with and without carry flag.
RotateL, RotateLC, RotateR, RotateRC represent the instructions for
rotate operations.

28
Module 2 Computer Organization

29
Module 2 Computer Organization

4. Multiplication and Division:


Two signed numbers can be multiplied or divided using
instructions Multiply and Divide.

performs the operation

If two n bit numbers are multiplied, the product is generally 2n bits


which may not fit into the register Rj. In such cases the product is
generally stored in two registers Rj and Rj+1 where lower order half in
register Rj and higher order half in Rj+1
The instruction

performs the operation

The quotient may be placed in Rj and the remainder may be placed in


Rj+1 or may be lost.
However some computers also perform these operations by some
basic operations like Add, Subtract, Shift and Rotate.

1. Which is the most appropriate matching for the following pairs

Column 1 Column 2
X:Indirect Addressing Loops
Y:Immediate Addressing Pointers
Z:Auto Decrement Addressing Constants

2. Which of the following addressing modes are suitable for program relocation at run time
a. Absolute Addressing
b. Base Addressing
c. Relative Addressing
d. Indirect Addressing
3. What is the most appropriate match for the items in the first column with the items in the
second column-

Column-1: Column-2:
X: Indirect addressing 1. Array implementation
Y: Indexed addressing 2. Writing relocatable code
Z: Base register addressing 3. Passing array as parameter

30
Module 2 Computer Organization

4.

5.

6.

31

You might also like