Computer Organization & Design: Programming Level
Computer Organization & Design: Programming Level
&
Design
Programming Level
1
Computer Organization & Design
2
Computer Organization & Design
Glossary of prerequisite topics
score, and impose
Yes
remedial action if
Glossary of topics not successful
No
Familiar with the topics? Take the Module
Yes
Current
Module Take Test
No
Pass?
Yes
Options
• Programming Level
• Computer: A computer with a storage component that may contain
both data to be manipulated and instructions to manipulate the data
is called a stored program machine. This simply implies that the
user is able to change the sequence of operations on the data.
• Program: The sequence of operations performed on the data is
called a program. More formally, it is a finite set of instructions
that specify the operands, operations, and the sequence by which
processing has to occur.
4
Computer Organization & Design
• Programming Level
• Instruction: An instruction is a group of bits that tells
the computer to perform a specific operation. It is
composed of two parts:
• Operation part
• Operand part
5
Computer Organization & Design
• Programming Level
• Operation part (operation code) is a group of bits that
defines the action to be performed.
• For each machine the set of operations is limited and defined.
• In general, a machine with n bits as op.code is capable of
supporting 2n distinct operations each having a distinct
encoding as op.code.
6
Computer Organization & Design
• Programming Level
• Operand part defines the element (s) needed for operation.
• Within the scope of a program, besides the op.code, one
needs four pieces of information (note majority of operations
are binary operations):
• 2 operands as sources
• 1 operand as a destination
• 1 operand to specify the location of the next instruction that should
be fetched.
7
Computer Organization & Design
• Programming Level
• Depending on how many of these pieces of
information are explicitly defined, instructions are
grouped into 5 classes: 4, 3, 2, 1, and 0-address
instructions.
8
Computer Organization & Design
• Programming Level
• 4 - address instruction: In which the operand part
explicitly contains four pieces of information.
• Programming Level
• 3 - address instruction: Most of the instructions in a
program are sequential in nature. Therefore, it is
possible to eliminate the address of the next instruction
from the operand part and assume that the next
instruction is in the next consecutive location in main
memory. This brings the concept of 3-address
instruction.
10
Computer Organization & Design
• Programming Level
• In case of 3 - address instruction a register, program
counter (PC), is used as a pointer to point to the next
instruction in sequence.
i) ADD A,B,C
• Meaning: add the contents of A to B and store the result in
C. In this case the program counter (PC) contains (i + 1).
11
Computer Organization & Design
• Programming Level
• 2 - address instruction: If the result of the operation
is going to be stored in one of the sources then we will
end up with a so-called 2-address instruction.
i) ADD A,B
• Meaning: add contents of A to B and store it either in
A or B. In this case the PC contains (i+1).
12
Computer Organization & Design
• Programming Level
• 1 - address instruction: In a 2-address instruction, if one of the
operands is being implicitly defined, then only one source is needed to
be explicitly defined in the operand part. This brings the concept of 1-
address instruction. This has been implemented by the introduction of a
register, accumulator (AC), which acts as a source as well as destination.
i) ADD A
• Meaning: add the contents of A to the AC and store the result in the AC,
PC contains (i + 1).
13
Computer Organization & Design
• Programming Level
• 0 - address instruction: Both operands are implicitly defined and
hence just the op.code is explicitly defined in the instruction. This
implementation is the so-called stack machine, where operand
values are assumed to be on top of the stack.
i) ADD
• Meaning: pop stack twice, add the contents of the two top most
elements together and push the result back into the stack. PC
contains (i + 1).
14
Computer Organization & Design
• Programming Level
• Calculate the length of 4, 3, 2, 1, and 0-address instructions for a
machine capable of supporting 15 instructions and a main memory
of 16K:
• 4-address instruction: 4 + 4 * 14 = 60
• 3-address instruction: 4 + 3 * 14 = 46
• 2-address instruction: 4 + 2 * 14 = 32
• 1-address instruction: 4 + 1 * 14 = 18
• 0-address instruction: 4
15
Computer Organization & Design
• Programming Level
• In case of 2-address instructions, the destination operand loses its initial
value. So in many cases it is advisable to save its initial value: i.e.,
16
Computer Organization & Design
• Programming Level
• In case of 0-address instructions, stack should be
loaded first. Two instructions namely PUSH and POP
are used to move data between main memory and
stack:
PUSHA
POP A
17
Computer Organization & Design
• Questions
• Compare and contrast 4, 3, 2, 1, and 0-address
instructions and programs against each other.
• Is it possible to write a pure 0-address program?
• What determines the length of PC?
18
Computer Organization & Design
• Working Problem
• Write a 4, 3, 2, 1, and 0-address program for
Y = A ** B - (C + D)
19
Computer Organization & Design
• Working Problem
• 4-address program
1) EXP A,B,T1,2
2) ADD C,D,T2,3
3) SUB T1,T2,Y,4
4) ...
Instruction length = 60 bits
Program size = 3 * 60 = 180 bits
20
Computer Organization & Design
• Working Problem
• 3 - Address Program
1) EXP A,B,T1 PC=2
2) ADD C,D,T2 PC=3
3) SUB T1,T2,YPC=4
4) ...
Instruction length = 46 bits
Program size = 3 * 46 = 138 bits
21
Computer Organization & Design
• Working Problem
• 2 - Address Program
1) MOVE A, T1 PC=2
2) EXP T1, B PC=3
3) MOVE C, T2 PC=4
4) ADD T 2, D PC=5
5) SUB T 1 , T2 PC=6
6) MOVE T1, Y PC=7
7) ...
Instruction length = 32 bits
Program size = 6 * 32 = 192 bits
22
Computer Organization & Design
• Working Problem
• 1 - Address Program
1) LOAD A PC=2
2) EXP B PC=3
3) STORE T1 PC=4
4) LOAD C PC=5
5) ADD D PC=6
6) STORE T2 PC=7
7) LOAD T1 PC=8
8) SUB T2 PC=9
9) STORE Y PC=10
10) ...
Instruction length = 18 bits
Program size = 9 * 18 = 162 bits
23
Computer Organization & Design
• Working Problem
• 0 - Address Program
1) PUSH A PC=2
2) PUSH B PC=3
3) EXP PC=4
4) PUSH C PC=5
5) PUSH D PC=6
6) ADD PC=7
7) SUB PC=8
8) POP Y PC=9
9) ...
• Programming Level
• The order of the instructions in a program should be the
same as the order of instructions in a post-fix format ─
Expression should be converted into a post-fix format.
• In-fix to post-fix conversion:
• Make fully parenthesized expression
• Move each operator to its nearest right parenthesis
• Delete all the left and right parentheses.
25
Computer Organization & Design
• Programming Level
• Convert the following in-fix expressions to their post-fix
format:
26
Computer Organization & Design
• Addressing Mode
• The way in which operands are specified in an instruction is called
the addressing mode. The ability to specify operands in different
ways brings a greater degree of flexibility to the computer.
• Implied Mode: In this mode operand(s) is(are) specified implicitly in the
definition of instruction.
ADD A Accumulator is in implied mode.
• Immediate Mode: In this mode operand value is defined in the instruction.
ADD 5
27
Computer Organization & Design
• Addressing Mode
• Index Mode: In this mode address of the operand is determined
by two values:
• Contents of the index register
• Displacement
• Effective address - i.e., address of operand, is determined by adding the contents of index
register to the displacement. Index mode is very effective when handling arrays, tables, …
28
Computer Organization & Design
• Addressing Mode
• Direct Addressing Mode: In this mode the address of operand is explicitly defined in the
instruction.
ADD A TO B
Register mode and direct mode are conceptually the same.
• Indirect Mode: In this mode, the address of the address of operand value is contained in
the instruction. Therefore, one needs two fetches to memory in order to retrieve the
operand value.
ADD Aindirect TO B
• Multilevel of Indirections: This is an extension of indirect mode, in which instruction
contains the address of the address of the ... of operand.
29
Computer Organization & Design
• Addressing Mode
30
Computer Organization & Design
• Addressing Mode
31
Computer Organization & Design
• Addressing Mode
• A variation of index mode is called base addressing
which allows re-locatability.
• Some systems allow a mixture of several addressing
modes, i.e., indirect-register mode.
32
Computer Organization & Design
• Questions
• How are different addressing modes defined in an instruction?
• How is the level of indirection defined?
• What is the application of multi-level of indirection?
• If 5 and 6 are immediate values then are
ADD 5 to 6
ADD 5 to A
valid instructions?
• What is the format and length of a 2-address instruction, where:
instruction set is of size 15
memory is of size 16K words
system supports 3 different addressing modes (immediate, direct, and indirect).
33
Computer Organization & Design
34
Computer Organization & Design
Mode Bit 0 1 3 4 15
Op. Code
36
Computer Organization & Design
Addressing
Mode Bit
37
Operation Code Operand Part
Computer Organization & Design
38
Computer Organization & Design
SZA (7004), CMA (7200), INC (7020), SZE (7002), CME (7100), SPA (7010), HLT (7001)
42
Computer Organization & Design
43
Computer Organization & Design
44
Computer Organization & Design
45
Computer Organization & Design
46
Computer Organization & Design
47
Computer Organization & Design
48
Computer Organization & Design
3 2
1
MAR PC
2
MBR +1
4
IR
49
Control Unit
Computer Organization & Design
50
Computer Organization & Design
• ADD A
q1c2t0: MAR (MBR(address))
q1c2t1: MBR (M[MAR])
q1c2t2: AC (AC) + (MBR)
q1c2t3: ...
52
Computer Organization & Design
• STA A
q3c2t0 : MAR (MBR(address))
q3c2t1 : MBR (AC)
q3c2t2 : M[MAR] (MBR)
q3c2t3 : ...
53
Computer Organization & Design
• ISZ A
q 5c 2 t0 : MAR (MBR(address))
q 5c 2 t1 : MBR (M[MAR])
q 5c 2 t2 : MBR (MBR) + 1
q 5c 2 t3 : M[MAR] (MBR), if (MBR=0) then PC (PC)+1 54
Computer Organization & Design
PC
B BSA A
(B + 1)
MBR
A A B+1
55
A+1
Computer Organization & Design
• CLE:
q7I’c2t0 (MBR6): E 0
q7I’c2t1 (MBR6): idle
q7I’c2t2 (MBR6): idle
q7I’c2t3 (MBR6): ...
56
Computer Organization & Design
57
Computer Organization & Design
58
Computer Organization & Design
59
Computer Organization & Design
IF LAG O FLAG
60
Computer Organization & Design
Computer: Computer:
BB. If IFLAG =1 then PC PC+1, DD. If OFLAG =1 then PC PC+1,
Goto BB. Goto DD.
AC INPR, IFLAG 0. OUTR AC, OFLAG 0.
61
Computer Organization & Design
62
Computer Organization & Design
63
Computer Organization & Design
64
Computer Organization & Design
65
Computer Organization & Design
66
Computer Organization & Design
67
Computer Organization & Design
• Questions
• What is the application of BSA?
• In the simple machine, what is the last instruction of a subroutine?
• How does the interrupt facility improve the efficiency of I/O
operations?
• Interrupt vector?
• Interrupt within interrupt?
• Format of interrupt handling program?
68
Computer Organization & Design
• Programming Level
Addressing Mode: The way operand is defined in the instruction.
Effective address: The address of the operand value.
Scratch pad memory: A collection of the resisters organized and
accessed like the main memory words.
Base addressing: This is similar to the index addressing. The contents
of a register (base register) will be added to every addresses in a
program. Before the execution of the program the contents of the base
register will be loaded. This allows the program to be loaded
anywhere in the main memory. The contents of the base register
should be guarded during the course of the program execution.
69
Computer Organization & Design
• Programming Level
• Relative addressing mode: The effective address is defined relative to the
current contents of the program counter.
• Instruction set: The collection of instruction supported by the machine.
• Instruction cycle: The sequence of micro-operations needed to fetch and
execute and instruction.
• Instruction fetch cycle: A subset of the instruction cycle to allow to fetch
the next machine instruction.
• Machine instruction: Binary representation of the assembly instruction.
70
Computer Organization & Design
• Programming Level
• Instruction execute cycle: A subset of the instruction cycle
that facilitates the execution of a machine instruction.
• Address generation cycle: A subset of the instruction cycle
that calculates the effective address of the operand.
• Interrupt Handling Program: A collection of system routines
developed for handling different variation of interrupts.
71
Computer Organization & Design
• Programming Level
• Precise interrupt:During the course of the execution of the
program, the location of interrupt can be determined.
• Imprecise interrupt:
During the course of the execution of the
program the approximate position of the interrupt can be
determined.
• Extended opcode: A technique that allows to extend the
instruction set by using part of the operand section of the
instruction as a modifier to the op-code.
72
Computer Organization & Design
• Programming Level
• Basic Block: A sequence of sequential instructions, i.e., a
sequence of instructions without any branches (except
possibly the last instruction).
• Program Counter: A special purpose register that holds the
address of the next instruction in sequence.
• Stack: A form of data structure that supports last-in-first-
out policy.
73
Computer Organization & Design
• Questions
Rewrite the indirect cycle to allow multilevel of indirections.
Within the scope of the simple machine, it is possible to
increase the set of the register reference instructions?
The length of the opcode for the simple machine is 3. So by a
simple calculation, the simple machine should have an
instruction set of size 8! However, as we have seen, the simple
machine has an instruction set of size 31! Explain.
What is the application of BSA?
74
Computer Organization & Design
• Questions
• Does the simple machine support a recursive call?
• What is the application of ISZ?
• Within the scope of the simple machine, what is the last instruction of a
subroutine?
• For the simple machine, write an assembly program to perform A - B?
• Does the synchronization scheme, discussed for the I/O operation,
improve computer utilization?
• How does the interrupt facility improve the efficiency of I/O operation?
75
Computer Organization & Design
• Questions
• What is a “subroutine”?
• What is a “subroutine call”?
• What is the difference between a subroutine call and a
goto statement?
• What is a “Jump-and Link” Instruction?
76