Chapter 6
Chapter 6
4/5/2017 1
Chapter Outline
I. Introduction
II. Machine Language
III. Assembly Language
IV. The Assembler
V. Program Loops
VI. Programing Arithmetic and Logic
Operations
VII. Subroutines
VIII.Input-Output Programming
4/5/2017 2
Introduction
•A computer system includes both
hardware and software. The designer
should be familiar with both of them.
•This chapter introduces basic
programming concepts and shows their
relation to the hardware representation of
instructions.
•A program may be : dependent or
independent on the computer that runs it.
4/5/2017 3
Instruction Set of the Basic Computer
Symbol Hex 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
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
4/5/2017 4
IOF F040 Turn interrupt off
Machine Language
•Program: A list of instructions that direct the
computer to perform a data processing task.
•Many programming languages (C++, JAVA).
However, the computer executes programs when
they are represented internally in binary form.
•Binary code: a binary representation of
instructions and operands as they appear in
computer memory.
•Octal or hexadecimal code: translation of binary
code to octal or hexadecimal representation.
4/5/2017 5
Hierarchy of programming languages
• symbolic code: The user uses symbols (Letter, numerals,
or special characters) for the operation, address and
other parts of the instruction code (Symbolic code).
• symbolic code: binary coded instruction
• The translation is done by a special program called
an assembler
• High-level programming language:
C++ : used to write procedures to solve a problem or
implement a task.
• Assembly language: concerned with the computer
hardware behavior.
• Compiler: high-level language program to binary
4/5/2017 6
Binary/Hex Code
• Binary Program to Add Two Numbers:
4/5/2017 7
Symbolic OP-Code
Location Instruction Comments
000 LDA 004 Load 1st operand into AC
001 ADD 005 Add 2nd operand to AC
002 STA 006 Store sum in location 006
003 HLT Halt computer
004 0053 1st operand
005 FFE9 2nd operand (negative)
006 0000 Store sum here
4/5/2017 8
Assembly-Language Program
• A further step is to replace:
4/5/2017 10
Assembly Language
4/5/2017 11
Rules of the Language
• Each line of an assembly language program is
arranged in three columns called fields:
1- The Label field: May be empty or specify a
symbolic address.
2- The Instruction field: Specifies a machine instruction or
pseudo instruction.
3- The Comment field: May be empty or it may include a
comment.
• Example:
ORG Lab
Lab, ADD op1 / this is an add operation.
Label Instruction comment
4/5/2017 13
Instruction field
4/5/2017 14
1-Memory-Reference Inst.
•Occupies two or three symbols separated by
spaces.
•The first must be a three-letter symbol defining
an MRI operation code. (ADD)
•The second is a symbolic address.
Ex: ADD OPR direct address MRI
•The third (optional)-Letter I to denote an
indirect address instruction
Ex: ADD OPR I Indirect address MRI
4/5/2017 15
Memory-Reference Inst. / cont.
• A symbolic address in the instruction field specifies
the memory location of the operand.
• This location MUST be defined somewhere in the
program by appearing again as a label in the first
column. Ex:
LDA X1
X1, HEX 40
Non-Memory-Reference Inst.
• Does not have an address part.
• It is recognized in the instruction field by one of the
three-letter symbols (CLA, INC, CLE,..).
4/5/2017 16
Pseudo instruction
•Not a machine instruction
•It is an instruction to the assembler giving
information about some phase of the
translation
Ex:
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 binary
HEX N
Hexadecimal number N to be converted to binary
4/5/2017 17
Comment Field
4/5/2017 18
Example:
• An assembly language program to subtract two
numbers
ORG 100 / Origin of program is location 100
LDA SUB / Load subtrahend to AC
CMA / Complement AC
INC / Increment AC
ADD MIN / Add minuend to AC
STA DIF / Store difference
HLT / Halt computer
MIN, DEC 83 / Minuend
SUB, DEC -23 / Subtrahend
DIF, HEX 0 / Difference stored here
END / End of symbolic program
Label Instruction comment
4/5/2017 19
TRANSLATION TO BINARY
4/5/2017 20
Address Symbol Table
•The translation process can be simplified if
we scan the entire symbolic program twice.
•No translation is done during the first scan.
We just assign a memory location to each
instruction and operand.
•Such assignment defines the address value of
labels and facilitates the translation during
the second scan.
4/5/2017 21
Address Symbol Table
•ORG & END are not assigned a numerical
location because they do not represent an
instruction or operand.
Address symbol Hex Address
MIN 106
SUB 107
DIF 108
4/5/2017 22
Example
LDA SUB
Address mode: direct I=0
Instruction: LDA 010
Address : SUB 107
4/5/2017 23
The Assembler
• An Assembler is a program that accepts a symbolic
language and produces its binary machine language
equivalent.
• The input symbolic program :Source program.
• The resulting binary program: Object program.
• Prior to assembly, the program must be stored in the
memory.
• A line of code is stored in consecutive memory
locations with two characters in each location. (each
character 8 bits) memory word 16 bits
4/5/2017 24
Example: storing the symbolic program in Memory
• PL3, LDA SUB I
• By referring to the ASCII code table, we get:
Memory
Symbol Hex code
word
1 PL 50 4C
2 3, 33 2C
3 LD 4C 44
4 A 41 20
5 SU 53 55
6 B 42 20
7 I CR 49 0D
4/5/2017 25
First Pass
4/5/2017 26
First Pass /cont.
•To keep track of instruction locations: the
assembler uses a memory word called a
location counter (LC).
•LC stores the value of the memory location
assigned to the instruction or operand
presently being processed.
•LC is initialized to the first location using the
ORG pseudo instruction. If there is no ORG
LC = 0.
4/5/2017 27
First Pass/ cont.
4/5/2017 28
Second Pass
• Machine instructions are translated in this pass by
means of a table lookup procedure.
4/5/2017 29
Assembler Tables
4/5/2017 30
Second Pass/ cont.
4/5/2017 31
Error Diagnostics
Example:
• Invalid machine code symbol.
• A symbolic address that did not appear as
a label.
4/5/2017 32
Program Loops
4/5/2017 33
Program Loops/ cont.
4/5/2017 34
Programming Arithmetic & Logic
Operations
• Software Implementation
- Implementation of an operation with a
program using machine instruction set
- Usually used: when the operation is not
included in the
instruction set
• Hardware Implementation
- Implementation of an operation in a computer
with one machine instruction
4/5/2017 35
Multiplication
4/5/2017 36
Multiplication / cont.
Example with four significant digits
4/5/2017 37
CTR - 8
P0 Example with four significant digits
E0
P
AC Y X = 0000 1111
Y = 0000 1011 0000 0000 X holds the multiplicand
0000 1111 0000 1111 Y holds the multiplier
cir EAC
0001 1110 0010 1101 P holds the product
0000 0000 0010 1101
Y AC 0111 1000 1010 0101
1010 0101
=0 =1
E
PP+X
E0
AC X
cil EAC
cil
X AC
CTR CTR + 1
0 =0
CTR Stop
4/5/2017 38
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
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
4/5/2017 39
Double Precision Addition
• When two 16-bit unsigned numbers are multiplied,
the result is a 32-bit product that must be stored in
two memory words.
• A number stored in two memory words is said to have
double precision.
4/5/2017 41
Double Precision Addition / cont.
4/5/2017 43
Shift Operations
4/5/2017 44
Logical Shift Operations
4/5/2017 45
Arithmetic Shift Operations
• Arithmetic shift right: it is necessary that the sign bit in
the leftmost position remain unchanged. But the sign bit
itself is shifted into the high-order bit position of the
number.
CLE / Clear E to 0
SPA / Skip if AC is positive, E remains 0
CME / AC is negative, set E to 1
CIR / Circulate E and AC
4/5/2017 46
Arithmetic Shift Operations /cont.
4/5/2017 47
Arithmetic Shift Operations /cont.
4/5/2017 49
Subroutines /cont.
•After executing the subroutine, a branch is
made back to the main program.
•It is necessary to store the return address
somewhere in the computer for the subroutine
to know where to return.
•In the basic computer, the link between the
main program and a subroutine is the BSA
instruction.
4/5/2017 50
Subroutines example- (CIL 4 times)
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
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 BUN SH4 I / Return to main program
110 MSK, HEX FFF0 / Mask operand
END
4/5/2017 51
Subroutines /cont.
4/5/2017 53
Subroutine Parameters and Data
Linkage /cont.
4/5/2017 54
Parameter Linkage
4/5/2017 55
Subroutine Parameters and Data
Linkage /cont.
•It is possible to have more than one operand
following the BSA instruction.
4/5/2017 56
Data Transfer
4/5/2017 57
Data transfer
/ 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
LDA MVE I / Bring address of destination
STA PT2 / Store in 2nd pointer
ISZ MVE / Increment return address
LDA MVE I / Bring number of items
STA CTR / Store in counter
ISZ MVE / Increment return address
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, --
4/5/2017 CTR, -- 58
Input-Output Programming
4/5/2017 59
Input-Output Programming /cont.
4/5/2017 60
Character Input
4/5/2017 61
Character Output
4/5/2017 62
Character Manipulation
4/5/2017 63
Subroutine to Input 2 Characters and pack into a word
4/5/2017 64
Buffer
•The symbolic program is stored in a section of the
memory called the buffer.
•A buffer is a set of consecutive memory locations
that stores data entered via the input device. Ex:
store input characters in a buffer
LDA ADS / Load first address of buffer
STA PTR / Initialize pointer
LOP, BSA IN2 /Go to subroutine IN2
STA PTR I / Store double character word in buffer
ISZ PTR / Increment pointer
BUN LOP / Branch to input more characters
HLT
ADS, HEX 500 / First address of buffer
PTR, HEX 0 / Location for pointer
4/5/2017 65
Table lookup
4/5/2017 66
Table lookup /cont.
4/5/2017 67
Table Lookup / cont.
4/5/2017 68
Program Interruptcont.
•Example: the computer is interrupted during
execution of the instruction at address 255
Memory
Before interrupt After interrupt cycle
0 0 256
1 0 BUN 1120 PC = 1 0 BUN 1120
Main Main
255 Program 255 Program
PC = 256 256
1120 1120
I/O I/O
Program Program
1 BUN 0 1 BUN 0
4/5/2017 70
Program Interrupt /cont.
4/5/2017 73
Service Routine /cont.
4/5/2017 74
Service Routine /cont.
4/5/2017 76
Interrupt Service Program
Loc.
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
4/5/2017 77
The end…….
• End of Chapter 5
………….Course To be continued
Thank you for your Listening
4/5/2017 78