0% found this document useful (0 votes)
76 views24 pages

CO Chapter 6

This document discusses programming the basic computer at the machine language and assembly language levels. It begins by introducing machine language and assembly language, and how an assembler is used to translate assembly language instructions into machine language. It then provides details on the instruction set and assembly language syntax for a basic computer, including label, instruction, and comment fields. Pseudo-instructions and an example assembly language program are presented. Advantages of assembly language programming are noted as understanding the microprocessor structure better, faster program execution, smaller program size, and full access to resources.

Uploaded by

Yassmine Ashraf
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)
76 views24 pages

CO Chapter 6

This document discusses programming the basic computer at the machine language and assembly language levels. It begins by introducing machine language and assembly language, and how an assembler is used to translate assembly language instructions into machine language. It then provides details on the instruction set and assembly language syntax for a basic computer, including label, instruction, and comment fields. Pseudo-instructions and an example assembly language program are presented. Advantages of assembly language programming are noted as understanding the microprocessor structure better, faster program execution, smaller program size, and full access to resources.

Uploaded by

Yassmine Ashraf
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/ 24

Computer Organization

CHAPTER 6
Programming the Basic Computer

Prof. Fawzy Ibrahim


Electronics and Communication Department
Misr International University (MIU)
Chapter Contents
6.1 Introduction
6.2 Machine Language
6.3 Assembly Language
6.4 Assembler
6.5 Program Loops
6.6 Programming Arithmetic and Logic Operations
6.7 Subroutines
6.8 Input-Output Programming

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.

• In high level programming languages, the programmer


writes his own program regardless what is going to
happen within the microprocessor and how the
microprocessor will run his program (machine
independent).

• Contrary to high level language, assembly programmer


should write instructions that determine exactly the
process in the microprocessor to solve his problem
(machine dependent).

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).

• The assembler is the one that do the job for the


assembly while compilers do it for the high level
language as shown in Fig. 6.1.

• Hierarchy of programming languages:


– Machine-language: Binary code, Octal or
hexadecimal code
– Assembly-language or Symbolic code needs
Assembler
– High-level language needs Compiler

5 of 24
6.2.2 How are Programs Understood by the Computer?

Program written in Translator program Program written in


programming machine language
language  Assembler
(object code)
 Compiler
(source code)
 Interpreter

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

Table 6.6 C++ Program to Add Two Numbers

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

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 I / Add an operand to AC
ISZ PTR / Increment pointer
ISZ CTR / Increment counter
BUN LOP / Repeat loop again
STA SUM / Store sum
HLT / Halt
ADS, HEX 150 / First address of operands
PTR, HEX 0 / Reserved for a pointer
NBR, DEC -100 / Initial value for a counter
CTR, HEX 0 / Reserved for a counter
SUM, HEX 0 / Sum is stored here
ORG 150 / Origin of operands is HEX 150
DEC 75 / First operand
.
. 23
DEC / Last operand
.
END / End of symbolic program
12 of 24
6.6 Programming Arithmetic and Logic Operations
Implementation of Arithmetic and Logic Operations

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.

Software Implementation example:

* Multiplication
- For simplicity, unsigned positive numbers
- 8-bit numbers gives 16-bit product

13 of 24
6.6.1 Multiplication Program
CTR  - 8
P0
X holds the multiplicand
E0 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
PP+X 0000 1111 0000 1111
E0 0001 1110 0010 1101
0000 0000 0010 1101
AC  X 0111 1000 1010 0101
1010 0101
cil EAC
cil

X  AC

CTR  CTR + 1 Fig. 6.2 Flowchart of multiplication Program


0 =0
CTR Stop

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

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
AL, - / Location of operands
AH, -
BL, -
BH, -
CL, -
CH, -

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)

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 again
OUT / Flag=1, output character
HLT
CHR, HEX 0057 / Character is "W"

21 of 24
6.8.1 Character Manipulation

Table 6.20 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 bits
BSA SH4 / 4 more bits
SCD, SKI
BUN SCD
INP / Input 2nd character
OUT
BUN IN2 I / Return

22 of 24
6.8.2 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

23 of 24
6.8.3 Interrupt Service Routine
Location

0 ZRO, - / Return address stored here


1 BUN SRV / Branch to service routine
100 CLA / Portion of running program
101 ION / Turn on interrupt facility
102 LDA X
103 ADD Y / Interrupt occurs here
104 STA Z / Program returns here after interrupt
/ Interrupt service routine
200 SRV, STA SAC / Store content of AC
CIR / Move E into AC(1)
STA SE / Store content of E
SKI / Check input flag
BUN NXT / Flag is off, check next flag
INP / Flag is on, input character
OUT / Print character
STA PT1 I / Store it in input buffer
ISZ PT1 / Increment input pointer
NXT, SKO / Check output flag
BUN EXT / Flag is off, exit
LDA PT2 I / Load character from output buffer
OUT / Output character
ISZ PT2 / Increment output pointer
EXT, LDA SE / Restore value of AC(1)
CIL / Shift it to E
LDA SAC / Restore content of AC
ION / Turn interrupt on
BUN ZRO I / Return to running program
SAC, - / AC is stored here
SE, - / E is stored here
PT1, - / Pointer of input buffer
PT2, - / Pointer of output buffer

24 of 24

You might also like