Programming The Basic Computer
Programming The Basic Computer
Programming The Basic Computer
• Introduction
• Machine Language
• Assembly Language
• Assembler
• Program Loops
• Subroutines
• Input-Output Programming
1
Programming the Basic Computer Introduction
INTRODUCTION
Those concerned with computer architecture should have a knowledge
of both hardware and software because the two branches influence
each other.
2
Programming the Basic Computer Machine Language
MACHINE LANGUAGE
Program:
is a list of instructions or statements for directing the computer to
perform a required data processing task
• Machine-language
- Binary code
- Octal or hexadecimal code
• Assembly-language (Assembler)
- Symbolic code
3
Programming the Basic Computer Machine Language
4
Programming the Basic Computer Machine Language
5
Programming the Basic Computer Assembly Language
ASSEMBLY LANGUAGE
6
Programming the Basic Computer Assembly Language
ASSEMBLY LANGUAGE
Syntax of the BC assembly language:
Each line of code is arranged in three columns called fields:
1. Label field
- May be empty or may specify a symbolic address of up to 3 characters
- Terminated by a comma
2. Instruction field
- Specifies a machine or a pseudo instruction
- May specify one of the following:
* Memory reference instructions (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 instructions
Non-MRI does not have an address part
* Pseudo instruction with or without an operand
- Symbolic address used in the instruction field must be defined as a label
3. Comment field
- May be empty or may include a comment
7
Programming the Basic Computer Assembly Language
ASSEMBLY LANGUAGE
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 binary
HEX N Hexadecimal number N to be converted to binary
8
Programming the Basic Computer
ASSEMBLY LANGUAGE
Problem 6-3: List the assembly language program for the following
Fortran program.
SUM = 0
SUM = SUM + A + B
DIF = DIF – C
SUM = SUM + DIF
9
Programming the Basic Computer Assembly Language
TRANSLATION TO BINARY
10
Programming the Basic Computer Assembler
yes
yes
Store symbol END
in address-
symbol table
together with no Go to
value of LC second
pass
Increment LC
11
Programming the Basic Computer Assembler
Pseudo yes no
ORG END
instr.
1. Pseudo-Instruction Table no
no
2. MRI Table DEC or
3. Non-MRI Table yes
MRI
no HEX
Convert
4. Address Symbol Table operand
Get operation code to binary
and set bits 2-4 Valid
no and store
non-MRI
instr. in location
Search address- given by LC
symbol table for yes
memory word contains MRI: binary equivalent
of symbol address
1 2 4 5 16 and set bits 5-16
Store binary Error in
I Opcode Address equivalent of line of
yes no instruction code
I in location
given by LC
Set Set
first first
bit to 1 bit to 0
12
Programming the Basic Computer
ASSEMBLER - Example
Problem 6-7:
a. Obtain the address-symbol table generated for the Symbol Hexa code
following program during the first pass of the assembler. AND 0 or 8
ADD 1 or 9
b. List the translated program in hexadecimal. LDA 2 or A
STA 3 or B
ORG 100 BUN 4 or C
100 LDA ADS BSA 5 or D
101 STA PTR ISZ 6 or E
102 LDA NBR CLA 7800
103 STA CTR CLE 7400
104 CLA CMA 7200
105 LOP, ADD PTR I CME 7100
106 ISZ PTR CIR 7080
107 ISZ CTR
CIL 7040
108 BUN LOP
INC 7020
109 STA SUM
10A SPA 7010
HLT
10B SNA 7008
ADS, HEX 150
10C PTR, HEX 0 SZA 7004
10D NBR, DEC -100 SZE 7002
10E CTR, HEX 0 HLT 7001
10F SUM, HEX 0 INP F800
ORG 150 OUT F400
150 DEC 75 SKI F200
. . SKO F100
. . ION F080
. .
1B3 DEC 23 IOF F040
END
13
Programming the Basic Computer Program Loops
PROGRAM LOOPS
Loop: A sequence of instructions that are executed many times,
each time with a different set of data
C program to add 100 numbers: int a[100], sum, i;
sum = 0;
for (i = 0; i < 100; i++)
Add 100 numbers: sum = sum + a[i];
14
Programming the Basic Computer Program Loops
PROGRAM LOOPS
Loop: A sequence of instructions that are executed many times,
each time with a different set of data
C program to add 100 numbers: int a[100], sum, i;
sum = 0;
for (i = 0; i < 100; i++)
Add 100 numbers: sum = sum + a[i];
15
Programming the Basic Computer Program Loops
PROGRAM LOOPS
Loop: A sequence of instructions that are executed many times,
each time with a different set of data
C program to add 100 numbers: int a[100], sum, i;
sum = 0;
for (i = 0; i < 100; i++)
Add 100 numbers: sum = sum + a[i];
16
Programming the Basic Computer Program Loops
PROGRAM LOOPS
Loop: A sequence of instructions that are executed many times,
each time with a different set of data
C program to add 100 numbers: int a[100], sum, i;
sum = 0;
for (i = 0; i < 100; i++)
Add 100 numbers: sum = sum + a[i];
17
Programming the Basic Computer Program Loops
PROGRAM LOOPS
Loop: A sequence of instructions that are executed many times,
each time with a different set of data
C program to add 100 numbers: int a[100], sum, i;
sum = 0;
for (i = 0; i < 100; i++)
Add 100 numbers: sum = sum + a[i];
18
Programming the Basic Computer
PROGRAM LOOPS
:
CLA
LOP, STA PTR I
ISZ PTR
ISZ CTR
BUN LOP
HLT
:
19
Programming the Basic Computer Programming Arithmetic and Logic Operations
LOGIC OPERATIONS
A ∨ B = (A’ ∧ B’)’
20
Programming the Basic Computer Programming Arithmetic and Logic Operations
SHIFT OPERATIONS
E AC
CLE
0 0
CIR
AC E
CLE
0 0
CIL
E AC
CLE / Clear E to 0
AC positive 0 0
SPA / Skip if AC is positive E AC
CME / AC is negative; set E to 1 AC negative 1 1
CIR / Circulate E and AC
21
Programming the Basic Computer Subroutines
SUBROUTINES
Subroutine
- 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: Shift the value of X and Y four times to the left
Memory Loc. ORG 100 / Main program
100 LDA X / Load X
101 0 BSA 109 101 BSA SH4 / Branch to subroutine
102 Next instruction 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 return address 109 SH4, HEX 0 / Store return address here
10A Subroutine 10A CIL / Circulate left once
10B CIL
10C CIL
10D CIL / Circulate left fourth time
10E AND MSK / Set AC(0-3) to zero
10F 1 BUN 109 10F BUN SH4 I / Return to main program
110 MSK, HEX FFF0 / Mask operand
END
22
Programming the Basic Computer Subroutines
24
Programming the Basic Computer
SUBROUTINES
Problem 6-21: Write a subroutine to subtract two numbers. In the
calling program, the BSA instruction is followed by the subtrahend
and minuend. The difference is returned to the main program in the
third location following the BSA instruction.
BSA SUB
DEC -23 /Subtrahend
DEC 83 /Minuend
HEX 0 /Difference
HLT
SUB, HEX 0
:
Problem 6-22: Write a subroutine to complement each word in a
block of data. In the calling program, the BSA instruction is followed
by two parameters: the starting address of the block and the number
of words in the block.
BSA COM
HEX 100 /1st address of block
DEC 16 /Number of items in block
HLT
COM, HEX 0
:
25
Programming the Basic Computer Input Output Program
26
Programming the Basic Computer Input Output Program
CHARACTER MANIPULATION
Program to input two characters and pack them into one 16-bit word
AC
FST, SKI
BUN FST 15 8 7 0
BUN SCD
INP / Input 2nd character
STA WRD / Store both characters 1st Char 2nd Char
HLT
WRD, -- / Two characters stored here
27
Programming the Basic Computer Input Output Program
PROGRAM INTERRUPT
Memory
Before interrupt After interrupt cycle
Interrupt cycle
0 0 256
1 0 BUN 1120 PC = 1 0 BUN 1120
Store return address
in location 0
M[0] ← PC 255 255
Main Main
PC = 256 Program 256 Program
28
Programming the Basic Computer Input Output Program
PROGRAM INTERRUPT
29
Programming the Basic Computer Input Output Program
30
Programming the Basic Computer Input Output Program
31
Programming the Basic Computer Input Output Program
32
Programming the Basic Computer Input Output Program
33
Programming the Basic Computer Input Output Program
34