0% found this document useful (0 votes)
6 views35 pages

CH 6

The document provides an overview of programming the Basic Computer, covering machine language, assembly language, and programming concepts such as loops and arithmetic operations. It details the instruction set, syntax, and examples of both assembly and high-level programming languages. Additionally, it explains the role of assemblers in translating symbolic assembly language into machine code.

Uploaded by

esmaaduzenlii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views35 pages

CH 6

The document provides an overview of programming the Basic Computer, covering machine language, assembly language, and programming concepts such as loops and arithmetic operations. It details the instruction set, syntax, and examples of both assembly and high-level programming languages. Additionally, it explains the role of assemblers in translating symbolic assembly language into machine code.

Uploaded by

esmaaduzenlii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

PROGRAMMING THE BASIC

COMPUTER
Introduction
Machine Language

Assembly Language
Assembler

Program Loops

Programming Arithmetic and Logic


Operations

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

• Various types of programming languages


- Hierarchy of programming languages

• Loading…
Machine-language
- Binary code
- Octal or hexadecimal code

• Assembly-language (Assembler)
- Symbolic code

• High-level language (Compiler)


Machine Language

COMPARISON OF PROGRAMMING LANGUAGES


• Binary Program to Add Two Numbers • Hexa program
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

• Program with Symbolic OP-Code • Assembly-Language


Location Instruction Comments
Program
ORG 0 /Origin of program is location 0
LDA A /Load operand from location A
000 LDA 004 Load 1st operand into AC ADD B /Add operand from location B
001 ADD 005 Add 2nd operand to AC
002 STA 006 Store sum in location 006 STA C /Store sum in location C
HLT /Halt computer
003 HLT Halt computer A, DEC 83 /Decimal operand
004 0053 1st operand
005 FFE9 2nd operand (negative) B, DEC -23 /Decimal operand
C, DEC 0 /Sum stored in location C
006 0000 Store sum here END /End of symbolic program

• 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

Example: Assembly language program to subtract two


numbers ORG 100 / Origin of program is location
LDA SUB 100
CMA / Load subtrahend to AC
INC / Complement AC
ADD MIN / Increment AC
STA DIF / Add minuend to AC
HLT / Store difference
MIN, DEC 83 / Halt computer
SUB DEC -23 / Minuend
, HEX 0 / Subtrahend
DIF, END / Difference stored here
/ End of symbolic program
Assembly Language

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

ASSEMBLER - SECOND PASS -


Second
PassMachine instructions are translated by means of table-lookup
procedures;
(1. Pseudo-Instruction Table, 2. MRI Table, 3. Non-MRI Table
4. Address Symbol
Second
pass Table)
LC <- 0
Don
Scan next line of e
code Set
LC
ye ye

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

Assembly-language program to add 100


numbers: ORG 100 / Origin of program is HEX 100
LDA ADS / Load first address of operand
STA PTR / Store in pointer
LDA NBR / Load -100
STA CTR / Store in counter
CLA / Clear AC
LOP, ADD PTR / Add an operand to AC
I / Increment pointer
ISZ PTR / Increment counter
ISZ CTR / Repeat loop again
BUN LOP / Store sum
STA SUM / Halt
ADS, HLT / First address of operands
PTR, HEX 150 / Reserved for a pointer
NBR, HEX 0 / Initial value for a counter
CTR, DEC -100 / Reserved for a counter
SUM, HEX 0 / Sum is stored here
HEX 0 / Origin of operands is HEX 150
ORG 150 / First operand
DEC. 75
.
. / Last operand
DEC 23 / End of symbolic program
END
Programming Arithmetic and Logic
Operations

PROGRAMMING ARITHMETIC AND LOGIC OPERATIONS

Implementation of Arithmetic and Logic


Operations
- Software Implementation
- Implementation of an operation with a program
using machine instruction set
- Usually when the operation is not included
in the instruction set
- Hardware Implementation
- Implementation of an operation in a computer
with one machine instruction

Software Implementation example:

* 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

Example with four significant


AC Y digits
X 0000 1111 P
cir
EAC = 0000 1011 0000
Y 0000
Y AC = 0000 1111 0000 1111
0001 1110 0010 1101
= = 0000 0000 0010
E
0 1 1101
P P+X 0111 1000 1010 0101
1010 0101
E 0

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 -

LDA AL / Load A low


ADD BL / Add B low, carry in E
STA CL / Store in C low
CLA / Clear AC
CIL / Circulate to bring carry into AC(16)
ADD AH / Add A high and carry
ADD BH / Add B high
STA CH / Store in C high
HLT
Programming Arithmetic and Logic
Operations
ASSEMBLY LANGUAGE PROGRAM
• Logic - Logic and Shift Operations -
operations
- BC instructions : AND, CMA, CLA
- 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
• Shift operations - BC has Circular Shift
only - Logical shift-right operation - Logical shift-left operation
CLE CLE
CIR CIL

- Arithmetic right-shift operation


CLE / Clear E to 0
SPA / Skip if AC is
CME positive
CIR / AC is negative
/ Circulate E and AC
Subroutine
s

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

SUBROUTINE - 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
DEC -16 data
HLT / Number of items to move
MVE HEX 0
, LDA MVE I / Subroutine MVE
STA PT1 / Bring address of source
ISZ MVE / Store in 1st pointer
LDA MVE I / Increment return address
STA PT2 / Bring address of destination
ISZ MVE / Store in 2nd pointer
LDA MVE I / Increment return address
STA CTR / Bring number of items
ISZ MVE / Store in counter
LDA PT1 I / Increment return address
LOP, STA PT2 I / Load source item
ISZ PT1 / Store in destination • Fortran
ISZ PT2 / Increment source pointer subroutine
ISZ CTR / Increment destination pointer SUBROUTINE MVE (SOURCE, DEST, N)
BUN LOP / Increment counter DIMENSION SOURCE(N), DEST(N)
BUN MVE / Repeat 16 times DO 20 I = 1, N
I / Return to main program 2 DEST(I) = SOURCE(I)
PT1, -- 0 RETURN
PT2, -- END
CTR, --
Input Output
Program

INPUT OUTPUT PROGRAM


Program to Input one Character(Byte)
CIF, SKI / Check input flag
BUN CIF / Flag=0, branch to check again
INP / Flag=1, input character
OUT / Display to ensure correctness
STA CHR / Store character
HLT
CHR, -- / Store character here

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

IN2, -- / Subroutine entry


FST, SKI
BUN FST
INP / Input 1st character
OUT
BSA SH4 / Logical Shift left 4
BSA SH4 bits
SCD, SKI / 4 more bits
BUN SCD
INP
OUT / Input 2nd character
BUN IN2 I
/ Return
Input Output
Program

PROGRAM INTERRUPT
Tasks of Interrupt Service Routine

- Save the Status of CPU


Contents of processor registers and Flags

- Identify the source of Interrupt


Check which flag is set

- Service the device whose flag is set


(Input Output Subroutine)

- Restore contents of processor registers and flags

- Turn the interrupt facility on

- Return to the running program


Load PC of the interrupted program
Input Output
Program
INTERRUPT SERVICE ROUTINE
Loc
. 0 ZRO - / Return address stored here
1 , BUN SRV / Branch to service routine
10 CLA / Portion of running program
0 ION / Turn on interrupt facility
10 LDA X
1 ADD Y / Interrupt occurs here
10 STA Z / Program returns here after
2 interrupt
10
3 STA SAC / Interrupt service routine
10 SRV, CIR / Store content of AC
4 STA SE / Move E into AC(1)
SKI / Store content of E
BUN NXT / Check input flag
20 INP / Flag is off, check next flag
0 OUT / Flag is on, input character
STA PT1 I / Print character
ISZ PT1 / Store it in input buffer
SKO / Increment input pointer
NXT, BUN EXT / Check output flag
LDA PT2 I / Flag is off, exit
OUT / Load character from output buffer
ISZ PT2 / Output character
LDA SE / Increment output pointer
EXT, CIL / Restore value of AC(1)
LDA SAC / Shift it to E
ION / Restore content of AC
BUN ZRO / Turn interrupt on
I / Return to running program
SAC - / AC is stored here
, - / E is stored here
SE, - / Pointer of input buffer
PT1, - / Pointer of output buffer
PT2,

You might also like