CO UNIT-2 Notes
CO UNIT-2 Notes
ADDRESSING MODES
A program operates on data that reside in the computer‟s memory. These
data can be organized in a variety of ways.
The different ways in which the location of an operand is specified in an
instruction are referred to as addressing modes.
Move #200, R0
places the value 200 in register R0. Clearly, the Immediate mode(#) is
only used to specify the value of a source operand.
Constant values are used frequently in high-level language programs. For
example, the statement
A=B+6
contains the constant 6. Assuming that A and B have been declared as
variables and may be accessed using the Absolute mode, this statement
may be compiled as follows:
Move B,R1
Add #6,R1
Move R1,A
To execute the Add instruction in Figure (a), the processor uses the value
B, which is in register R1, as the effective address of the operand. It
requests a read operation from the memory to read the contents of
location, B. The value read is the desired operand, which the processor
adds to the contents of register R0.
Indirect addressing through a memory location is also possible as shown
in Figure (b). In this case, the processor first reads the contents of
memory location A and then, requests a second read operation using the
value B as an address to obtain the operand.
The register or memory location that contains the address of an operand
is called a pointer.
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.
The following Figure illustrates two ways of using the Index mode. 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 present.
RELATIVE ADDRESSING
The Index mode is defined by using general-purpose processor registers.
A useful version of this mode is obtained if the program counter, PC, is
used instead of a general purpose register. Then, X(PC) can be used to
address a memory location that is X bytes away from the location
presently pointed to by the program counter. Since the addressed
location is identified “relative” to the program counter, the name Relative
mode is associated with this type of addressing.
Relative mode — The effective address is determined by the Index mode
using the program counter in place of the general-purpose register Ri.
Smt. Kavitha M, Assistant professor, Dept. of CSE, SIT, Tumkur-3 Page 5
Computer Organization - Unit-2
This mode can be used to access data operands. But, its most common
use is to specify the target address in branch instructions. An instruction
such as
Branch>0 LOOP
causes program execution to go to the branch target location identified
by the name LOOP if the branch condition is satisfied. This location can
be computed by specifying it as an offset from the current value of the
program counter. Since the branch target may be either before or after
the branch instruction, the offset is given as a signed number.
ADDITIONAL MODES
The following two modes are useful for accessing data items in successive
locations in the memory.
Autoincrement mode — The effective address of the operand is the
contents of a register specified in the instruction. After accessing the
operand, the contents of this register are automatically incremented to
point to the next item in a list.
We denote the Autoincrement mode by putting the specified register in
parentheses, to show that the contents of the register are used as the
effective address, followed by a plus sign to indicate that these contents
are to be incremented after the operand is accessed. Thus, the
Autoincrement mode is written as
(Ri )+
Implicitly, the increment amount is 1 when the mode is given in this
form. But in a byte addressable memory, this mode would only be useful
in accessing successive bytes of some list. To access successive words in
a byte-addressable memory with a 32-bit word length, the increment
must be 4.
Computers that have the Auto increment mode automatically increment
the contents of the register by a value that corresponds to the size of the
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.
The keyboard and the display are separate devices as shown in
Figure 2.19.
Read operation:
A program monitors SIN flag, and when SIN is set to 1, the processor
reads the contents of DATAIN buffer associated with the keyboard. 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.
Write Operation:
A program monitors SOUT flag, and when SOUT is set to 1, the processor
transfers a character code to DATAOUT buffer associated with the
display. 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.
Let us assume that bit b3 in OUTSTATUS register corresponds to SOUT
flag. The write operation just described may now be implemented by the
machine instruction sequence
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
instruction 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 into the
output buffer.
The buffer registers DATAIN and DATAOUT and the status flags SIN and
SOUT are part of circuitry commonly known as a device interface.
As the characters are read in, one by one, they are stored in a data area
in the memory and then echoed back out to the display. The program
finishes when carriage return, CR, is read. The address of the first byte
location of the memory where the line is to be stored is LOC. Register R0
is used to point to this area, and it is initially loaded with the address
STACKS
begins. 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.
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 onto the stack, they are placed in successively
lower address locations.
Subtract #4,SP
Move NEWITEM,(SP)
where, the Subtract instruction subtracts the source operand 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.
Move (SP),ITEM
Add #4,SP
These two instructions move the top value from the stack into location
ITEM and then increment the stack pointer by 4 so that it points to the
new top element.
If the processor has the Autoincrement and Autodecrement addressing
modes, then the push operation can be performed by the single
instruction
Move NEWITEM,−(SP)
Move (SP)+,ITEM
QUEUES
Another useful data structure that is similar to the stack is called a queue.
Data are stored in and retrieved from a queue on a first-in–first-out (FIFO)
basis. Thus, if we assume that 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.
There are two important differences between how a stack and a queue are
implemented. One end of the stack is fixed (the bottom), while the other
end rises and falls as data are pushed and popped. 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.
SUBROUTINES
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.
PARAMETER PASSING
Figure 2.25 shows how the program for adding a list of numbers can be
implemented as a subroutine, with the parameters passed through
registers.
The size of the list, n, contained in memory location N, and the address,
NUM1, of the first number, are passed through registers R1 and R2.
The calling program pushes the address NUM1 and the value n onto the
stack and calls 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. Since these registers may contain
valid data that belong to the calling program, their contents should be
saved by pushing them onto the stack. We have used a single
instruction, MoveMultiple, to store the contents of registers R0 through
R2 on the stack. Many processors have such instructions. The top of the
stack is now at level 3. The subroutine accesses the parameters n and
NUM1 from the stack using indexed addressing. Note that it does not
change the stack pointer because valid data items are still at the top of
the stack. The value n is loaded into R1 as 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 the three registers used
by the subroutine are restored from the stack. Now the top item on the
stack is the 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.
Note the nature of the two parameters, NUM1 and n, passed to the
subroutines in Figures 2.25 and 2.26. The purpose of the subroutines is
to add a list of numbers. Instead of passing the actual list entries, the
calling program passes the address of the first number in the list. This
technique is called passing by reference. The second parameter is passed
by value, that is, the actual number of entries, n, is passed to the
subroutine.
During execution of the subroutine, six locations at the top of the stack
contain entries that are needed by the subroutine. These locations
constitute a private work space for the subroutine, created at the time
the subroutine is entered and freed up when the subroutine returns
control to the calling program. Such space is called a stack frame.
If the subroutine requires more space for local memory variables, they
can also be allocated on the stack.
Move FP,−(SP)
Move SP,FP
After these instructions are executed, both SP and FP point to the saved
FP contents.
Space for the three local variables is now allocated on the stack by
executing the instruction
Subtract #12,SP
Add #12,SP
and pops the saved old value of FP back into FP. At this point, SP points
to the return address, so the Return instruction can be executed,
transferring control back to the calling program.
The calling program is responsible for removing the parameters from the
stack frame, some of which may be results passed back by the
subroutine. The stack pointer now points to the old TOS.
FUNDAMENTAL CONCEPTS
The instructions of a program are loaded in sequential locations in main
memory. To execute a program, processor fetches one instruction at a time and
performs the operations specified. The processor keeps track of the address of
the memory location containing the next instruction using a dedicated register,
called the Program Counter (PC). After fetching an instruction, the contents of
PC are updated to point to the next instruction in the sequence. A branch
instruction loads a new address into the PC.
Let us assume that each instruction is of 4 bytes and is stored in one memory
word. To execute an instruction, the processor has to perform the
following three steps:
1. Fetch the contents of the memory location pointed to by the PC. The
contents of this location are interpreted as instruction to be executed.
They are loaded into another special purpose register called
Instruction Register (IR). Symbolically, this operation is denoted as
IR [PC].
If the instruction occupies more than one word, step 1 and 2 must be
repeated so as to fetch the complete instruction. These two steps are
usually referred as fetch phase; step 3 constitutes the execution
phase.
To know how these operations are implemented, we need to know the
internal structure of the processor. The various functional blocks of the
CPU can be organized and interconnected in a variety of ways. The
The data and address lines of the external memory bus are connected to
the internal processor bus via Memory Data Register (MDR) and the
Memory Address Register (MAR). Data may be loaded into MDR either
from the memory bus or from internal processor bus. Data stored in
MDR may be placed on either bus. The input of the MAR is connected to
the internal bus and its output is connected to the external bus.
The control lines of the memory bus are connected to the instruction
decoder and control logic block. This unit is responsible for issuing the
signals that control the operation of all the units inside the processor
and for interacting with the memory bus.
The number and function of processor register R0 through R(n-1) vary
from processor to processor. Some may be general purpose; others may
special purpose like stack pointer or index registers.
Three registers Y, Z & TEMP are transparent to the programmmer, i.e,
they are never referred directly by an instruction. They are used by the
processor for temporary storage during execution of some instructions.
The multiplexer MUX selects either the output of the register Y or a
constant value 4.This is provided as input A to the ALU. The constant 4
is used to increment the contents of PC. The B input of the ALU is
obtained directly from the processor bus. Thus, the „select‟ line of MUX
can be either select4 or select Y.
The registers, the ALU and the interconnecting bus are collectively
referred to as „data path’.
REGISTER TRANSFERS :
Register gating and timing of data transfers:
When Rin is set to 1, the data on the bus are loaded into Ri and when
Rout is set to 1, the contents of Ri are placed on the bus. When Rout or
Rin is 0, data cannot be transferred between the processor bus and the
register.
Example:
All operations and data transfers within the processor takes place within
time periods defined by the processor clock.
The control signals that govern a particular transfer are asserted at the
start of the clock cycle. (In above example, R1out and R4in are set to 1).
The registers consist of edge-triggered flip-flops. Hence, at the next active
edge of the clock, the flip-flops that constitute R4 will load the data
present at their inputs. At the same time, the control signals R1out and
R4in will return to 0.
When edge-triggered flip-flops are not used, two or more clock signals
may be needed to guarantee proper transfer of data. This is known as
multiphase clocking.
Example:
1. R1out, Yin
3. Zout, R3in
The signals are activated for the duration of the clock cycle
corresponding to that step, with all other signals deactivated during that
time.
In step 1, output of register R1 and inputs of register Y are enabled,
causing the contents of R1 to be transferred via the bus to Y register.
In step 2, select Y signal of MUX is selected, so the contents of Y register
is transferred to input A of the ALU. Also, contents of R2 are gated onto
the bus and, hence, to the input B of ALU. The add control signal of ALU
is set , and hence addition is performed. The result is moved into register
Z since Zin=1.
In step 3, contents of Z are transferred to destination register R3 using
Zout and R3in signals. This is an additional step, since only one register
output can be enabled during a clock cycle in a single bus
structure.
When the control input MDRinE =1, the multiplexer selects the data
on the external memory bus. This data will be loaded into the flip-
flop at the rising edge of the clock.
The Q output of the flip-flop is connected to the bus via another
tri-state gate.
When MDRoutE =0, the gate‟s output is in the high-impedence state.
This corresponds to the open-circuit state of the switch.
When MDRoutE=1, the gate drives the external memory bus to 0 or
1, depending on the value of Q.
Assume that output of MAR is enabled all the time and its contents are
always available on the address lines of the memory bus.
When a new address is loaded into MAR, it will appear on the memory
bus at the beginning of the next clock cycle, as shown in fig7.5. Read
control signal is activated at the same time MAR is loaded.
Read control signal will cause the bus interface circuit to send a read
command, MR, on the bus.
MDRinE is set to 1 for exactly the same period as the read command, MR.
MDRinE is activated while waiting for the response from the memory.
The data received from the memory are loaded into MDR at the end of the
clock cycle in which the MFC signal is received.
In the next clock cycle, MDRout is activated to transfer the data to register
R2.
As in case of the read operation, the Write control signal causes the memory
bus interface hardware to issue a Write command on the memory bus. The
processor remains in step3 until the memory operation is completed and an
MFC response is received.