CO Chapter 6
CO Chapter 6
CHAPTER 6
Programming the Basic Computer
2 of 24
6.1 Introduction
Those concerned with computer architecture should have a knowledge of both hardware
and software because the two branches influence each other.
Table 6.1 Instruction Set of the Basic Computer
Symbol Hexa code Description
AND 0 or 8 AND M to AC
ADD 1 or 9 Add M to AC, carry to E
LDA 2 or A Load AC from M
STA 3 or B Store AC in M
BUN 4 or C Branch unconditionally to m
BSA 5 or D Save return address in m and branch to m+1
ISZ 6 or E Increment M and skip if zero
CLA 7800 Clear AC
CLE 7400 Clear E
CMA 7200 Complement AC
CME 7100 Complement E
CIR 7080 Circulate right E and AC
CIL 7040 Circulate left E and AC
INC 7020 Increment AC, carry to E
m: effective address SPA 7010 Skip if AC is positive
M: memory word SNA 7008 Skip if AC is negative
(operand) found at m SZA 7004 Skip if AC is zero
SZE 7002 Skip if E is zero
HLT 7001 Halt computer
INP F800 Input information and clear flag
OUT F400 Output information and clear flag
SKI F200 Skip if input flag is on
SKO F100 Skip if output flag is on
ION F080 Turn interrupt on
IOF F040 Turn interrupt off
3 of 24
6.2 Machine Language
6.2.1 Definitions
• Program is a list of instructions or statements for
directing the computer to perform a required data
processing task.
4 of 24
6.2 Machine Language
6.2.1 Definitions (cont’d)
• Both of assembly and high level language should be
converted to machine language which is the language
that the microprocessor understands (binary).
5 of 24
6.2.2 How are Programs Understood by the Computer?
Processed
By CPU
Fig. 6.1 The language translation process
6 of 24
6.2.4 Comparison Of Programming Languages
Table 6.2 Binary Program to Table 6.3 Hexadecimal program
Add Two Numbers to Add Two Numbers
Location Instruction
Location Instruction Code
000 2004
0 0010 0000 0000 0100 001 1005
1 0001 0000 0000 0101 002 3006
10 0011 0000 0000 0110 003 7001
11 0111 0000 0000 0001 004 0053
100 0000 0000 0101 0011 005 FFE9
101 1111 1111 1110 1001 006 0000
110 0000 0000 0000 0000
Table 6.4 Program with Symbolic OP-Code Table 6.5 Assembly-Language Program
to Add Two Numbers to Add Two Numbers
Location Instruction Comments ORG 0 /Origin of program is location 0
000 LDA 004 Load 1st operand into AC LDA A /Load operand from location A
001 ADD 005 Add 2nd operand to AC ADD B /Add operand from location B
002 STA 006 Store sum in location 006 STA C /Store sum in location C
003 HLT Halt computer HLT /Halt computer
004 0053 1st operand A, DEC 83 /Decimal operand
005 FFE9 2nd operand (negative) B, DEC -23 /Decimal operand
006 0000 Store sum here C, DEC 0 /Sum stored in location C
END /End of symbolic program
int A= 83 , B =-23, C;
C = A + B;
7 of 24
6.3 Assembly Language
6.3.1 Syntax of the Basic Computer assembly language
Each line of the assembly language is arranged in three columns called fields as:
1- Label field
- May be empty or may specify a symbolic address consists of up to 3 characters
- Terminated by a comma.
- Symbolic address used in the instruction field must be defined somewhere
as a label.
2- Instruction field
- Specifies a machine or a pseudo instruction which includes one of:
a) Memory reference instruction (MRI).
MRI consists of two or three symbols separated by spaces such as.
ADD OPR (direct address MRI)
ADD PTR I (indirect address MRI)
b) Register reference or input-output instruction.
Non-MRI does not have an address part
c) Pseudo instruction with or without an operand.
3- Comment field
- May be empty or may include a comment
8 of 24
6.3.2 Pseudo-instructions
Table 6.7 Definition of Pseudo-instructions
ORG N Hexadecimal number N is the memory location for the
instruction or operand listed in the following line
END Denotes the end of symbolic program
DEC N Signed decimal number N to be converted to the binary
HEX N Hexadecimal number N to be converted to the binary
Example:
Table 6.8 Assembly language program to subtract two numbers
ORG 100 / Origin of program is location 100
LDA B / Load subtrahend to AC
CMA / Complement AC
INC / Increment AC
ADD A / Add minuend to AC
STA DIF / Store difference
HLT / Halt computer
A, DEC 83 / Minuend
B, DEC -23 / Subtrahend
DIF, HEX 0 / Difference stored here
END / End of symbolic program
9 of 24
6.3.3 Translation to Binary
Table 6.9 Listing of Program of Table 6.8
Hexadecimal Code
Symbolic Program
Location Content
ORG 100
100 2107 LDA SUB
101 7200 CMA
102 7020 INC
103 1106 ADD MIN
104 3108 STA DIF
105 7001 HLT
106 0053 MIN, DEC 83
107 FFE9 SUB, DEC -23
108 0000 DIF, HEX 0
END
10 of 24
6.3.4 Advantages of the Assembly Programming
• Understanding the internal structure of the microprocessor and its effect to
programming.
• Assembly programs runs two to three times faster than any equivalent C
or Pascal program.
• Assembled assembly program is much smaller in size than the equivalent
compiled high level language program.
• Assembly provides full access to the resources of the microprocessor.
• Terminate and stay resident (TSR) programs are easily done using
assembly.
• Communication programs and driver programs can be done only by
assembly
6.6.5 Disadvantages of the Assembly Programming
• Requires deep understanding of the microprocessor and its structure.
• Simple tasks requires too many lines of assembly code while it could be
few lines in any other high level language
11 of 24
6.5 Program Loops
Loop: A sequence of instructions that are executed many times, each with a
different set of data int A[100] = {1,2,3,4…};
int SUM = 0;
Example: C++ program to add 100 numbers: for (i=0;i<100;i++)
SUM = SUM + A[i];
Table 6.13 Assembly-language program to add 100 numbers
1- Software Implementation
- Implementation of an operation with a program using machine
instruction set.
- Usually when the operation is not included in the instruction set.
2- Hardware Implementation
- Implementation of an operation in a computer with one
machine instruction.
* Multiplication
- For simplicity, unsigned positive numbers
- 8-bit numbers gives 16-bit product
13 of 24
6.6.1 Multiplication Program
CTR - 8
P0
X holds the multiplicand
E0 Y holds the multiplier
P holds the product
AC Y
cir EAC
Example with four significant digits
Y AC
X = 0000 1111 P
=0 =1 Y = 0000 1011 0000 0000
E
PP+X 0000 1111 0000 1111
E0 0001 1110 0010 1101
0000 0000 0010 1101
AC X 0111 1000 1010 0101
1010 0101
cil EAC
cil
X AC
14 of 24
6.6.1 Assembly Language Multiplication Program
ORG 100
LOP, CLE / Clear E
LDA Y / Load multiplier
CIR / Transfer multiplier bit to E
STA Y / Store shifted multiplier
SZE / Skip if E is zero
BUN ONE / Bit is one; goto ONE
BUN ZRO / Bit is zero; goto ZRO
ONE, LDA X / Load multiplicand
ADD P / Add to partial product
STA P / Store partial product
CLE / Clear E
ZRO, LDA X / Load multiplicand
CIL / Shift left
STA X / Store shifted multiplicand
ISZ CTR / Increment counter
BUN LOP / Counter not zero; repeat loop
HLT / Counter is zero; halt
CTR, DEC -8 / This location serves as a counter
X, HEX 000F / Multiplicand stored here
Y, HEX 000B / Multiplier stored here
P, HEX 0 / Product formed here
END
15 of 24
6.6.2 Assembly Language Program
for Double Precision Addition
16 of 24
6.6.3 Logic and Shift Operations
• Basic Computer instructions include AND, CMA, CLA only
• Other logic operations can be implemented as follows.
Example: Assembly Language Program for OR operation
LDA A / Load 1st operand
CMA / Complement to get A’
STA TMP / Store in a temporary location
LDA B / Load 2nd operand B
CMA / Complement to get B’
AND TMP / AND with A’ to get A’ AND B’
CMA / Complement again to get A OR B
• Basic Computer instructions have: CLE, CIR, CIL only
• Other shift operations can be implemented as follows.
Example: Logical shift-right operation CLE
CIR
Example: Logical shift-left operation CLE
CIL
CLE / Clear E to 0
Example: Arithmetic right-shift operation SPA / Skip if AC is positive
CME / AC is negative
CIR / Circulate E and AC
17 of 24
6.7 Subroutines
• Subroutine is a set of common instructions that can be used in a program
many times.
• Subroutine linkage : a procedure for branching to a subroutine and returning to
the main program
Example: Write a subroutine SH4 that shifts a word left four times
Location
ORG 100 / Main program
100 LDA X / Load X
101 BSA SH4 / Branch to subroutine
102 STA X / Store shifted number
103 LDA Y / Load Y
104 BSA SH4 / Branch to subroutine again
105 STA Y / Store shifted number
106 HLT
107 X, HEX 1234
108 Y, HEX 4321
/ Subroutine to shift left 4 times
109 SH4, HEX 0 / Store return address here
10A CIL / Circulate left once
10B CIL
10C CIL
10D CIL / Circulate left fourth time
10E AND MSK / Set AC(13-16) to zero
10F BUN SH4 I / Return to main program
110 MSK, HEX FFF0 / Mask operand
END
18 of 24
6.7.1 Subroutine Parameters and Data Linkage
Linkage of Parameters and Data between the Main Program and a Subroutine
- via Registers - via Memory locations - ….
Example: Subroutine performing LOGICAL OR operation (requires two parameter)
Location ORG 200
200 LDA X / Load 1st operand into AC
201 BSA OR / Branch to subroutine OR
202 HEX 3AF6 / 2nd operand stored here
203 STA Y / Subroutine returns here
204 HLT
205 X, HEX 7B95 / 1st operand stored here
206 Y, HEX 0 / Result stored here
207 OR, HEX 0 / Subroutine OR
208 CMA / Complement 1st operand
209 STA TMP / Store in temporary location
20A LDA OR I / Load 2nd operand
20B CMA / Complement 2nd operand
20C AND TMP / AND complemented 1st operand
20D CMA / Complement again to get OR
20E ISZ OR / Increment return address
20F BUN OR I / Return to main program
210 TMP, HEX 0 / Temporary storage
END
19 of 24
6.7.2 Subroutine Example: Moving a Block of Data
/ Main program
BSA MVE / Branch to subroutine
HEX 100 / 1st address of source data
HEX 200 / 1st address of destination data
DEC -16 / Number of items to move
HLT
MVE, HEX 0 / Subroutine MVE
LDA MVE I / Bring address of source
STA PT1 / Store in 1st pointer
ISZ MVE / Increment return address C++ Code
LDA MVE I / Bring address of destination
STA PT2 / Store in 2nd pointer
ISZ MVE / Increment return address void MVE (int SOURCE[20], int DEST[20], int N)
LDA MVE I / Bring number of items {
STA CTR / Store in counter for (i=0; i<20; i++)
ISZ MVE / Increment return address DEST[i] = SOURCE[i];
LOP, LDA PT1 I / Load source item }
STA PT2 I / Store in destination
ISZ PT1 / Increment source pointer
ISZ PT2 / Increment destination pointer
ISZ CTR / Increment counter
BUN LOP / Repeat 16 times
BUN MVE I / Return to main program
PT1, --
PT2, --
CTR, --
20 of 24
6.8 Input Output Program
Program to Input one Character (Byte)
21 of 24
6.8.1 Character Manipulation
22 of 24
6.8.2 Program Interrupt
23 of 24
6.8.3 Interrupt Service Routine
Location
24 of 24