CH 6
CH 6
COMPUTER
Introduction
Machine Language
Assembly Language
Assembler
Program Loops
Subroutines
Input-Output Programming
Introduction
INTRODUCTION
Those concerned with computer architecture should
have a knowledge of both hardware and software
because the two branches influence each other.
Instruction Set of the Basic
l
Computer
Symbo 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 m: effective address
STA 3 or B Store AC in M M: memory word (operand)
BUN 4 or C Branch unconditionally to m found at 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
SPA 7010 Skip if AC is positive
SNA 7008 Skip if AC is negative
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
Fetch R T0: AR PC
R T1: IR M[AR], PC PC + 1
Decode R T2: D0, ..., D7 Decode IR(12 ~ 14),
AR IR(0 ~ 11), I IR(15)
Indirect D7 IT3: AR M[AR]
Interrupt
T0 T1 T2 (IEN)(FGI + R 1
FGO):
Memory-
RT0:
RT1:
RT2:
Loading…
AR 0, TR PC
M[AR] TR, PC 0
PC PC + 1, IEN 0, R 0, SC 0
Reference
AND D0T4: DR M[AR]
D0T5: AC AC DR, SC 0
D1T4: DR M[AR]
ADD
D1T5: AC AC + DR, E Cout, SC 0
LDA D2T4: DR M[AR]
D2T5:
AC DR, SC 0
STA D3T4:
D4T4: M[AR] AC, SC 0
BUN PC AR, SC 0
D5T4:
BSA M[AR] PC, AR AR + 1
D5T5:
D6T4: PC AR, SC 0
ISZ DR M[AR]
D6T5:
D6T6: DR DR + 1
M[AR] DR, if(DR=0) then (PC PC + 1),
SC 0
Register-
Reference D7I T3 = r (Common to all register-reference instr)
IR(i) = Bi (i = 0,1,2, ..., 11)
r: SC 0
rB11: AC 0
CLA rB10: E 0
CLE rB9: AC AC
CMA rB8: E E
CME rB7: AC shr AC, AC(15) E, E AC(0)
CIR rB6:
CIL AC shl AC, AC(0) E, E AC(15)
rB5: AC AC + 1
INC rB4:
SPA If(AC(15) =0) then (PC PC + 1)
rB3:
SNA rB2: If(AC(15) =1) then (PC PC + 1)
SZA rB1: If(AC = 0) then (PC PC + 1)
SZE rB0: If(E=0) then (PC PC + 1)
HLT S 0
D7IT3 = p
Input-Output IR(i) = Bi (Common to all input-output
p: instructions)
pB11: (i = 6,7,8,9,10,11)
INP pB10: SC 0
OUT pB9: AC(0-7) INPR, FGI 0
SKI pB8: OUTR AC(0-7), FGO 0
SKO pB7: If(FGI=1) then (PC PC + 1)
ION pB6: If(FGO=1) then (PC PC + 1)
IOF IEN 1
IEN 0
Machine Language
MACHINE LANGUAGE
• Program
A list of instructions or statements for directing
the computer to perform a required data
processing task
• Loading…
Machine-language
- Binary code
- Octal or hexadecimal code
• Assembly-language (Assembler)
- Symbolic code
• BASIC Program
DIM A, B, C as INTEGER
A=83
B=-23
C=A+B
Assembly Language
ASSEMBLY LANGUAGE
Syntax of the BC assembly language
Each line is arranged in three columns called fields
Label field
- May be empty or may specify a symbolic
address consists of up to 3 characters
- Terminated by a comma
Instruction field
- Specifies a machine or a pseudo instruction
- May specify one of
* Memory reference instr. (MRI)
MRI consists of two or three symbols separated by spaces.
ADD OPR (direct address MRI)
ADD PTR I (indirect address MRI)
* Register reference or input-output instr.
Non-MRI does not have an address part
* Pseudo instr. with or without an operand
Symbolic address used in the instruction field must be
defined somewhere as a label
Comment field
- May be empty or may include a comment
Assembly Language
PSEUDO-INSTRUCTIONS
ORG N
Hexadecimal number N is the memory loc.
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
TRANSLATION TO BINARY
Hexadecimal
Code
Location Symbolic
Content Program
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 SUB
DEC -23
FFE9 ,
108 0000 DIF, HEX 0
END
Assembler
Assembler
ASSEMBLER - FIRST PASS -
Source Program - Symbolic Assembly Language Program
Object Program - Binary Machine Language Program
Two pass assembler
1st pass: generates a table that correlates all user defined
(address) symbols with their binary equivalent value
2nd pass: binary translation
First First
pass
pass LC :=
0
Scan next line of code Set LC
ye
s
no no
Labe OR
l G
ye
s ye
Store EN s
in
symbol D
symbol
address-
together
table no Go
value of LC
with second
to
pas
s
Increment
LC
Assembler
Loading…
s no s
Pseud ye
s OR END
o instr. G
no no
DEC or
ye no HE
s MR X Convert
I operan
Get operation code to binary
d
and set bits 2- Valid no
non-MRI and
4 in location
store
instr.
Search given by LC
symbol table
address- ye
binary
for s
of symbol
equivalent
and set bits 5-
address
16 Store Error
equivalent
binary line
in
ye no instructio
of code
of
s I in location
n
given by LC
Set Set
first first
bit to bit to
1 0
Assemble all parts Increment
binary instruction
of LC
store in location given by
and
LC
Program Loops
PROGRAM LOOPS
Loop: A sequence of instructions that are executed many times,
each with a different set of data
BASIC program to add 100 numbers: DIM A(100), SUM,J as INTEGER
SUM = 0
FOR J = 1 to 100
SUM = SUM + A(J)
NEXT
* Multiplication
- For simplicity, unsigned positive numbers
- 8-bit numbers -> 16-bit product
Programming Arithmetic and Logic
Operations
FLOWCHART OF A PROGRAM - Multiplication -
CTR - 8
P 0
X holds the multiplicand
Y holds the multiplier
E 0 P holds the product
AC X
cil EAC
ci
l
X AC
CTR CTR + 1
=
0 CT 0 Stop
R
Programming Arithmetic and Logic
Operations
ASSEMBLY LANGUAGE PROGRAM -
Multiplication -
ORG 100
LOP, CLE / Clear E
LDA Y / Load multiplier
CIR / Transfer multiplier bit to E
STA Y / Store shifted multiplier
SZE / Check if bit 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
LDA X / Load multiplicand
ZRO, CIL / Shift left
STA X / Store shifted multiplicand
ISZ CTR / Increment counter
BUN LOP / Counter not zero; repeat loop
HLT / Counter is zero; halt
DEC -8 / This location serves as a
CTR, HEX 000F counter
X, HEX / Multiplicand stored here
Y, 000B / Multiplier stored here
P, HEX 0 / Product formed here
END
Programming Arithmetic and Logic
Operations
ASSEMBLY LANGUAGE PROGRAM
- Double Precision Addition -
Subroutine
SUBROUTINES
- 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
Exampl
e
Loc 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
10 CIL / Circulate left once
A CIL
10 CIL
B CIL / Circulate left fourth time
10 AND MSK / Set AC(3-0) to zero
C BUN SH4 I / Return to main program
10 MSK, HEX FFF0 / Mask operand
D END
10E
10F
110
Subroutine
s
Program to Output a
Character
LDA CHR / Load character into AC
COF, SKO / Check output flag
BUN COF / Flag=0, branch to check
OUT again
HLT / Flag=1, output character
CHR, HEX
0057 / Character is "W"
Input Output
Program
CHARACTER MANIPULATION
Subroutine to Input 2 Characters and pack into a word
PROGRAM INTERRUPT
Tasks of Interrupt Service Routine