Subject: Computer Organization Sub Code: 21Cs34 Semester: 3
Subject: Computer Organization Sub Code: 21Cs34 Semester: 3
21CS34
www.vtuupdates.com Page 1
COMPUTER 21CS34
ORGANIZATION
MODULE 1: BASIC STRUCTURE OF COMPUTERS
Introduction
BASIC
CONCEPTS
• Computer Architecture (CA) is concerned with the structure and behaviour of the computer.
• CA includes the information formats, the instruction set and techniques for addressing memory.
• In general covers, CA covers 3 aspects of computer-design namely: 1) Computer Hardware, 2)
Instruction set Architecture and 3) Computer Organization.
1. Computer Hardware
It consists of electronic circuits, displays, magnetic and optical storage media and
communication facilities.
2. Instruction Set Architecture
It is programmer visible machine interface such as instruction set, registers, memory
organization and exception handling.
Two main approaches are 1) CISC and 2)RISC.
(CISC Complex Instruction Set Computer, RISC Reduced Instruction Set
Computer)
3. Computer Organization
It includes the high level aspects of a design, such as
→ memory-system
→ bus-structure &
→ design of the internal CPU.
It refers to the operational units and their interconnections that realize the architectural
specifications.
It describes the function of and design of the various units of digital computer that store and
process information.
FUNCTIONAL UNITS
• A computer consists of 5 functionally independent main parts:
1) Input
2) Memory
3) ALU
4) Output&
5) Control units.
www.vtuupdates.com Page 2
COMPUTER 21CS34
ORGANIZATION
www.vtuupdates.com Page 3
COMPUTER 21CS34
ORGANIZATION
www.vtuupdates.com Page 4
COMPUTER 21CS34
ORGANIZATION
BUS STRUCTURE
•A bus is a group of lines that serves as a connecting path for several devices.
•A bus may be lines or wires.
•The lines carry data or address or control signal.
•There are 2 types of Bus structures: 1) Single Bus Structure and 2) Multiple Bus Structure.
1) Single Bus Structure
Because the bus can be used for only one transfer at a time, only 2 units can actively use the
bus at any given time.
Bus control lines are used to arbitrate multiple requests for use of the bus.
Advantages:
1. Low cost&
2. Flexibility for attaching peripheral devices.
2) Multiple Bus Structure
Systems that contain multiple buses achieve more concurrency in operations.
Two or more transfers can be carried out at the same time.
Advantage: Better performance.
Disadvantage: Increased cost.
www.vtuupdates.com Page 5
COMPUTER 21CS34
ORGANIZATION
PERFORMANCE
• The most important measure of performance of a computer is how quickly it can execute programs.
• The speed of a computer is affected by the design of
1) Instruction-set.
2) Hardware & the technology in which the hardware is implemented.
3) Software including the operating system.
• Because programs are usually written in a HLL, performance is also affected by the compiler that
translates programs into machine language. (HLL High Level Language).
• For best performance, it is necessary to design the compiler, machine instruction set and hardware in a
co-ordinated way.
www.vtuupdates.com Page 6
COMPUTER 21CS34
ORGANIZATION
• Let us examine the flow of program instructions and data between the memory & the processor.
• At the start of execution, all program instructions are stored in the main-memory.
• As execution proceeds, instructions aare fetched into the processor, and a copy is placed in the cache.
• Later, if the same instruction is needed a second time, it is read directly from the cache.
• A program will be executed faster
if movement of instruction/data between the main-memory
main and the processor is minimized
which is achieved by using the cache.
PROCESSOR CLOCK
• Processor circuits are controlled by a timing signal called a Clock.
• The clock defines regular time intervals called Clock Cycles.
• To execute a machine instruction, the processor divides the action to be performed into a sequence of
basic steps such that each step can be completed in one clock cycle.
• Let P = Length of one clock
cycle R = Clock rate.
• Relation between P and R is given by
(1)
• Equ1 is referred to as the basic performance equation.
• To achieve high performance, the computer designer must reduce the value of T, which means
reducing N and S, and increasing R.
The value of N is reduced if source program is compiled into fewer machine instructions.
The value of S is reduced if instructions have a smaller number of basic steps to perform.
The value of R can be increased by using a higher frequency clock.
• Care has to be taken while modifying values since changes in one parameter may affect the other.
CLOCK RATE
• There are 2 possibilities for increasing the clock rate R:
1) Improving the IC technology makes logic logic-circuits faster.
This reduces the time needed to compute a basic step. (IC integrated circuits).
www.vtuupdates.com Page 7
COMPUTER 21CS34
ORGANIZATION
This allows the clock period P to be reduced and the clock rate R to be increased.
2) Reducing the amount of processing done in one basic step also reduces the clock period P.
• In presence of a cache, the percentage of accesses to the main
main-memory is small.
Hence, much of performance--gain
gain expected from the use of faster technology can be realized.
The value of T will be reduced by same factor as R is increased ‘.’ S & N are not affected.
Let us assume that both operations take 3 clock cycles each for completion.
• As shown in above figure, 6 clock cycles are required to complete two operations.
PERFORMANCE MEASUREMENT
• Benchmark refers to standard task used to measure how well a processor operates.
• The Performance Measure is the time taken by a computer to execute a given benchmark.
• SPEC selects & publishes the standard programs along with their test results for different aapplication
domains. (SPEC System Performance Evaluation Corporation).
www.vtuupdates.com Page 8
COMPUTER 21CS34
ORGANIZATION
• SPEC Rating is given by
Problem 1:
List the steps needed to execute the machine instruction:
Load R2, LOC
in terms of transfers between the components of processor and some simple control commands. Assume
that the address of the memory-location
location containing this instruction is initially in register PC. Solution:
1. Transfer the contents of register PC to register MAR.
2. Issue a Read command to memory.
And, then wait until it has transferred the requested word into register MDR.
3. Transfer the instruction from MDR into IR and decode it.
www.vtuupdates.com Page 9
COMPUTER 21CS34
ORGANIZATION
4. Transfer the address LOCA from IR to MAR.
5. Issue a Read command and wait until MDR is loaded.
6. Transfer contents of MDR to the ALU.
7. Transfer contents of R0 to the ALU.
8. Perform addition of the two operands in the ALU and transfer result intoR0.
9. Transfer contents of PC to ALU.
10. Add 1 to operand in ALU and transfer incremented address to PC.
Problem 2:
List the steps needed to execute the machine instruction:
Add R4, R2, R3
in terms of transfers between the components of processor and some simple control commands. Assume
that the address of the memory-location containing this instruction is initially in register PC. Solution:
1. Transfer the contents of register PC to register MAR.
2. Issue a Read command to memory.
And, then wait until it has transferred the requested word into register MDR.
3. Transfer the instruction from MDR into IR and decode it.
4. Transfer contents of R1 and R2 to the ALU.
5. Perform addition of two operands in the ALU and transfer answer into R3.
6. Transfer contents of PC to ALU.
7. Add 1 to operand in ALU and transfer incremented address to PC.
Problem 3:
(a) Give a short sequence of machine instructions for the task “Add the contents of memory-location A
to those of location B, and place the answer in location C”. Instructions:
Load Ri,
LOC
and
Store Ri, LOC
are the only instructions available to transfer data between memory and the general purpose registers. Add
instructions are described in Section 1.3. Do not change contents of either location A or B.
(b) Suppose that Move and Add instructions are available with the formats:
Move Location1,
Location2 and
Add Location1, Location2
These instructions move or add a copy of the operand at the second location to the first location,
overwriting the original operand at the first location. Either or both of the operands can be in the memory
or the general-purpose registers. Is it possible to use fewer instructions of these types to accomplish the
task in part (a)? If yes, give the sequence.
Solution:
(a)
Load A,
R0
Load B,
www.vtuupdates.com Page 10
COMPUTER 21CS34
ORGANIZATION
R1 Add
R0, R1
Store R1, C
(b) Yes;
Move
B, C
Add A,
C
Problem 4:
(a) Program execution time T is to be examined for a certain high-level language program. The program
can be run on a RISC or a CISC computer. Both computers use pipelined instruction execution, but
pipelining in the RISC machine is more effective than in the CISC machine. Specifically, the effective
value of S in the T expression for the RISC machine is 1.2, bit it is only 1.5 for the CISC machine. Both
machines have the same clock rate R. What is the largest allowable value for N, the number of
instructions executed on the CISC machine, expressed as a percentage of the N value for the RISC
machine, if time for execution on the CISC machine is to be longer than on the RISC machine?
(b) Repeat Part (a) if the clock rate R for the RISC machine is 15 percent higher than that for the CISC
machine.
Solution:
(a) Let TR = (NR X SR)/RR& TC = (NC X SC)/RC be execution times on RISC and CISC
processors. Equating execution times and clock rates, we have
1.2NR = 1.5NC
Then
NC/NR = 1.2/1.5 = 0.8
Therefore, the largest allowable value for NC is 80% of NR.
Problem 5:
(a) Suppose that execution time for a program is proportional to instruction fetch time. Assume that
fetching an instruction from the cache takes 1 time unit, but fetching it from the main-memory takes 10
time units. Also, assume that a requested instruction is found in the cache with probability 0.96. Finally,
assume that if an instruction is not found in the cache it must first be fetched from the main- memory into
the cache and then fetched from the cache to be executed. Compute the ratio of program execution time
without the cache to program execution time with the cache. This ratio is called the speedup resulting
from the presence of the cache.
(b) If the size of the cache is doubled, assume that the probability of not finding a requested instruction
there is cut in half. Repeat part (a) for a doubled cache size.
www.vtuupdates.com Page 11
COMPUTER 21CS34
ORGANIZATION
Solution:
(a) Let cache access time be 1 and main-memory access time be 20. Every instruction that is
executed must be fetched from the cache, and an additional fetch from the main-memory must be
performed for 4% of these cache accesses.
Therefore,
(b)
Problem 6:
A program contains 1000 instructions. Out of that 25% instructions requires 4 clock cycles,40%
instructions requires 5 clock cycles and remaining require 3 clock cycles for execution. Find the total
time required to execute the program running in a 1 GHz machine.
Solution:
N = 1000
25% of N= 250 instructions require 4 clock cycles.
40% of N =400 instructions require 5 clock
cycles. 35% of N=350 instructions require 3
clock cycles.
T = (N*S)/R= (250*4+400*5+350*3)/1X109 =(1000+2000+1050)/1*109= 4.05 μs.
Problem 7:
For the following processor, obtain the performance.
Clock rate = 800 MHz
No. of instructions executed = 1000
Average no of steps needed / machine instruction = 20
Solution:
www.vtuupdates.com Page 12
COMPUTER 21CS34
ORGANIZATION
www.vtuupdates.com Page 13
COMPUTER 21CS34
ORGANIZATION
BYTE-ADDRESSABILITY
• In byte-addressable memory, successive addresses refer to successive byte locations in the memory.
• Byte locations have addresses 0, 1, 2. . . ..
• If the word-length is 32 bits, successive words are located at addresses 0, 4, 8. . with each word having
4bytes.
• Consider a 32-bit integer (in hex): 0x12345678 which consists of 4 bytes: 12, 34, 56, and78.
Hence this integer will occupy 4 bytes in memory.
Assume, we store it at memory address starting1000.
www.vtuupdates.com Page 14
COMPUTER 21CS34
ORGANIZATION
On little-endian, memory will look like
Address Value
1000 78
1001 56
1002 34
1003 12
WORD ALIGNMENT
• Words are said to be Aligned in memory if they begin at a byte-address that is a multiple of the
number of bytes in a word.
• For example,
If the word length is 16(2 bytes), aligned words begin at byte-addresses 0, 2, 4 . . . ..
If the word length is 64(2 bytes), aligned words begin at byte-addresses 0, 8, 16 . . . ..
• Words are said to have Unaligned Addresses, if they begin at an arbitrary byte-address.
MEMORY OPERATIONS
• Two memory operations are:
1) Load (Read/Fetch)&
2) Store(Write).
• The Load operation transfers a copy of the contents of a specific memory-location to the processor.
The memory contents remain unchanged.
• Steps for Load operation:
1) Processor sends the address of the desired location to the memory.
2) Processor issues ‘read’ signal to memory to fetch the data.
3) Memory reads the data stored at that address.
4) Memory sends the read data to the processor.
www.vtuupdates.com Page 15
COMPUTER 21CS34
ORGANIZATION
• The Store operation transfers the information from the register to the specified memory-location.
This will destroy the original contents of that memory-location.
• Steps for Store operation are:
1) Processor sends the address of the memory-location where it wants to store data.
2) Processor issues ‘write’ signal to memory to store the data.
3) Content of register(MDR) is written into the specified memory-location.
Move LOC, R1 Transfer data from memory-location LOC to register R1. The contents of
LOC are unchanged by the execution of this instruction, but the old
contents of register R1 are overwritten.
Add R1, R2, R3 Add the contents of registers R1 and R2, and places their sum into register
R3.
www.vtuupdates.com Page 16
COMPUTER 21CS34
ORGANIZATION
Instruction Syntax Example Description Instructions
Type for
Operation
Three Opcode Add A,B,C Add the contents of
Addres Source1,Source2,D memory-locations A & B.
s estination Then, place the result into
location C.
Two Address Opcode Source, Add A,B Add the contents of Move B, C
Destination memory-locations A & B. Add A, C
Then, place the result into
location B, replacing the
original contents of
this location.
Operand B is both a source
and a destination.
Zero Opcode [no Push Locations of all operands are Not possible
Addres Source/Destination] defined implicitly.
s The operands are stored in a
pushdown stack.
• Access to data in the registers is much faster than to data stored in memory-locations.
• Let Ri represent a general-purpose register. The instructions:
Load A,Ri
Store Ri,A
Add A,Ri
are generalizations of the Load, Store and Add Instructions for the single-accumulator case, in which
register Ri performs the function of the accumulator.
• In processors, where arithmetic operations as allowed only on operands that are in registers, the task
C<-[A]+[B] can be performed by the instruction sequence:
Move A,Ri
Move B,Rj
Add Ri,Rj
Move Rj,C
www.vtuupdates.com Page 17
COMPUTER 21CS34
ORGANIZATION
Program Explanation
• Consider the program for adding a list of n numbers (Figure2.9).
• The Address of the memory-locations containing the n numbers are symbolically given as NUM1,
NUM2…..NUMn.
• Separate Add instruction is used to add each number to the contents of registerR0.
• After all the numbers have been added, the result is placed in memory-location SUM.
BRANCHING
• Consider the task of adding a list of ‘n’ numbers (Figure2.10).
• Number of entries in the list ‘n’ is stored in memory-location N.
www.vtuupdates.com Page 18
COMPUTER 21CS34
ORGANIZATION
• Register R1 is used as a counter to determine the number of times the loop is executed.
• Content-location N is loaded into register R1 at the beginning of the program.
The Loop is a straight line sequence of instructions executed as many times as needed. The loop starts at
location LOOP and ends at the instruction Branch>0.
• During each pass,
→ address of the next list entry is determined and
→ that entry is fetched and added to R0.
• The instruction Decrement R1 reduces the contents of R1 by 1 each time through the loop.
• Then Branch Instruction loads a new value into the program counter. As a result, the processor
fetches and executes the instruction at this new address called the Branch Target.
• A Conditional Branch Instruction causes a branch only if a specified condition is satisfied. If the
condition is not satisfied, the PC is incremented in the normal way, and the next instruction in sequential
address order is fetched and executed.
CONDITION CODES
• The processor keeps track of information about the results of various operations. This is accomplished
by recording the required information in individual bits, called condition code flags.
• These flags are grouped together in a special processor-register called the condition code register (or
statue register).
• Four commonly used flags are:
www.vtuupdates.com Page 19
COMPUTER 21CS34
ORGANIZATION
1) N (negative) set to 1 if the result is negative, otherwise cleared to0.
2) Z (zero) set to 1 if the result is 0; otherwise, cleared to0.
3) V (overflow) set to 1 if arithmetic overflow occurs; otherwise, cleared to0.
4) C (carry) set to 1 if a carry-out results from the operation; otherwise cleared to0.
ADDRESSING MODES
• The different ways in which the location of an operand is specified in an instruction are referred to as
Addressing Modes (Table 2.1).
1. Register Mode
• The operand is the contents of a register.
• The name (or address) of the register is given in the instruction.
• Registers are used as temporary storage locations where the data in a register are accessed.
• For example, the instruction
MoveR1,R2 ;Copy content of register R1 into register R2
www.vtuupdates.com Page 20
COMPUTER 21CS34
ORGANIZATION
2. Absolute (Direct) Mode
• The operand is in a memory-location.
• The address of memory-location is given explicitly in the instruction.
• The absolute mode can represent global variables in the program.
• For example, the instruction
MoveLOC,R2 ;Copy content of memory-location LOC into registerR2.
3. Immediate Mode
• The operand is given explicitly in the instruction.
• For example, the instruction
Move#200,R0 ;Place the value 200 in registerR0.
• Clearly, the immediate mode is only used to specify the value of a source-operand.
www.vtuupdates.com Page 21
COMPUTER 21CS34
ORGANIZATION
• To execute the Add instruction in fig 2.11 (a), the processor uses the value which is in register R1, as
the EA of the operand.
• It requests a read operation from the memory to read the contents of location B. The value read is the
desired operand, which the processor adds to the contents of registerR0.
• 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
value B as an address to obtain the operand.
Program Explanation
• In above program, Register R2 is used as a pointer to the numbers in the list, and the operands are
accessed indirectly throughR2.
• The initialization-section of the program loads the counter-value n from memory-location N into R1
and uses the immediate addressing-mode to place the address value NUM1, which is the address of the
first number in the list, into R2. Then it clears R0 to0.
• ThefirsttwoinstructionsintheloopimplementtheunspecifiedinstructionblockstartingatLOOP.
• The first time through the loop, the instruction Add (R2), R0 fetches the operand at location NUM1 and
adds it to R0.
• The second Add instruction adds 4 to the contents of the pointer R2, so that it will contain the address
value NUM2 when the above instruction is executed in the second pass through the loop.
www.vtuupdates.com Page 22
COMPUTER 21CS34
ORGANIZATION
• 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.
www.vtuupdates.com Page 23
COMPUTER 21CS34
ORGANIZATION
6. RELATIVE MODE
• This is similar to index-mode with one difference:
The effective-address is determined using the PC in place of the general purpose register Ri.
• The operation is indicated as X(PC).
• X(PC) denotes an effective-address of the operand which is X locations above or below the current
contents of PC.
• Since the addressed-location is identified "relative" to the PC, the name Relative mode is associated
with this type of addressing.
• This mode is used commonly in conditional branch instructions.
• An instruction such as
Branch >0 LOOP ;Causes program execution to go to the branch target location
identified by name LOOP if branch condition is satisfied.
www.vtuupdates.com Page 24
COMPUTER 21CS34
ORGANIZATION
ASSEMBLY LANGUAGE
• We generally use symbolic-names to write a program.
• A complete set of symbolic-names and rules for their use constitute an Assembly Language.
• The set of rules for using the mnemonics in the specification of complete instructions and programs is
called the Syntax of the language.
• Programs written in an assembly language can be automatically translated into a sequence of machine
instructions by a program called an Assembler.
• The user program in its original alphanumeric text formal is called a Source Program, and the
assembled machine language program is called an Object Program.
For example:
MOVE R0,SUM;The term MOVE represents OP code for operation performed by instruction.
ADD#5,R3 ;Adds number 5 to contents of register R3 & puts the result back intoregisterR3.
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.
www.vtuupdates.com Page 25
COMPUTER 21CS34
ORGANIZATION
1) Label is an optional name associated with the memory-address where the machine language
instruction produced from the statement will be loaded.
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,
depending on the type of instruction.
4) Comment Field is used for documentation purposes to make program easier to understand.
ASSEMBLY AND EXECUTION OF PRGRAMS
• Programs written in an assembly language are automatically translated into a sequence of machine
instructions by the Assembler.
• Assembler Program
→ replaces all symbols denoting operations & addressing-modes with binary-codes used in
machine instructions.
www.vtuupdates.com Page 26
COMPUTER 21CS34
ORGANIZATION
→ replaces all names and labels with their actual values.
→ assigns addresses to instructions & data blocks, start
starting
ing at address given in ORIGIN directive
→ inserts constants that may be given in DATAWORD directives.
→ reserves memory-space
space as requested by RESERVE directives.
• Two Pass Assembler has 2passes:
1. First Pass: Work out all the addresses of labels.
As the assembler
mbler scans through a source-program,
source it keeps track of all names of numerical-
values that correspond to them in a symbol-table.
2. Second Pass: Generate machine code, substituting values for the labels.
When a name appears a second time in the source-program, it is replaced with its value from
the table.
• The assembler stores the object-program
program on a magnetic-disk.
magnetic The object-program must be loaded into
the memory of the computer before it is executed. For this, a Loader Program is used.
• Debugger Program is used to help the user find the programming errors.
• Debugger program enables the user
→ to stop execution of the object
object-program
program at some points of interest &
→ to examine the contents of various processor-registers
processor registers and memory
memory-location.
www.vtuupdates.com Page 27
COMPUTER 21CS34
ORGANIZATION
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 as Move, Load or Store.
• For example, contents of the keyboard character buffer DATAIN can be transferred to register R1 in
the processor by the instruction
Move Byte DATAIN,R1
• The Move Byte 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 to be tested is
indicated by the first operand.
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).
• The various operations performed on stack:
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).
• If we assume a byte-addressable memory with a 32-bit word length,
1) The push operation can be implemented as
Subtract #4, SP
Move NEWITEM,(SP)
2) The pop operation can be implemented as
Move (SP),
www.vtuupdates.com Page 28
COMPUTER 21CS34
ORGANIZATION
ITEM Add #4, SP
• Routine for a safe pop and push operation as
follows:
www.vtuupdates.com Page 29
COMPUTER 21CS34
ORGANIZATION
QUEUE
• Data are stored in and retrieved from a queue on a FIFO basis.
• Difference between stack and 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 and
insertion respectively.
3) Without further control, a queue would continuously move through the memory of a computer
in the direction of higher addresses. One way to limit the queue to a fixed region in memory is to
use a circular buffer.
SUBROUTINES
• A subtask consisting of a set of instructions which is executed many times is called a Subroutine.
• A Call instruction causes a branch to the subroutine (Figure:2.16).
• At the end of the subroutine, a return instruction is executed
• 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 is referred to as its
Subroutine Linkage method.
• The simplest subroutine linkage method is to save the return-address in a specific location, which may
be a register dedicated to this function. Such a register is called the Link Register.
• When the subroutine completes its task, the Return instruction returns to the calling-program by
branching indirectly through the link-register.
• The Call Instruction is a special branch instruction that performs the following operations:
→ Store the contents of PC into link-register.
→ Branch to the target-address specified by the instruction.
• The Return Instruction is a special branch instruction that performs the operation:
→ Branch to the address contained in the link-register.
www.vtuupdates.com Page 30
COMPUTER 21CS34
ORGANIZATION
www.vtuupdates.com Page 31
COMPUTER 21CS34
ORGANIZATION
PARAMETER PASSING
• The exchange of information between a calling-program and a subroutine is referred to as
Parameter Passing (Figure: 2.25).
• The parameters may be placed in registers or in memory-location, where they can be accessed by the
subroutine.
• Alternatively, parameters may be placed on the processor-stack used for saving the return-address.
• Following is a program for adding a list of numbers using subroutine with the parameters passed
through registers.
STACK FRAME
• Stack Frame refers to locations that constitute a private work-space for the subroutine.
• The work-space is
→ created at the time the subroutine is entered &
→ freed up when the subroutine returns control to the calling-program (Figure: 2.26).
www.vtuupdates.com Page 32
COMPUTER 21CS34
ORGANIZATION
Program for adding a list of numbers using subroutine with the parameters passed to stack.
www.vtuupdates.com Page 33
COMPUTER 21CS34
ORGANIZATION
• Fig: 2.27 show an example of a commonly used layout for information in a stack-frame.
• Frame Pointer (FP) is used to access the parameters passed
→ to the subroutine &
→ to the local memory-variables.
• The contents of FP remains fixed throughout the execution of the subroutine, unlike 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 old TOS.
• The calling-program saves 4 parameters on the stack (Figure2.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 fig2.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.
www.vtuupdates.com Page 34
COMPUTER 21CS34
ORGANIZATION
www.vtuupdates.com Page 35
COMPUTER 21CS34
ORGANIZATION
• After SUB2 executes its Return instruction; the result is stored in register R2 bySUB1.
• SUB1thencontinuesitscomputations&eventuallypassesrequiredanswerbacktomain-programonthestack.
• When SUB1 executes return statement, the main-program stores this answers in memory-location
RESULT and continues its execution.
LOGIC INSTRUCTIONS
• Logic operations such as AND, OR, and NOT applied to individual bits.
• These are the basic building blocks of digital-circuits.
• This is also useful to be able to perform logic operations is software, which is done using instructions
that apply these operations to all bits of a word or byte independently and in parallel.
• For example, the instruction
Not dst
www.vtuupdates.com Page 36
COMPUTER 21CS34
ORGANIZATION
ROTATE OPERATIONS
• 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.
• 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 is simply rotated.
In the other version, the rotation includes the C flag.
www.vtuupdates.com Page 37
COMPUTER 21CS34
ORGANIZATION
www.vtuupdates.com Page 38
COMPUTER 21CS34
ORGANIZATION
• CISC approach results in instructions of variable length, dependent on the number of operands and the
type of addressing modes used.
• In RISC (reduced instruction set computers), any instruction occupies only one word.
• The RISC approach introduced other restrictions such as that all manipulation of data must be done on
operands that are already in registers.
Ex: Add R1,R2,R3
• In RISC type machine, the memory references are limited to only Load/Store operations.
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:
A program for the expression
is:
Load A
Multiply B
Store RESULT
Load C
Multiply D
Add RESULT
Store RESULT
www.vtuupdates.com Page 39
COMPUTER 21CS34
ORGANIZATION
Problem 2:
Registers R1 and R2 of a computer contains the decimal values 1200 and 4600. What is the effective
effective-
address of the memory operand in each of the following instructions?
(a) Load 20(R1),R5
(b) Move#3000,R5
(c) StoreR5,30(R1,R2)
(d) Add-(R2),R5
(e) Subtract(R1)+,R5
Solution:
(a) EA = [R1]+Offset=1200+20 = 1220
(b) EA = 3000
(c) EA = [R1]+[R2]+Offset = 1200+4600+30=5830
(d) EA = [R2]-1 = 4599
(e) EA = [R1] = 1200
Problem 3:
Registers R1 and R2 of a computer contains the decimal values 2900 and 3300. What is the effective-
effective
address of the memory operand in each of the following instructions?
(a)Load 1,55(R2)
(b)Move #2000,R7
(c) Store95(R1,R2),R5
(d) Add(R1)+,R5
(e) Subtract‐(R2),R5
Solution:
a) Load R1,55(R2) This is indexed addressing mode. So EA =55+R2=55+3300=3355.
b) Move #2000,R7 This is an immediate addressing mode. So, EA =2000
c) Store 95(R1,R2),R5 This is a variation of indexed addressing mode, in which contents of 2
registers are added with the offset or index to generate EA. 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.
e) Subtract -(R2),R5 This is Auto decrement mode. Here, R2 is subtracted
subtrac by 4 bytes
(assuming 32-bt processor) to generate the EA, so, EA=3300‐4=3296.
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
www.vtuupdates.com Page 40
COMPUTER 21CS34
ORGANIZATION
Solution:
www.vtuupdates.com Page 41
COMPUTER ORGANIZATION 21CS34
T
h
e
a
s
s
D cause the object program memory image constructed by the assembler to indicate that 300
e
is to be placed at memory word location 1000 at the time the program is loaded into memory
m
prior to execution.
b
The Move instruction places 300 into memory word location 1000 when the instruction is
l
executed as part of a program.
e
rProblem 6:
Register R5 is used in a program to point to the top of a stack. Write a sequence of instructions using
dthe Index, Auto increment, and Auto decrement addressing modes to perform each of the following
itasks:
r(a) Pop the top two items off the stack, and them, and then push the result onto the stack.
e(b) Copy the fifth item from the top into registerR3.
c(c) Remove the top ten ite ms from the stack.
t
i
vSolution:
e (a) Move(R5)+,R0
s Add (R5)+, R0
Move R0, -(R5)
(b) Move 16(R5),R3
O (c) Add #40,R5
R
I roblem 7:
P
CGonsider the following possi bilit ies for sa ving the return address of a subroutine:
I(a) In the processor register.
N(b) In a memory-location associated with the call, so that a different location is used whe n the
subroutine is called from different places
a( c) On a stack.
Which
n of these possi bilit ies supports subroutine nesti ng and which support s subroutine
rdecursion(that is, a subroutine that calls itself)?
Solution:
D (a) Neither nesting nor recursion is supported.
A (b) Nesting is supported, because different Call instructions will save the return address at
T different memory-locations. Recursion is not supported.
A (c) Both nesting and recursion are supported.
W
O
R
www.vtuupdates.com Page 41