An Introduction To System Programming - Based On The PDP11 PDF
An Introduction To System Programming - Based On The PDP11 PDF
on the PDPll
Macmillan Computer Science Series
Consulting Editor
Professor F. H. Sumner, University of Manchester
"The titles marked with an asterisk were prepared du ring the Co nsulting Edito rship of
Professor J.5. Rohl, University of Western Australia.
An Introduction to
System Programming -
Based on the PDP!!
Derrick Morris
Department of Computer Science
University of Manchester
M
MACMILLAN PRESS
LONDON
© Derrick Morris 1983
Published by
THE MACMILLAN PRESS LTO
London and Basingstoke
Companies and representatives
throughout the world
Preface
Acknowledgements
1. 1 Number Representation 1
1. 2 Binary Arithmetic 2
1. 3 Negative Number Representation 3
1. 4 Notations for Binary Numbers 5
1. 5 Radix Conversion 6
1. 6 Exercises 6
5. 1 A SAL Program 62
5 .2 Constants 62
5 . 3 Instructions 65
5.4 Operands 66
5.5 Constant Statements 66
5.6 Directive Statements 67
5 .7 The Special Name ' Dot ' 68
5 . 8 Comment Statements 69
5. 9 Library Subroutines 69
5. 10 Simple Input Output Subroutines 69
5.10 .1 Octal Input 70
5. 10 . 2 Octal Output 71
5. 11 An Example of a Complete SAL Program 71
5 . 12 Exercises 73
6. Arithmetic Subroutines 76
6. 1 Multiplication 77
6 . 2 Division 79
6 .3 Double Length Arithmetic 82
6 .3 . 1 Double Length Addition and Subtraction 82
6.3.2 Multiplication 83
6 . 4 Floating Point Arithmetic 85
6.4. 1 Floating Point Addition 86
6.4.2 Floating Point Subtraction 90
6.4.3 Floating Point Multiply 90
6 .4. 4 FloaJlng Po int Division 93
6 .4.5 Floating Po int Round ing Errors 94
6 . 5 Binary Coded Decimal Arithmetic 95
6. 5. 1 16-Blt Unsigned Binary Coded Decimal Add'ttlon 96
6.5.2 32-Blt BCD Addition 98
6.5.3 BCD Subtraction. 99
6 .5.4 BCD MUltiplication and Division 100
6.5.5 Conversion from BCD to Binary 100
6 . 6 Input Output of Decimal Numbers 101
6.6. 1 Decimal Input Subroutine 102
6 . 6 . 2 Decimal Output Subroutine 103
6. 7 Exercises 105
7. Implementallon of the Assembler 107
Appendices
Index
Preface
1. 1 NUMBER REPRESENTATION
famil iar decimal system . except for the obvious difference In the range
of digit values . In any number system each digit position can express a
restricted range of values . 0 to r-sl . where r Is the radix. and
successive digit positions represent successive powers of the radix.
Obviously decimal numbers use the radix 10. the individual digits values
are In the range 0 to 9. and the significance of the d igit positions Is:
1
UNITS (10°). TENS (10 ) . HUNDREDS (102.). In the binary system the
o 1
sl~~plflcance of the digit positions is : UNITS (2 i . TWOS (2 i. FOURS
(2 ). The value of a number Is the sum of the values of the digits
multiplied by their significance .
DECIMAL BINARY
1 1
2 10
3 11
31 11111
0000000000000001
0000000000000010
0000000000000011
0000000000001010
0000000000011111
1. 2 BINARY ARITHMETIC
Arithmetic in binary is simple but tedious . The basic rules for addition
of binary digits are
0+0 ... 0
0+1 1
The Binary System 3
HO = 1
H1 .. 10 (or 0 and carry 1)
otherwise the process Is similar to the one used when adding decimal
numbers. Consider. for example. the addition In binary of the decimal
numbers 27 and 3
0000000000000011
+ 0000000000011011
0000000000011110 30 decimal
It follows that the basic rules for subtraction of binary digits are
0-0 .. 0
0-1 .. 1 (but 1 has been borrowed)
1-0 = 1
1-1 = 0
0000000000011110
- 0000000000000011
0000000000011011 = 27 decimal
0000000000001100
x 0000000000000101
0000000000110000
+ 0000000000001100
0000000000111100 60
1 + -2
0000000000000001
+ 1111111111111110
---------
1111111111111111 - -1
----------
1 - -2
0000000000000001
- 1111111111111110
---------
0000000000000011
_._----- - - - 3
The Binary System 5
~ X -2
0000000000000001
x 1111111111111110
1000000000000000
+ 0100000000000000
+ 0010000000000000
+ 0000000000000010
1111111111111110 - - 2
000 Is represented by 0
001 Is represented by 1
010 Is represented by 2
011 Is represented by 3
100 Is represented by 4
101 Is represented by 5
"0 Is represented by 6
"1 Is represented by 7
0000 Is represented by 0
0001 Is represented by 1
0010 Is represented by 2
1001 Is represented by 9
1010 Is represented by A
1011 Is represented by B
1100 Is represented by C
1101 Is represented by D
1110 Is represented by E
1111 Is represented by F
1. 5 RADIX CONVERSION
The formal way to convert the number (N) from radix rJ. to radix rz.
Is to fIrst express rz In radix r.1.. then repeatedly divide N by re,
workIng In radix r.1.. until an answer of zero arises . The digits of the
result In radix re , starting from the least significant end. are given by
the remainders generated from each dIvision . For example . consider
The Binary System 7
2)105
2)52 REM 1
2)26 REM 0
2)13 REM 0
2)6 REM 1
2)3 REM 0
2>1 REM 1
0 REM 1
11 5 3
Thus ANS = 1101001 2 + 2 + 2 + 2° = 105 decimal
101011010 = REM 0
111010 = 0 REM
000000001000100+1 = 000000001000101
1. 6 EXERCISES
2. Now write each of the above numbers In octal and sum them.
Convert the answer back to decimal, and check It by doing the
sum In decimal.
0000000000001111
0000000000011111
0000000001100100
1111111111100000
0000000001100100 x 0000000001100100 =
1111111110000000 / 0000000000001010 =
Check the answers by converting the original numbers and the
answers to decimal .
The registers In the control unit are called the present Instruction
register (PI>. and the program counter register ( PC) . The control
unit's function Is to take the Instructions of a program one at a time
from the store . and to cause the computer as a whole to 'obey' them .
by transmitting the appropriate control signals and data along the wires
which It controls . Normally the Instructions are obeyed sequentially in
the order In which they appear In the store. but some instructions,
called branch Instructions, alter this sequence. Thus some Instruction
sequences may be obeyed many times and others may be obeyed
conditionally (I. e . only If certain values arise In the calculation) . The
value In the PC roglster Is the means by which the computer 'keeps Its
place' In the program. In fact It Is always the number of the store line
containing the next Instruction . The reason why the control unit contains
a register (PI> to remember the present Instruction. Is that Its
execution will take place In stages. at fixed time Intervals, and the
control unit needs to 'Iook at' the bits In the Instruction to decide which
control signals It must generate at each stage. Obviously this Implies
that a lot of control circuitry Is wired Into the PI register .
A Simple Computer - MUD tt
The operation of the control unit can be summarised as follows
1) PI <= the number from the store line whose number Is given by
PC
2) PC <= PC+l
3) examine PI and cause the Instruction It specifies to be obeyed
4) repeat action (1).
2 .3 OVERALL OPERATION
The Importance of being very clear about the difference between a store
line number and the number It contains cannot be overstressed.
determines the maximum number of store lines which the computer can
have. The division we shall assume Is 4 bits for F and 12 bits for S.
Hence the computer can have 16 functions and 4096 store lines. We
shall only consider the first 8 Instructions as shown In Table 2. 1. The
rest may be assumed to be for control of the computer's Input/output
devices. which allow programs and data to be Input and results to be
printed. The notation '[S)' should be read as 'the content of the store
line whose address Is given by S'.
4 pc<-S PC is reset to S.
I
5 IF +VE PC<-S I PC is reset to S only if ACC>O.
I
6 IF ;t 0 PC<-S I PC is reset to S only if ACC;tO.
I
7 STOP I computer stops.
I
Control Unit
(CU)
Address
PC
PI
Data
Store
Arith Unit
(AU)
2 .6 PROGRAMMING MUO
START
o
+2
+2
+2
=6
o
+X
+X
Y times
+X
The main difficulty arises from the fact that X and Yare not known
explicitly. Instead It Is given that X Is In store line 40 and Y Is In store
line 41 . Thus the notion of repeatedly obeying an add Instruction a
variable number of times is needed . One solution Is
z--o
L1: Z<=Z+X
Y<=Y-1
IF Yl'!O GO BACK TO L1
STOP
X 10
Y 6 S 4 3 210
Z 0 10 20 30 40 SO 60
It Is also the case that this program will generate the correct answers
for negative and zero operands provided l.ihat the computer uses 2's
complement notation and works modulo 2 . There may however be a
time advantage In treating negative numbers and zero specially . For
example, If thls1.Sls not done. multiplication by -1 will be achieved by
multiplying by 2 -1
ACC<=[L2)
ACC=>[S2]
L1: ACC <=[S2]
ACC+ [SO)
ACC=> [S2]
ACC<=[Sl]
ACC- [L3]
ACC=>[Sl)
IF ACC ~ O. PC = L 1
STOP
L2 : 0
L3: 1
F S
4 12
1
FIND MAX El.EIENT
Two Instructions (labelled L3 and L5) of the sequence for finding the
largest entry In a list. Which Is given below. are altered In this way.
Also In the symbolic form. the list Is assumed to start In the store line
whose address Is given by L1STSTART. and finish In the store line
preceding the one whose address Is L1STEND.
A Simple Computer - MUD 19
ACC<=[L1STSTARTJ
ACC,;=> [ANSJ
L1: ACC<=[ANSJ
L3 : ACC-[L1STSTART+1J
IF ACC>O PC=L2
L4: ACC<=[L1STSTART+ll
ACC=> [ANSJ
L2: ACC<=[L3J
ACC+[L5J
ACC=>[L3J
ACC<=[L4]
ACC+[L5]
ACC=>[L4]
ACC-[L6J
IF ACC;tO PC=L 1
STOP
L5: 1
L6: ACC = [LISTEN OJ
ANS :O
This Is only the case on Initial entry to the program. As It runs the
sequence Is repeated only from L1 at which time the accumulator will
not already contain [ANSl. Second, the means by which the repetitive
part of the program Is terminated. Is that the Instruction at L4 will
eventually become 'ACC <= [LISTEN OJ' after progressing through the
sequence
2 . 7 EXERCISES
Rl <=Rl+10
Rl <=R1+R2
Rl <=R1+lR2l
The meaning of the first two should be obvious. and that of the last
Instruction Is that the number in the store line whose address is given
by the value of R2 Is added to the number In Rl. after Which the
number In Rl Is replaced by the result.
R
E
G Main
I Data Store
S
T
E
R
S
Arith
Unit
(AU)
lZ] <=[X]+[y]
meaning add the contents of the store lines whose addresses are given
by X and Y, and place the result in the store line whose address is
given by Z. However, this would Impose a requirement for the
Instruction format to accommodate three addresses . On a 16-blt
computer this Is not practicable and the PDPll uses a 'two address
format' . In this case the result replaces one of the two operands, thus
the general form of Instruction is
lX]<=[X]+[Y] .
Clear (set to 0)
Negate (replace by 2's complement>
Increment (add one) .
HALT.
F2 As Ad
Fl Ad
FO
and the branch Instructions .
Instructions must then always have this value: thus the effective part of
F 1 Is only 6 bits. and at least one combination of these must be
reserved to denote an FO. In fact the branch Instructions also must be
distinguished and resultant function encoding Is not completely
straightforward .
--------
1"2 As Ad zi I Ad I 1"0
4 6 6 10 6 16
3. 1 ADDRESSING MODES
Mode 0 Is called Reg/ster Mode. and In this case the number In the
specified register is used as the operand. and of course replaced by
the result In the case of Ad . Thus the Instruction
26 An Introduction to System Programming - Based on the PDP' 1
R1 = 1000
R2 = 2
STORE LINE 1000 = 2
STORE LINE 1002 = 10
STORE LINE 1004 -4
Rl = 1000
R2 = 1002
STORE LINE 1000 = 100
STORE LINE 1002 = 1006
STORE LINE 1004 = 1008
STORE LINE 1006 = -2
STORE LINE 1008 = -1
S. 1. 5 Special Registers
Until now all eight registers have been treated the same. but A6 and
A7 serve special functions. In fact A7 Is the program counter, hence It
Is automatically incremented (by 2) each time an Instruction Is taken
from the store. and each time a constant required by a mode 6
operand Is accessed. The effects of specifying A7 with certain
addressing modes are both Interesting and useful. They are discussed
below. A6 is the 'stack pointer' . Because A6 and A7 serve these
special functions. they are usually referred to as SP and PC.
Any addressing mode can be used with PC . the program counter . but
the effects of using 2 . 3. 6 and 7 with PC are the most Interesting
and are given special names . The other modes are rarely. If ever.
used with PC.
062702
000005
062737
000003
000006
which adds the constant 3 Into the number In store line 6 . In order to
decode this kind of Instruction. It Is necessary to remember the rule
given In 3 . 1. 1. that the source operand address Is always computed
before the destination operand address .
Mode 6 with PC causes the constant from the next store line to be
added to the number from PC to form the address of the operand .
Thus the constant gives the position of the operand relative to the
position of the Instruction containing It. and Is therefore called relative
mode . More strictly It Is relative to the address of the store line
following the constant because the program counter ( PC) Is
Incremented as the constant Is taken from store. Its significance
hinges on the property that If a sequence of Instructions. and the data
The Structure of the PDP11 31
The double operand Instructions with functions F2. and the single
operand Instructions with functions F 1 provide the computational
facilities . They are mostly readily understandable functions which can be
defined In tabular form In terms of the way the operands (SOURCE and
DEST) are used . but some amplification Is given of points of possible
difficulty.
Only those functions whose description Involves the ' bec o me s' symbol
«=) cause a change In the value of a store line or register . In the
case of the other functions. the result formed In the arithmetic unit Is
lost when the next computational Instruction Is obeyed . Nevertheless
they serve a useful purpose because the conditional branch Instructions .
as will be seen later. branch according to the value of the last
computed result. Thus as their names Imply. functions 02 and 03 are
used for comparing and testing operands.
The operators used above may need some discussion . Add (+) afl.~
subtract (-) are the straightforward 2's complement modulo 2
operations that were encountered In MUO. The logical operators - . If,
and V are new. The not operator ( ") Is the simplest. It Is a unary
operator which Indicates that the following operand Is to be l's
32 An Introduction to System Programming - Based on the PDP11
0 0 0 0 0
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0
The names chosen for the functions should help us remember their
actions. For example. the bit set function sets to one all the digits In
the destination which correspond with ones In the source operand
leaving the rest unchanged. and the bit clear function sets to zero all
the digits In the destination which correspond with ones In the source
leaving the rest unchanged . Realistic examples of the use of these
functions cannot be given until other functions have been described.
.. •
I I
DeST
I~I I I
•
; I I
DeST
I~I I I II JROL
Figure 3.2 Pictorial Description of ROR and ROL
Rotate right (ROR) moves all the digits of the destination one place
to the right. The least significant digit has no digit position to move
Into. therefore It Is placed In C and the previous value of C Is moved
to the vacant. most significant. digit position. Rotate left (ROU Is
similar but the digits are moved left. Figure 3.2 Illustrates the actions
of ROR and ROL.
The Arithmetic Unit has some Internal memory . and Included In this are
four single bit quantities which contain Information relating to the most
recent result. They Include the carry bit CC) already described . a zero
bit CZ> which Is set to one only If the last result was zero. a sign bit
CN) which Is set to one only If the last result was negative. and an
overflow bit CV) which Is set only If the last result Is In error because
of arithmetic overflow. With some functions the setting of these
condition codes Is not as might be expected and these exceptions are
shown In Table 3.4 .
J«>V **0-
CMP **** no other action
BIT **0- no other action
BIC **0-
BIS **0-
ADD ****
SUB ****
CLR 0100
COM **01
INC ..-_1t_
DEC ***-
NEG ***1 C cleared if result .. 0, otherwise set .. 1
ACe ****
sac 1t***
TST **00 no other action
ROR **1* V is cleared if N-e, otherwise set .. 1
ROL **1* V is cleared if N-e, otherwise set .. 1
ASR **1* V is cleared if N-e, otherwise set .. 1
ASL ****
SWAB **00
and to achieve better range they are shifted one place left before being
added to the program counter . This exploits the fact that store lines
only have even numbered addresses . Thus the format of a branch
instruction Is
FUNCTION OFFSET
Fb x
a a
and the action Is PC<=PC+2X. If the condition specified by Fb is
satisfied . otherwise It has no action .
The group BGE . BlT. BGT and BlE are used for quite
straightforward comparisons but the means by which they achieve the
required effects are possibly obscure . Consider BGE which branches If
the last result was greater than or equal to zero . This might be used
after comparing two numbers . X and Y say . by means of the compare
Instruction CMP X Y. which will subtract Y from X. As we would
expect . a BGE Instruction following this CMP Instruction would branch If
X > Y. This condition can be detected from the sign of X-V provided
that no arithmetic overflow occurs . In other words If N=O and V=O. X Is
greater than or equal to Y. If overflow does occur then the sign of the
result Is wrong. Therefore X is also greater than Y if the result of X-V
is negative but overflow has occurred . Hence the conditions for BGE
must Include N=l and V=l. The BlT function Is the converse expecting
a negative result from X-V In the no overflow case . An additional test
for zero as shown In the Table is necessary with the functions BGT and
BlE . excluding zero In the former and Including It In the latter.
R1 contains X
R2 contains Y
RO Is to contain the answer Z
005000 RO<=O
060100 RO<=RO+R1
005302 R2<=R2-1
001375 BRANCH NOT EQUAL (ZERO>
38 An Introduction to System Programming - Based on the PDP"
This example Illustrates many of the features of the PDPll that have
been described above . Obviously It does not Illustrate all the functions.
nor all the addressing modes . but students who find It understandable
are making satisfactory progress. Those who do not understand the
example should take It as a signal that they need to read again the
preceding description of the PDPll.
3.4 SUBROUTINES
004737
002000
was In store lines 1000 and 1002, Its execution would ba aqulvalenl 10
the two Instructions
012746 -[SP)<=1004
001004
012707 PC<=2000
002000
010246 stack R2
005000 multiply sequence as above
060100
005302
001375
012602 unstack R2
000207
3. 6 BYTE ADDRESSING
A final Important aspect of the PDPll order code that we must consider
Is Its ability to operate on ' Bytes' . We have already said that the
addresses In the PDPll can refer to half words. These a-bit quantities
are called bytes and most of the computational Instructions can operate
on bytes as well as on 16-blt full store lines (or words) . The two
The Structure of the PDPf 1 41
L F2
7
000207
That Is the specified register Is copied to PC. and the register Is then
reset from the stack. It Is. therefore . necessary to advance the
register R to the first Instruction after the 'Inllne' parameters If the
facility to store parameters after the JSR Instruction Is exploited. This
can sometimes be achieved by using auto-Increment at the time of
accessing the parameters.
The condition codes are set as for the other logical functions such as
BIC .
077 R OFFSET
3. 8 EXERCISES
R3 <= 0
R4 <= 10
R5 <= R2
(98) <= 0
[R3-6) <= 20
a) 005000
012701
000140
066100
000400
005301
005301
001373
b) 060202
010246
060202
060202
062602
a) RO <= Rll
b) RO <= INTEGER PART (Rl/R2)
4 Operation and Bootstrapping
From the point of view of manual operation there are two main types of
PDPll. Early versions of the machine such as the 11/20 and 11/40
have a control console with lamps and switches. The LSI based systems
have the absolute minimum of switches . and manual control Is achieved
by commands typed on a console teletype or VDU. Some of the larger
systems offer a combination of both facilities but these are not
discussed . It Is convenient to describe first the early system since the
basic actions available are similar.
Most of the operations that can be Initiated from the control panel
depend upon the setting of a special Internal register. called the
ADDRESS REGISTER. Therefore. before any other action can be
performed. It Is necessary to operate the load address key ( LOAD
ADDR) . This key causes the number defined by the setting of the
SWITCH REGISTER to be copied Into the ADDRESS REGISTER. The
deposit key (DEP) provides the means for writing numbers Into the
store. Each time It Is operated. It caples the number. which Is set up
on the SWITCH REGISTER. Into the store line whose address Is given
by the value of the ADDRESS REGISTER. If the DEP key Is operated
repeatedly. the number In the ADDRESS REGISTER will be repeatedly
Incremented to address successive store lines. Thus to load a program
Into the store of the PDPll by means of the console facilities we
proceed as follows .
Note that it Is only when either the DEP key or the EXAM key Is
repeatedly operated that the content of the ADDRESS REGISTER Is
Incremented . Alternate operation of these keys will leave the ADDRESS
REGISTER unchanged . This means that If an Incorrect value Is noticed
after operating EXAM It can be corrected simply by setting the correct
value on the SWITCH REGISTER and operating DEP.
The LSI types of PDP11 present an apparent contradiction. They are the
simpler and cheaper systems In hardware terms yet they have a more
sophisticated mechanism for manual control. However. this Is easily
explained .
It must have been obvious from the description of the control unit of
the simple computer In Chapter 3. that It bore strong resemblance to a
program. In fact. the operation of a control unit can be formally
expressed as a program. and this Is often done when programs written
In the machine code of one computer are to be run on another. An
actual example Is given In Section 5. 11. In the form of a PDP11
program that emulates the action of the MUO processor. This technique
Is In fact the basis of computers that are said to be microprogrammed .
In such computers the hardware provides a very primitive Instruction set
and there Is a program written In this Instruction set. permanently
resident In the machine. which Interprets the user Instruction set.
LSI11 s are microprogrammed In this way and the microprogram Is
contained In a read only memory Inaccessible to the user. Therefore.
without Incurring any extra hardware cost. except possibly as a result of
Increasing the size of the microprogram store . quite sophisticated
control operations can be provided . In fact all the later models of
PDP" are microprogrammed but some. such as the 11/40 . maintain
control console compatibility with earlier models such as the "/20.
others such as the 11/34 have a different key arrangement but a
functional similarity. and on the very latest models no hand switches
are provided.
keys.
LF this causes the given value to be deposited In the open store line
al')d the next store line Is opened which means that Its address
and value Is printed .
t this has similar action to LF except the new store line opened Is
the previous store line .
CR this causes the given value to be deposited but there Is then no
open store line, therefore the next command should be a I or G .
If any of the above keys Is operated without first giving a new value,
the value of the open store line Is unchanged but the action of moving
onto a new open store line stili applies .
RO/OOOOOO LF CR
RlI000001 LF CR
R2/000002 LF CR
R3/000003 CR LF
•
The bold characters above Indicate the computers response to a user
typing the characters shown In normal type. assuming that the values of
RO. R1. R2 and R3 were O. 1, 2 and 3 at the time . The It symbol Is
output by the computer when It Is ready to accept a new I or G
command .
There are two other ways In which the user can regain console
control from a program . One Is by setting the HALT switch , before
starting the program. In this case the computer will automatically halt
the program after each Instruction and return to console mode. This
enables the user to 'single shot' through the program by repeated
operation of the P command. Although this Is not to be recommended
50 An Introduction to System Programming - Based on the PDP11
4 .3 AN OCTAL LOADER
The main Information following the t will be the list of octal numbers
representing the program . and the loader could place them In fixed
positions In the store, but It Is more useful to allow the user to specify
a position . Therefore. the first number after the Initial t will be taken
as the address of the first store line Into which the program. which
then follows. Is to be loaded. Each number In this list must be
separated from the adjacent ones and the space or newline characters
would be the obvious choice, so that If the tape Is printed each
number. or group of numbers separated by spaces , will appear on a
separate line . The list of numbers must be terminated by a further
definite character and "l" Is again used for this purpose . When this
second ' f ' character Is encountered the loader will halt. Usually the
Operation and Bootstrapping 51
user will take control at this point and manually start the program. If.
however. there are further Instructions to be read a new tape can be
placed In the reader and operation of CONT (or typing P) will restart
the loader .
Thus . a simple program written for the loader will have the form
The octal loader has a main program section and two subroutines .
One subroutine Is responsible for reading the Individual characters from
paper tape and a second will read complete octal numbers. Thus the
operation of the main program can be expressed as In Figure 4.2.
1
OCTAL LDAIEl
1000 ->
STACK I LOADER AVAIL TO USER
Operation and Bootstrapping 53
l
READ OCTAL NUMBER
The code for the loader can now be derived from the given flowcharts.
It was not written out In a single pass because the addresses In some
Instructions depend upon the position that other later instructions
occupy . but after some trial and error and some tedious counting the
following sequence results. Note that an Instruction not mentioned on
the flowchart has been added to the main program. to set SP to the
chosen address so that the JSR Instructions will function properly. Also
the operands of the JSR Instructions are expressed In relative mode . so
that the loader could In fact be placed anywhere In the store. although
it Is written against addresses 1000 upwards to help readers who wish
to check the detail of the encoding .
This loader can now be keyed Into the machine as described earlier
and each time an octal program. punched on paper tape. Is to be run
the following sequence of actions will be required
A simple test for the loader would be to use It to load the sequence
of Instructions given In Chapter 3 to find the largest number In a list of
numbers. It Is only necessary to decide where the sequence Is to be
placed In the store and to append a suitable list of numbers for the
Instructions to scan. Thus the program and data to be punched Is
t2000
013700 002040 012701 000002 026100 002042 003402 016100
002042 062701 000002 020127 000020 001366 000000 000001
000002 000100 177000 000101 000000 177777 000100
t
If this paper tape Is now fed Into the computer as described above,
and entered at address 2000. The computer will halt with the PC =
2036 and an answer In register 0 which will be '0' .
Any reader with access to a PDP" who keys In the octal loader,
and uses It to read some octal programs will appreciate that In practice
It becomes necessary to repeat the manual loading sequence many
times . The loader Is vulnerable to being overwritten by accident. and
on machines with 'MOS' stores the contents are lost each time the
machine Is switched off. For this reason It Is attractive to devise a
means by which the octal loader can Itself be read In from paper tape.
The points marked • are the places at which the bootstrap can be
restarted If for any reason the content of the store Is lost.
BINOUT requires to be given the addresses of the first and last lines
Operation and BootstrapplnQ 57
BININ will be written to be keyed Into store with Its last Instruction
Immediately preceding the store line at which loading Is to commence .
It will load until there Is no more paper tape . Further operating
Information for BINOUT and BININ . that will be required If a computer Is
available to try them In practice. Is that execution of both Is to be
started at their first Instruction. Also the second line of BININ must
always be the absolute address of Its last line . Finally. the tape
produced by BINOUT Is to be placed In the reader for reading
backwards from either of the two 366 characters which are the fourth
and fifth last character punched by BINOUT.
1
TRANSMIT SEIJ.EIa
Only one problem remains, namely how Is the above sequence and
the loader to be Input. They can of course be keyed In but this Is
tedious since together they occupy over 100 lines of store .
If the teletype has a paper tape attachment then BINOUT and BININ
can be used to reduce the problem to more manageable proportions.
Failing this the problem can be allevlated by using the teletype directly
connected to computer X to get to a state where one final character
will cause It to output the required code In some more optimal format.
At this point the system can be reconfigured to Include the PDPll. The
only code that then has to be keyed In would send the final character
and load the response. This Is a rather Inelegant approach which Is
not pursued here. Perhaps the best solution of all Is the one mentioned
below .
4.7 EXERCISES
RO = -1
R1 16
R2 =4
R7 = 2000 8 ,
Again use the EXAMINE facility to check the result.
R1 4
R2 = -1
R7 = 2000
Again operate start. or type 2000G, and If you can observe the
time needed to complete the multiplication . Check that the answer
In RO Is correct and compute the approximate average Instruction
time of your computer.
5. Key Into the store of a PDP11 the loader given In 4 .3. 'Start'
execution of the loader at the first Instruction (octal 1000) and
either type the sequence of Instructions given on page 60. After $
has been typed the loader should halt. Now 'start' the sequence
at the first Instruction (octal 2000). It should halt at address 2036
octal. Examine the content of register O.
5 A Simple Assembly Language
5. 1 A SAL PROGRAM
5 .2 CONSTANTS
• - 2000
STARr:J«lV t~OOO,SP; STARr OP OCTAL LOADER
LllJSR PC,RDCH
CMP RO,t't
BNE Ll.
JSR PC, RDOOl'
K>V Rl.,RZ
LZ :JSR PC,RDOOl'
K>V Rl., (RZ)+
BR LZ
RDOOl':BEGIN; READ OCTAL NUMBER SUBROUTINE
CLR Rl.
L3 I JSR PC, RDCB
CMP RO,t'O
BMI Ll.
CMP "7,RO
BPI. LZ
LllCMP RO," t
BNE L3
HALT
JMP
LZIASL Rl.
ASLRl.
ASL Rl.
ADD RO,Rl.
SUB "O ,Rl.
L4IJSR PC, RDCB
CMP RO,t'O
BMI LS
CMP t'7,RO
BPI. LZ
LSIRTS PC
END
RDCHI BEGIN; READ CHARACTER SUBROUTINE
STATLN - l.77560
INC otSTATIM
Ll.1 TSTB OtSTATIM
BPI, Ll.
K>V OtSTATLN+2, RO
BIC t200,RO
RTS PC
END
ENTER
to say
1 Is equivalent to 000001
12 Is equivalent to 000012
5555555 Is equivalent to 155555.
Note that because the first byte of a storellne Is the rightmost one the
characters appear to be Interchanged .
Labels are names which have been given values elsewhere In the
program. When used as constants their values are substituted . Names
always begin with a letter which may be followed by up to five further
letters and/or digits . If a constant Is specified by a name that has
been earlier defined, it is called a backward reference. whereas If the
name Is later defined. It Is called a forward reference . There are some
contexts In which a forward reference Is not allowed . and these will be
Identified as they arise .
+ (meaning add)
- (meaning subt:ract:)
" (meaning mUltiply)
I (meaning divide)
& (meaning logical •and' )
I (meaning logical (inclusive) 'or')
-1& 177400!'A+256"16 .
This would be evaluated 'left to right' with all operators having equal
precedence to yield
More realistic examples usually involve labels and are often quite
simple. for example Ll+2. and COUNT-l. would be typical. It should
be noted that forward references are not permitted except when the
(forward referenced) name is the entire expression . Thus. at a point In
the program where Ll is not yet defined . ' L I ' alone Is a legitimate
form of expression, but ' Ll + l' and "- L'l ' are not .
5. 3 INSTAUCTIONS
BEQ L1
Ll:
5. 4 OPERANDS
There are 14 operand forms In SAL which correspond to the eight basic
addressing modes. the four modes that have special significance In
conjunction with PC. and alternative notations for two modes which
arise out of a rationalisation In the notation. Using the symbol <C> to
represent any constant and <R> to represent any register. the operand
forms can be defined thus
Registers 0 - S are denoted RO. R1. ... RS. but registers 6 and 7
are denoted by the names SP and PC to remind us of their special
functions .
BEGIN
Ll:
BEGIN
BEGIN
-) Ll
END
Ll :
END
-) L1
END
name constant
Examples are
STATLN - 177560
L3 - L4
BUFf' - STATLN+2
COUNT - 10.
COUNT2 - 20.
BEGIN
COUNTl - COUNT
COUNT - 30 .
K>V #COUNT, Rl 012701
000036
K>V #COUNT1, R2 012702
000012
K>V #COUNT2, R3 012703
000000
END
The label setting directive always generates an explicit value for the
label: hence. Inside the block COUNT1 and COUNT have the values 10
and 30 decimal. All references to names which have already been
defined In the current block are replaced by their values as they are
assembled . References to names not yet defined In the current block
(e . g . COUNT2) are treated as forward references . and are left blank
at least until they are defined within the block or until the end of the
block. At the end of the block COUNT1 and COUNT cease to be
defined as 10 and 30. COUNT1 becomes undefined and COUNT reverts
to Its previous value of 10. Also the definition of COUNT2 becomes
operative again and the blank line previously left In the program
becomes 20 .
The symbol ": ' Is treated as a pseudo name. and the value associated
with It Is always the address of the store line Into which the next
statement will be assembled . Although It can be used In any context
where a normal name Is permitted. the most common use Is In
controlling the position of code and data at assembly time. For example
A Simple Assembly Language 69
the statement
. = 2000
will result In the assembly of sUbsequent statements Into store line 2000
(octal) onwards . Another fairly common use Is In leaving areas of store
empty to be used as working space. For example. two areas each of
size 128 (decimal) store lines called AREA 1 and AREA2 can be created
using
AREAl :
. = . +256.
AREA2:
. = . +256.
5 .9 LIBRARY SUBROUTINES
123
ABO
+9 - 3C
Again for the present It Is enough to assume that the Input for a
program will be the sequence of characters Immediately following the
ENTER statement. and the output Is to be printed.
5 . 10 . 1 Octal Input
The requirement here Is for a subroutine to read the next octal number
from the Input and convert It to binary. Its detailed specification Is a
matter of Individual choice. We shall assume that the subroutine Is to
Ignore any characters which precede the first octal digit. then treat all
following octal digits as part of the number . stopping when a character
other than an octal digit Is encountered . The result will be placed In
Rl and no other register except RO will be disturbed. A very similar
subroutlna. forming part of the octal loader . was given In Figure 4. 3 .
In both cases as each new octal character Is read the previous octal
digits are moved three binary places to the left. and an octal digit.
computed from the value of the character minus the value of character
A Simple Assembly Language 71
INOCT : BEGIN
CLR R1
L1: JSR PC, INCH
CMP RO,I'O
BMI L1
CMP 1'7, RO
BMI L1
L2 : ASL R1
ASL Rl
ASL RI
BIC 1177770.RO
ADD RO.Rl
JSR PC. INCH
CMP RO ,I'O
BMI L3
CMP 1'7. RO
BPL L2
L3 : RTS PC
END
The octal Print subroutine will be given a value, say In R1. and Its
function Is to determine and print the code for each digit In the octal
representation of the value . It Is assumed that a binary digit followed by
5 octal digits are to be printed, regardless of the values of the leading
digits. The operation of the subroutine Is summarised In flowchart form
In Figure 5 .5 . This Is a precise flowchart corresponding exactly to the
Instructions which appear In the OUTOCT subroutine of Appendix 2.
1
OCTAL PRrNT (RD
If an octal digit Is typed, It and all following octal digits are treated
as an octal number to be placed In a variable called OCT . NO.
Normally a '/' or a ' G' should follow an octal number. The former will
cause the OCT . NO to be copied to another variable called ADDR, and
then both ADDR and the content of the store line whose address It
contains are printed. The latter causes the MUO program counter to be
set to the value of OCT . NO, the MUO ACC to be cleared. and
Inter pretati on of MUO instructions begins . Note that ADDR and PC are
doubled before use. because they represent MUO words and the PDPll
addressing Is In byte units. If '/' Is typed without a preceding octal
number ADDR is unchanged. likewise If G Is typed without a preceding
octal number . tho program counter Is set to zero. After ADDR has
been set. consecutive lines can be Inspected by repeatedly typing LF.
A SImple Assembly Language 73
They can be altered by typing new values In octal before the LF . When
an MUO program Is 'running' the user can stop It and regain control by
striking any key (except break). In which case the current value of PC
and ACC will be printed . A STOP Instruction In the program has the
same effect. The program can be caused to continue by typ ing 'P·.
The INOCT subroutine used here Is very similar to that In Section
5 .10.1. except the first octal digit has been read before entry.
Appendix 2 gives the SAL encoding for the program and all the
necessary subroutines except INCH. OUTCH and IENQ which are based
on Information not presented until Chapter B.
5. 12 EXERCISES
a) MOV #0 . AS
b) ADD #1. A4
c) SUB #1. A2
d) ADD Ll. Ll
e) MOV -2( AD. 8-2( AD
2. Again translate the Instructions given below Into octal. Each of the
sequences of Instructions could have been replaced by single
Instructions. What are the Instructions?
a) COM AS
INC AS
c) NEG A2
DEC A2
10
NUl
INTERPRETER
+
23 21
<CH 0)
(~)
a
4
Pe-ocT.tI> 22
CI..EAR ACt PRINT ?
25
<CH a P)
.-------l2B - - - - - ,
svrrtH ON FPART a= PI
F~ ACt-IS PART a= P[]
Fall ACt=>IS PART a= P[]
F-2I ACt+IS PART a= P[]
F-31 Act-IS PART a= P[]
Fa40 PC-S PART a= PI
F"5, IF ACC>-O. PeaS PART a= PI
F-6o IF ACt/-o. PCa5 PART a= PI
F-7, lEALT WITH INOOX 19
30 F-e, PRINT ACt IN OCTAL
00
MOV (A3). A3
MOV (A3) . A4
MOV (SP)+. A3
b) MOV IVEC1. R1
MOV 'VEC2. R2
Ll:ADD CRl> , CR2)
INC R1
INC R1
INC R2
INC R2
CMP R1. #VEC1 + LIMIT
BNE L1
c) CLR R4
MOV #1. Rl
MOV #100000. R2
L2 : BIT R3, R2
BEQ L1
ADD Rl. R4
Ll : ROR R2
ROL R1
BCC L2
6 Arithmetic Subroutines
M x base L
If computers worked with decimal numbers the base would be 10. but
In binary computers It Is usually 2 . or a power of 2 such as 8 or 16.
Another form of arithmetic that Is often provided on computers Is binary
coded decimal arithmetic. In this case a number is diVided Into groups
of 4 bits. and each group Is used to represent a decimal digit. For
example. the number 1879 would be represented by the binary pattern
because the combinations 1010. 1011. 1100. 1101. 111 0 and 1111 are
not used as decimal digits . the system stili has some attraction
because of Its simple correspondence with the decimal form of the
numbers. When a large amount of Input/output has to be processed In
a relatively simple way. It is sometlmos thought more effle!ent to work
with binary coded decimal than to convert such numbers to binary on
Input. and back to decimal on output. Commercial data processing
tends to have this characteristic . hence the computers Intended
primarily for such applications have arithmetic units which operate
directly on binary coded decimal numbers . Also the principal data
processing language. Cobol. has truncation and rounding rules which
assume that the operands are decimal numbers. thus producing a
requirement for binary coded decimal arithmetic even If It Is not
provided by the hardware . When binary coded decimal arithmetic has to
be provided by software. the time advantage Is eroded. and the only
Justification Is to support a Cobol complier .
6. 1 MULTIPUCATION
1001
x 1001
1001000
+ 1001
1010001 81
starting with the least significant. then the repetition can terminate
when all the non-zero bits have been considered. A flowchart of the
method applied to the generation of
Is given In Figure 6. 1.
I
Ml1.TIPLY
d~l
,------+-~--
,CLEAR CARRY
0ND ROR MULTIPLlER
we can see that the square of the largest positive number expressible
In 16 bits Is a number that cannot be expressed in less than 31 bits .
6 .2 DIVISION
10100 - 20
101)1100100
101
101
101
000 '" REM
The notation should be clear except for the dividend which must be
treated as a 32-blt number. Obviously this requires two PDP11 words
and Initially the least significant one contains the actual dividend whilst
the other contains zero. Each time through the loop the whole of this
32-blt number. referred to as DIVIDEND. Is shifted one place left. Thus
the most significant part called DIVTOP contains the digits which are to
be compared with the divisor . The successive digits of ANSWER are
generated at Its least significant end. hence ANSWER Is also shifted left
on each pass through the loop . Since the loop Is executed sixteen
times the ANSWER digit resulting from the first execution eventually
arrives at the most significant end .
11/3 = 3 REM 2
-11/3 = -3 REM -2
11/-3 = -3 REM 2
- 11 / - 3 = 3 REM -2
In double length arithmetic on the PDPll. two 16-blt store lines are
used for each number . as they were In the case of the DIVIDEND In
the above division SUbroutine .
When two double length numbers aro added. using the 16-blt arithmetic
provided by the PDP 11. special action is needed to propagate the carry
which arises from the addition of the two least significant 16-blt parts.
into the most significant 16 bits of the answer. Thus . the first step is
the addition of the two least significant parts. This addition sets 'C'
according to whether or not the addition produces carry. Before any
fur.ther arithmetic functions are used. which would alter ·C·. an add
carry instruction CADC) must be obeyed to Incorporate the carry in the
Arithmetic Subroutines 83
ADD 2( an . 2( AO)
ADC (AO)
ADD (Al). (AO>
(l) -1 +
(2) _23~ - ~
( RO) 2(RO) (JU) 2(JU) V
100000 000000 000000 000001
SUB 2( JU ),2( RO) 177777 0
sac (RO) 077777 1
SUB (JU),(RO) 077777 0
6.3.2 Multiplication
DLMULT I BEGIN
K>V 2(RO), -( SP)
K>V (RO), -( SP ); STACK MULTIPLICAND
CLR 2(RO)
CLR (RO) ; ANS - 0
LOOP I CLC
ROR (RJ.)
ROR 2( RJ.) ; ROR MULTIPLIER
BeC Ll
ADD 2(SP), 2(RO)
ADC (RO)
ADD (SP), (RO); ANS - ANS+MULTIPLICAND
L11 ASL 2(SP)
ROL (SP) ; MULTIPLICAND*2
TST 2(Rl)
BNE LOOP
TST (Rl)
BNE LOOP
ADD #4, SP
RTS PC
END
BEGIN
CLA AO
CLA -CSP); m . s , part of ANS
CLA -CSP); m. s . part of MULTIPLICAND
L2: AOA A2
BCC Ll
ADD at . AO
ADC 2CSP)
ADD CSP) . 2CSP)
L1: ASL Al
Arithmetic Subroutines 86
AOL CSP)
TST A2
SNE L2
TST CA6)+
TST CA6)+
SEQ L3
SEV
L3: ATS PC
END
5 E' M'
1 8 23
However, the actual stored exponent CE') and mantissa CM') are not
the true values for the number they represent . and the difference Is
described below . Other relevant details are mentioned In relation to
specific examples.
The second peculiarity of the PDP11 floating point format is that the
true exponent (E) Is represented In ' exc ess 128 ' form. This means that
each actual exponent In the machine (E ') Is bigger than Its true value
by 128. Consider now the following examples
E1 E2
Let the two operands be represented by M1x2 and M2x2 The rules
of simple algebra allow their sum to be expressed as
This Implies two steps in the addition ; first make the two exponents the
same by mUltiplying one of the mantlssae by 2 to the power of the
exponent difference (called aligning the manttssae) • and second add
the two mantlssae.
A numerical example will make this clear. Take the two numbers as
and -1/2 . Their binary floating point forms will be
Arithmetic Subroutines 87
1
M1= .l.E1=1 <'lx2) 0
and M2 = -. 1. E2 = 0 C- . 1x2 ) .
A second point which Is less evident from the given example is that.
In general. aligning the mantlssae results in one mantissa becoming
either substandard or superstandard before the addition takes place. It
Is Important to choose the mantissa to be scaled. so that the
superstandard case does not arise . Then If any digits are lost In the
addition they will be at the least significant end. When this rule Is
observed the result of addition can never be more than one place
superstandard .
2J
F.P. ADD
18
ANS = A
19
<EXIT)
11
PIICK ANS
12
<EXIT)
sac 4(SP)
SUB (SP), 4( SP ) CMA-eMA-CMB
BPI. LS BRANCH IF CMA>CMB
NEG 6( SP ) THIS PATH IS NOT SHOWN ON F. C.
ADC 4(SP)
NEG 4( SP ) ; NEG CMA
ADD #100000, ( RO ); CHANGE SIGN OF ANS
LS: BIT #000200,4(SP)
BNE L4 ; BRANCH t."MA NOT SUBSTANDARD
ASI. 6(SP)
ROI. 4( SP ) ; LEI.'T SHIFT CMA
SUB #000200,10.(SP) ; DEC E ANS
BGT LS ; BRANCH UNLESS UNDERFLOW
CLR (RO)
CI.R 2( RO) I FORCE F.P. ZERO
BR L6
SMALUB: ADD #4,SP ; CORRECT STACK
K)V (Rl),(RO)
K)V 2(Rl),2(RO)
RTS PC ; EXIT WITH ANS - A
FAULT : ?
END
14
F.P. ~HPLY
, . . . - - - - - - - - l 4 - - - - ,1
MULHPLY ella, ell> TO lIlTA1N
A 48-Bn PROV1SlONSALK MANHSSA<TIO
ROR (Rl)
ROR 2(Rl) ; ROTATE ~IPLIER
ace Ll
ADD 2(R2), 6(SP) ; ADD ~IPLlCAND
ADC 4(SP)
ADC 2(SP)
ADD (R2), 4(SP)
ADC 2(SP)
ADD (SP), 2(SP)
Ll : ASL 2( R2) ; MULTIPLlCAND*2
ROL (R2)
ROL (SP)
TST 2(Rl) REPEAT UNTIL
BNE MIDOP ~IPLIER - 0
TST (Rl)
BNE MIDOP
TST 2(SP)
BMI L2 BRANCH 'l'M STANDARD
ASL 6(SP)
ROL 4(SP)
ROL 2(SP) LEFT SHIFT 'l'M
Sl1B .()()OLOO, 12(SP)
L2: BIT '140000, 12( SP)
BNE OFWW BRANCH TE Otrr OF RANGE
K:>V 10( SP ) , (RO)
K:>VB 3( SP ), ( RO)
K:>VB 2( SP), 3(RO)
K:>VB 5(SP), 2(RO)
BIC '000200, (RO)
ASL 12(SP)
BIS 12(SP), ( RO)
ADD .14, SP
Rl'S PC
OFWW: ADD 110, SP
8MI FPZERO
; FAULT
FPZERO: CLR (RO)
CLR 2( RO)
ADD '4, SP
Rl'S PC
END
Floating point operations often result .In an answer containing more bits
In Its mantissa than exist In the mantlssae of the numbers being added .
If only the appropriate number of mantissa bits are retained throughout.
24 In our case. the answers are said to be rounded by truncation.
This Is the effect In the subroutines given above. However. a minor
adjustment could retain the extra bits. as the operation proceeds.
thereby making other rounding strategies possible . In fact. In the case
of multiplication a 48-blt mantissa Is already generated. The extra bits
In the case of addition would be those lost by the alignment shift on
the mantissa . Again there could be up to a total of 48 bits.
. 12 • . 13 • . 14 . . 15
TRUNCATION . 5-2-( n+ 1)
2-(n+1)
FORCE 1
CONDITiONAL FORCE 0
Consider first the effect of using binary arithmetic with the BCD
numbers 1879 and 912. Straight binary addition gives the following
A result of 1 In the ' exc lusive or' Indic a te s that carry has been
received . Thus, by means ot two XOR functions a bit pattern can be
created that contains a one In the least significant bit ot a BCD digit If
the next least significant BCD digit gave carry . The 'not' ot this pattern
with the surrounding bits removed by a BIC instruction can be used to
create the required 6's by means of right shifts by 2 and 3 . The
complete instruction sequence is given below .
Ll: Rl'S PC
END
The rlghthand column above gives the values that would result If the
subroutine was called with AO = BCD 1879 (014171> and A 1 = BCD 912
(004422). The exit value of AO Is 023621 octal which Is
and carry will equal O. The significance of achieving the correct setting
of C Is that the above subroutine can be used In programming
multi-length BCD addition .
MOV 2( R2) • RO
MOV 2( R3) • Rl
JSR PC. BCDADD
MOV RO . 2( A2)
MOV (R2). RO
ADC RO
MOV (A3). Rl
JSR PC. BCDADD
MOV RO. (R2)
In practice BCD decimal numbers are usually signed and one full
Arithmetic Subroutines 99
BCD digit. say the most significant. Is used to represent the sign. The
coding of addition In this case Is left as an exercise for the reader.
6 . 5. 3 BCD Subtraction
BNE Ll
SEC
Ll : ATS PC
END
A flowchart for this conversion Is given in Figure 6.6. The coding given
below is derived directly from the flowchart . It assumes that the BCD
number is given In Al and a result Is to be returned In AO. Also It
assumes that no other registers may be changed; therefore. It uses the
stack for local working space.
BCDBIN : BEGIN
CLA AO
MOV '4. -(SP)
Ll : CLA -(SP)
ASL Al
AOL (SP)
ASL Al
AOL (SP)
ASL Al
AOL (SP)
ASL Al
AOL (SP)
ASL AO
MOV AO. -(SP)
ASL AO
ASL AO
ADD (SP>+ . AO
ADD (SP>+ . AO
DEC (SP)
BNE Ll
TST (SP)+
ATS PC
END
Arithmetic Subroutines 101
1
BClBIN
I ---,:=i::::::==::J 4 - - - -----,
SHIFT TI£ 1m OPERAND 4 PLACES LEFT
PLACIP«; TI£ IIlST SIGIHF ICANT
lECIMAL DIGIT 011 TI£ STACK
DECIN : BEGIN
MOV R2. -(SP)
CLR R2
Ll : JSR PC. INCH
CMP RO, #' ; COMPARE WITH SPACE
BLE L1 IGNORE SPACE AND ALL CONTROL CHS
CMP RO . #'+
BEQ L5
CMP RO, #'-
BNE L2
DEC R2
L5 : JSR PC INCH
L2 : CMP RO. #'0
BLT FAULT
CMP RO , #'9
BGT FAULT
ASL Rl
ADD Rl. RO
ASL Rl
ASL Al
ADD AO. Al
L4: JSR PC INCH
CMP AO, #'0
BLT L3
CMP AO, #'9
BGT L3
L3: TST A2
BEQ L7
NEG Al
L7: MOV (-SP)+, R2
RTS PC
FAULT: HALT; SUITABLE ACTION TO BE DECIDED
END
Arithmetic Subroutines 103
1
INI
,-------.--.J 1S -
STACK R2 AND SET R2 = 0
- ----,
14
,--- ------'7
!EC[ AL
READ CHARACTER
[S IT A !ECI~AL D[GIT
T [J;C[MAL
Y 9
, . - - + - - { IS R2 = 0
1
OlIn
6.7 EXERCISES
+100
- .5
-.1
+1/9
o represents +
8 represents -
a line buffer
a source buffer
a name list
a character table
a function table
Implementation of the Assembler 109
The line buffer must allow for the maximum number of Ite m s likely to
occur on one line, say 32. Th is is a fa irly generous allocation because
It will become clear that such a line buffer Is only half full when
dealing with In str uc ti o n s of the complexity of
TX: . WORD "TH, "IS," 1."5 , "A , "ME , "55, "AG,"E ,"FO,"R .rou . "TP , oUT
1) a name
2) a constant
3) one of the symbols + , <, ", / , I. =, 8, It. &, " . , NL
4) an opening bracket used In a (R) context
5) an opening bracket used in a (R)" context
6) an opening bracket used In a -( R) context
The reason for distinguishing the first three should be obvious. When
the subroutine which deals with operands is considered, the
convenience of distinguishing the three types of open ing bracket and
encoding the complete bracketed structure as a single item will also be
clear.
As each line is read, spaces and erases are Ignored and ":' and
. WORD are encoded as ' na m e s' . Also, " : ': the comment symbol, Is
translated to NL and the rest of the line is ig no r e d . Any other spurious
characters cause the line to be faulted . The detailed encoding of the
six items Is as follows
fixed size . It bocomes much easier to examine the context of any given
Item. for example . to code tho test "Is the next Item a name and the
one following it a : " .
SA L1 0000001051102100000010000001
0000001030514100000010000001
0000121000000100000010000001
ADD 1400. L 1
0000001042101100010410000001
000043100UO()()1000000IOOOOOOI
0000011000400100000010000001
0000541000000100000010000001
0000001030514100000010000001
0000121000000100000010000001
L3 .- 49
0000001031514100000010000001
Implementation 01 the Assembler 111
0000751000000100000010000001
0000011000061100000010000001
0000121000000100000010000001
The name list structure reflects the block structure of the user
program. Its first two words (labelled NLiST and LEVEL> point to tho
next free position . and the first name of the current block .
respectively. A new level Is started for each BEGIN and an entry for a
dummy name is made at this new level whose value contains the start
of the previous level . Names are usually only looked up at the current
revet . except for those occurring In the label setting directive. which
are looked up 'globally' through all levels . An entry for a name may
either define Its value or conta in a pointer to a chain of references .
depending on whether it has been defined or merely referenced .
A level is removed for each END at which time defined names are
' for g ott en' and outstanding forward references are moved back to the
previous level (or they are filled In if the label Is defined at the
previous level) . If there are no unfilled references to add to the
previous level. the removal of a level simply requires NLiST to be reset
to LEVEL and LEVEL to be reset to the value contained In the dummy
name entry that It addresses . This simple mechan ism allows blocks to
be nested to any depth .
There Is some difficulty In deciding how large the name list should
be . It does not need to contain all the names used In a program
because the names Internal to each block are deleted . and the space
Is recovered. at the end of each block. As a minimum It must be
large enough to hold all the names of the largest block . The nesting
of blocks Increases this requirement ; therefore In the Implementation
described here a namellst of 320 words will be used wh ich provides for
up to 64 names.
MOV #L1. R3
01 27 03
00 24 00
However. If L1 has not been defined the assembler would not be able
to complete the second of these two words . In this case an entry would
Implementation of the Assembler 113
L1 10
NEXT -
INSTRUCTION
Figure 7.2a
01 67 04
RELATIVE VALUE OF L2
L1 0
L2 0
012703
o
016704
NEXT ..
INSTRUCTION
Figure 7.2b
t t4 An Introduction to System Programming - Based on the PDPt t
If the first reference to an undefined label Is In a branch
Instruction . then the space left In the program to accommodate the
value . when It Is known. Is the a-bit offset field of the branch
Instruction pointed to by the fourth word of the name list entry. For
example. if the next instruction is
BNE L3
Ll ,-To
L2 10j. -
L3 110
Figure 7.2c
ADD Ill. R2
the assembler would generate the linked structure of Figure 7 .2d. That
is the name list entry points to the most recent entry wh ich points to
the next most recent entry and so on. Th is structure Is called a linked
list or chain . Each pointer in the chain is called a link and when a
zero link is encountered It signifies the end of the chain . The same
chaining rules can apply to words which indicate word relative
references .
L1 1'10
L2 101" f-
L3 I 10 012703
o
016704
Figure 7. 2d
Ll = 2400
116 An Introduction to System Programming - Based on the PDP11
= 2000
BEGIN
l2 = Ll + 400
MOV 110., Rl
L3 : MOV LHRl>. l2(Rl>
CLR LHRl>
DEC Rl
DEC R1
BNE L3
CMP R2. tL5
BMI OK
CMP R2, tL4
BPL OK
NOTOK: MOV L1. R3
There Is an entry In this Table for each function mnemonic and for the
directives ·BEGIN' . 'END' . 'ENTER'. Each entry contains three fields
thus
The field sizes are 5 x 8 bits for the function name, 8 bits for the
type and 16 bits for the value. A typical entry would be
The lists used by the assembler and their sizes are as follows. and
the abbreviated names are the way they will SUbsequently be
referenced .
The other Important part of establish ing the overall design of a program
Is to decide how It Is to be subdivided Into subroutines. This will not
be clear at the start. and the design will usually have progressed some
way before Is does become clear. With hindsight Figure 7.4 in d ic ates
the breakdown of the SAL assembler into subroutines and shows where
they are called. Their specifications are given In Section 7 .3.2 .
BEGIN
~
ETC.
7. 3. 1 Global variables
These are variables whose values are used and possibly altered by
more than one subroutine. Hence. they are declared at the start of the
main block of the assembler. Inside which the other subroutines are
nested. Mostly they are contained In store lines. but some that are
frequently used are In registers . They are referred to on flowcharts and
In the code by the names which are given below. together with a
summary of their functions
120 An Introduction to System Programming - Based on the PDP11
7. 3 . 2 Subroutine specifications
There are a few other subroutines which are mainly concerned with
monitoring errors .
8 Control of Input and Output
ADDRESS
DEVICE CONTROLLERS
reader Is started and It becomes 1 when the reader has stopped, and
a character has been copied to the buffer . Given that the control/status
and buffer lines for the paper tape reader have addresses 177550 and
177552 octal respectively. the following annotated SAL subroutine to
read the next character to RO should now be Intelligible.
READCH : BEGIN ;
MOV 11. "177550 ;START PAPER TAPE READER
WAIT : BIT "177550, '200
BEQ WAIT ; WAIT UNTIL DONE BIT SET
MOV "177552. RO ;COPY PTR BUFFER TO RO
BIC #200, RO ; REMOVE PARITY BIT
RTS PC
END
Another very simple device from a control point of view Is the console
teletype. It Is convenient to use the term teletype to categorise this
device although It may In fact be a VDU. There Is one slight
complication In that a teletype Is both an Input device and an output
device . However. this resolves Itself quite simply since It Is allocated
two pairs of control/status and buffer lines . and the Input and output
mechanisms are Independent.
This was In fact done In the read character subroutine forming part of
the octal loader In Section 4 .3. Thus the subroutine given there was
equivalent to
BEGIN
INC 11177560 ; SET TELETYPE INPUT START BIT
WAIT TSTB 11177560
BPL WAIT ; WAIT UNTIL DONE BIT SET
MOV 11177562; RO ; READ CHARACTER TO RO
BIC 1200 . RO ; CLEAR PARITY BIT
RTS R7
END
On some teletypes the start bit has no significance and the first
Instruction above Is redundant . Characters are Input whenever the user
strikes a key. or at a fixed rate If a tape reader attachment Is
operative. On other kinds of teletype a character can only be Input
after a start signal has been given.
OUTCH : BEGIN
WAIT : TSTB 8t177564
BPL WAIT
MOV RO.8.177566
RTS R7
END
Here again there Is a wait at the start. In case the output of the
previous character has not been completed . Once the device Is ready a
new character can be output by copying It to the device buffer. There
Is no need to set a start bit since writing to the buffer Implies start.
8. 3 BOOTSTRAP SEQUENCES
BININ BEGIN
L1 K:>V #X,RO 012700
X
INC @#177560 005237
177560
WAIT TSTB .#177560 105737
177560
BPL WAIT 100375
MOVB .#177562,(RO)+ 113720
177562
X BR Ll 000766
END
beginning and repeat the same operation . When the byte 372 Is read
the effect Is to change the BA L1 Instruction to a BA WAIT Instruction;
hence AO Is not now reset . The next character is therefore read to the
most significant byte of the Instruction which Is now BA WAIT; hence It
must be 001 If the program Is not to be altered further . SUbsequent
characters on the tape are now loaded Into store Immediately following
the BININ sequence until there Is no more tape in the reader. In effect
the 366 characters are nothing more than a ' le ader ' on which the tape
Is positioned. and there couid be an arbitrary number of them .
TELETYPE COMPUTER X
INSTATUS ~77560 INSTATUS ~76500
INBUFl"ER ~77562 INBUFFER ~76502
OUTSTATUS ~77564 OUTSTATUS ~76504
OUTBUFFER ~77566 OUTBUFl"ER ~76506
Although It may not be obvious from what has already been said.
the above sequence relies on the speed of the teletype being at least
equal to the speed of the connection Into computer X. Devices such as
teletypes are often a substantial distance from the computer and the
device controllers do not expect a . response after each character has
been printed. Instead. they themselves set the done bit at the rate at
which the device Is designed to operate. Therefore. It Is necessary for
the teletype to match the speed of the device controller, even when
TRANSMIT Is Interposed.
8.4 BUFFERING
READ READ
STATEMENT A STATEMENT B
READER BUSY
READ
STATEMENT A
READ
STATEMENT B
I
READER BUSY
8. 5 INTERRUPTS
the control unit earlier. The control unit was presented as a mechanism
that continuously executes the sequence
Stack PS
Stack PC
Reset PS
Reset PC .
134 An Introduction to System Programming - Based on the PDP11
Thus the next Instruction obeyed Is the first one of the Interrupt
routine.
The function of the other bits In the processor status word Is not
the same on all PDP 11s. In general they are concerned wIth the
mechanIsms that operating system software would use to prevent user
programs from InterferIng wIth the Input/output devices . or areas of
store allocated to other programs . It Is only necessary here to consider
the use of three other bits called the processor priorIty bits. These are
positIoned thus
_ _PROCESSOR PRIORITY
I I I INIZIVICI
7 6 543 2 1 0
and to make an entry for RINT In the Interrupt vector. There will also
be a need to allocate space for some 'polnters' Into the buffer which
will be used to remember positions In the buffer. The pointer NXICH will
be used for the position of the next character to be read; and the next
position to be used when a reader Interrupt occurs will be given by
NXIPOS. This organisation could apply to any Input device which has
the characteristic of delivering characters one at a time. It would not
apply to card readers which can only be stopped at the end of a card .
OR
C "'-1- - " T T T T 7 T T T T T T T T T T - r r r T 1 7 7 7 7 r - - - - . - -
NXPOS
buffer full
buffer empty.
136 An Introduction to System Programming - Based on the PDP' 1
All Increments to the pointers must take account of the cyclic nature of
the buffer . In order to keep this In mind the symbol + will be used .
This notation represents an operation which Is Identical to + unless the
result Is beyond the end of buffer, In which case It Is forced to the
corresponding position at the start of the buffer.
1
RINT
SlBLFFER FUW
11
INCH
RINT : BEGIN
PTRS = 177550
PTRB = 177552
TST (UPTRS
BMI FAULT
MOV RO. -( R6)
MOV NXPOS. RO
MOVB ...PTRB . (RO)
BICB 1200. (RO) +
BIC .200. RO
MOV RO . NXPOS
INC RO
BIC .200. RO
CMP RO. NXCH
BEQ FULL
INC PTRS
CLR RSWITCH
Control 01 Input and Output 139
ll : MOVCA6)+. AO
AT'
FUll: INC ASWITCH
BA Ll
FAULT : TST 8'PTAS
BMI FAULT
AT!
END
INCH : BEGIN
ll: CMP NXCH . NXPOS
BEQ II
MOVB 8NXCH . AO
INC NXCH
BIC '200. NXCH
TST ASWITCH
BEQ L2
INC IIPTAS
ClA ASWlTCH
L2 : ATS PC
END
I
DUTCH
lYE?,r:-::--::-b t2 - - - ___
~~~ FULL <r.E. NXOCH = NXOPOS~
I
PINT
LPTS =
LPTB =
OUTCH : BEGIN
Ll: MOV NXOPOS. - CSP)
INC CSP)
BIC '200. CSP)
CMP NXOCH. CSP) +
BEQ L1
?
TST PSWITCH
BNE L2
MOV AO. IINXOPOS
INC NXOPOS
BIC '200 . NXOPOS
L3: ?
ATS PC
L2 : MOV AO. I
CLA PSWITCH
BA L3
END
PINT : BEGIN
CMP NXOPOS . NXOCH
BEQ L1
MOV IINXOCH. IOPTB
INC NXOCH
BIC '200. NXOCH
ATS PC
L1: INC PSWITCH
ATS PC
END
Even when the discussion Is limited to floppy discs there are many
types from which to choose . We will consider the FDl771 as used on
the PDP 11/03 systems.
The transfers take place In two stages . Inside the disc control
hardware . which 15 In fact Itself a microcomputer. there 15 a one
sector buffer. Commands exist to copy complete sectors between the
disc and the buffer. Other commands allow the buffer to be read from
or written to In 16-blt word units. with the consecutive words all
passing through AXDB. Thus the following actions are required to read
a sector from disc to main memory
AXCS = 177170
AXDB = 177172
AXSA = 177174
DREAD: BEGIN; Rl = DISC ADDRESS. RO = STORE ADDRESS
MOV R1 . AXSA ; SET DISC ADDRESS
MOV '7. AXCS ; START READ TRANSFER
WAIT: TSTB AXCS
BMI WAIT ; WAIT FOR COMPLETION
MOV '100. -CSP) ; SET COUNT
MOV '3 . AXCS ISSUE 'EMPTY BUFFER'
COPY: MOV AXDB. CRO)+
DEC CSP)
BNE COpy
TST CSP)+
RTS PC
END
DWRITE : BEGIN
MOV '100. -CSP) ; SET COUNT
MOV '1. AXCS ISSUE ' FILL BUFFER'
COPY: MOV CRO)+. AXDB
DEC CSP)
BNE COpy
MOV ai . AXSA SET DISC ADDRESS
MOV '5. AXCS START WRITE TRANSFER
WAIT : TSTB AXCS
BMI WAIT
TST CSP)+
RTS PC
END
8. 9 EXERCISES
Write suitable subroutines for the INCH . OUTCH and IENQ assumed
In the program given In Section 5. 11 . The INCH subroutine Is
expected to return the next character of Input In RO and OUTCH Is
expected to print the character In RO . IENQ Is a variant of INCH
which should return the next character In RO If one has been
typed , otherwise It should return zero.
END
ENTEA MUOINT
Appendix 3 Flowcharts for the SAL Assembler
DOT: . WORD 0 ; GLOBAL DATA DECLARATIONS
FNAME: . WORD 0
IADDR: . WORD 0
XMODE : . WORD 0
LBUFF = XMODE +2
SBUFF = LBUFF + 256.
NLIST = SBUFF + 80
LEVEL = NLIST + 2
. = NLIST + 640 .
25
SAL - MAIN FI..IJWI:IlART
SETOOTOOSP
INITIALISE x.rsr
r------'g
laD LItE ,00 ITElIISE
CSAL.ll
SET IPlll ,00 IAlDl
Zl
YES 12 23
...._---4-- -=< IS ll£ 2Nl ITElI I 1 ADVAt«:E IPlll 100
ASSEIIlI£ A VAUE
FOR ll£ EXPR
rsAL.2J
6
FAIl..T B
1
SAL. 1 - READ L1t£ AND ITEMISE
r "
READ AND STORE2 A COl4PLElE
L1t£ INlU 'SBLfF' I
t
IINrTlALlSE 11£ miISED L1t£ 8l.fFER 1
' LBLfF'
SET lP POIWTER TO SCAN 11£ L1t£
t
SWITCH ACCOROIN~ TO 11£
VALlE CF 11£ NEXT CHARACTER
I
AND ACT AS F[ll()VS
t 5
CHARAClER ACTIONS
SPACE. ND ACTION
lELINITER. STORE 11£ lELINllER
lECOIGIT. READ & STORE CONSTANT ITEM
LETTER COR'. '), READ & STORE NAIE ITEM
WlTE (' ), READ & STORE CHAR CONST
ClIl1ES ("), READ & STORE IlOLllLE CHAR CONST
OPEN BRACKET. IF PREV ITEM ~ "- " TI£N
ALlER IT TO A -3 ITEM
ELSE STORE A -I ITEM
CLOSE BRACKET. O£CK THAT 11£ PREVIOUS 2 IlEICS
ARE ' (' " REGISlER NAIE.
JE.E1E 11£ PREVIOUS ITEM AND
STORE REGISlER NO IN 11£ ' (' ITEM
FINALLY IF teeT CH = ' +' l:HAN(C
11£ BR!IO<ET ITEM TO A -2 ITEM
f-ES
7
END
READL : BEGIN
LBUFF = LBUFF
Ll : MOV 'SBUFF. R3 ; BOX 2
B2A: JSR PC. ItIiNCH
MOVB RO. CR3) +
CMP RO . '12
BNE B2A
MOV 'LBUFF+256. . R2 ; BOX 3
B3A: CLR -CR2)
CMP R2. 'LBUFF
BNE B3A
MOV 'SBUFF. R3
L2: IG: MOVB CR3) +" RO ; BOX 4
MOVB CHTAB CRO). RO
ASL RO
JMP ItSWITCH CRO>
SWITCH: . WORD 00. DO. ALPHA. NL . STOP. LB . RB
. WORD ILLCH. DQUOTE. SQUOTE. DELIM. XOPR. IG
156 An Introduction to System Programming - Based on the PDP11
1
SAl.2 - ASSEIIllE EXPf5SIllN
N~--....J,- --"",\
.------+-----{
N 9------.,
r - - - - f - - - - " - ( IS Tl£ !£Xl ITEM AN Il'ERATlJR ?
N ,-----'
.---+--<
13 Zl
(SW = Q) <SW • l>
28
FALlT 5
ASSX : BEGIN
CLR -(SP) ; BOX 2 STACK SW
Appendix 3 The Flowcharts and Code for SAL 159
MOV It'+ . A3 ; A3 Is FN
ClA (A1) ; (Al> Is EXPA
CMP (A2). A3 ;BOX 3
BEQ II
CMP (A2) . It'-
BNE l2
ll : MOV (A2) . A3 ; BOX 4
ADD 1t8. A2
L2: TST (A2) ; BOX 5
BEQ L10
L3 : CMP (A2) . 1t1 ; BOX 6
BNE L4
MOV 2(A2). AS ; BOX 7 AS Is OPD
ADD 1t8. A2
L5 : JSA PC. 8ltEVALFN ; BOX 8
INC (SP) ; BOX 19
MOV (A2) . A3 ; BOX 9
BMI L8
CMPB CHTAB (A3) • #11 .
BNE L8
ADD #8. A2 ; BOX 10
TST (A2) ; BOX 11
BNE L3
LlO : JSA PC. 8#FINDN ; BOX 22
TST AO ; BOX 23
BlE L6
MOV (A4). AS ; BOX 24
ADD 1t8. A2
BA L5
L6 : TST (SP) ; BOX 26
BNE l7
CMP 8#lEVEL. 8ltFNAME ; BOX 14
BNE L7
MOV UXMODE. A3 ; BOX 15
ASL A3
JSA PC . 8ltADDAEF
ADD 1t8. A2
ADD 1t2. A1
BA L9
L8 : 1ST 8#XMODE ; BOX 12
BEQ B12A
SUB ai . ( AD
SUB 1t2. ( AD
B12A : ADD 1t2. Al
L9 : TST (SP)+ ; BOX 18
ATS PC
L4 : MOV #3 . AO ; BOX 17
JSA PC . 8#FPAINT
L7 : MOV #5. AO ; BOX 29
JSA PC. 8#FPAINT
END
160 An Introduction to System Programming - Based on the PDP11
7
SAL.. 3 - C04!'rLE OPERAND
N 9-----
r5 Tl£ taT [TEI4 AN "G" 7
N 12 - - - - - - - ,
. . . - -- - - +- - ---<r5 Tl£ taT [TEll -VE r. E. A " (" 7
,.- --1[ --,.
(.
rF Tl£ taT [TEI4 r s A REGrS!ER REG = REGr5lEll NlMEl ADVANCE rPTR
SET REG = rrsNlJIER
rON
, . -- -- - - 1 . ______.,
AOO 6 TO MOlE AND
UD< MEAD AT Tl£ OPERAND TYPE
I4llE
COMPO : BEG IN
CLR -CSP) ; BOX 8
CMP CR2), 11:'8 ;BOX 9
BNE Ll
MOV 11:10 . CSP) ; BOX 10
ADD 11:8 , R2
Ll : CMP CR2), 11:'11: ; BOX 11
BEQ L2
TST CR2) ; BOX 12
BPL L3
ADD 2C R2) , CSP) ; B OX 4
ADD 11:8 . R2
CMP - 8 CR2 ). 11:-2 ; BOX 15
BEQ L4
ADD 11:20 . CSP) ; BOX 16
CMP -8C R2) • 11:-3 ; BOX 17
Appendix 3 The Flowcharts and Code lor SAL 161
BNE L5
ADD #20 . CSP)
L5 : MOV CSP)+. R4 ; BOX 19
RTS PC
L2 : ADD #8 . R2 ; BOX 23
JSR PC. 8#ASSX
ADD #27. CSP) ; B OX 25
BR L5
L3 : JSR PC. 8#REGNO ; BOX 1
TST RO
BMI L9
ADD RO. CSP)
BR L6
L9 : ADD #60. CSP) ;BOX 4
MOV R2. R4 ;R4 = START OF EXPR
CMP CR2) . #2
BGE B4A
ADD #8. R4 ; IGNORE INITIAL OPD
B4A : MOV CR4). RO
BMI L7 ; BRACKET FOUND
CMPB CHTAB CRO) • #1l.
BNE B4B ; END OF EXPR
ADD #16 . • R4 ; MOVE TO NEXT OPR
BR B4A
B4B:
INC 8#XMODE ; BOX 32
JSR PC. 8#ASSX
CLR 8#XMODE
BIS #7. CSP)
BR L5
L7 : JSR PC. 8#ASSX ;BOX 5
BIS 2CR2). CSP)
L6: ADD #8. R2 ; BOX 2
BR L5
L4 : BIT CSP) . #70 ; BOX 27
BEQ L8
BIS #70 . CSP) ; BOX 28
CLR CRl>+
BR L5
L8: ADD #10. CSP) ; BOX 22
BR L5
END
162 An Introduction to System Programming - Based on the PDP11
I
SAL. 4 - COMPILE INSTRl£TION
oor IN
,----J16-
CALL SlIlROl/Tlt-l; FOR
"I£GIN" [SAL. 9J
"00" [SAL. IOJ
"ENTER" [SAL. llJ
17
CEXIT)
Appendix 3 The Flowcharts and Code for SAL 163
1
SAL. 5 - FIND HAlE
GETS N1NIEXINSTAllJS)
12
EXrT
Appendix 3 The Flowcharts and Code for SAL 166
1
SAL. 6 - SET HAlE
<USES NIIlEX/NSTATUS AS SET BY FIt{JN)
11
EXIT
I
SAL.7 - ADO ~
<USES NINlEXINSTATUS AS SET BY FINDNl
1r----...I14 - - - ,
ADO THIS REFERENCE TO 11£
ABSOLUTE REF LIST
';=:±:=:=J29 - - . , 1
ADO THIS REFE~NCE TO 11£
~mVE REF LIST
1..-----l12 - - - ,
SET 11£ HAlE VALlE TO OOT
SlIl11lACT I Fill lFFSET REF 15
<EXIn
'------,t:==:::lg ----,
STORE DlSPl.ACElENT
IN lFFSET lFaxm
Mil POINTER TO unn
IN 11£ REFERENCE CHAIN
10
EXIT
Appendix 3 The Fl owc har ts and Code for SAL 169
1
SAl.. 8 - COII'ILE BRAtol
COMPBR : BEGIN
TST (R2) ; BOX 2
BNE L1
JSR PC. 8#FINDN ; BOX 3
TST RO : BOX 4
BGT L2
MOV 11=1. R3 ; BOX 5
JSR PC. I#ADDREF
L3 : ADD 11=8. R2 ; BOX 11
RTS PC
L1 : MOV 11=12 .• RO ; BOX 7
JSR PC. 8ltFPRINT
L2: MOV (R4). R4 : BOX 8
SUB R1. R4
ASR R4
MOVB R4. -2( RD
ADD 11=200. R4 ;BOX 9
BIT 1t177400. R4
BEQ L3
MOV 1t4. RO ; BOX 10
JSR PC. IItFPRINT
END
Appendix 3 The Flowcharts and Code for SAL 171
I
SAl... 9 - PROCESS !'£GIN
BEGIN : BEGIN
JSR PC. 8ltPRTAD ; BOX 2
MOV 8ItNLlST . R4 ; BOX 3
CLR (R4)+
CLR (R4)+
MOV It 100000. (R4) +
MOV 8ItLEVEL. (R4) +
CLR (R4)+
MOV 8ItNLlST. 8ltLEVEL ; BOX 4
MOV 8ItLEVEL . 8ltFNAME
MOV R4. tUNLIST
RTS PC
END
1 72 An Introduction to System Programming - Based on the PDP"
1
SAl.. 10 - PRll:ESS 00
15
EXIT
j-rt:==:::J18
110O HAlE AND I5'EREI«:E
TO f'fa. LEVEL
Appendix 3 The Flowcharts and Code fo r SAL 173
END : BEGIN
NUST = NUST
MOV A2 . -(SP)
JSA PC . 8,PATAD ; BOX 14
CMP 8'LEVEL. 'NUST + 4 ; BOX 2
BEQ Ll
MOV 8'LEVEL. A2 ; BOX 3 A2 Is PTA
MOV 8'NUST . - ( SP)
MOV A2. 8,NUST
MOV 6(A2) . 8'LEVEL
MOV 8'LEVEL. 8'FNAME
L3 : ADD '10 . • A2 ; BOX S
CMP A2 . (SP)
BEQ L2
TST 4( A2) ; BOX 6
BMI L3
MOV A2. AS ; BOX 7
BA B20B
B20A:
MOV 6(A2). (AS) ; BOX 9
BA L3
LS: MOV IUNLIST . AS ; BOX 18
MOV (A2) +. (AS) +
MOV (A2)+. (AS) +
MOV (A2) +. (AS) +
MOV (A2)+ . (AS) +
MOV (A2) +. ( AS) +
MOV AS. 8#NLlST
SUB #10 . • A2
BA L3
L6 : MOV (AS). A4 ; BOX 19
DEC A4
MOV 6(A2) . ( AS)
MOV A2 . AS
ADD #6 . AS
B19B : TST (AS)
BEQ B19A
BIT (AS) . Itl
BNE B19A
MOV (AS). AS
BA B19B
B19A :
BNE L7 ; BOX 21
MOV A4 . (AS) ; BOX 22
BA L3
L7 : MOV (AS) . AS
DEC AS ; BOX 23
B23B : TSTB (AS)
BEQ B23A
MOVB (AS) . -(SP)
ASL (SP)
ADD (SP)+. AS
BA B23B
B23A :
SUB AS . A4 ; BOX 24
ASA A4
MOVB A4. (AS)
ADD #200 . A4
BIT 1t177400 . A4
BEQ L3
MOV #4. AO
JSA PC . 8ltFPAINT
Ll : MOV #7. AO ; BOX 13
JSA PC . 8#FPAINT
L2: TST (SP) + ; BOX lS
MOV (SP)+ . A2
RTS PC
END
Appendix 3 The Flowcharts and Code for SAL t 75
1
SAL. 11 - PAOCESS ENTER
ENTEA: BEGIN
NLIST = NLIST
MOV tNLlST . 8tFNAME; BOX 2
CLA 8#XMOD E
JSA PC • .#ASSX
JSA PC . • #END ; BOX 3
CMP tNLlS T + 4 . 8#NLlST ; BOX 4
BNE Ll
CMP #NLlST+4 . .#LEVEL
BNE Ll
MOV - ( Al> • PC ; BOX 5
L1 : MOV #7 . AO ; BOX 6
JSA PC. 8#FPAINT
END
176 An Introduction to System Programming - Based on the PDP11
l
MISCELLAtBlUS
ROOTlt£S
.-------2----~
FAULT
PRINTS 11£ FAULT NlER AND 11£ Lltt CF
11£ PROOlAM WHICH CONTAINS 11£ FAULT
,.----4----,
PRThD
PRINTS ClJlHENT Lltt AND
VALLE CF lXlT IN OCTAL
,----5----,
!aXl
CI£CKS lllI\T 11£ ClJll<ENT ITEM
IS A REGISTER HAle
AND GIVES ITS NUII£R IN RO
,-----6-----,
EVALFN
USED BY ASSX TO EVALUATE EXPRESSIONS
REG a = FN (+. -. &. V. *. /)
REG 5 = OPD
(RL> = EXPRESSION
FPRINT: BEGIN
MOV RO . -(SP)
MOV #'F. RO
JSR PC. 8#OUTCH
MOV (SP) +. RO
ADD #'0. RO
JSR PC. 8#OUTCH
JSR PC . 8#PRTLN
JMP 8#RESET
END
PRTAD: BEGIN
MOV Rl. -(SP)
MOV 8#IADDR. Rl
JSR PC. 8#OUTOCT
JSR PC. 8#PRTLN
MOV (SP) +. Rl
RTS PC
END
Appendix :3 The Flowcharts and Code for SAL 177
PATLN : BEGIN
MOV A1 , - CSP)
MOV 'SBUFF. A1
MOV t ' , AO
JSA PC , 8tOUTCH
Ll : MOVB CAl>+, AO
JSA PC, 8'OUTCH
CMPB - H A l> , '12
BNE Ll
MOV CSP) +, A1
ATS PC
END
AEGNO : BEGIN
MOV '-1 , AO
MOV 2CA2) , -CSP)
SWAB CSP)
TST CA2)
BNE NOTAEG
TST 4CA2)
BNE NOTAEG
TS T 6CA2)
BNE NOTREG
CMP CSP) , ,·7A
BGT Ll
CMP CSP) , ' ·OA
BGE AEG
CMP CSP) , '·CP
BNE Ll
MOV '7 , AO
BA NOTAEG
Ll : CMP CSP) , ' ·PS
BNE NOTAEG
MOV '6 . AO
BA NOTAEG
AEG : MOV CSP) . AO
SUB '·OA . AO
NOTAEG : TST CSP)+
ATS PC
END
BNE L3
XOR R3, (RD
RTS PC
L3 : CMP #'& . R3
BNE L4
COM R3
BIC R3, (RD
RTS PC
L4 : ; INSERT HERE A SUITABLE
; MUL T AND DIV SEQUENCE
; TO COMPLETE THIS PROCEDURE
END
OUTOCT : BEGIN
MOV #'0, RO
ASL Rl
ADC RO
JSR PC • • #OUTCH
MOV #5 . - ( SP)
U: ASL Rl
ROL Rl
ROL Rl
ROL Rl
MOV at. RO
ROR Rl
BIC #177770, RO
ADD #'0. RO
JSR PC. 8#OUTCH
DEC (SP)
BNE Ll
TST (SP) +
RTS PC
END
ENTER START
Appendix 4 Code for the SAL Assembler
The following Is the octal code for the assembler given In Appendix 3.
Each line represents B octal words and the address of the first Is given
In the lefthand column.
Absolute 30
Accumulator 11
Address 11 .22
Address register 46
Addressing mode 25.30
- Absolute 30
- Auto-decrement 26 .41
- Auto-Increment 26.41
- Deferred 28
- Index 26
- Immediate 30
- Register 25
Anomalies 41
Arithmetic
- Binary 2
- Binary coded decimal 95
- Double length 76.82
- Floating point 76.85
- Integer 76
Arithmetic operations
- Add 2.82.86.96
- Divide 79.93
- Multiply 3 .14.40 .77.83.90
- Subtract 3.82.90.99
Arithmetic unit 11.23.34
Assembler 45
Assembly language 45.61
Binary coded decimal 76
Binary digit 1
Binary number 1
Bit 1
Block structure 61. 112
Bootstrap 44.50.55 .59.127
Branch Instruction 31.35.37
Buffer 108 .130 .135.139
Byte 40
Carry 32.34
Chain 112
Comment 69
Complement 4
Condition code 35
Console 45 .59
Control unit 10.12.23
Conversion 6 . 100
Cross-compile 57
Cyclic 135
Data 12
Data structure 108
Deferred 28
Destination 24
Device 123
Directive 67
Disc 124.141
Double length 76
Duplex
- Full 126
- Half 126
Exponent 16.85.92
Floating point 76
Floppy disc 141
Format 11.16.24.41
Forward reference 108
Function 11.24
Global variable 119
Hexadecimal 5
Idling 130
Immediate 30
Input 69.101.124.135
Instruction 10.31. 65
Interpreter 71
Interrupt 132.136.140
- Entry 133
- Vector 133
Itemise 109
Jump 40
Keyboard 123
Label 61
Label reference 112
Level 111
Line buffer 109
Lineprinter 123
Linked 115
Loader 44
Mantissa 76.85
Microprogram 48
Memory 9
Mnemonic 31. 33. 35. 36. 61
Mode 25
Multiplication 3.14
Namellst 111
Negative 3
Octal 5
Octal loader 50
Offset 36.38.42
Operands 24.31.66
Output 69.101.124.139
Overflow 34.37
Program 9.71
Program counter 10.29.39
Programming 13
Processor priority 134
Processor status 134
Radix 2.6
Range 5.37
Real time 130
Register 22
Remainder 81
Rotate 33
Rounding 77
Rounding errors 94
Scope 61
Side effect 28
Source 24
Special registers 29
Specification 120
Stack 29.40
Stack pointer 29
Store 9
Standard 85
Standardisation 91
Subroutine 20.38.69.118
Substandard 87
Superstandard 87
Switch 45
Switch register 45
System software 44
Truncation 77
Variable 10
Word length 2