MCS BASIC-52
Versions 1 & 1.1

OPERATING AND
REFERENCE MANUAL

intelIntel Corporation makes no warranty for the use of its products and assumes no responsibility for any
errors which may appear in this document nor does it make a commitment to update the information
contained herein.
Intel retains the right to make changes to these specifications at any time, without notice.
Contact your local sales office to obtain the latest specifications before placing your order.
The following are trademarks of Intel Corporation and may only be used to identify Intel Products:
Above, BITBUS, COMMputer, CREDIT, Data Pipeline, FASTPATH, Genius, i, I,
ICE, iCEL, iCS, iDBP, iDlS, 121CE, iLBX, im~ iMDDX, iMMX, Insite, Intel, intel,
intelBOS, Intelevision, inteligent Identifier, inteligent Programming, Intellec,
Intellink, iOSP, iPDS, iPSC, iRMX, iSBC, iSBX, iSDM, iSXM, KEPROM, LibraryManager, MAP-NET, MCS, Megachassis, MICROMAINFRAME, MULTIBUS,
MULTICHANNEL, MULTIMODULE, MultiSERVER, ONCE, OpenNET, OTP,
PC-BUBBLE, Plug-A-Bubble, PROMPT, Promware, QUEST, QueX,
Quick-Pulse Programming, Ripplemode, RMX/80, RUPI, Seamless, SLD, UPI,
and VLSiCEL, and the combination of ICE, iCS, iRMX, iSBC, iSBX, MCS, or UPI
and a numerical suffix, 4-SITE.
MDS is an ordering code only and is not used as a product name or trademark. MDS~ is a registeredtrademark of Mohawk Data Sciences Corporation.
~MULTIBUS is a patented Intel bus.
Additional copies of this manual or other Intel literature may be obtained from:Intel Corporation
Literature Distribution
Mail Stop SC6-59
3065 Bowers Avenue
Santa Clara, CA 95051
INTEL CORPORATION 1986

intelCHAPTER 1
Introduction
1.1
Introduction to MCS BASIC-52 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3
Getting Started-What Happens After Reset . . . . . . . . . . . . . . . . . . . . . . . . .
1.4
Definition of Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.5
What's the difference between Version 1.0 and Version 1.1 . . . . . . . . . . . . . .

1
2
2
4
9

CHAPTER 2
Description of Commands
2.1
RUN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2
CONT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.3
LIST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.4
LIST# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.5
LlST@ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.6
NEW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.7
NULL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

13
14
15
16
17
18
19

CHAPTER 3
Description of EPROM File Commands
3.1
RAM and ROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2
XFER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.3
PROG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.4
PROG1 and PROG2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.5
FPROG, FPROG1 and FPROG2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.6
PROG3, PROG4, FPROG3, and FPROG4 (Version 1.1 only) . . . . . . . . . . . .
3.7
PROG5, PROG6, FPROG5, and FPROG6 (Version 1.1 only) . . . . . . . . . . . .

21
22
23
24
25
26
27

CHAPTER 4
Description of Statements
4.1
BAUD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.2
CALL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.3
CLEAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.4
CLEARS and CLEARI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.5
CLOCK1 and CLOCK0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.6
DATA-READ-RESTORE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.7
DIM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.8
DO-UNTIL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.9
DO-WHILE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.10 END . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.11 FOR-TO-STEP-NEXT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.12 GOSUB-RETURN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.13 GOTO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.14 ON GOTO-ON GOSUB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.15 IF-THEN-ELSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.16 INPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.17 LET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.18 ONERR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.19 ONEXT1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.20 ONTIME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.21 PRINT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.22 PRINT# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.23 PH0., PH1., PH0. #, PH1. # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.24 PRlNT@, PHO.@, PH1.@ (Version 1.1 Only) . . . . . . . . . . . . . . . . . . . . . . .
4.25 PUSH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

28
29
30
31
32
33
35
36
37
38
39
41
43
44
45
47
49
50
51
52
54
57
58
59
60

intelCHAPTER 4
Description of Statements
4.26 POP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.27 PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.28 REM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.29 RETI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.30 STOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.31 STRING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.32 UI1 AND UI0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.33 UO1 and UO0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.34 IDLE (Version 1.1 only) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.35 RROM (Version 1.1 only) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.36 LD@ and ST@ (Version 1.1 only) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.37 PGM (Version 1.1 only) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

61
62
63
64
65
66
67
68
69
70
71
72

CHAPTER 5
Description of Arithmetic/Logical Operators and Expressions
5.1
Dual Operand (DYADIC) Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2
Unary Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2.1 General Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2.2 Log Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2.3 Trig Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.3
Understanding Precedence of Operators . . . . . . . . . . . . . . . . . . . . . . . . . .
5.4
How Relational Expressions Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

74
76
76
78
78
80
81

CHAPTER 6
Description of String Operators
6.1
What are Strings? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6.2
The ASC Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6.3
The CHR Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

82
83
85

CHAPTER 7
Special Operators
7.1
Special Function Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.2
Examples of Manipulating Special Function Operators . . . . . . . . . . . . . . . . .
7.3
System Control Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

86
94
95

CHAPTER 8
Error Messages, Bells, Whistles, and Anomalies
8.1
Error Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.2
Disabling Control-C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.3
Implementing "Fake DMA" . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.4
Run Trap Option (Version 1.1 only) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.5
Anomalies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

96
100
101
102
103

CHAPTER 9
Assembly Language Linkage
9.1
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.2
General Purpose Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.3
Unary Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.4
Special Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.5
Dual Operand Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.6
Added Link Routines to Version 1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.7Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.8
I/O Resource Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

104
106
113
115
118
122
129
131

intelCHAPTER 10
System Configuration
10.1 Memory/Hardware Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.2 EPROM Programming Configuration/Timing . . . . . . . . . . . . . . . . . . . . . . . .
10.3 Serial Port Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

132
135
136

CHAPTER 11
Reset Options (Version 1.1 only)....................................

145

CHAPTER 12
Command/Statement Extensions (Version 1.1 only) . . . . . . . . . . . . . . . . . . . . . . .

153

CHAPTER 13
Mapping User Code Memory (Version 1.1 only) . . . . . . . . . . . . . . . . . . . . . . . . . .

159

APPENDIX A
1.1
Memory Usage (Version 1.0 and Version 1.1) . . . . . . . . . . . . . . . . . . . . . . .
1.2
Using the PWM Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3
Baud Rates and Crystals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.4
Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.5
Instruction Set Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.6
Floating Point Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.7
Storage Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.8
Format of an MCS BASIC-52 program . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.9
Answers to a Few Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.10 Pin-out List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.11 8052AH Special Function Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.12 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

162
170
174
176
183
184
185
188
190
192
193
199

APPENDIX B
Instruction Set Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

200

INDEX

213

.......................................................

intelCHAPTER 1
Introduction
1.1 INTRODUCTION TO MCS BASIC-52
Welcome to MCS BASIC-52. This program functions as a BASIC interpreter occupying 8K of ROM inINTEL's 8052AH microcontroller. MCS BASIC-52 provides most of the features of "standard"BASICS, plus many additional features that apply to control environments and to the architecture of the8052AH.
The design goal of MCS BASIC-52 was to develop a software program that would make it easy for a
hardware/software designer to interact with the 8052 device; but, at the same time not limit the designer
to the slow and sometimes awkward constructs of BASIC. This program is not a "toy" like many of theso called TINY BASICS. It is a powerful software tool that can significantly reduce the design time ofmany projects. MCS BASIC-52 is ideal for so called imbedded systems, where terminals are notattached to system, but the system controls and manipulates equipment and data.
MCS BASIC-52 offers many unique hardware and software features, including the ability to store andexecute the user program out of an EPROM, the ability to process interrupts within the constructs of aBASIC program, plus an accurate real time clock. In addition, the arithmetic routines and l/O routines
contained in MCS BASIC-52 can be accessed with assembly language CALL routines. This feature canbe used to eliminate the need for the user to write these sometimes difficult and tedious programs.
All of the above are covered in this document. This is NOT a "How to Write Basic programs" manual.
Many excellent texts on this subject have been produced. Your local computer store can recommend
many such texts.
The descriptions of many of the statements in this manual involve rather detailed discussions that relate
to interfacing MCS BASIC-52 to assembly language programs. If the user is not interested in using
assembly language with MCS BASIC-52 these discussions may be ignored. If you are only interested inprogramming the MCS BASIC-52 device in BASIC, you can treat all statements the same way theywould be in any standard BASIC interpreter.
In reading this manual, you will find that some information may he repeated two or three times. This is
not an accident. Years of experience have proven that one of the most frustrating experiences one
encounters with manuals is trying to find a particular piece of information that the reader knows is in
the manual, but can't remember where.

-1-intel
1.2 GETTING STARTED
If you are like most engineers, technicians, hobbyists and humans, and don't like to read manuals, thissection is for you. The purpose of this section is to get you off on the right foot. If you are in the HighAnxiety Mode and just want to see if the darn chip works, wire the device in the minimum hardware
configuration as suggested in the Hardware Configuration chapter of this manual, apply power, andwatch what happens. NOTHING! That's because after power is applied to the MCS BASIC-52 device,
the program initializes the 8052AH hardware and goes into an AUTO-BAUD search routine. You must
touch the space bar on the serial input device in order to get MCS BASIC-52 to SIGN ON. The
message that will appear is *MCS-51 BASIC Vx.x*. If a space character is not the first character sent to
the MCS BASIC-51 device after reset, you can spend a lot of time trying to figure out what went
wrong. So do yourself a favor, read this section and touch the space bar before you call your local Intel
Field Applications Engineer. We received a number of questions asking how the AUTO-BAUD searchroutine worked. As a result this routine is listed in Chapter 11 of this manual.

1.3 WHAT HAPPENS AFTER RESET?
After RESET, MCS BASIC-52:
1) Clears the INTERNAL 8052AH memory2) Initializes the internal registers and pointers3) Tests, clears, and sizes the EXTERNAL memory
BASIC then assigns the top of EXTERNAL RANDOM ACCESS MEMORY to the SYSTEMCONTROL VALUE MTOP and uses this number as the random number seed. BASIC assigns thedefault crystal value, 11.0592 Mhz, to the SYSTEM CONTROL VALUE-XTAL and uses this default
value to calculate all time dependent functions, such as the EPROM programming timer and theinterrupt driven REAL TIME CLOCK. Finally. BASIC checks external memory location 8000H to see
if the baud rate information is stored. If the baud rate is stored, MCS BASIC-52 initializes the baud rate
generator (the 8052AH's SPECIAL FUNCTION REGISTER -- T2CON) with this information andsigns on. If it isn't stored, BASIC interrogates the serial port input and waits for a space character to be
typed. This sounds like a lot, but on the 8052AH, it doesn't take much time.

-2-

intel
1.3 WHAT HAPPENS AFTER RESET?
MCS BASIC-52 initializes the 8052AH's Special Function Registers, TMOD, TCON, and T2CON withthe following values:
TCON - 244 (0F4H)
TMOD - 16 (10H)
T2CON - 52 (34H)
After Reset the console device should display the following:*MCS-51(tm) BASIC Vx.x*
READY
To see if everything is OK after Reset, type the following:>PRINT XTAL, TMOD, TCON, T2CON(BASIC should respond)
11059200 16 244 52
If it does, everything is working properly. If it does not make sure that the external memory, the serial
port, and the oscillator are connected and working. Hardware debug begins here.
In the Appendix of this manual is a QUICK REFERENCE GUIDE. It provides a short description of allof the COMMANDS and STATEMENTS implemented in MCS BASIC-52 You might want to use thissection to gain a quick understanding, of the MCS BASIC-52 software package. Those of you who arefamiliar with the BASIC language will notice that most of the STATEMENTS and COMMANDS usedin MCS BASIC-52 are "standard," so getting started should not be a problem.

-3-

intel
1.4 DEFINITION OF TERMS:
COMMANDS:MCS BASIC-52 operates in two modes, the COMMAND or direct mode and the interpreter or RUN
mode. MCS BASIC-52 Commands can only be entered when the processor is in the COMMAND or
direct mode. MCS BASIC-52 takes immediate action after a command has been entered. This document
will use the terms RUN MODE and COMMAND MODE to refer to the two different modes ofoperation.
STATEMENTS:
A BASIC program is comprised of statements. Every statement begins with a line number, followed by
the statement body, and terminated with a Carriage Return (cr), or a colon (:) in the case of multiple
statements per line. Some statements can be executed in the COMMAND MODE, others cannot. The
DESCRIPTION OF STATEMENTS section of this manual describes whether a statement can be
executed in the COMMAND mode or only in the RUN mode.
There are three general types of statements in MCS BASIC-52: ASSIGNMENTS, INPUT/OUTPUT,
and CONTROL. The DESCRIPTION OF STATEMENT section of this manual explains what type isassociated with each statement.
•

EVERY line in a program must have a statement line number ranging between 0 and 65535
inclusive.

•

Statement numbers are used by BASIC to order the program statements sequentially.

•

In any program, a statement number can be used only once.

•

Statements need not be entered in numerical order, because BASIC will automatically order themin ascending order.

•

A statement may contain no more than 72 characters in Version 1.0 and no more than 79 in Version
1.1.

•

Blanks (spaces) are ignored by BASIC and BASIC automatically inserts blanks during LIST.

•

More than one statement can be put on a line, if separated by a colon (:), but only one statement
number is allowed per line.

FORMAT STATEMENTS:
Format Statements may only be used within the PRINT STATEMENT. The format statements include
TAB([expr]), SPC([exprl], USING(special symbols), and CR (carriage return with no line feed). Detailsof the format statements are provided in the description of the PRINT STATEMENT section of thismanual.

-4-intel
1.4 DEFINITION OF TERMSDATA FORMAT:The range of numbers that can be represented in MCS BASIC-52 is:= 1E-127 to +-.99999999E+127.
There are eight digits of significance in MCS BASIC-52. Numbers are internally rounded to fit this
precision. Numbers may be entered and displayed in four formats: integer, decimal, hexadecimal, andexponential.
EXAMPLE: 129, 34.98, 0A6EH, 1.23456E + 3
INTEGERS:In MCS BASIC-52, integers are numbers that ranges from 0 to 65535 or 0FFFFH. All integers can beentered in either decimal or hexadecimal format and all hexadecimal numbers must begin with a valid
digit (e.g. the number A000H must be entered 0A000H). When an operator, such as .AND. requires aninteger, MCS BASIC-52 will truncate the fraction portion of number so it will fit the integer format. Allline numbers used by MCS BASIC-52 are integers. This document will refer to integers and line
numbers, respectively in the following manner:[integer] -- [In num]
NOTE -- Throughout this document the brackets [] are used only to indicate an integer, constant, etc.
They are NOT entered when typing the actual number or variable.
CONSTANTS:
A constant is a real number that ranges from +- 1E - 127 to +- .99999999E + 127. A constant, of course,
can be an integer. This document will refer to constants in the following manner:[const]OPERATORS:
An operator performs a pre-defined operation on variables and/or constants. Operators require either
oneor two operands. Typical two operand or dyadic operators include ADD (+), SUBTRACT (-), MULTIPLY (*), and DIVIDE (/). Operators that require only one operand are often referred to as UNARYOPERATORS. Some typical UNARY OPERATORS are SIN, COS, and ABS.

-5-

intel
1.4 DEFINITION OF TERMS
VARIABLES:In Version 1.0 of MCS BASIC-52 a variable could be defined as either a letter, (i.e. A, X, I), a letter
followed by a number, (i.e. Q1, T7, L3), a letter followed by a ONE DIMENSIONED expression, (i.e.
J(4), G(A+6), I(10*SIN(X))), or a letter followed by a number followed by a ONE DIMENSIONED
expression (i.e. A1(8), P7(DBY(9)), W8(A+B). In Version 1.1 variables can be defined in the samemanner as in Version 1.0, however variables may also contain up to 8 letters or numbers including theunderline character. This permits the user to use a more descriptive name for a given variable.
Examples of valid variables in Version 1.1 of MCS BASIC-52 are as follows:
FRED VOLTAGE1 I – 11 ARRAY(ELE_1)
When using the expanded variable names available in Version 1.1 of MCS BASIC-52 it is important tonote that 1) It takes longer for MCS BASIC-52 to process these expanded variable names and 2) The
user may not use any keyword as part of a variable name (i.e. the variables TABLE and DIET could not
be used because TAB and IE are reserved words). BAD SYNTAX ERRORS will be generated if theuser attempts to define a variable that contains a reserved word.
Variables that include a ONE DIMENSIONED expression [expr] are often referred to as
DIMENSIONED or ARRAYED variables. Variables that only involve a letter or a letter and a number
are called SCALAR variables. The details concerning DIMENSIONED variables are covered in thedescription of the STATEMENT ROUTINE DIM. This document will refer to VARIABLES as:[var].
MCS BASIC-5 allocates variables in a "static" manner. That means each time a variable is used,BASIC allocates a portion of memory (8 bytes) specifically for that variable. This memory cannot be
de-allocated on a variable by variable basis. That means if you execute a statement like Q = 3, later on
you cannot tell BASIC that the variable Q no longer exists so, please "free up" the 8 bytes of memorythat belong to Q. Sorry. it doesn't work this way. The only way the user can clear the memory that isallocated to variables is to execute a CLEAR STATEMENT. This Statement "frees" all memory
allocated to variables.
IMPORTANT NOTE:
Relative to a dimensioned variable, it takes MCS BASIC-52 a lot less time to find a scalar variable.
That's because there is no expression to evaluate in a scalar variable. So, if you want to make a program
run as fast as possible, use dimensioned variables only when you have to. Use scalars for intermediatevariables, then assign the final result to a dimensioned variable.
EXPRESSIONS:
An expression is a logical mathematical formula that involves OPERATORS (both unary and dyadic),
CONSTANTS, and VARIABLES. Expressions can be simple or quite complex, i.e. 12*EXP(A)/100,
H(I)+55, or (SIN(A)*SIN(A)+COS(A)*COS(A))/2. A "stand alone" variable [var] or constant [const] isalso considered an EXPRESSION. This document will refer to EXPRESSIONS as:[expr] .

-6-

intel
1.4 DEFINITION OF TERMSRELATIONAL EXPRESSIONS:
Relational expressions involve the operators EQUAL (=), NOT EQUAL (<>), GREATER THAN (>),
LESS THAN (<), GREATER THAN OR EQUAL TO (>=) and LESS THAN OR EQUAL TO (<=).
They are used in control statements to "test" a condition (i.e. IF A < 100 THEN . . .). Relational
expressions ALWAYS REQUIRE TWO OPERANDS. This document will refer to RELATIONAL
EXPRESSIONS as:[rel expr].
SPECIAL FUNCTION OPERATORS:
Virtually all of the special function registers on the 8052AH can be accessed by using the special
function operators. The exceptions are PORTS 0, 2 and 3 and non-I/O associated registers such as
ACC, B, and PSW. Other SPECIAL FUNCTION OPERATORS are XTAL and TIME. Details of the
SPECIAL FUNCTION OPERATORS are covered in the section SPECIAL FUNCTION
OPERATORS.
SYSTEM CONTROL VALUES:
The system control values include the following: LEN (which returns the length of the program), FREE
(which designates how many bytes of RAM are not used that are allocated to BASIC), and MTOP
(which is the last memory location that is assigned to BASIC). Details of the system control values arecovered in the section SYSTEM CONTROL VALUES.

-7-

intel
1.4 DEFINITION OF TERMSSTACK STRUCTURE:
MCS BASIC-52 reserves the first 512 bytes of EXTERNAL DATA MEMORY to implement two"software" stacks. These are the control stack and the arithmetic stack or ARGUMENT STACK.
Understanding how the stacks work in MCS BASIC-52 is NOT NECESSARY if the user wishes only
to program in BASIC. However, understanding the stack structure is necessary if the user wishes to link
MCS BASIC-52 to ASSEMBLY language routines. The details of how to link to assembly language arecovered in the ASSEMBLY LANGUAGE LINKAGE section of this manual.
CONTROL STACK -- The control stack occupies locations 96 (60H) through 254 (0FEH) in external
ram memory. This memory is used to store all information associated with loop control (i.e. DOWHILE,
DO-UNTIL, and FOR-NEXT) and basic subroutines (GOSUB). The stack is initialized to 254 (OFEH)
and "grows down."
ARGUMENT STACK -- The ARGUMENT STACK occupies locations 301 (12DH) through 510(1FEH) in external ram memory. This stack stores all constants that MCS BASIC-52 is currently using.
Operations such as ADD, SUBTRACT, MULTIPLY, and DIVIDE always operate on the first two
numbers on the ARGUMENT STACK and return the result to the ARGUMENT STACK. The
argument stack is initialized to 510 (1FEH) and "grows down" as more values are placed on the
ARGUMENT STACK. Each floating point number placed on the ARGUMENT STACK requires 6
BYTES of storage.
INTERNAL STACK -- The stack pointer on the 8052AH (SPECIAL FUNCTION REGISTER, SP) is
initialized to 77 (4DH). The 8052AH's stack pointer "grows up" as values are placed on the stack. In
MCS BASIC-52 the user has the option of placing the 8052AH's STACK POINTER anywhere (above
location 77) in internal memory. The details of how to do this are covered in the ASSEMBLYLANGUAGE LINKAGE section of this manual.
LINE EDITOR:MCS BASIC-52 contains a minimum level line editor. Once a line is entered the user may not change
the line without re-typing the line. However, it is possible to delete characters while a line is in theprocess of being entered. This is done by entering a RUBOUT or DELETE character (7FH). The
RUBOUT character will cause the last character entered to be erased from the text input buffer.
Additionally, a control-D will cause the entire line to be erased. In Version 1.1 of MCS BASIC-52,
Control-Q (X-ON) and Control S (X-OFF) recognition have been added to the serial port. The user iscautioned not to accidentally type a Control-S when entering information because the MCS BASIC-52
will no longer respond to the console device. Control-Q is used to bring the console device back to life
after Control-S is typed.
NOTE -- In this document a carriage return is indicated by the symbol (cr). The carriage return is the
RETURN key on most keyboards.

-8-

intel
1.5 WHAT'S THE DIFFERENCE BETWEEN V1.0 AND V1.1
Thanks to feedback from many of the users of MCS BASIC-52, a number of changes and additions
have been made to Version 1.1. All of these changes and additions were made to enhance the usefulnessof theproduct and yet retain 100% compatibility, well almost 100% compatibility with the original version.
To make things simple, all of the changes will be mentioned here and a reference will be provided as towhere the reader of this manual may obtain more information about the change or addition.
The only change that has been made to V1.1 that is not compatible with V1.0 is with the
IF_THEN_ELSE STATEMENT when used with multiple statements per line. In V1.0, the following
two examples would function in the same manner.
EXAMPLE 1:
10 IF A=B THEN C=A : A=A/2 : GOTO 100
20 PRINT A
EXAMPLE 2:
10 IF A=B THEN C=A
12 A=A/2
14 GOTO 100
20 PRINT A
They function in the same manner because V1.0 treats the delimiter (:) exactly the same as a carriage
return (cr) in every case. However, V1.1 executes the remainder of the line if and only if the test A = B
proves to be true. This means in EXAMPLE 1 IF A did equal B, V1.1 would then set C=A, then set A =A/2, then execute line 100. IF A did not equal B, V1.1 would then PRINT A and ignore the statements
C=A: A=A/2: GOTO 100. V1.1 will execute EXAMPLE 2 exactly the same way as V1.0. This same
logical interpretation holds true for the ELSE statement as well. This example dictates a simple rule formaintaining IF_THEN_ELSE compatibility between the two versions. IF THE DELIMITER (:) IS
NOT USED IN AN IF_THEN_ELSE STATEMENT, V1.0 AND V1.1 WILL TREAT THE
STATEMENTS IN THE SAME MANNER!!
This change was made because most users of MCS BASIC-52 felt that the V1.1 interpretation of thisstatement was more useful because fewer GOTO statements need be employed in a typical program.
Additionally, V1.1 accepts inputs in either lower or upper case, whereas V1.0 converted lower case toupper case. V1.1 will however, convert keywords from lower case to upper case during the LlSTing ofa program. Finally, MCS BASIC-52 V1.1 runs between 2% and 10% faster than V1.0. Typically, thisshould not cause any problems.
As far as the user is concerned, these are the only changes that may affect the operation of a typicalprogram. Now, on to the additions.

-9-intel
1.5 WHAT'S THE DIFFERENCE BETWEEN V1.0 AND V1.1
ADDITIONS TO MCS BASIC-52 V1.1:•

X-ON (control Q) and X-OFF (control S) have been added. These permit the user to "stop" (controlS) and start (control Q) the display of characters during a LIST or PRINT. This feature also permits
synchronization with external l/O (input/output) devices. The X-OFF (control S) functions on a line
by line basis, not on a character by character basis.

•

Five new statements have been added. These include IDLE, LD@, ST@, PGM, and RROM.
Details of these statements are listed under the DESCRIPTION OF STATEMENTS section of thismanual.

•Six new RESET options have been provided. They permit the user to assign the top of memory(MTOP) during reset, and allow the user to write specific RESET programs in assembly language.
Additionally, they provide an option where the memory WILL NOT be cleared during RESET.
More information on the specific RESET OPTIONS is detailed in the DESCRIPTIONS OF
EPROM FILE COMMANDS under PROGI, PROG2, PROG3, PROG4, PROG5, and PROG6COMMANDS and in Chapter 11 of this manual.

•

The Timing of the EPROM programming algorithm has been significantly relaxed between thevarious strobes required for the EPROM programming function. This relaxed timing permits theuser to program devices such as the 8751H and the 8748/9 using the EPROM programming
capabilities of the MCS BASIC-52 device. Details of the timing changes are in Chapter 10 of thismanual.

•During EPROM programming, the INT0/DMA REQUEST pin of the MCS BASIC-52 device istreated as a ready input pin. This allows for a simple direct connection to EEPROM devices such as
the 2817A. For normal EPROM programming, INT0 must be kept high or the programming hangs
up. Details concerning the use of EEPROMS with the MCS BASIC-52 device are provided in
Chapter 10 of this manual.

•

A RUN TRAP option has been provided. This option traps the MCS BASIC-52 interpreter in the
program RUN mode and will not permit the user to exit this mode. Details of this option arecovered in Chapter 8.4 of this manual.

•

A user STATEMENT/COMMAND expansion option has been provided. This permits the user toeasily add new or custom STATEMENTS and COMMANDS to MCS BASIC-52. Details of thisoption are covered in Chapter 12 of this manual.

- 10 -

intel
1.5 WHAT'S THE DIFFERENCE BETWEEN V1.0 AND V1.1
ADDITIONS TO MCS BASIC-52 V1.1:•

A number of new assembly language user OP BYTES have been added. These permit the user tomake better use of the STATEMENT/COMMAND expansion option previously described. Detailsof these new OP BYTES are presented in Chapter 9.6 of this manual.

•

The length of the input buffer has been increased from 72 characters to 79 characters and the
ERROR: LINE TOO LONG has been eliminated. Instead, when the cursor reaches the 79th
position a bell character will be echoed every time the user attempts to enter another character.

•

A new variation on the PRINT (including PH0. and PH1.) and LIST statements have been added.
This new option is evoked with an @ character (EXAMPLE: PRINT@ or LIST@ and permits theuser to write specific output drivers for these statements and commands. When the @ PRINT or
LIST is evoked, MCS BASIC-52 CALLS external code memory location 403CH. The user must
put the specific output driver in this location. More details of this option is in Description ofStatements section of this manual.

•

The control stack has been made more "forgiving." This means that the user can execute a GOSUB
to a subroutine that contains a FOR-NEXT loop and return from the subroutine without completing
the FOR-NEXT loop. Version 1.0 would yield a C-STACK ERROR under these circumstances, V
1.1 yields no error.

•

The question mark character ? is interpreted as a PRINT statement (EXAMPLE: (PRINT 10+20 is
the same as ? 10+20). The symbols P. remains a shorthand notation for PRINT just as in V1.0.

•

The FOR-NEXT statement can be executed in the direct mode. This lets the user write shortroutines in the DIRECT MODE to, for example, display a region of memory (EXAMPLE: FOR I =200H to 210H: PH0. XBY(I): NEXT I)•

Variables can be up to 8 characters in length, however, only the first character, the last character,and the total number of characters are of significance. This lets the user better describe variables
that are used in a program. Chapter 1.4 details the limitations on the expanded variables in Version
1.1 .

- 11 -

intel
1.5 WHAT'S THE DIFFERENCE BETWEEN V1.0 AND V1.1
ADDITIONS TO MCS BASIC-52 V1.1:•

The CALL statement vectors to locations 4100H through 41FFH if the CALL integer is between 0
and 7FH inclusive. This means that CALL 0 will vector to location 4100H, CALL 1 to location
4102H, CALL 2 to location 4104H, etc. This permits the user to easily generate assembly language
CALL tables by using simple integers with the CALL statement. Anyway, CALL 0 through CALL
1FFFH was not too useful because these numbers vectored into the MCS BASIC-52 ROM.

•

The error message anomaly for an invalid line number on a GOTO or GOSUB STATEMENT has
been eliminated on V1.1 of MCS BASIC-52. The correct line number is now processed anddisplayed by the error processor.

•

The FOR-TO-{STEP}-NEXT statement can be executed in the COMMAND MODE in version 1.1
of MCS BASIC-52. Additionally, the NEXT statement does not require a variable in version 1.1.
Details of these features are covered in the Description of Statements section of this manual.

•

The REM statement can be executed in the COMMAND MODE. If the user is employing some
type of UPLOAD/DOWNLOAD routine with a computer, this lets the user insert REM statements,
without line numbers in the text and not download them to the MCS BASIC-52 device. This helps
to conserve memory.

•

Version 1.1 is also a little less "crashable" than version 1.0. This is due to a more extensive "typechecking" on control transfer routines (i.e. GOTO, GOSUB).

- 12 -

intelCHAPTER 2
Description of Commands

2.1 DESCRIPTION OF COMMANDSCOMMAND: RUN(cr)
ACTION TAKEN:
After RUN(cr) is typed all variables are set equal to zero, all BASIC evoked interrupts are cleared andprogram execution begins with the first line number of the selected program. The RUN command andthe GOTO statement are the only way the user can place the MCS BASIC-52 interpreter into the RUN
mode from the COMMAND mode. Program execution may be terminated at any time by typing acontrol-C on the console device.
VARIATIONS:
Unlike some Basic interpreters that allow a line number to follow the RUN command (i.e., RUN 100),MCS BASIC-52 does not permit such a variation on the RUN command. Execution always begins withthe first line number. To obtain the same functionality as the RUN[ln num] command, use the GOTO[lnnum] statement in the direct mode. SEE STATEMENT GOTO.
EXAMPLE:>10 FOR I=1 TO 3
>20 PRINT>30 NEXT
>RUN
1
2
3
READY
>

- 13 -

intel
2.2 DESCRIPTION OF COMMANDS:
COMMAND: CONT(cr)
ACTION TAKEN:
If a program is stopped by typing a control-C on the console device or by execution of a STOP
statement, you can resume execution of the program by typing CONT(cr). Between the stopping and there-starting of the program you may display the values of variables or change the values of variables.
However, you may NOT CONTinue if the program is modified during the STOP or after an error.
VARIATIONS:
None.
EXAMPLE:>10 FOR I=1 TO 10000>20 PRINT I>30 NEXT I
>RUN
1
2
3
4
5

-

(TYPE CONTROL-C ON CONSOLE)

STOP - IN LINE 20
READY>PRINT I
6
>I=10
>CONT
10
11
12

- 14 -

intel2.3 DESCRIPTION OF COMMANDS:
COMMAND: LlST(cr)
ACTION TAKEN:
The LlST(cr) command prints the program to the console device. Note that the list command "formats"the program in an easy to read manner. Spaces are inserted after the line number and before and afterstatements. This feature is designed to aid in the debugging of MCS BASIC-52 programs. The "listing"of a program may be terminated at anytime by typing a control-C on the console device.
VARIATIONS:
Two variations of the LIST COMMAND are possible with MCS BASIC-52. They are:
LIST [ln num] (cr) and
LIST [ln num] -- [ln num] (cr)The first variation causes the program to be printed from the designated line number (integer) to the endof the program. The second variation causes the program to be printed from the first line number(integer) to the second line number (integer). NOTE -- the two line numbers MUST BE SEPARATEDBY A DASH - .
EXAMPLE:
READY
>LIST
10 PRINT "LOOP PROGRAM"
20 FOR I=1 TO 3
30 PRINT I
40 NEXT I
50 ENDREADY>LIST 30
30 PRINT I
40 NEXT I
50 ENDREADY>LIST 20-40
20 FOR I=1 TO 3
30 PRINT I
40 NEXT I- 15 -

intel
2.4 DESCRIPTION OF COMMANDSCOMMAND: LlST#(cr)ACTION TAKEN:
The LlST#(cr) command prints the program to the LIST device. The BAUD rate to this device must be
initialized by the STATEMENT -- BAUD[expr]. All comments that apply to the LIST command apply
to the LIST# command. The LlST#(cr) command is included to permit the user to make "hard copies"
of a program. The output to the list device is on P1.7 of the MCS BASIC-52 device.

- 16 -

intel
2.5 DESCRIPTION OF COMMANDSCOMMAND: LlST@(cr) (VERSION 1.1 ONLY)ACTION TAKEN:
The LIST@ command does the same thing as the LIST command except that the output is directed to auser defined output driver. This command assumes that the user has placed an assembly language
output routine in external code memory location 403CH. To enable the @ driver routine the user must
SET BIT 27H (39D) in the internal memory of the MCS BASIC-52 device. BIT 27H (39D) is BIT 7 ofinternal memory location 24H (36D). This BIT can be set by the BASIC statement
DBY(24H)=DBY(24H).OR. 80H or by a user supplied assembly language routine. If the user evokes
the @ driver routine and this bit is not set, the output will be directed to the console driver. The only
reason this BIT must be set to enable the @ driver is that it adds a certain degree of protection from
accidentally typing LIST@ when no assembly language routine exist. The philosophy here is that if theuser sets the bit, the user provides the driver or else!!!
When MCS BASIC-52 calls the user output driver routine at location 403CH, the byte to output is in
the accumulator and R5 of register bank 0 (RB0). The user may modify the accumulator (A) and thedata pointer (DPTR) in the assembly language output routine, but cannot modify any of the registers in
RB0. This is intended to make it real easy for the user to implement a parallel or serial output driver
without having to do a PUSH or a POP.

- 17 -

intel2.6 DESCRIPTION OF COMMANDSCOMMAND: NEW(cr)
ACTION TAKEN:
When NEW(cr) is entered, MCS BASIC-52 deletes the program that is currently stored in RAM
memory. In addition, all variables are set equal to ZERO, all strings and all BASIC evoked interrupts
are cleared. The REAL TIME CLOCK, string allocation, and the internal stack pointer value (location
3EH) are NOT effected. In general, NEW (cr) is used simply to erase a program and all variables.

- 18 -

intel2.7 DESCRIPTION OF COMMANDSCOMMAND: NULL [integer](cr)
ACTION TAKEN:
The NULL[integer] (cr) command determines how many NULL characters (00H) MCS BASIC-52 will
output after a carriage return. After initialization NULL = 0. The NULL command was more importantback in the days when a "pure" mechanical printer was the most common I/0 device. Most modern
printers contain some kind of RAM buffer that virtually eliminates the need to output NULL characters
after a carriage return. NOTE -- the NULL count used by MCS BASIC-52 is stored in internal RAM
location 21 ( 15H). The NULL value can be changed dynamically in a program by using a DBY(21) =[expr] statement. The [expr] can be any value between 0 and 255 (0FFH) inclusive.
VARIATIONS:
None.

- 19 -

intelCHAPTER 3
Description of EPROM File Commands
DESCRIPTION OF EPROM FILE COMMANDS
One of the unique and powerful features of MCS BASIC-52 is that it has the ability to execute andSAVE programs in an EPROM. MCS BASIC-52 actually generates all of the timing signals needed toprogram most EPROM devices. Saving programs in EPROMS is a much more attractive and
RELIABLE alternative relative to cassette tape, especially in control and/or noisy environments.
The hardware needed to permit MCS BASIC-52 to program an EPROM device is minimal, typically
only one NAND gate, three or four transistors, and a few resistors are all that is required. Details of the
hardware requirements are in the EPROM PROGRAMMING section of this manual.
MCS BASIC-52 can save more than one program in an EPROM. In fact, it can save as many programsas the size of the EPROM memory permits. The programs are stored sequentially in the EPROM andany program can be retrieved and executed. This sequential storing of programs is referred to as the
EPROM FILE. The following commands permit the user to generate and manipulate the EPROM FILE.

- 20 -

intel3.1 DESCRIPTION OF EPROM FILE COMMANDSCOMMANDS: RAM(cr) and ROM [integer] (cr)ACTION TAKEN:
These two commands tell the MCS BASIC-52 interpreter whether to select the current program (thecurrent program is the one that will be displayed during a LIST command and executed when RUN is
typed) out of RAM or EPROM. The RAM address is assumed to be 512 (200H) and the EPROM
address begins at 32784 (8010H).
RAM
When RAM(cr) is entered MCS BASIC-52 selects the current program from RAM MEMORY. This is
usually considered the "normal" mode of operation and is the mode that most users interact with thecommand interpreter.
ROM
When ROM [integer] (cr) is entered MCS BASIC-52 selects the current program out of EPROM
memory. If no integer is typed after the ROM command (i.e. ROM (cr)) MCS BASIC-52 defaults to
ROM 1. Since the programs are stored sequentially in EPROM the integer following the ROM
command selects which program the user wants to run or list. If you attempt to select a program that
does not exist (i.e. you type in ROM 8 and only 6 programs are stored in the EPROM) the message
ERROR: PROM MODE will be displayed.
MCS BASIC-52 does not transfer the program from EPROM to RAM when the ROM mode is selected.
So, you cannot EDIT a program in the ROM mode. If you attempt to edit a program in the ROM mode,by typing in a line number, the message ERROR: PROM MODE will be displayed. The following
command to be described, XFER, permits one to transfer a program from EPROM to RAM for editing
purposes.
Since the ROM command does NOT transfer a program to RAM, it is possible to have different
programs in ROM and RAM simultaneously. The user can "flip" back and forth between the two modes
at any time. Another added benefit of NOT transferring a program to RAM is that all of the RAM
memory can be used for variable storage if the PROGRAM is stored in EPROM. The SYSTEMCONTROL VALUES --MTOP and FREE always refer to RAM not EPROM.
VARIATIONS:
None.

- 21 -

intel3.2 DESCRIPTION OF EPROM FILE COMMANDSCOMMAND: XFER(cr)
ACTION TAKEN:
The XFER (transfer) command transfers the current selected program in EPROM to RAM and thenselects the RAM mode. If XFER is typed while MCS BASIC-52 is in the RAM mode, the program
stored in RAM is transferred back into RAM and the RAM mode is selected. The net result is that
nothing happens except that a few milli-seconds of CPU time is used to do a wasted move. After the
XFER command is executed, the user may edit the program in the same manner any RAM program
may be edited.
VARIATIONS:
None.

- 22 -

intel
3.3 DESCRIPTION OF EPROM FILE COMMANDSCOMMAND: PROG(cr)
ACTION TAKEN:
The PROG COMMAND programs the resident EPROM with the current selected program. The current
selected program may reside in either RAM or EPROM. This command assumes that the hardware isconfigured in the manner described in the EPROM PROGRAMMING section of this manual.
After PROG (cr) is typed, MCS BASIC-52 displays the number in the EPROM FILE the program will
occupy.
EXAMPLE:
>LIST
10
20
30

FOR I=1 TO 10
PRINT I
NEXT I

READY
>PROG
12
READY
>ROM 12
READY
>LIST
10
20
30

FOR I=1 TO 10
PRINT I
NEXT IREADY>
In this example, the program just placed in the EPROM is the 12th program stored.
VARIATIONS:
None.

- 23 -

intel
3.4 DESCRIPTION OF EPROM FILE COMMANDSCOMMANDS: PROG1(cr) and PROG2(cr)
ACTION TAKEN:
PROG1
Normally, after power is applied to the MCS BASIC-52 device, the user MUST type a "space"character to initialize the 8052AH's serial port. As a convenience, MCS BASIC-52 contains a PROG1
COMMAND. What this command does is program the resident EPROM with the BAUD RATE
information. So, the next time the MCS BASIC-52 device is "powered up," i.e. RESET, the chip will
read this information and initialize the serial port with the stored baud rate. The "sign-on" message will
be sent to the console immediately after the MCS BASIC-52 device completes its reset sequence. The"space" character no longer needs to be typed. Of course, if the BAUD rate on the console device ischanged a new EPROM must be programmed to make MCS BASIC-52 compatible with the newconsole.
PROG2
The PROG2 command does everything the PROG1 command does, but instead of "signing-on" andentering the COMMAND MODE, the MCS BASIC-52 device immediately begins executing the firstprogram stored in the resident EPROM.
THIS IS AN IMPORTANT FEATURE !!
By using the PROG2 command it is possible to RUN a program from a RESET condition and NEVER
connect the MCS BASIC-52 chip to a console. In essence, saving PROG2 information is equivalent totyping a ROM 1, RUN command sequence. This is ideal for control applications, where it is not always
possible to have a terminal present. In addition. this feature permits the user to write a special
initialization sequence in BASIC or ASSEMBLY LANGUAGE and generate a custom "sign-on"
message for specific applications.

- 24 -

intel
3.5 DESCRIPTION OF EPROM FILE COMMANDSCOMMANDS: FPROG(cr), FPROG1(cr), AND FPROG2(cr)
ACTION TAKEN:FPROG(cr), FPROG1(cr), and FPROG2(cr) do exactly the same thing as PROG(cr), PROG1(cr), and
PROG2(cr) respectively, except that the algorithm used to perform the programming function is the
INTEL "INTELLIGENT" fast programming algorithm. The user MUST provide a way to increase VCC
to the EPROM to 6 volts.

- 25 -

intel3.6 DESCRIPTION OF EPROM FILE COMMANDSCOMMANDS: PROG3(cr). PROG4(cr), FPROG3(cr), FPROG4(cr) (VERSION 1.1 ONLY)ACTION TAKEN:
PROG3
The PROG3 COMMAND functions the same way as the PROG1 COMMAND previously described,
except that PROG3 also saves the system control value, MTOP, when it is evoked. During a RESET or
power-up sequence MCS BASIC-52 will only clear the external data memory up to the MTOP valuethat was saved when the PROG3 COMMAND was evoked. This permits the user to "protect" regions ofmemory from being cleared during a RESET or power-up condition. In typical use, the PROG3
COMMAND assumes that the user is saving some critical information in some type of battery-backedup or non-volatile memory and does not want this information to be destroyed during a RESET or
power-up sequence.
PROG4
The PROG4 COMMAND is a combination of the PROG2 and PROG3 COMMAND. PROG4 saves thesame information as PROG3, but also executes the first program stored in the EPROM after a RESET
or power-up condition.
FPROG3 and FPROG4
The FPROG3 and FPROG4 commands save the same information as the PROG3 and PROG4
commands respectively, except that the INTELIigent(tm) algorithm is used to program the EPROM.
VARIATIONS:
None.

- 26 -

intel3.7 DESCRIPTION OF EPROM FILE COMMANDSCOMMANDS: PROG5(cr), PROG6(cr), FPROG5(cr), FPROG6(cr) (VERSION 1.1 ONLY)
ACTlON TAKEN:
PROG5 & FPROG5
The PROG5 command saves both the baud rate information and the MTOP information, just like the
PROG3 command previously described. However, during a RESET or power-up condition the MCS
BASIC-52 device examines external data memory location 5FH (95 decimal). If the user has placed thevalue 0A5H (165 decimal) in this location, the MCS BASIC-52 device will not clear the external
memory during a RESET or power-up condition. This permits the user to "save" programs in external
memory, providing some type of battery back-up scheme has been employed.
Normally, when using the PROG5 command to establish the RESET or power-up condition, the MCS
BASIC-52 device will enter the command mode after RESET or power-up. However, if the user wishes
to execute the program stored in external memory, the character 34H (52 decimal) needs to be placed inexternal memory location 5EH (94 decimal). Placing a 34H in location 5EH causes MCS BASIC-52 toenter the "RUN TRAP MODE." Details of this mode are presented in chapter 8 of this manual.
PROG6 & FPROG6
Does the same thing as PROG5, but CALLS external program memory location 4039H during a
RESET or power-up sequence. This option also requires the user to put the character 0A5H in external
memory location 5FH to insure that external RAM will not be cleared during RESET or power-up. The
user must put an assembly language initialization routine in external code memory location 4039H orelse this RESET mode will crash. When the user returns from the customized assembly language
RESET routine, three options exist:
OPTION 1 FOR PROG6
If the CARRY BIT is CLEARED (CARRY = 0) upon return from the user RESET routine MCS
BASIC-52 will enter the auto-baud rate determining routine. The user must then type a space character(20H) on the terminal to complete the RESET routine and produce a RESET message on the terminal.
OPTION 2 FOR PROG6
If the CARRY BIT is SET (CARRY = 1) and BIT 0 of the ACCUMULATOR is CLEARED (ACC. 0 =0) MCS BASIC-52 will produce the standard sign-on message upon return from the user supplied
RESET routine. The baud rate will be the one that was saved when the PROG6 option was used.
OPTION 3 FOR PROG6
If the CARRY BIT is SET (CARRY = 1) and BIT 0 of the ACCUMULATOR is SET (ACC. 0 = 1),
MCS BASIC-52 will execute the first program stored by the user in EPROM (starting address of the
program is 8010H) upon return from the user supplied RESET routine.

- 27 -

intelCHAPTER 4
Description of Statements

4.1 DESCRIPTION OF STATEMENTS
STATEMENT: BAUD [expr]
MODE: COMMAND AND/OR RUN
TYPE: CONTROLThe BAUD [expr] statement is used to set the baud rate for the software line printer port resident on the
MCS BASIC-52 device. In order for this STATEMENT to properly calculate the baud rate, the crystal(special function operator -- XTAL) must be correctly assigned (e.g. XTAL = 9000000). MCS BASIC52 assumes a crystal value of 11.0592 MHz if no XTAL value is assigned. The software line printerport is P1.7 on the 8052AH device. The main purpose of the software line printer port is to let the user
make a "hard copy" of program listings and/or data. The COMMAND LIST# and the STATEMENTPRINT# direct outputs to the software line printer port. If the BAUD [expr] STATEMENT is not
executed before a LIST# or PRINT# command/statement is entered, the output to the software line
printer port will be at about 1 BAUD and it will take A LONG TIME to output something. You may
even think that BASIC has crashed, but it hasn't. It's just outputting at a VERY SLOW rate. So be sure
to assign a BAUD rate to the software printer port BEFORE using LIST# or PRINT#. The maximum
baud rate that can be assigned by the BAUD statement depends on the crystal. In general, 4800 is areasonable maximum baud- rate. however the user may want to experiment with different rates. The
software serial transmits 8 data bits, 1 start bit, and two stop bits. No parity is transmitted.
EXAMPLE:BAUD 1200
Will cause the line printer port to output data at 1200 BAUD.
VARIATIONS:
None.

- 28 -

intel
4.2 DESCRIPTION OF STATEMENTS
STATEMENT: CALL [integer]
MODE: COMMAND AND/OR RUN
TYPE: CONTROLThe CALL [integer] STATEMENT is used to call an assembly language program. The integer
following CALL is the address where the user must provide the assembly language routine. To return toBASIC the user must execute an assembly language RET instruction. Examples of how to use the
CALL [integer] instruction are given in the ASSEMBLY LANGUAGE LINKAGE section of thismanual.
EXAMPLE:CALL 9000H
Will cause the 8052AH to execute the assembly language program beginning at location 9000H (i.e. theprogram counter will be loaded with 9000H).
VARIATIONS: (VERSION 1.1 ONLY)
If the integer following the CALL statement is between 0 and 127 (7FH), Version 1.1 of MCS BASIC52 will multiply the user integer by two, then add 4100H and vector to that location. This means that
CALL 0 will call location 4100H, CALL 1 will call 4102H, CALL 2 -- 4104H and so on. This permits
the user to generate a simple table of assembly language routines without having to enter 4 digit hex
integers after the CALL statement from the user supplied RESET routine.

- 29 -

intel
4.3 DESCRIPTION OF STATEMENTS
STATEMENT: CLEAR
MODE: COMMAND AND/OR RUN
TYPE: CONTROLThe CLEAR STATEMENT sets all variables equal to 0 and resets all BASIC evoked interrupts andstacks. This means that after the CLEAR statement is executed an ONEX1 or ONTIME statement must
be executed before MCS BASIC-52 will acknowledge interrupts. ERROR trapping via the ONERR
statement will also not occur until an ONERR[integer] STATEMENT is executed. The CLEAR
STATEMENT does not affect the real time clock that is enabled by the CLOCK1 STATEMENT.
CLEAR also does not reset the memory that has been allocated for STRINGS, so it is NOT necessary toenter the STRING [expr], [expr] STATEMENT to re-allocate memory for strings after the CLEAR
STATEMENT is executed. In general, CLEAR is simply used to "erase" all variables.
VARIATIONS:
None.

- 30 -

intel
4.4 DESCRIPTION OF STATEMENTS
STATEMENTS: CLEARI (clear interrupts)
CLEARS (clear stacks)
MODE: COMMAND AND/OR RUN
TYPE: CONTROL
CLEARIThe CLEARI STATEMENT clears all of the BASIC evoked interrupts. Specifically, the ONTIME andONEX1 interrupts are DISABLED after the CLEARI STATEMENT is executed. This is accomplishedby clearing bits 2 and 3 of the 8052AH's special function register, IE and by clearing the status bits that
determine whether MCS BASIC-52 or the user is controlling these interrupts. The real time clock whichis enabled by the CLOCK1 STATEMENT is not affected by CLEARI. This statement can be used to
selectively DISABLE interrupts during specific sections of the users BASIC program. The ONTIME
and/ or ONEX1 STATEMENTS MUST BE EXECUTED AGAIN before the specific interrupts will beenabled.
CLEARS
The CLEARS statement RESETS all of MCS BASlC-52's STACKS. The CONTROL and
ARGUMENT STACKS are reset to their initialization value, 254 (0FEH) and 510 (1FEH) respectively.
The INTERNAL STACK (the 8052AH's STACK POINTER, SPECIAL FUNCTION REGISTER-SP)
is loaded with the value that is in INTERNAL RAM location 62 (3EH). This statement can be used to"purge" the stack should an error occur in a subroutine. In addition, this statement can be used toprovide a "special" exit from a FOR-NEXT, DO-WHILE, or DO-UNTIL loop.
EXAMPLE OF CLEARS:>10 PRINT "MULTIPLICATION TEST. YOU HAVE 5 SECONDS">20 FOR I = 2 TO 9>30 N = INT(RND*10) : A - N*I>40 PRINT "WHAT IS ",N,"*",I,"?": CLOCK1>50 TIME = O : ONTIME 5,200 : INPUT R : IF R <> A THEN 100>60 PRINT "THAT'S RIGHT" TIME=0 : NEXT I>70 PRINT "YOU DID IT. GOOD JOB" : END>100 PRINT "WRONG - TRY AGAIN" : GOTO 50>200 REM WASTE CONTROL STACK, TOO MUCH TIME
>210 CLEARS : PRINT "YOU TOOK TOO LONG" : GOTO 10
NOTE: When the CLEARS and CLEARI STATEMENTS are LISTED they will appear as CLEAR S
and CLEAR I respectively. Don't be alarmed, that is the way it's supposed to work.

- 31 -

intel
4.5 DESCRIPTION OF STATEMENTS
STATEMENTS: CLOCK1 and CLOCK0
MODE: COMMAND AND/OR RUN
TYPE: CONTROL
CLOCK1The CLOCK1 STATEMENT enables the REAL TIME CLOCK feature resident on the MCS BASIC52 device. The special function operator TIME is incremented once every 5 milliseconds after the
CLOCK1 STATEMENT has been executed. The CLOCK1 STATEMENT uses TIMER/COUNTER 0
in the 13-bit mode to generate an interrupt once every 5 milliseconds. Because of this, the special
function operator TIME has a resolution of 5 milliseconds.
MCS BASIC-52 automatically calculates the proper reload value for TIMER/COUNTER 0 after the
crystal value has been assigned (i.e. XTAL=value). If no crystal value is assigned, MCS BASIC-52
assumes a value of 11.0592 MHz. The special function operator TIME counts from 0 to 65535.995
seconds. After reaching a count of 65535.995 seconds TIME overflows back to a count of zero.
Because the CLOCK1 STATEMENT uses the interrupts associated with TIMER/COUNTER 0 (theCLOCK1 statement sets bits 7 and 2 in the 8052AH's special function register, IE) the user may not use
this interrupt in an assembly language routine if the CLOCK1 STATEMENT is executed in BASIC.
The interrupts associated with the CLOCK1 STATEMENT cause MCS BASIC-52 programs to run atabout 99.6% of normal speed. That means that the interrupt handling for the REAL TIME CLOCK
feature only consumes about .4% of the total CPU time. This very small interrupt overhead is attributed
to the very fast and effective interrupt handling of the 8052AH device.
CLOCK0
The CLOCK0 (zero) STATEMENT disables or "turns off" the REAL TIME CLOCK feature. This
statement clears bit 2 in the 8052AH's special function register, IE. After CLOCK0 is executed, thespecial function operator TIME will no longer increment. The CLOCK0 STATEMENT also returns
control of the interrupts associated with TIMER COUNTER 0 back to the user, so this interrupt may be
handled at the assembly language level. CLOCK0 is the only MCS BASIC-52 statement that candisable the REAL TIME CLOCK. CLEAR and CLEARI will NOT disable the REAL TIME CLOCK.
VARIATIONS:
None.

- 32 -

intel4.6 DESCRIPTION OF STATEMENTS
STATEMENTS: DATA -- READ -- RESTORE
MODE: RUN
TYPE: ASSIGNMENTDATADATA specifies expressions that may be retrieved by a READ STATEMENT. If multiple expressions
per line are used, they MUST be separated by a comma.
READ
READ retrieves the expressions that are specified in the DATA STATEMENT and assigns the value ofthe expression to the variable in the READ STATEMENT. The READ STATEMENT MUSTALWAYS be followed by one or more variables. If more than one variable follows a READ
STATEMENT, they MUST be separated by a comma.
RESTORERESTORE "resets" the internal read pointer back to the beginning of the data so that it may be readagain.
EXAMPLE:>10 FOR I=1 TO 3
>20 READ A,B>30 RRINT A,B>40 NEXT I>50 RESTORE>60 READ A,B>70 PRINT A,B>80 DATA 10,20,10/2,20/2,SIN(PI),COS(PI)
>RUN
10
5
0
10

2O
10
-1
20

VARIATIONS:
None.

- 33 -

intel4.6 DESCRIPTION OF STATEMENTS
Explanation of previous example:
Every time a READ STATEMENT is encountered the next consecutive expression in the DATASTATEMENT is evaluated and assigned to the variable in the READ STATEMENT. DATASTATEMENTS may be placed anywhere within a program, they will NOT be executed nor will theycause an error. DATA STATEMENTS are considered to be chained together and appear to be one BIG
DATA STATEMENT. If at anytime all the DATA has been read and another READ STATEMENT is
executed then the program is terminated and the message ERROR: NO DATA IN LINE XX is printed
to the console device.

- 34 -

intel
4.7 DESCRIPTION OF STATEMENTS
STATEMENT: DIM
MODE: COMMAND AND/OR RUN
TYPE: ASSIGNMENT
DIM reserves storage for matrices. The storage area is first assumed to be zero. Matrices in MCS
BASIC- 52 may have only ONE DIMENSION and the size of the dimensioned array MAY NOT
exceed 254 elements. Once a variable is dimensioned in a program it may not be re-dimensioned. An
attempt to re-dimension an array will cause an ARRAY SIZE ERROR. If an arrayed variable is usedthat has NOT been dimensioned by the DIM STATEMENT, BASIC will assign a default value of 10 tothe array size. All arrays are set equal to zero when the RUN COMMAND, NEW COMMAND, or the
CLEAR STATEMENT is executed. The number of bytes allocated for an array is 6 times the (arraysize plus 1). So, the array A(100) would require 606 bytes of storage. Memory size usually limits thesize of a dimensioned array.
VARIATIONS:
More than one variable can be dimensioned by a single DIM STATEMENT, i.e., DIM A(10), B(15),A1(20).
EXAMPLE:DEFAULT ERROR ON ATTEMPT TO RE-DIMENSION ARRAY>10 A(5)=10>20 DIM A(5)>RUN

- BASIC ASSIGNS DEFAULT OF 10 TO ARRAY SIZE HERE
- ARRAY CANNOT BE RE-DIMENSIONEDERROR ARRAY SIZE - IN LINE 20
20 DIM A(5)-------x- 35 -

intel
4.8 DESCRIPTION OF STATEMENTS
STATEMENTS: DO -- UNTIL [rel expr]
MODE: RUN
TYPE: CONTROLThe DO -- UNTIL [rel expr] instruction provides a means of "loop control" within an MCS BASIC-52
program. All statements between the DO and the UNTIL [rel expr] will be executed until the relational
expression following the UNTIL statement is TRUE. DO -- UNTIL loops may be nested.
EXAMPLES:SIMPLE DO-UNTIL

NESTED DO-UNTIL>10 A=0>20 DO>30 A=A+I>40 PRINT A>50 UNTIL A=4>60 PRINT "DONE">RUN>10 DO : A=A+1 : DO : B=B+1>20 PRINT A,B,A*B>30 UNTIL B=3>40 B=0>50 UNTIL A=3
>RUN

1
2
3
4
DONE
READY
>

1
1
1
2
2
2
3
3
3

1
2
3
1
2
3
1
2
3

READY
>
VARIATIONS:
None- 36 -

1
2
3
2
4
6
3
6
9

intel
4.9 DESCRIPTION OF STATEMENTS
STATEMENTS: DO -- WHILE [rel expr]
MODE: RUN
TYPE: CONTROLThe DO -- WHILE [rel expr] instruction provides a means of "loop control" within an MCS BASIC-52
program. This operation of this statement is similar to the DO -- UNTIL [rel expr] except that allstatements between the DO and the WHILE [rel expr] will be executed as long as the relational
expression following the WHILE statement is true. DO -- WHILE and DO -- UNTIL statements can benested.
EXAMPLES:SIMPLE DO-WHILE

NESTED DO-WHILE - DO-UNTIL>10 DO>20 A=A+1>30 PRINT A>40 WHILE A<4>50 PRINT "DONE">RUN>10 DO : A=A+1 : B=B+1>20 PRINT A,B,A*B>30 WHILE B<>3>40 B=0>50 UNTIL A=3
>RUN

1
2
3
4
DONE

1
1
1
2
2
2
3
3
3

READY
>

1
2
3
1
2
3
1
2
3

READY
>
VARIATIONS:
None- 37 -

1
2
3
2
4
6
3
6
9

intel4.10 DESCRIPTION OF STATEMENTS
STATEMENT: END
MODE: RUN
TYPE: CONTROLThe END STATEMENT terminates program execution. The continue command, CONT will not
operate it the END STATEMENT is used to terminate execution (i.e., a CAN'T CONTINUE ERROR
will be printed to the console). The last statement in an MCS BASIC-52 program will automaticallyterminate program execution if no END STATEMENT is used.
EXAMPLES:LAST STATEMENT TERMINATIONEND STATEMENT TERMINATION>1O FOR I=1 TO 4
>20 PRINT I>30 NEXT I>RUN>1O FOR I=1 TO 4
>20 GOSUB 100>30 NEXT I>40 END>100 PRINT I>110 RETURN
>RUN

1
2
3
4

1
2
3
4

READY
>

READY
>
VARIATIONS:
None- 38 -

intel4.11 DESCRIPTION OF STATEMENTS
STATEMENTS: FOR -- TO -- {STEP} -- NEXT
MODE: RUN VERSION 1.0 (COMMAND AND/OR RUN in Version 1.1)
TYPE: CONTROLThe FOR -- TO -- {STEP}—NEXT STATEMENTS are used to set up and control loops.
EXAMPLE:
10 FOR A=3 TO C STEP D
20 PRINT A
30 NEXT A
If B = 0, C = 10, and D = 2, the PRINT STATEMENT at line 20 will be executed 6 times. The valuesof "A" that will be printed are 0, 2, 4, 6, 8, 10. "A" represents the name of the index or loop counter.
The value of "B" is the starting value of the index, the value of "C" is the limit value of the index, andthe value of "D" is the increment to the index. If the STEP STATEMENT and the value "D" areomitted, the increment value defaults to 1, therefore, STEP is an optional statement. The NEXT
STATEMENT causes the value of "D" to be added to the index. The index is then compared to thevalue of "C," the limit. If the index is less than or equal to the limit, control will be transferred back tothe statement after the FOR STATEMENT. Stepping "backwards" (i.e. FOR I = 100 TO 1 STEP-1) ispermitted in MCS BASIC-52. Unlike some BASICS, the index MAY NOT be omitted from the NEXT
STATEMENT in MCS BASIC-52 (i.e. the NEXT statement MUST always be followed by theappropriate variable).
EXAMPLES:>10 FOR I=1 TO 4
>20 PRINT I>30 NEXT I>RUN>10 FOR I=0 TO 8 STEP 2>20 PRINT I>30 NEXT I
>RUN

1
2
3
4

0
2
4
6
8

READY
>

READY
>

- 39 -

intel4.11 DESCRIPTION OF STATEMENTS
In Version 1.1 of MCS BASIC-52 it is possible execute the FOR-TO-{STEP}-NEXT statement in theCommand Mode. This makes it possible for the user to do things like display regions of memory by
writing a short program like FOR I=512 TO 560: PH0. XBY(I),: NEXT I. It may also have other uses,
but they haven't been thought of.
Also Version 1.1 allows the NEXT statement to be used without a variable following the statement.
This means that programs like:
EXAMPLE:
10 FOR I = 1 TO 100
20 PRINT I
30 NEXT
Are permitted in Version 1.1 of MCS BASIC-52. The variable associated with the NEXT is always
assumed to be the variable used in the last FOR statement.

- 40 -

intel4.12 DESCRIPTION OF STATEMENTS
STATEMENTS: GOSUB[ln num] -- RETURN
MODE: RUN
TYPE: CONTROLGOSUBThe GOSUB [In num] STATEMENT will cause MCS BASIC-52 to transfer control of the program
directly to the line number ([ln num]) following the GOSUB STATEMENT. In addition, the GOSUB
STATEMENT saves the location of the STATEMENT following GOSUB on the control stack so that a
RETURN STATEMENT can be performed to return control.
RETURN
This statement is used to "return" control back to the STATEMENT following the most recently
executed GOSUB STATEMENT. The GOSUB-RETURN sequence can be "nested" meaning that asubroutine called by the GOSUB STATEMENT can call another subroutine with another GOSUB
STATEMENT.
EXAMPLES:SIMPLE SUBROUTINENESTED SUBROUTINES>10 FOR I = 1 TO 5
>20 GOSUB 100>30 NEXT I>100 PRINT I>110 RETURN>RUN>10 FOR I = 1 TO 3
>20 GOSUB 100>30 NEXT I>40 END>100 PRINT I,>110 GOSUB 200>120 RETURN>200 PRINT I*I>210 RETURN
>RUN

1
2
3
4
5
READY
>

1 1
2 4
3 9
READY
>

- 41 -

intel4.12 DESCRIPTION OF STATEMENTSNOTE -- The Control Stack on Version 1.1 permits a graceful exit from incompleted control loops,
given the following example:
EXAMPLE:.
.

50

GOSUB 1000.
.

1000 FOR I = 1 TO 101010 IF X = I THEN 1040
1020 PRINT I*X
1030 NEXT I
1040 RETURN
Version 1.1 would permit the programmer to exit the subroutine even though the FOR-NEXT loop
might not be allowed to complete if X did equal 1. Version 1.0 of MCS BASIC-52 would yield a CSTACK error if the FOR-NEXT loop was not allowed to complete before the RETURN statement was
executed.

- 42 -

intel4.13 DESCRIPTION OF STATEMENTS
STATEMENT: GOTO [ln num]
MODE: COMMAND AND/OR RUN
TYPE: CONTROLThe GOTO [ln num] STATEMENT will cause BASIC to transfer control directly to the line number([ln num]) following the GOTO STATEMENT.
EXAMPLE:
50 GOTO 100
Will, if line 100 exists, cause execution of the program to resume at line 100. If line number 100 does
not exist the message ERROR: INVALID LINE NUMBER will be printed to the console device.
Unlike the RUN COMMAND the GOTO STATEMENT, if executed in the COMMAND MODE, does
not CLEAR the variable storage space or interrupts. However, if the GOTO STATEMENT is executedin the COMMAND MODE after a line has been edited, MCS BASIC-52 will CLEAR the variable
storage space and all BASIC evoked interrupts. This is a necessity because the variable storage and theBASIC program reside in the same RAM memory. So editing a program can destroy variables.
NOTE -- (Version 1.0 only)
Because of the way MCS BASlC-52's text interpreter processes a line, when an INVALID LINENUMBER ERROR occurs on the GOTO, GOSUB, ON GOTO, and ON GOSUB STATEMENTS theline AFTER the GOTO or GOSUB STATEMENT will be printed out in the error message. This may be
confusing, but it was a trade-off between execution speed, code size, and error handling. Error handling
lost.
EXAMPLE:>10 GOTO l00>20 PRINT X>RUN
ERROR INVALID LINE NUMER - IN LINE 20
20 PRINT X----------x
Version 1.1 does not exhibit this particular anomaly.

- 43 -

intel4.14 DESCRIPTION OF STATEMENTS
STATEMENTS: ON [expr] GOTO[ln num], [ln num], . . . [ln num]ON [expr] GOSUB[ln num], [ln num], . . . [ln num]
MODE: RUN
TYPE: CONTROLThe value of the expression following the ON statement is the number in the line list that control will betransferred to.
EXAMPLE:
10 ON Q GOTO 100,200,300
If Q was equal to 0, control would be transferred to line number 100. If Q was equal to 1, control would
be transferred to line number 200. If Q was equal to 2, GOTO line 300, etc. All comments that apply to
GOTO and GOSUB apply to the ON STATEMENT. If Q is less than ZERO a BAD ARGUMENT
ERROR will be generated. If Q is greater than the line number list following the GOTO or GOSUB
STATEMENT, a BAD SYNTAX ERROR will be generated. The ON STATEMENT provides"conditional branching" options within the constructs of an MCS BASIC-52 program.

- 44 -

intel4.15 DESCRIPTION OF STATEMENTS
STATEMENTS: IF -- THEN -- ELSE
MODE: RUN
TYPE: CONTROLThe IF statement sets up a conditional test. The generalized form of the IF -- THEN -- ELSE statement
is as follows:[ln num] IF [rel expr] THEN valid STATEMENT ELSE valid STATEMENT
A specific example is as follows:>10 IF A=100 THEN A=0 ELSE A=A+1
Upon execution of line 10 IF A is equal to 100, THEN A would be assigned a value of 0. IF A does notequal 100, A would be assigned a value of A + 1 . If it is desired to transfer control to different line
numbers using the IF statement, the GOTO statement may be omitted. The following examples would
yield the same results:>20 IF INT(A)< 10 THEN GOTO 100 ELSE GOTO 200>20 IF INT(A)< 10 THEN 100 ELSE 200
Additionally, the THEN statement can be replaced by any valid MCS BASIC-52 statement, as shown
below:>30 IF A <> 10 THEN PRINT A ELSE 10>30 IF A <> 10 PRINT A ELSE 10
The ELSE statement may be omitted. If it is, control will pass to the next statement.
EXAMPLE:>20 IF A=10 THEN 40>30 PRINT A
In this example. IF A equals 10 then control would be passed to line number 40. If A does not equal 10
line number 30 would be executed.

- 45 -

intel4.15 DESCRIPTION OF STATEMENTSCOMMENTS ON IF-THEN-ELSEVersion 1.1 is not compatible with V1.0 when the IF _ THEN _ ELSE STATEMENT is used withmultiple statements per line. In V1.0, the following two examples would function in the same manner.
EXAMPLE 1:
10 IF A=B THEN C=A : A=A/2 : GOTO 100
20 PRINT
EXAMPLE 2:
10 IF A=B THEN C=A
12 A=A/2
14 GOTO 100
20 PRINT
They function in the same manner because V1.0 treats the delimiter (:) exactly the same as a carriage
return (cr) in every case. However, V1.1 executes the remainder of the line if and only if the test A = B
proves to be true. This means in EXAMPLE 1 IF A did equal B, V1.1 would then set C=A, then set A =A/2, then execute line 100. IF A did not equal B, V1.1 would then PRINT A and ignore the statements
C=A: A=A/2: GOTO 100. V1.1 will execute EXAMPLE 2 exactly the same way as V1.0. This same
logical interpretation holds true for the ELSE statement as well. This example dictates a simple rule formaintaining IF _ THEN FLSE compatibility between the two versions. IF THE DELIMITER (:) IS
NOT USED IN AN IF_THEN ELSE STATEMENT, V1.0 AND V1.1 WILL TREAT THE
STATEMENTS IN THE SAME MANNER!!
This change was made because most users of MCS BASIC-52 felt that the V1.1 interpretation of thisstatement was more useful because fewer GOTO statements need be employed in a typical program.

- 46 -

intel4.16 DESCRIPTION OF STATEMENTS
STATEMENTS: INPUT
MODE: RUN
TYPE: INPUT/OUTPUT
The INPUT statement allows users to enter data from the console during program execution. One or morevariables may be assigned data with a single input statement. The variables must be separated by a comma.
EXAMPLE:INPUT A,B
Would cause the printing of a question mark (?) on the console device as a prompt to the operator to input
two numbers separated by a comma. If the operator does not enter enough data, then MCS BASIC- 52
responds by outputting the message TRY AGAIN to the console device.
EXAMPLE:>10 INPUT A,B>20 PRINT A,B>RUN?1
TRY AGAIN
?1,2
1 2
READY
The INPUT statement may be written so that a descriptive prompt is printed to tell the user what totype. The message to be printed is placed in quotes after the INPUT statement. If a comma appears
before the first variable on the input list, the question mark prompt character will not be displayed.
EXAMPLES:>10 INPUT"ENTER A NUMBER,"A>20 PRINT SQR(A)>RUN>10 INPUT"ENTER A NUMBER",A>20 PRINT SQR(A)>RUNENTER A NUMBER?100
10ENTER A NUMBER-100
10- 47 -

intel4.16 DESCRIPTION OF STATEMENTS
Strings can also be assigned with an INPUT statement. Strings are always terminated with a carriage return(cr). So, if more than one string input is requested with a single INPUT statement, MCS BASIC- 52 willprompt the user with a question mark.
EXAMPLES:>10 STRING 110,10>20 INPUT "NAME: "$(1)>30 PRINT "HI ",$(1)>RUN>10 STRING 110,10>20 INPUT "NAMES: ",$(1),$(2)>30 PRINT "HI ",$(1)," AND ",$(2)>RUN

NAME; SUSAN
HI SUSANNAMES BILL
?ANN
HI BILL AND ANN

READY

READY

Additionally, strings and variables can be assigned with a single INPUT statement.
EXAMPLE:>10 STRING 100,10>20 INPUT"NAME(CR), AGE - ",$(1),A>30 PRINT "HELLO ",$(1),", YOU ARE " ,A, "YEARS OLD"
>RUN
NAME(CR), AGE - FRED
?15
HELLO FRED. YOU ARE 15 YEARS OLD
READY>- 48 -

intel4.17 DESCRIPTION OF STATEMENTS
STATEMENT: LET
MODE: COMMAND AND/OR RUN
TYPE: ASSIGNMENT
The LET statement is used to assign a variable to the value of an expression. The generalized form of
LET is:LET [var] = [expr]
EXAMPLES:LET A = 10*SIN(B)/100 or
LET A = A + 1
Note that the = sign used in the LET statement is not equality operator, but rather a "replacement"
operator and that the statement should be read A is replaced by A plus one. THE WORD LET IS
ALWAYS OPTIONAL, i.e.
LET A = 2 is the same as A = 2
When LET is omitted the LET statement is called an IMPLIED LET. This document will use the word
LET to refer to both the LET statement and the IMPLIED LET statement.
The LET statement is also used to assign the string variables, i.e:
LET $(1)="THIS IS A STRING" or
LET $(2)=$(1)Before Strings can be assigned the STRING [expr], [expr] STATEMENT MUST be executed, or else a
MEMORY ALLOCATION ERROR will occur.
SPECIAL FUNCTION VALUES can also be assigned by the LET statement. i.e.:
LET IE = 82H or
LET XBYTE(2000H)=5AH or
LET DBYTE(25)=XBYTE(1000)- 49 -

intel4.18 DESCRIPTION OF STATEMENTS
STATEMENT: ONERR[ln num]
MODE: RUN
TYPE: CONTROLThe ONERR[ln num] statement lets the programmer handle arithmetic errors, should they occur, during
program execution. Only ARITH. OVERFLOW, ARITH. UNDERFLOW, DIVIDE BY ZERO, andBAD ARGUMENT errors can be "trapped" by the ONERR statement, all other errors are not. If anarithmetic error occurs after the ONERR statement is executed, the MCS BASIC-52 interpreter willpass control to the line number following the ONERR[ln num] statement. The programmer can handle
the error condition in any manner suitable to the particular application. Typically, the ONERR[ln num]
statement should be viewed as an easy way to handle errors that occur when the user provides
inappropriate data to an INPUT statement.
With the ONERR[ln num] statement, the programmer has the option of determining what type of error
occurred. This is done by examining external memory location 257 (101H) after the error condition istrapped. The error codes are as follows:
ERROR CODE = 10 - DIVIDE BY ZEROERROR CODE = 20 - ARITH. OVERFLOWERROR CODE = 30 - ARITH. UNDERFLOWERROR CODE = 40 - BAD ARGUMENT
This location may be examined by using an XBY(257) statement.

- 50 -

intel4.19 DESCRIPTION OF STATEMENTS
STATEMENT: ONEX1 [ln num]
MODE: RUN
TYPE: CONTROLThe ONEX1 [ln num] statement lets the user handle interrupts on the 8052AH's INT1 pin with a BASIC
program. The line number following the ONEX1 statement tells the MCS BASIC-52 interpreter whichline to pass control to when an interrupt occurs. In essence, the ONEX1 statement "forces" a GOSUB to
the line number following the ONEX1 statement when the INT1 pin on the 8052AH is pulled low. The
programmer must execute a RETI statement to exit from the ONEX1 interrupt routine. If this is notdone all future interrupts on the INT1 pin will be "locked out" and ignored until a RETI is executed.
The ONEX1 statement sets bits 7 and 2 of the 8052AH's interrupt enable register IE. Before an interrupt
can be processed, the MCS BASIC-52 interpreter must complete execution of the statement it iscurrently processing. Because of this, interrupt latency can vary from microseconds to tens of
milliseconds. The ONTIME [expr], [ln num] interrupt has priority over the ONEX1 interrupt. So, the
ONTIME interrupt can interrupt the ONEX1 interrupt routine.

- 51 -

intel4.20 DESCRIPTION OF STATEMENTS
STATEMENT: ONTIME [expr], [ln num]
MODE: RUN
TYPE: CONTROL
Since MCS BASIC-52 processes a line in the millisecond time frame and the timer/counters on the
8052AH operate in the micro-second time frame, there is an inherent incompatibility between thetimer/counters on the 8052AH and MCS BASIC-52. To help solve this situation the ONTIME [expr],[ln num] statement was devised. What ONTIME does is generate an interrupt every time the SPECIALFUNCTION OPERATOR, TIME, is equal to or greater than the expression following the ONTIME
statement. Actually, only the integer portion of TIME is compared to the integer portion of theexpression. The interrupt forces a GOSUB to the line number ([ln num]) following the expression([expr]) in the ONTIME statement.
Since the ONTIME statement uses the SPECIAL FUNCTION OPERATOR, TIME, the CLOCK1
statement must be executed in order for ONTIME to operate. If CLOCK1 is not executed the SPECIALFUNCTION OPERATOR, TIME, will never increment and not much will happen.
Since the ONTIME statement generates an interrupt when TIME is greater than or equal to theexpression following the ONTIME statement, how can periodic interrupts be generated? That's easy,
the ONTIME statement must be executed again in the interrupt routine:
EXAMPLE:>10 TIME=0 : CLOCK1 : ONTIME 2,100 : DO
>20 WHILE TIME<10 : END>100 PRINT "TIMER INTERRUPT AT -",TIME,"SECONDS">110 ONTIME TIME+2,100 : RETI>RUNTIMER INTERRUPT AT - 2.045 SECONDSTIMER INTERRUPT AT - 4.045 SECONDSTIMER INTERRUPT AT - 6.045 SECONDSTIMER INTERRUPT AT - 8.045 SECONDSTIMER INTERRUPT AT - 10.045 SECONDS
READY
You may wonder why the TIME that was printed out was 45 milliseconds greater than the time that theinterrupt was supposed to be generated. That's because the terminal used in this example was running at
4800 BAUD and it takes about 45 milliseconds to print the message TIMER INTERRUPT AT -" ".

- 52 -

intel4.20 DESCRIPTION OF STATEMENTS
If the programmer does not want this delay, a variable should be assigned to the SPECIAL FUNCTION
OPERATOR, TIME, at the beginning of the interrupt routine.
EXAMPLE:>10 TIME=0 : CLOCK1 : ONTIME 2,100 DO>20 WHILE TIME<10 : END>100 A=TIME>110 PRINT "TIMER INTERRUPT AT -",A,"SECONDS">120 ONTIME A+2,100 : RETI>RUNTIMER INTERRUPT AT - 2 SECONDSTIMER INTERRUPT AT - 4 SECONDSTIMER INTERRUPT AT - 6 SECONDSTIMER INTERRUPT AT - 8 SECONDSTIMER INTERRUPT AT - 10 SECONDS
READY
Like the ONEX1 statement, the ONTIME interrupt routine must be exited with a RETI statement.
Failure to do this will "lock-out" all future interrupts.
The ONTIME interrupt has priority over the ONEX1 interrupt. This means that the ONTIME interrupt
can interrupt the ONEX1 interrupt routine. This priority was established because time related functions
in control applications were viewed as critical routines. If the user does not want the ONEX1 routine to
be interrupted by the ONTIME interrupt, a CLOCK0 or a CLEARI statement should be executed at the
beginning of the ONEX1 routine. The interrupts would have to be re-enabled before the end of the
ONEX1 routine. The ONEX1 interrupt cannot interrupt an ONTIME routine.
The ONTIME statement in MCS BASIC-52 is unique, relative to most BASICS. This powerfulstatement eliminates the need for the user to "test" the value of the TIME operator periodicallythroughout the BASIC program.

- 53 -

intel4.21 DESCRIPTION OF STATEMENTS
STATEMENT: PRINT or P. (? VERSION 1.1 ONLY)
MODE: COMMAND and/or RUN
TYPE: INPUT/OUTPUT
The PRINT statement directs MCS BASIC-52 to output to the console device. The value of
expressions, strings, literal values, variables or test strings may be printed out. The various forms may
be combined in the print list by separating them with commas. If the list is terminated with- a comma,
the carriage return/line feed will be suppressed. P. is a "shorthand" notation for PRINT. In Version 1.1 ?
is also "shorthand" notation for PRINT.
EXAMPLES:>PRINT 10*10,3*3
100 9>PRINT "MCS-51"
MCS-51>PRINT 5,1E3
5 1000

Values are printed next to one another with two intervening blanks. A PRINT statement with noarguments causes a carriage return/line feed sequence to be sent to the console device.
SPECIAL PRINT FORMATTING STATEMENTS
TAB([expr])
The TAB([expr]) function is used in the PRINT statement to cause data to be printed out in exact
locations on the output device. TAB([expr]) tells MCS BASIC-52 which position to begin printing thenext value in the print list. If the printhead or cursor is on or beyond the specified TAB position, MCS
BASIC-52 will ignore the TAB function.
EXAMPLE:>PRINT TAB(5),"X",TAB(10),"Y"X
Y
SPC([expr])
The SPC([expr]) function is used in the PRINT statement to cause MCS BASIC-52 to output the
number of spaces in the SPC argument.
EXAMPLE:>PRINT A,SPC(5),B
may be used to place an additional 5 spaces between the A and B over and above the two that would
normally be printed.

- 54 -

intel4.21 DESCRIPTION OF STATEMENTSCR
The CR function is interesting and unique to MCS BASIC-52. When CR is used in a PRINT statement
it will force a carriage return, but no line feed. This can be used to create one line on a CRT device thatis repeatedly updated.
EXAMPLE:>10 FOR I=1 TO 1000
>20 PRINT I,CR,>30 NEXT I
will cause the output to remain only on one line. No line feed will ever be sent to the console device.
USING(special characters)
The USING function is used to tell MCS BASIC-52 what format to display the values that are printed.
MCS BASIC-52 "stores" the desired format after the USING statement is executed. So, all outputs
following a USING statement will be in the format evoked by the last USING statement executed. The
USING statement need not be executed within every PRINT statement unless the programmer wants tochange the format. U. is a "shorthand" notation for USING. The options for USING are as follows:
USING(Fx) -- This will force MCS BASIC-52 to output all numbers using the floating point format.
The value of x determines how many significant digits will be printed. If x equals 0.
MCS BASIC-52 will not output any trailing zeros, so the number of digits will varydepending upon the number. MCS BASIC-52 will always output at least 3 significant
digits even if x is 1 or 2. The maximum value for x is 8.
EXAMPLE:>10 PRINT USING(F3),1,2,3>20 PRINT USING(F4),1.2,3>30 PRINT USING(F5),1,2,3>40 FOR I=10 TO 40 STEP 10>50 PRINT I>60 NEXT I>RUN
1.00 E 0 2.00 E 0 3.00 E 01.000 E 0 2.000 E 0 3.000 E 01.0000 E 0 2.0000 E 0 3.0000 E 01.0000 E+1
2.0000 E+1
3.0000 E+1
4.0000 E+1
READY- 55 -

intel4.21 DESCRIPTION OF STATEMENTS
USING(#.#) -- This will force MCS BASIC-52 to output all numbers using an integer and/or fraction
format. The number of "#" 's before the decimal point represents the number ofsignificant integer digits that will be printed in the fraction. The decimal point may beomitted, in which case only integers will be printed. USING may be abbreviated U. .
USING (###.###), USING(######) and USING(######.##) are all valid in MCS
BASIC-52. The maximum number of "#" characters is 8. If MCS BASIC-52 cannot
output the value in the desired format (usually because the value is too large) a question
mark (?) will be printed to console device, then BASIC will output the number in the
FREE FORMAT described below.
EXAMPLE:>10 PRINT USING(##.##),1,2,3>20 FOR I=1 TO 120 STEP 20>30 PRINT I>40 NEXT I
>RUN
1.00
1.00
21.00
41.00
61.00
81.00
? 101

2.00

3.00

READY
NOTE: The USlNG(Fx) and the USING(#.#) formats will always "align" the decimal points whenprinting a number. This feature makes displayed columns of numbers easy to read.
USING(0) -- This argument lets MCS BASIC-52 determine what format to use. The rules are simple, if
the number is between +- 99999999 and +- .1, BASIC will display integers and fractions.
If it is out of this range, BASIC will use the USING(F0) format. Leading and trailingzeros will always be suppressed. After reset, MCS BASIC-52 is placed in the USING(0)format.

- 56 -

intel4.22 DESCRIPTION OF STATEMENTS
STATEMENT: PRINT# or P.# (?# VERSION 1.1 ONLY)
MODE: COMMAND and/or RUN
TYPE: INPUT/OUTPUT
The PRINT#, P.#, and ?# (in Version 1.1 only) statement does the same thing as the PRINT, P. and ?
(in Version 1.1 only) statement except that the output is directed to the list device instead of the console
device. The BAUD rate to the list device must be initialized by the STATEMENT -- BAUD[expr]
before the PRINT#, P.#, or, ?# statement is used. All comments that apply to the PRINT, P. or, ?
statement apply to the PRINT#, P.#, or ? statement. P.# and ?# (in Version 1.1 only) are "shorthand"notations for PRINT#.

- 57 -

intel4.23 DESCRIPTION OF STATEMENTS
STATEMENTS: PH0., PH1., PH0.#, PH1.#
MODE: COMMAND and/or RUN
TYPE: INPUT/OUTPUT
The PH0. and PH 1. statements do the same thing as the PRINT statement except that the values are
printed out in a hexadecimal format. The PH0. statement suppresses two leading zeros if the number tobe printed is less than 255 (0FFH). The PH1. statement always prints out four hexadecimal digits. The
character "H" is always printed after the number when PH0. or PH1. is used to direct an output. The
values printed are always truncated integers. If the number to be printed is not within the range of valid
integer (i.e. between 0 and 65535 (0FFFFH) inclusive), MCS BASIC-52 will default to the normal
mode of print. If this happens no "H" will be printed out after the value. Since integers can be entered ineither decimal or hexadecimal form the statements PRINT, PH0., and PH1. can be used to perform
decimal to hexadecimal and hexadecimal to decimal conversion. All comments that apply to the PRINT
statement apply to the PH0. and PH1. statements. PH0.# and PH1.# do the same thing as PH0. and PH1.
respectively, except that the output is directed to the list device instead of the console device.
EXAMPLES:
>PH0.
04H

2*2

>PH1. 2*20004H

>PRINT 99H
153>PH0. 100
64H

>PH0.
3E8H

1000

>PH1. 1000
03E8H

>P. 3E8H
1000>PH0. PI
03H

- 58 -

intel4.24 DESCRIPTION OF STATEMENTS
STATEMENT: PRINT@, PH0.@, PH1.@ (VERSION 1.1 ONLY)
MODE: COMMAND AND/OR RUN
TYPE: INPUT/OUTPUT
The PRINT@ (P.@ OR ?@), PH0.@, and PH1.@ statements do the same thing as the PRINT (P.@ or?@), PH0., and PH1. statements respectively except that the output is directed to a user defined output
driver. These statements assume that the user has placed an assembly language output routine inexternal code memory location 403CH. To enable the @ driver routine the user must SET BIT 27H(39D) in the internal memory of the MCS BASIC-52 device. BIT 27H (39D) is BIT 7 of internal
memory location 24H (36D). This BIT can be set by the BASIC statement DBY(24H) =DBY(24H).OR.80H or by a user supplied assembly language routine. If the user evokes the @ driver
routine and this bit is not set, the output will be directed to the console driver. The only reason this BIT
must be set to enable the @ driver is that it adds a certain degree of protection from accidentally typing
LIST@ when no assembly language routine exist. The philosophy here is that if the user sets the bit, theuser provides the driver or else!!!
When MCS BASIC-52 calls the user output driver routine at location 403CH, the byte to output is in
the accumulator and R5 of register bank 0 (RB0). The user may modify the accumulator (A) and thedata pointer (DPTR) in the assembly language output routine, but cannot modify any of the registers in
RB0. This is intended to make it real easy for the user to implement a parallel or serial output driver
without having to do a PUSH or a POP.

- 59 -

intel4.25 DESCRIPTION OF STATEMENTS
STATEMENT: PUSH[expr]
MODE: COMMAND AND / OR RUN
TYPE: ASSIGNMENT
The arithmetic expression, or expressions following the PUSH statement are evaluated and thensequentially placed on MCS BASIC-52's ARGUMENT STACK. This statement, in conjunction with
the POP statement provide a simple means of passing parameters to assembly language routines. In
addition, the PUSH and POP statements can be used to pass parameters to BASIC subroutines and to"SWAP" variables. The last value PUSHED onto the ARGUMENT STACK will be the first value
POPPED off the ARGUMENT STACK.
VARIATIONS:
More than one expression can be pushed onto the ARGUMENT stack with a single PUSH statement.
The expressions are simply followed by a comma: PUSH[expr],[expr],..[expr]. The last value PUSHED
onto the ARGUMENT STACK will be the last expression [expr] encountered in the PUSH
STATEMENT.
EXAMPLES:
SWAPPING
VARIABLESSUBROUTINEPASSING>10 A=10>20 B=20>30 PRINT A,C>40 PUSH A,C>50 POP A,B>60 PRINT A,B>RUN>10 PUSH 1,3,2>20 GOSUB 100>3O POP R1,R2>40 PRINT R1,R2>5O END>100 REM QUADRATIC A=2,B=3,C=1 IN EXAMPLE>110 POP A,B,C>120 PUSH (-B+SQR(B*B-4*A*C))/(2*A)>130 PUSH (-B-SOR(B*B-4*A*C))/(2*A)>140 RETURN
>RUN

10
20

20
10

READY
>

-1 -.5READY>- 60 -

intel4.26 DESCRIPTION OF STATEMENTS
STATEMENT: POP[var]MODE: COMMAND AND / OR RUN
TYPE: ASSIGNMENTThe top of the ARGUMENT STACK is assigned to the variable following the POP statement and the
ARGUMENT STACK is "-POPPED" (i.e. incremented by 6). Values can be placed on the stack byeither the PUSH statement or by assembly language CALLS. NOTE -- If a POP statement is executedand no number is on the ARGUMENT STACK, an A-STACK ERROR will occur.
VARIATIONS:
More than one variable can be popped off the ARGUMENT stack with a single POP statement. The
variables are simply followed by a comma (i.e. POP [var],[var], ..[var]).
EXAMPLES:
See PUSH statement.
COMMENT:The PUSH and POP statements are unique to MCS BASIC-52. These powerful statements can be used
to "get around" the GLOBAL variable problems so often encountered in BASIC PROGRAMS. This
problem arises because in BASIC the "main" program and all subroutines used by the main program are
required to use the same variable names (i.e. GLOBAL VARIABLES). It is not always convenient to
use the same variables in a subroutine as in the main program and you often see programs re-assign a
number of variables (i.e. A=Q) before a GOSUB STATEMENT is executed. If the user reserves somevariable names JUST for subroutines (i.e. S1, S2) and passes variables on the stack as shown in theprevious example, you will avoid any GLOBAL variable problems in MCS BASIC-52.

- 61 -

intel4.27 DESCRIPTION OF STATEMENTS
STATEMENT: PWM [expr], [expr], [expr]
MODE: COMMAND and/or RUN
TYPE: INPUT/OUTPUT
PWM stands for PULSE WIDTH MODULATION. What it does is generate a user defined pulse
sequence on P1.2 (bit 2 of I/O PORT 1) of the MCS BASIC-52 device. The first expression following
the PWM statement is the number of clock cycles the pulse will remain high. A clock cycle is equal to
12/XTAL, which is 1.085 microseconds at 11.0592 MHz. The second expression is the number of clock
cycles the pulse will remain low and the third expression is the total number of cycles the user wishes to
output. All expressions in the PWM statement must be valid integers (i.e. between 0 and 65535(0FFFFH) inclusive). Additionally, the minimum value for the first two expressions in the PWM
statement is 25.
The PWM statement can be used to create "audiable" feedback in a system. In addition, just for fun, theprogrammer can play music using the PWM statement. More details about using the PWM statement
are in the appendix.
EXAMPLE:>PWM 100,100,1000
At 11.0592 MHz would generate 1000 cycles of a square wave that has a period of 217 microseconds(4608 Hz) on P1.2.

- 62 -

intel4.28 DESCRIPTION OF STATEMENTS
STATEMENT: REM
MODE: RUN (Version 1.0) COMMAND AND/OR RUN (Version 1.1)
TYPE: CONTROL -- PERFORMS NO OPERATION
REM is short for REMark. It does nothing, but allows the user to add comments to a program.
Comments are usually needed to make a program a little easier to understand. Once a REM statement
appears on a line the entire line is assumed to be a remark, so a REM statement may not be terminatedby a colon (:) however, it may be placed after a colon. This can be used to allow the programmer toplace a comment on each line.
EXAMPLES:>10 REM INPUT ONE VARIABLE>20 INPUT A>30 REM INPUT ANOTHER VARIA3LE>40 INPUT B>50 REM MULTIPLY THE TWO>60 Z=A*B>70 REM PRINT THE ANSWER>80 PRINT Z>10 INPUT A : REM INPUT ONE VARIABLE>20 INPUT B : REM INPUT ANOTHER VARIABLE>30 Z=A*B : REM MULTIPLY THE TWO>40 PRINT Z : REM PRINT THE ANSWER
The following will NOT work because the entire line would be interpreted as a REMark, so the PRINT
statement would not be executed:>10 REM PRINT THE NUMBER : PRINT ANOTE -- The reason the REM statement was made executable in the command mode in Version 1.1 of
MCS BASIC-52 is that if the user is employing some type of UPLOAD/DOWNLOAD routine with acomputer, this lets the user insert REM statements, without line numbers in the text and not download
them to the MCS BASIC-52 device. This helps to conserve memory.

- 63 -

intel4.29 DESCRIPTION OF STATEMENTS
STATEMENT: RETI
MODE: RUN
TYPE: CONTROLThe RETI statement is used to exit from interrupts that are handled by an MCS BASIC-52 program.
Specifically, the ONTIME and the ONEX1 interrupts. The RETI statement does the same thing as the
RETURN statement except that it also clears a software interrupt flags so interrupts can again be
acknowledged. If the user fails to execute the RETI statement in the interrupt procedure, all futureinterrupts will be ignored.

- 64 -

intel4.30 DESCRIPTION OF STATEMENTS
STATEMENT: STOP
MODE: RUN
TYPE: CONTROLThe STOP statement allows the programmer to break program execution at specific points in a
program. After a program is STOPped variables can be displayed and/or modified. Program execution
may be resumed with a CONTinue command. The purpose of the STOP statement is to allow for easy
program "debugging." More details of the STOP-CONT sequence are covered in the DESCRIPTION
OF COM- MAND -- CONT section of this manual.
EXAMPLE:>10 FOR I=1 TO 100>20 PRINT I>30 STOP>40 NEXT I>RUN
1STOP - IN LINE 40
READY
>CONT
2
Note that the line number printed out after the STOP statement is executed is the line number following
the STOP statement, NOT the line number that contains the STOP statement!!!

- 65 -

intel4.31 DESCRIPTION OF STATEMENTS
STATEMENT: STRING [expr], [expr]
MODE: COMMAND and/or RUN
TYPE: CONTROLThe STRING [expr],[expr] statement allocates memory for strings. Initially, no memory is allocated forstrings. If the user attempts to define a string with a statement such as LET $(1)="HELLO" before
memory has been allocated for strings, a MEMORY ALLOCATION ERROR will be generated. The
first expression in the STRING [expr],[expr] statement is the total number of bytes the user wishes toallocate for string storage. The second expression denotes the maximum number of bytes that are ineach string. These two numbers determine the total number of defined string variables.
You might think that the total number of defined strings would be equal to the first expression in the
STRING [expr],[expr] statement divided by the second expression. Ha,ha, do not be so presumptuous.
MCS BASIC-52 requires one additional byte for each string, plus one additional byte overall. Thismeans that the statement STRING 100,10 would allocate enough memory for 9 string variables, ranging
from $(0) to $(8) and all of the 100 allocated bytes would be used. Note that $(0) is a valid string in
MCS BASIC-52.
After memory is allocated for string storage, neither commands, such as NEW nor statements, such as
CLEAR, will "de-allocate" this memory. The only way memory can be de-allocated is to execute a
STRING 0,0 statement. STRING 0,0 will allocate no memory to string variables.
IMPORTANT NOTE
Every time the STRING [expr],[expr] statement is executed, MCS BASIC-52 executes the equivalent of
a CLEAR statement. This is a necessity because string variables and numeric variables occupy the same
external memory space. So, after the STRING statement is executed, all variables are "wiped-out."
Because of this, string memory allocation should be performed early in a program (like the firststatement or so) and string memory should never be "re-allocated" unless the programmer is willing todestroy all defined variables.

- 66 -

intel4.32 DESCRIPTION OF STATEMENTS
STATEMENTS: UI1 and UI0 (USER INPUT)
MODE: COMMAND and/or RUN
TYPE: CONTROLUI1The UI1 statement permits the user to write specific console input drivers for MCS BASIC-52. AfterUI1 is executed BASIC will call external program memory location 4033H when a console input isrequested. The user must provide a JUMP instruction to an ASSEMBLY LANGUAGE INPUTROUTINE at this location. The appropriate ASCII input from this routine is placed in the 8052AH's
accumulator and the user input routine returns back to BASIC by executing an ASSEMBLYLANGUAGE RET instruction. The user must NOT modify any of the 8052AH's registers in theassembly language program with the exception of the MEMORY and REGISTER BANK allocated to
the USER. THE ASSEMBLY LAN- GUAGE LINKAGE section of this manual explains what memory
MCS BASIC-52 allocates to the user and how the user may allocate additional memory if needed.
In addition to providing the INPUT driver routine for the UI1 statement, the user must also provide a
CONSOLE STATUS CHECK routine. This routine checks to see if the CONSOLE DEVICE has acharacter ready for MCS BASIC-52 to read. BASIC CALLS external memory location 4036H to check
the CONSOLE STATUS. The CONSOLE STATUS ROUTINE sets the CARRY BIT to 1 (C = 1) if acharacter is ready for BASIC to read and CLEARS the CARRY BIT (C = 0) if no character is ready.
Again, the contents of the REGISTERS must not be changed. MCS BASIC-52 uses the CONSOLESTATUS CHECK routine to examine the keyboard for a control-C character during program execution
and during a program LISTING. This routine is also used to perform the GET operation.
UI0
The UI0 statement assigns the console input console routine back to the software drivers resident on the
MCS BASIC-52 device. UI0 and UI1 may be placed anywhere within a program. This allows theBASIC program to accept inputs from different devices at different times.
NOTE: The UI0 and UI1 function is controlled by BIT 30 (IEH) in the 8052AH's internal memory. BIT
30 is in internal memory location 35.6 (23.6H) i.e. the sixth bit in internal memory location 35 (23H).
When BIT 30 is SET (BIT 30 = 1), the user routine will be called. When BIT 30 is CLEARED (BIT 30= 0), the MCS BASIC-52 input driver routine will be used. The assembly language programmer can use
this information to change the input device selection in assembly language.

- 67 -

intel4.33 DESCRIPTION OF STATEMENTS
STATEMENTS: UO1 and UO0 (USER OUTPUT)
MODE: COMMAND AND/OR RUN
TYPE: CONTROLUO1The UO1 STATEMENT permits the user to write specific console output drivers for MCS BASIC-52.
After UO1 is executed BASIC will call external program memory location 4030H when a console
output is requested. The user must provide a JUMP instruction to an ASSEMBLY LANGUAGEOUTPUT ROUTINE at this location. MCS BASIC-52 places the output character in REGISTER 5 (R5)of REGISTER BANK 0 (RB0). The user returns back to BASIC executing an assembly language RET
instruction. The user must NOT modify any of the 8052AH's REGISTERS, including the
ACCUMULATOR during the user output procedure with the exception of the MEMORY andREGISTER BANK allocated to the user. UO1 gives the user the freedom to write custom output
routines for MCS BASIC-52.
UO0
UO0 STATEMENT assigns the console output routine back to the software drivers resident on the
MCS BASIC-52 device. UO0 and UO1 may be placed anywhere within a program. This allows theBASIC program to output characters to different devices at different times.
NOTE: The UO0 and UO1 function is controlled by BIT 28 (1CH) in the 8052AH's internal memory.
BIT 28 is in the internal memory location 35.4 (23.4H), i.e. the fourth bit in the internal memory
location 35 (28H). When BIT 28 is SET (BIT 28 = 1), the user routines will be called. When BIT 28 iscleared, (BIT 28 = 0), the MCS BASIC-52 output drivers will be used. The assembly language
programmer can use this information to change the output device selection in assembly language.

- 68 -

intel4.34 DESCRIPTION OF STATEMENTS
STATEMENT: IDLE (VERSION 1.1 ONLY)
MODE: RUN
TYPE: CONTROLThe IDLE statement forces the MCS BASIC-52 device into a "wait until interrupt mode." Execution ofstatements is halted until either an ONTIME [expr], [ln num] or an ONEX1 [ln num] interrupt isreceived. The user must make sure that one or both of these interrupts have been enabled before
executing the IDLE instruction or else the MCS BASIC-52 device will enter a "wait forever mode" andfor all practical purposes the system will have crashed.
When an ONTIME [expr], [ln num] or an ONEX1 [ln num] is received while in the IDLE mode, the
MCS BASIC-52 device will execute the interrupt routine, then execute the statement following the
IDLE instruction. Hence, the execution of the IDLE instruction is terminated when an interrupt isreceived.
While in the IDLE mode, the MCS BASIC-52 device asserts the /DMA ACKNOWLEDGE pin (PORT
1, BIT 6 = 0) to indicate that the IDLE instruction is active and that no external bus activity will occur.
This PIN is physically pin 7 on the MCS BASIC-52 device. When the MCS BASIC-52 device exits
from the IDLE mode, this pin is placed back into the logically 1 (non-active) state.
The user may also exit from the IDLE mode with an assembly language interrupt routine. This isaccom- plished by setting BIT 33 (21H) (which is in Bit addressable RAM location 36.1) whenreturning from the assembly language interrupt routine. If this bit is not set by the user, the MCS
BASIC-52 device will remain in the IDLE mode when the user assembly language routine returns toBASIC.
An attempt to execute the IDLE statement in the direct mode will yield a BAD SYNTAX ERROR.

- 69 -

intel4.35 DESCRIPTION OF STATEMENTS
STATEMENT: RROM [integer] (VERSION 1.1 ONLY)
MODE: COMMAND AND/OR RUN
TYPE: CONTROLRROM stands for RUN ROM. What it does is select a program in the EPROM file, then execute theprogram. The integer after the RROM statement selects what program in the EPROM file is to be
executed. In the COMMAND mode RROM 2 would be equivalent to typing ROM 2, then RUN. But,
notice that RROM [integer] is a statement. This means that a program that is already executing canactually force the execution of a completely different program that is in the EPROM file. This gives theuser the ability to "change programs" on the fly.
If the user executes a RROM [integer] statement and an invalid integer is entered (say 6 programs arecontained in the EPROM file and the user enters RROM 8, or no EPROM is in the system), no error
will be generated and MCS BASIC-52 will execute the statement following the RROM [integer]
statement.
NOTE -- Every time the RROM [integer] statement is executed, all variables and strings are set equal
to zero, so variables and strings CANNOT be passed from one program to another by using the RROM
[integerl statement. Additionally, all MCS BASIC-52 evoked interrupts are cleared.

- 70 -

intel4.36 DESCRIPTION OF STATEMENTS
STATEMENTS: LD@ [expr] and ST@ [expr] (VERSION 1.1 ONLY)
MODE: COMMAND AND/OR RUN
TYPE: INPUT/OUTPUTST@
The ST@ [expr] statement lets the user specify where MCS BASIC-52 floating point numbers are to be
stored. The expression [expr] following the ST@ statement specifies the address of where the number is
to be stored and the number is assumed to be on the argument stack. The ST@ [expr] statement isdesigned to be used in conjunction with the LD@ [expr] statement. The purpose of these two statements
is to allow the user to save floating point numbers anywhere in memory with the assumption that theuser will employ some type of battery back-up or non-volatile scheme with this memory.
LD@
The LD@ [expr] statement lets the user retrieve floating point numbers that were saved with the ST@[expr] statement. The expression [expr] following the LD@ statement specifies where the number isstored and after executing the LD@ [expr] statement, the number is placed on the argument stack.
EXAMPLE: Saving and retrieving a ten element array at location array at location 0F000H
10 REM *** ARRAY SAVE ***20 FOR I = 0 TO 930 PUSH A(I) : REM PUT ARRAY VALUE ON STACK
40 ST@ 0F005H+6*I : REM STORE IT, SIX BYTES PER NUMBER
50 NEXT I
60 REM *** GET ARRAY ***70 FOR I = 0 TO 980 LD@ 0F005H+6*I
90 POP B(I)
100 NEXT I
Remember that each floating point number requires 6 bytes of storage. Also note that expression in theST@ [expr] and LD@ [expr] statements point to the most significant byte of the stored number. Hence.
ST@ (0F005H) would save the number in locations 0F005H, 0F004H, 0F003H, 0F002H, 0F01H, and
0F000H.

- 71 -

intel4.37 DESCRIPTION OF STATEMENTS
STATEMENT: PGM
MODE: COMMAND AND/OR RUN
TYPE: INPUT/OUTPUT
The PGM statement gives the user the ability to program an EPROM or EEPROM while executing aBASIC program. The PGM statement requires that the user set up internal memory locations 18H(24D), 19H (25D), 1AH (26D), 1BH (27D), 1EH (30D) and 1GH (31D). Note that these internal
memory locations are normally reserved for the user!!
The User must initialize these internal memory locations with the following:
EXAMPLE:LOCATION

CONTENTS

1BH:19H(27D:25D)

THE ADDRESS OF THE SOURCE INFORMATION THAT IS TO BE
PROGRAMMED INTO THE EPROM ú LOCATION 19H IS THE LOW
BYTE AND LOCATION 1BH IS THE HIGH BYTE

1AH:18H(26D:24D)

THE ADDRESS - 1 OF THE EPROM LOCATION(S) THAT ARE TO
BE PROGRAMMED, LOCATION 18H IS TH LOW BYTE AND
LOCATION 1AH IS THE HICH BYTE

1FH:1EH(31D:30D)

THE NUMBER OF 8YTES THAT THE USER WANTS TO PROGRAMLOCATION 1EH IS THE LOW BYTE AND LOCATION 1FH IS
THE HIGH 8YTE

The user must also initialize the width of the desired EPROM programming pulse and store the value ininternal memory locations 40H (64D) (high byte) and 41H (65D) (low byte). The reload for a 50
millisecond EPROM programming pulse is calculated as follows:
10
20
30
40
50

REM R = RELOAD VALUE. W = WIDTH IN SECONDS (50 MILLISECONDS)
W = .05
R = 65536 - W * XTAL/12
DBY(40H) = R/256
DBY(41H) = R .AND. 0FFH

In addition, the user must also SET or CLEAR BIT 38.3 (26.3H) to select the INTELligent EPROM
programming algorithm. The Bit is SET to select INTELligent programming and CLEARED to selectthe normal 50 millisecond algorithm. To SET the BIT, execute a DBY(38) = DBY(38) .OR. 8H
Statement, to CLEAR the BIT, execute a DBY(38) = DBY(38) .AND. 0F7H instruction.

- 72 -

intel4.37 DESCRIPTION OF STATEMENTS
IMPORTANT NOTE!
When executed in the RUN mode, The PGM statement will not generate an error if the EPROM fails toprogram properly. Instead, the control of the program will be passed back to the user just as if the
EPROM programmed properly. The user must then examine locations 1EH and 1FH. If the contents oflocations 1EH and 1FH both equal zero, then the EPROM programmed properly. If they do not, then an
ERROR occurred during the programming process. The user can then examine locations 1AH:18H todetermine what location in the EPROM failed to program.
Well, this sounds like a lot to do just to program an EPROM, but it's not so bad. The following programis an example of a universal EPROM/EEPROM programmer built around MCS BASIC-52. This
program can program a block of RAM into an EPROM or EEPROM that is addressed at 8000H orabove.
EXAMPLE:
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380

PRINT "UNIVERSAL PROM PROGRAMMER" : PRINT "WHAT TYPE OF DEVICE ?"PRINT : PRINT "1 = EEPROM" : PRINT "2 = INTELLIGENT EPROM"PRINT "3 = NORMAL (50 MS) EPROM" : PRINT : INPUT "TYPE (1,2,3) - ",TON (T-1) GOSUB 340,350,360
REM this sets up intelligent programming if needed
IF W=.001 THEN DBY(26)=DBY(26).OR.8 ELSE DBY(26)=DBY(26).AND.0F7H
REM calculate pulse width and save it
PUSH (65536-(W*XTAL/12)) : GOSUB 380
POP G1 : DBY(40H)=G1 : POP G1 : DBY(41H)=G1 : PRINTINPUT " STARTING DATA ADDRESS - ",S : IF S<512.OR.S>0FFFFH THEN 100
PRINT : INPUT " ENDING DATA ADDRESS - ",E
IF E<S.OR.E>0FFFFH THEN 110
PRINT : INPUT " PROM ADDRESS - ",P : IF P<8000H.OR.P>0FFFFH THEN 130
REM calculate the number of bytes to program
PUSH (E-S)+1 : GOSUB 380 : POP G1 : DBY(31)=G1 : POP G1 : DBY(30)=G1
REM set up the eprom address
PUSH (P-1) : GOSUB 380 : POP G1 : DBY(26)=G1 : POP G1 : DBY(24)=G1
REM set up the source address
PUSH S : GOSUB 380 : POP G1 : DBY(27)=G1 : POP G1 : DBY(25)=G1
PRINT : PRINT "TYPE A 'CR' ON THE KEYBOARD WHEN READY TO PROGRAM"
REM wait for a 'cr' then program the eprom
X=GET : IF X<>0DH THEN 220
REM program the eprom
PGM
REM see if any errors
IF (DBY(30).OR.DBY(31))=0 THEN PRINT "PROGRAMMING COMPLETE" : END
PRINT : PRINT "***ERROR***ERROR***ERROR***ERROR***" : PRINT
REM these routines calculate the address of the source andREM eprom location that failed to program
S1=DBY(25)+256*DBY(27) : S1=S1-1 : D1=DBY(24)+256*DBY(26)PH0. "THE VALUE ",XBY(S1), : PH1. " WAS READ AT LOCATION ",S1 : PRINT
PH0. "THE EPROM READ ",XBY(D1), : PH1. " AT LOCATION ",D1 : END
REM these subroutines set up the pulse width
W=.0005 : RETURN
W=.001 : RETURN
W=.05 : RETURN
REM this routine takes the top of stack and returns high, low bytes
POP G1 : PUSH (G1.AND.0FFH) : PUSH (INT(G1/256)) : RETURN- 73 -

intelCHAPTER 5
Description of Arithmetic / Logic Operators and Expressions

5.1 DUAL OPERAND OPERATORSMCS BASIC-52 contains a complete set of arithmetical and logical operators. Operators are divided
into two groups, dual operand or dyadic operators and single operand or unary operators. The
generalized form of all dual operand instructions is as follows:[expr] OP [exprl, where OP is one of the following operators:+ ADDITION OPERATOR
EXAMPLE:PRINT 3+2
5
/ DIVISION OPERATOR
EXAMPLE:PRINT 100/5
20** EXPONENTIATION OPERATOR
Raises the first expression to the power of the second expression. The power any number can be raised
to is limited to 255. The notation ** was chosen instead of the sometimes used ^ symbol because the"up arrow" symbol appears different on various terminals. To eliminate confusion the ** notation waschosen.
EXAMPLE:PRINT 2**3
8
* MULTIPLICATION OPERATOR
EXAMPLE:PRINT 3*3
9
- SUBTRACTION OPERATOR
EXAMPLE:PRINT 9-6
3

- 74 -

intel
5.1 DUAL OPERAND OPERATIONS.AND. LOGICAL AND OPERATOR
EXAMPLE:PRINT 3.AND.2
2.OR. LOGICAL OR OPERATOR
EXAMPLE:PRINT 1.OR.4
5.XOR. LOGICAL EXCLUSIVE OR OPERATOR
EXAMPLE:PRINT 7.XOR.6
1
COMMENTS ON LOGICAL OPERATORS .AND., .OR., and .XOR.
These operators perform a BIT-WISE logical function on valid INTEGERS. That means botharguments for these operators must be between 0 and 65535 (0FFFFH) inclusive. If they are not, MCS
BASIC-52 will generate a BAD ARGUMENT ERROR. All non-integer values are truncated, NOT
rounded.
You may wonder why the notation .OP. was chosen for the logical functions. The only reason for this isthat MCS BASIC-52 eliminates ALL spaces when it processes a user line and inserts spaces before andafter STATEMENTS when it LISTS a user program. MCS BASIC-52 does not insert spaces before andafter operators. So, if the user types in a line such as 10 A = 10 * 10, this line will be listed as 10 A=
10*10. All spaces entered by the user before and after the operator will be eliminated. The .OP. notation
was chosen for the logical operators because a line entered as 10 B = A AND B would be listed as 10 B
= AANDB. This just looked confusing, so the dots were added to the logical instructions and theprevious example would be listed as 10 B=A.AND.B, which is easier to read.

- 75 -

intel
5.2.1 UNARY OPERATORS -- GENERAL PURPOSE
ABS([expr])
Returns the ABSOLUTE VALUE of the expression.
EXAMPLES:PRINT ABS(5)5PRINT ABS(-5)
5

NOT([expr])
Returns a 16 bit one's complement of the expression. The expression must be a valid integer (i.e.
between 0 and 65535 (0FFFFH) inclusive). Non-integers will be truncated, not rounded.
EXAMPLES:PRINT NOT(65000)
535

PRINT NOT(0)65535

INT([expr])
Returns the integer portion of the expression.
EXAMPLES:PRINT INT(3.7)
3PRINT INT(100.876)
100

SGN([expr])
Will return a value of + 1 if the argument is greater than zero, zero if the argument is equal to zero, and-1 if the argument is less than zero.
EXAMPLES:PRINT SGN(52)1

PRINT SGN(0)0- 76 -PRINT SGN(-8)-1intel
5.2.1 UNARY OPERATORS -- GENERAL PURPOSE
SQR([expr])
Returns the square root of the argument. The argument may not be less than zero. The result returned
will be accurate to within + / - a value of 5 on the least significant digit.
EXAMPLES:PRINT SQR(9)3PRINT SQR(45)6.7082035

PRINT SQR(100)10RND
Returns a pseudo-random number in the range between 0 and 1 inclusive. The RND operator uses a 16bit binary seed and generates 65536 pseudo-random numbers before repeating the sequence. The
numbers generated are specifically between 0/65535 and 65535/65535 inclusive. Unlike most BASICS,
the RND operator in MCS BASIC-52 does not require an argument or a dummy argument. In fact, if anargument is placed after the RND operator, a BAD SYNTAX error will occur.
EXAMPLES:PRINT RND
.30278477
PI
PI is not really an operator, it is a stored constant. In MCS BASIC-52, PI is stored as 3.1415926. Mathexperts will notice that PI is actually closer to 3.141592653, so proper rounding for PI should yield thenumber 3.1415927. The reason MCS BASIC-52 uses a 6 instead of a 7 for the last digit is that errors in
the SIN, COS and TAN operators were found to be greater when the 7 was used instead of 6. This isbecause the number PI/2 is needed for these calculations and it is desirable, for the sake of accuracy tohave the equation PI/2 + PI/2 = PI hold true. This cannot be done if the last digit in PI is an odd number,so the last digit of PI was rounded to 6 instead of 7 to make these calculations more accurate.

- 77 -

intel
5.2.2 UNARY OPERATORS -- LOG FUNCTIONS
LOG([expr])
Returns the natural logarithm of the argument. The argument must be greater than 0. This calculation iscarried out to 7 significant digits.
EXAMPLES:PPINT LOG(12)2.484906

PRINT LOG(EXP(1))
1

EXP([expr])
This function raises the number "e" (2.7182818) to the power of the argument.
EXAMPLES:PRINT EXP(1)2.7182818

PRINT EXP (L0G (2))
2

5.2.3 UNARY OPERATORS -- TRIG FUNCTIONSSlN([expr])
Returns the SIN of the argument. The argument is expressed in radians. Calculations are carried out to 7
significant digits. The argument must be between +- 200000.
EXAMPLES:PRINT SIN(PI/4).7071067

PRINT SIN(0)0COS([expr])
Returns the COS of the argument. The argument is expressed in radians. Calculations are carried out to7 significant digits. The argument must be between +- 200000.
EXAMPLES:PRINT COS(PI/4)
7071067

PRINT(COS(0))
1- 78 -

intel
5.2.3 UNARY OPERATORS -- TRIG FUNCTIONSTAN([expr])
Returns the TAN of the argument. The argument is expressed in radians. The argument must be
between +- 200000.
EXAMPLES:PRINT TAN(PI/4)
1

PRINT TAN(0)0ATN([expr])
Returns the ARCTANGENT of the argument. The result is in radians. Calculations are carried out to 7
significant digits. The ATN operator returns a result between -PI/2 (3.1415926/2) and PI/2.
EXAMPLES:PRINT ATN(PI)1.2626272

PRINT ATN(1).78539804

COMMENTS ON TRIG FUNCTIONSThe SIN, COS, and TAN operators use a Taylor series to calculate the function. These operators first
reduce the argument to a value that is between 0 and PI/2. This reduction is accomplished by thefollowing equation:
REDUCED ARGUMENT = (user arg/PI - INT(user arg/PI)) * PI
The REDUCED ARGUMENT, from the above equation, will be between 0 and PI. The REDUCEDARGUMENT is then tested to see if it is greater than PI/2. If it is, then it is subtracted from PI to yield
the final value. If it isn't, then the REDUCED ARGUMENT is the final value.
Although this method of angle reduction provides a simple and economical means of generating theappropriate arguments for a Taylor series. there is an accuracy problem associated with this technique.
The accuracy problem is noticed when the user argument is large (i.e. greater than 1000). That is
because significant digits, in the decimal (fraction) portion of REDUCED ARGUMENT are lost in the(user arg/PI - INT(user arg/PI)) expression. As a general rule, try to keep the arguments for the TRIG
functions as small as possible!

- 79 -

intel
5.3 UNDERSTANDING PRECEDENCE OF OPERATORSThe hierarchy of mathematics dictates that some operations are carried out before others. If you
understand the hierarchy of mathematics, it is possible to write complex expressions using only aminimum amount of parentheses. It is easy to illustrate what precedence is all about, examine thefollowing equation:
4+3*2 = ?
Should you add (4+3) then multiply seven by 2, or should you multiply (3*2) then add 4? Well, thehierarchy of mathematics says that multiplication has precedence over addition, so you would multiply(3*2) first then add 4, So,
4+3*2 = 10
The rules for the hierarchy of math are simple. When an expression is scanned from left to right an
operation is not performed until an operator of lower or equal precedence is encountered. In theexample addition could not be performed because multiplication has higher precedence. The
precedence of operators from highest to lowest in MCS BASIC-52 is as follows:
1) OPERATORS THAT USE PARENTHESES ( )2) EXPONENTATION (**)
3) NEGATION (-)4) MULTIPLICATION (*) AND DIVISION (/)5) ADDITION (+) AND SUBTRACTION (-)6) RELATIONAL EXPRESSIONS (=, <>, >, >=, <, <=)7) LOGICAL AND (.AND.)
8) LOGICAL OR (.OR.)
9) LOGICAL XOR (.XOR.)
Relative to operator precedence, the rule of thumb should always be, when in doubt, use parentheses.

- 80 -

intel5.4 HOW RELATIONAL EXPRESSIONS WORK
Relational expressions involve the operators =, <>, >, >=, <, and < = . These operators are typicallyused to "test" a condition. In MCS BASIC-52 relational operators return a result of 65535 (0FFFFH) if
the relational expression is true, and a result of 0, if the relation expression is false. But, where is theresult returned? It is returned to the argument stack. Because of this, it's possible to actually display the
result of a relational expression.
EXAMPLES:PRINT 1=00

PRINT 1>065535

PRINT A<>A
0

PRINT A=A
65535

It may seem strange to have a relational expression actually return a result, but it offers a unique benefit
in that relational expressions can actually be "chained" together using the logical operators .AND.,.OR.. and .XOR.. This makes it possible to test a rather complex condition with ONE statement.
EXAMPLE:>1O IF A<B AND A<C .OR. A>D THEN. . . . . .
Additionally, the NOT([expr]) operator can be used.
EXAMPLE:>10 IF NOT(A>B). AND. A<C THEN. . . . . . .
By "chaining" together relational expressions with logical operators, it is possible to test very particularconditions with one statement. When using logical operators to link together relational expressions, it isvery important that the programmer pay careful attention to the precedence of operators. The logicaloperators were assigned lower precedence, relative to relational expressions, just to make the linking ofrelational expressions possible without using parentheses.

- 81 -

intelCHAPTER 6
Description of String Operators

6.1 WHAT ARE STRINGS ?
A string is a character or a bunch of characters that are stored in memory. Usually, the characters stored
in a string make up a word or a sentence. Strings are handy because they allow the programmer to deal
with words instead of numbers. This is useful because it allows one to write "friendly" programs, whereindividuals can be referred to by their names instead of a number.
MCS BASIC-52 contains ONE dimensioned string variable, $([expr]). The dimension of the string
variable (the [expr] value) ranges from 0 to 254. This means that 255 different strings can be definedand manipulated in MCS BASIC-52. Initially, NO memory is allocated for strings. Memory is allocatedby the STRING [expr], [expr] STATEMENT. The details of this statement are covered in the
DESCRIPTION OF STATEMENTS chapter of this manual.
In MCS BASIC-52, strings can be defined in two ways, with the LET STATEMENT and with the
INPUT STATEMENT.
EXAMPLE:>10 STRING 100,20>20 $(1)="THIS IS A STRING, ">30 INPUT "WHAT'S YOUR NAME? - ",$(2)>40 PRINT $(1),$(2)>RUN
WHAT'S YOUR NAME?. - FRED
THIS IS A STRING, FREDSTRINGS can also be assigned to each other with a LET statement.
EXAMPLE:$(2)=$(1)Would assign the STRING value in $(1) to the STRING $(2).

- 82 -

intel
6.2 THE ASC OPERATORIn MCS BASIC-52, two operators manipulate STRINGS. These operators are ASC( ) and CHR( ). Admittedly, the string operators contained in MCS BASIC-52 are not quite as powerful as the string
operators contained in some BASICS. But surprisingly enough, by using the string operators available
in MCS BASIC-52 it is possible to manipulate strings in almost any way imaginable. This in itself is acommendable feat since MCS BASIC-52 was designed primarily to be a sophisticated BASIC language
oriented controller, not a string manipulator. The string operators available in MCS BASIC-52 are as
follows:
ASC( )The ASC( ) operator returns the integer value of the ASCII character placed in the parentheses.
EXAMPLE:>PRINT ASC(A)65
65 is the decimal representation for the ASCII character "A." In addition, individual characters in a predefined ASCII string can be evaluated with the ASC( ) operator.
EXAMPLE:>10 $(1)="THIS IS A STRING">20 PRINT $(1)>30 PRINT ASC($(1),1)
THIS IS A STRING
84
When the ASC( ) operator is used in the manner shown above, the $([expr]) denotes what string isbeing accessed and the expression after the comma "picks out" an individual character in the string. In
the above example, the first character in the string was picked out and 84 is the decimal representation
for the ASCII character " T ."- 83 -

intel
6.2 THE ASC OPERATOR
EXAMPLE:>10 $(1)="ABCDEFGHIJKL">20 FOR X=1 TO 12>30 PRINT ASC($(1),X),>40 NEXT X>RUN
65 66 67 68 69 70 71 72 73 74 75 76
The numbers printed in the previous example are the values that represent the ASCII characters A,B,C,. . . L.
Additionally, the ASC( ) operator can be used to change individual characters in a defined string.
EXAMPLE:>10 $(1)="AECDEFGHIJKL">20 PRINT $(1)>30 ASC($(1),1)=75>40 PRINT $(1)>50 ASC($(1),2)=ASC($(1),3)>60 PRINT $(1)>RUN
AECDEFGHIJKLKBCDEFGHIJKLKCCDEFGHIJKL
In general, the ASC( ) operator lets the programmer manipulate individual characters in a string. A
simple program can determine if two strings are identical.
EXAMPLE:>10 $(1)="SECRET" : REM SECRET IS THE PASSWORD>20 INPUT "WHAT'S THE PASSWORD - ",$(2)>30 FOR I=l TO 6
>40 IF ASC($(l),I)=ASC($(2),I) THEN NEXT I ELSE 70>50 PRINT "YOU GUESSED IT"'
>60 END>70 PRINT "WRONG. TRY AGAIN" : GOTO 20>RUN
WHAT'S THE PASSWORD - SECURE
WRONG, TRY AGAIN
WHAT'S THE PASSWORD - SECRETYOU GUESSED IT- 84 -

intel
6.3 THE CHR OPERATOR
CHR( )The CHR( ) operator is the converse of the ASC( ) operator. It converts a numeric expression to an
ASCII character.
EXAMPLE:>PRINT CHR(65)A
Like the ASC( ) operator, the CHR( ) operator can also "pick out" individual characters in a defined
ASCII string.
EXAMPLE:>10 $(1)"MCS BASIC-52">20 FOR I=1 TO 12 : PRINT CHR($(1),I), : NEXT I>30 PRINT : FOR I=12 TO 1 STEP -1>40 PRINT CHR($(1),I), : NEXT I>RUNMCS BASIC-52
25-CISAB SCM
In the above example, the expressions contained within the parentheses, following the CHR operator
have the same meaning as the expressions in the ASC( ) operator.
Unlike the ASC( ) operator, the CHR( ) operator CANNOT be assigned a value. A statement such as
CHR($(1),1) = H, is INVALID and will generate a BAD SYNTAX ERROR. Use the ASC( ) operator
to change a value in a string. The CHR( ) operator can only be used within a print statement!

- 85 -

intelCHAPTER 7
Special Operators

7.1 SPECIAL FUNCTION OPERATORSSPECIAL FUNCTION OPERATORS are called SPECIAL FUNCTION OPERATORS because they
di- rectly manipulate the I/O hardware and the memory addresses on the 8052AH device. All SPECIALFUNCTION OPERATORS, with the exception of CBY([expr]) and GET, can be placed on either sideof the replacement operator ( = ) in a LET STATEMENT.
EXAMPLES:
A = DBY(100) and DBY(100) = A+2
Both of the above are valid statements in MCS BASIC-52. The SPECIAL FUNCTION OPERATORSin MCS BASIC-52 include the following:
CBY([expr])
The CBY([exprl] operator is used to retrieve data from the PROGRAM or CODE MEMORY address
space of the 8052AH. Since CODE memory cannot be written into on the 8052AH, the CBY([expr])
operator cannot be assigned a value. It can only be read.
EXAMPLE: A = CBY(1000) Causes the value in code memory space 1000 to be assigned to thevariable A. The argument for the CBY([exprl] operator MUST be a valid integer (i.e. between 0 and
65535 (0FFFFH) ). If it is not, a BAD ARGUMENT ERROR will occur.
DBY([expr])
The DBY([expr]) operator is used to retrieve or assign a value to the 8052AH's internal data memory.
Both the value and argument in the DBY operator must be between 0 and 255 inclusive. This is because
there are only 256 internal memory locations in the 8052AH and one byte can only represent a quantity
between 0 and 255 inclusive.
EXAMPLES:
A=DBY(B) and DBY(250) = CBY(1000)
The first example would assign variable A the value that is in internal memory location B. B would
have to be between 0 and 255. The second example would load internal memory location 250 with thesame value that is in program memory location 1000.

- 86 -

intel7.1 SPECIAL FUNCTION OPERATORSXBY([expr])
The XBY([expr]) operator is used to retrieve or assign a value to the 8052AH's external data memory.
The argument in the XBY([expr]) operator must be a valid integer (i.e. between 0 and 65535(0FFFFH)) . The value assigned to the XBY([expr]) operator must be between 0 and 255. If it is not a
BAD ARGUMENT ERROR will occur.
EXAMPLES:XBY(4000H)=DBY(100) and A=XBY(0F000H)
The first example would load external memory location 4000H with the same value that was in internal
memory location 100. The second example would make the variable A equal to the value in external
memory location 0F000H.
GETThe GET operator only produces a meaningful result when used in the RUN mode. It will always returna result of zero in the command mode. What GET does is read the console input device. Actually, ittakes a "snapshot" of the console input device. If a character is available from the console device, thevalue of the character will be assigned to GET. After GET is read in the program, GET will be assigned
the value of zero until another character is sent from the console device. The following example will
print the decimal representation of any character sent from the console:
EXAMPLE:>10 A=GET>20 IF A<>0 THEN PRINT A>30 GOTO 10>RUN
65 (TYPE "A" ON CONSOLE)
49 (TYPE "1" ON CONSOLE)
24 (TYPE "CONTROL-X" ON CONSOLE)
50 (TYPE '2' ON CON8OLE)
The reason the GET operator can be read only once before it is assigned a value of zero is that this
implementation guarantees that the first character entered will always be read, independent of where the
GET operator is placed in the program.

- 87 -

intel7.1 SPECIAL FUNCTION OPERATORSThe following operators directly manipulate the 8052AH's special function registers. Specific details of
the operation of these registers is in the MICROCONTROLLER USERS HANDBOOK, available fromINTEL.
IE
The IE operator is used to retrieve or assign a value to the 8052AH's special function register IE. Since
the IE register on the 8052AH is a BYTE register, the value assigned to IE must be between 0 and 255.
The IE register on the 8052AH contains an unused bit, BIT IE.6. Since this bit is "undefined," it may beread as a random one or zero, so the user may want to mask this bit when reading the IE register. Thiscan be done with a statement like A = IE.AND.0BFH. The only statements in MCS BASIC-52 thatwrite to the IE register are the CLOCK0, CLOCK1, ONEX1, CLEAR, and CLEARI statements.
EXAMPLES:
IE = 81H and A = IE.AND.0BFH

IP
The IP operator is used to retrieve or assign a value to the 8052AH's special function register IP. Since
the IP register on the 8052AH is a BYTE register, the value assigned to IP must be between 0 and 255.
The IP register on the 8052AH contains two unused bits, BIT IP.6 and IP.7. Since these bits are "undefined," they may be read as a random 1 or 0, so the user may want to mask these bits when reading
the IP register. This can be done with a statement such as B=IP.AND.3FH. MCS BASIC-52 does notwrite to the IP register during initialization, so user can establish whatever interrupt priorities are
required in a given application.
EXAMPLES:
IP = 3 and A = IP.AND.3FH

PORT1
The PORT1 operator is used to retrieve or assign a value to the 8052AH's P1 I/O port. Since P1 on the
8052AH is a BYTE wide register, the value assigned to P1 must be between 0 and 255 inclusive.
Certain bits on P1 have pre-defined functions. If the user does not implement any of the hardware
associated with these pre-defined functions, The PORT1 instruction can be used in any manner
appropriate in the application.

- 88 -

intel7.1 SPECIAL FUNCTION OPERATORSPCONThe PCON operator is used to retrieve or assign a value to the 8052AH's PCON register. In the
8052AH, only the most significant bit of the PCON register is used, all other bits are undefined. Setting
this bit will double the baud rate if TIMER/COUNTER1 is used as the baud rate generator for the serial
port. PCON is a byte register.
RCAP2
The RCAP2 operator is used to retrieve and/or assign a value to the 8052AH's special function registers
RCAP2H and RCAP2L. This operator treats RCAP2H and RCAP2L as a 16-bit register pair. RCAP2H
is the high byte and RCAP2L is the low byte. The RCAP2H and RCAP2L registers are thereload/capture registers for TIMER2. The user must use caution when writing to RCAP2 registerbecause RCAP2 controls the BAUD rate of the serial port on the MCS BASIC-52 device. The
following can be used to determine what BAUD rate the MCS BASIC-52 device is operating at:
BAUD = XTAL/(32*(65536-RCAP2) )T2CON
The T2CON operator is used to retrieve and/or assign a value to the 8052AH's special function register
T2CON. The T2CON is a byte register that controls TIMER2's mode of operation and determines
which timer (TIMER1 or TIMER2) is used as the 8052AH's baud rate generator. MCS BASIC-52
initializes T2CON with the value 52 (34H) and assumes that its value is never changed. Randomlychanging the value of T2CON, without knowing what you are doing can "crash" the serial port on the
8052AH. Beware!

- 89 -

intel7.1 SPECIAL FUNCTION OPERATORSTCONThe TCON operator is used to retrieve and/or assign value to the 8052AH's special function register
TCON. TCON is a byte register that is used to enable or disable TIMER0 and TIMER1, plus theinterrupts that are associated with these timers. Additionally, TCON determines whether the externalinterrupt pins on the 8052AH are operating in a level sensitive or edge-triggered mode. MCS BASIC-52
initializes TCON with the value 244 (0F4H) and assumes that it is never changed. The value 244(0F4H) places both TIMER0 and TIMER1 in the run (enabled) mode. If the user disables the operationof TIMER0, by clearing BIT 4 in the TCON register, the REAL TIME CLOCK will NOT work. If theuser disables the operation of TIMER1, by clearing BIT 6 in the TCON register, the EPROM
programming routines, the software serial port. and the PWM statement will NOT work. Use caution
when changing TCON!!!
TMOD
The TMOD operator is used to retrieve and/or assign a value to the 8052AH's special function register
TMOD. TMOD is a byte register that controls TIMER0 and TlMER1's mode of operation. MCS
BASIC- 52 initializes the TCON register with a value of 16 (10H). The value 16 (10H) places TIMER0
in mode 0, which is a 13-bit counter mode and TIMER1 in mode 1, which is a 16-bit counter mode.
MCS BASIC-52 assumes that the modes of these two timer/counters are never changed. If the userchanges the mode of TIMER0, the REAL TIME CLOCK will not operate properly. If the user changes
the mode of TIMER1, EPROM programming. the software serial port, and the PWM statement will notwork properly. If the user does not use these features available in MCS BASIC-52, either timer/countercan be placed in any mode required by the specific application.

- 90 -

intel7.1 SPECIAL FUNCTION OPERATORSTIMEThe TIME operator is used to retrieve and/or assign a value to the REAL TIME CLOCK resident in
MCS BASIC-52. After reset, TIME is equal to 0. The CLOCK1 statement enables the REAL TIME
CLOCK.
When the REAL TIME CLOCK is enabled, the SPECIAL FUNCTION OPERATOR, TIME will
increment once every 5 milliseconds. The TIME operator uses TIMER0 and the interrupts associatedwith TIMER0 on the 8052AH. The unit of TIME is seconds and the appropriate XTAL value must be
assigned to insure that the TIME operator is accurate.
When TIME is assigned a value with a LET statement (i.e. TIME = 100), only the integer portion of
TIME will be changed.
EXAMPLE:>CLOCK1(enable REAL TIME CLOCK)>CLOCK0(disable REAL TIME CLOCK)>PRINT TIME
3.315

(display TIME)>TIME = 0

(set TIME = 0)

>PRINT TIME
.315

(display TIME)(only the integer is changed)

The "fraction" portion of TIME can be changed bymanipulating the contents of internal memory
location 71 (47H). This is accomplished by a DBY(71)statement. Note that each count in internal memory
location 71 (47H) represents 5 milliseconds of TIME.
Continuing with the EXAMPLE:>DBY(71) = 0(fraction of TIME = 0)

>PRINT TIME
0
>DBY(71) = 3(fraction of TIME = 3, 15 ms)>PRINT TIME
1.5 E-2- 91 -

intel7.1 SPECIAL FUNCTION OPERATORSThe reason only the integer portion of TIME is changed when assigned a value is that it allows the user
to generate accurate time intervals. For instance, let's say you want to create an accurate 12 hour clock.
There are 43200 seconds in a 12 hour period, so an ONTIME 43200,[ln num] statement is used. Now,
when the TIME interrupt occurs the statement TIME = 0 is executed, but the millisecond counter is not
re-assigned a value so if interrupt latency happens to exceed 5 milliseconds, the clock will still remain
accurate.
TIMER0The TIMER0 operator is used to retrieve or assign a value to the 8052AH's special function registers
TH0 and TL0. This operator treats the byte registers TH0 and TL0 as a 16-bit register pair. TH0 is thehigh byte and TL0 is the low byte. MCS BASIC-52 uses TH0 and TL0 to implement the REAL TIME
CLOCK function. If the user does not implement the REAL TIME CLOCK function (i.e. does not use
the statement CLOCK1) in the BASIC program TH0 and TL0 may be used in any manner suitable tothe particular application.
TIMER1The TIMER1 operator is used to retrieve or assign a value to the 8052AH's special function registers
TH1 and TL1. This operator treats the byte registers TH1 and TL1 as a 16-bit register pair TH1 is thehigh byte and TL1 is the low byte. MCS BASIC-52 uses TH1 and TL1 to implement the timings for thesoftware serial port, the EPROM programming feature, and the PWM statement. If the user does not use
any of these features TH1 and TL1 may be used in any manner suitable to the particular application.
TIMER2The TIMER2 operator is used to retrieve or assign a value to the 8052AH's special function registers
TH2 and TL2. This operator treats the byte registers TH2 and TL2 as a 16-bit register pair. TH2 is thehigh byte and TL2 is the low byte. MCS BASIC-52 uses TH2 and TL2 to generate the baud rate for theserial port. If the user does not use TIMER2 to clock the serial port, TH2 and TL2 may be used in any
manner suitable to the particular application.

- 92 -

intel7.1 SPECIAL FUNCTION OPERATORSXTALThe XTAL operator tells MCS BASIC-52 what frequency the system is operating at. The XTAL
operator is used by MCS BASIC-52 to calculate thc REAL TIME CLOCK reload value, the PROM
programming timing, and the software serial port baud rate generation. The XTAL value is expressed in
Hz. So,XTAL = 9000000
would set the XTAL value to 9 MHz.

- 93 -

intel7.2 EXAMPLES OF MANIPULATING SPECIAL FUNCTION VALUES
Using the logical operators available in MCS BASIC-52. it is possible to write to or read from any byteof the special function registers that MCS BASIC-52 treats as a register pair:
EXAMPLE:WRITING TO THE HIGH BYTE
>TIMER0 = (TIMER0 .AND. 00FFH)+ INT(256*(USER BYTE))EXAMPLE:WRITING TO THE LOW BYTE
>TIMER0 = (TIMER0 .AND. 0FF00H) + (USER BYTE)
EXAMPLE:READING HIGH BYTE
>PH0. INT(TIMER0/256)
EXAMPLE:READING LOW BYTE
>PH0. TIMER0 .AND. 0FFH
TIMER1 can function as the baud rate generator for MCS BASIC-52. To assign TIMER1 as the baudrate generator, the following instructions must be executed:>TMOD = 32
TIMER1 in auto reload mode
>TIMER1 = 256*(256-(65536-RCAP2)/12) - load TIMER1>T2CON = 0
use TIMER1 as baud rate gen
This sequence of instructions can be executed in either the direct mode or as part of a program. When
TIMER1 is used as the baud rate generator. TIMER2 can be used in anyway suitable to the application.
The PROG, FPROG, LIST#, PRINT# and PWM commands/statements cannot be used when TIMER1
functions as the baud rate generator for the MCS BASIC-52 device. Certain crystals may not be able touse TIMER1 as the baud rate generator, especially at high (above 2400) baud rates.

- 94 -

intel7.3 SYSTEM CONTROL VALUESThe SYSTEM CONTROL VALUES determine or reveal how memory is allocated by MCS BASIC-52.
MTOP
After reset, MCS BASIC-52 sizes the external memory and assigns the last valid memory address to the
SYSTEM CONTROL VALUE, MTOP. MCS BASIC-52 will not use any external RAM memory
beyond the value assigned to MTOP. If the user wishes to allocate some external memory for an
assembly language routine the LET statement can be used (e.g. MTOP = USER ADDRESS). If the user
assigns a value to MTOP that is greater than the last valid memory address, a MEMORY
ALLOCATION ERROR will be generated.
EXAMPLES:>PRINT MTOP
2047
>MTOP=2000>PRINT MTOP
2000
LEN
The SYSTEM CONTROL VALUE, LEN. tells the user how many bytes of memory the current
selected program occupies. Obviously, LEN cannot be assigned a value, it can only be read. A NULL
program (i.e. no program) will return a LEN of 1. The 1 represents the end of program file character.
FREE
The SYSTEM CONTROL VALUE, FREE, tells the user how many bytes of RAM memory areavailable to the user. When the current selected is in RAM memory, the following relationship will
always hold true.
FREE = MTOP -- LEN -- 511
NOTE: Unlike some BASICS, MCS BASIC-52 does not require any "dummy" arguments for the
SYSTEM CONTROL VALUES.

- 95 -

intelCHAPTER 8
Error Messages, Bells, Whistles, and Anomalies

8.1 ERROR MESSAGESMCS BASIC-52 has a relatively sophisticated ERROR processor. When BASIC is in the RUN mode
the generalized form of the ERROR message is as follows:
ERROR: XXX - IN LINE YYYYYY BASIC STATEMENT-----------x
Where XXX is the ERROR TYPE and YYY is the line number of the program in which the error
occurred. A specific example is:
ERROR BAD SYNTAX - IN LINE 10
10 PRINT 34*21*--------------x
The X signifies approximately where the ERROR occurred in the line number. The specific location of
the X may be off by one or two characters or expressions depending on the type of error and where theerror occurred in the program. If an ERROR occurs in the COMMAND MODE only the ERROR TYPE
will be printed out NOT the Line number. This makes sense, because there are no line numbers in the
COMMAND MODE. The ERROR TYPES are as follows:
BAD SYNTAXA BAD SYNTAX error means that either an invalid MCS BASIC-52 COMMAND, STATEMENT, or
OPERATOR was entered and BASIC cannot process the entry. The user should check and make surethat everything was typed in correctly. In Version 1.1 of MCS BASIC-52 a BAD SYNTAX ERROR is
also generated if the programmer attempts to use a reserved keyword as part of a variable.
BAD ARGUMENT
When the argument of an operator is not within the limits of the operator a BAD ARGUMENT ERROR
will be generated. For instance. DBY(257) would generate a BAD ARGUMENT ERROR because theargument for the DBY operator is limited to the range 0 to 255. Similarly, XBY(5000H) = -1 would
generate a BAD ARGUMENT ERROR because the value of the XBY operator is limited to the range 0
to 255.

- 96 -

intel8.1 ERROR MESSAGESARITH. UNDERFLOW
If the result of an arithmetic operation exceeds the lower limit of an MCS BASIC-52 floating point
number, an ARITH. UNDERFLOW ERROR will occur. The smallest floating point number in MCS
BASIC-52 is +- 1E-127. For instance, 1E-80/1E + 80 would cause an ARITH. UNDERFLOW ERROR.
ARITH. OVERFLOW
If the result of an arithmetic operation exceeds the upper limit of an MCS BASIC-52 floating point
number, an ARITH. OVERFLOW ERROR will occur. The largest floating point number in MCS
BASIC-52 is +-.99999999E+127. For instance, 1E+70*1E+70 would cause an ARITH. OVERFLOWERROR.
DIVIDE BY ZERO
A division by ZERO was attempted i.e. 12/0, will cause a DIVIDE BY ZERO ERROR.
ILLEGAL DIRECT (VERSION 1.0 ONLY)
Some statements, such as IF-THEN and DATA cannot be executed while the MCS BASIC-52 device isin the COMMAND MODE. If you attempt to execute one of these statements the message ERROR:ILLEGAL DIRECT will be printed to the console device. The ILLEGAL DIRECT ERROR is nottrapped in Version 1.1 of MCS BASIC-52. ILLEGAL DIRECT ERRORS return a BAD SYNTAXERROR in Version 1. 1.
LINE TOO LONG (VERSION 1.0 ONLY)
If you type in a line that contains more than 73 characters the message ERROR: LINE TOO LONG will
be printed to the console device. MCS BASlC-52's input buffer can only handle up to 73 characters.
NOTE
This error does not exist in Version 1.1. Instead the input buffer has been increased to 79 characters and
MCS BASIC-52 will echo a bell character to the user terminal if too many characters are entered into
the input buffer.
NO DATA
If a READ STATEMENT is executed and no DATA STATEMENT exists or all DATA has been readand a RESTORE instruction was not executed the message ERROR: NO DATA -- IN LINE XXX will
be printed to the console device.

- 97 -

intel8.1 ERROR MESSAGESCAN'T CONTINUE
Program execution can be halted by either typing in a control-C to the console device or by executing a
STOP STATEMENT. Normally, program execution can be resumed by typing in the CONT command.
However, if the user edits the program after halting execution and then enters the CONT command, a
CAN'T CONTINUE ERROR will be generated. A control-C must be typed during program execution
or a STOP STATEMENT must be executed before the CONT command will work.
PROGRAMMING
If an error occurs while the MCS BASIC-52 device is programming an EPROM, a PROGRAMMINGERROR will be generated. An error encountered during programming destroys the EPROM FILE
STRUCTURE, so the user cannot save any more programs on that particular EPROM once a
PROGRAMMING ERROR occurs.
A-STACKAn A-STACK (ARGUMENT STACK) error occurs when the argument stack pointer is forced "out ofbounds." This can happen if the user overflows the argument stack by PUSHing too many expressions
onto the stack, or by attempting to POP data off the stack when no data is present.
C-STACKA C-STACK (CONTROL STACK) error will occur if the control stack pointer is forced "out ofbounds." 158 bytes of external memory are allocated for the control stack, FOR -- NEXT loops require
17 bytes of control stack DO -- UNTIL, DO -- WHILE, and GOSUB require 3 bytes of control stack.
This means that 9 nested FOR -- NEXT loops is the maximum that MCS BASIC-52 can handle because
9 times 17 equals 153. If the user attempts to use more control stack than is available in MCS BASIC52 a C-STACK error will be generated. In addition, C-STACK errors will occur if a RETURN is
executed before a GOSUB a WHILE or UNTIL before a DO, or a NEXT before a FOR.

- 98 -

intel8.1 ERROR MESSAGES
I-STACKAn I-STACK (INTERNAL STACK) error occurs when MCS BASIC-52 does not have enough stackspace to evaluate an expression. Normally, I-STACK errors will not occur unless insufficient memory
has been allocated to the 8052AH's stack pointer. Details of how to allocate memory to the stack pointerare covered in the ASSEMBLY LANGUAGE LINKAGE section of this manual.
ARRAY SIZE
If an array is dimensioned by a DIM statement and then you attempt to access a variable that is outsideof the dimensioned bounds, an ARRAY SIZE error will be generated.
EXAMPLE:>DIM A(10)>PRINT A(11)ERROR: ARRAY SIZEREADYMEMORY ALLOCATIONMEMORY ALLOCATION ERRORS are generated when user attempts to access STRINGS that are"outside" the defined string limits. Additionally, if the SYSTEM CONTROL VALUE, MTOP is
assigned a value that does not contain any RAM memory, a MEMORY ALLOCATION ERROR will
occur.

- 99 -

intel8.2 DISABLING CONTROL-C
In some applications, it may be desirable or even a requirement that program execution not accidentallybe halted. Under "normal" operation the execution of any MCS BASIC-52 program can be terminatedby typing a "control-C" on the console device. However, it is possible to disable the "control-C" breakfunction in MCS BASIC-52. This is accomplished by setting BIT 48 (30H) to a one. BIT 48 is locatedin internal memory location 38.0 (26.0H). This BIT may be set by executing the following statement in
an MCS BASIC-52 program:
DBY(38) = DBY(38).OR.01H
Once this BIT is set to a one, the control-C break function, for both LIST and RUN operations will bedisabled. The user has the option to create a custom break character or string of characters by using the
GET operator. The following is an example of how to implement a custom break character:
EXAMPLE:>10 STRING 100,10: A=1: REM INITIALIZE STRINGS>20 $(1) = "BREAK" : REM "BREAK" IS THE PASSWORD>30 DBY(38) = DBY(38).OR.1 : REM DISABLE CONTROL-C>40 FOR I=1 T0 1000 : REM DUMMY LOOP
>50 J=SIN(I)>60 K=GET : IF K<>0 THEN 100 ELSE NEXT I>70 END>100 IF K=ASC($(1),A) THEN A=A+1 ELSE A=1>110 REM TEST FOR MATCH>120 IF A=1 THEN NEXT I>130 IF A=6 THEN 200 ELSE NEXT I>140 END>200 PRINT "BREAK">210 DBY(38)=DBY(38).AND.0FEH : REM ENABLE CONTROL-C
In this example, typing the word BREAK will stop program execution. In other words, BREAK is a
password.

- 100 -

intel8.3 IMPLEMENTING "FAKE DMA"The MCS BASIC-52 device does not contain an hardware mechanism that supports Direct Memory
Access (DMA). However, the DMA function is supported in software by MCS BASIC-52. During
DMA operation MCS BASIC-52 guarantees that no external memory access will be performed. To
enable the DMA function, the following must be performed:
1) BIT 49, which is located in internal memory location 38.1 (26.1H) must be set to a one. This can beaccomplished in BASIC by using the statement -- DBY(38) = DBY(38).0R.02H
2) BIT 0 and BIT 7 of the SPECIAL FUNCTION REGISTER, IE (Interrupt enable) must be set to aone. This can be accomplished in BASIC by using the statement -- IE = IE.OR.81H
After the three BITS mentioned above are set to a one, external interrupt zero (/INT0) acts as a DMA
input pin. /INT0 is pin 12 on the 8052AH. Whenever /INT0 is pulled low (to a logical zero state), the
MCS BASIC-52 device will enter the DMA mode and no accesses will be made to external memory.
Toacknowledge that MCS BASIC-52 has entered the DMA mode, MCS BASIC-52 outputs a zero on pin 7(P1.6). In essence, PORT 1.6 is the /DMA ACK pin of the MCS BASIC-52 device. In mostapplications, this pin would be used to disable three-state buffers that would be placed on PORT2,
PORT0, and the address latch of the MCS BASIC-52 system. After the user pulls the /INT0 pin high,
MCS BASIC-52 will output a one on P1.6 and normal program execution will continue. During this"fake DMA" cycle, the MCS BASIC-52 program does nothing except wait for the /INT0 pin to bepulled high. So, program execution is halted.
It should be noted that although this "fake DMA" operation does provide the same functionality as anormal DMA hardware mechanism, it also takes substantially longer for the normal DMA REQUEST -DMA ACKNOWLEDGE cycle to be performed. That is because MCS BASIC-52 uses interrupts toimplement the DMA operation, instead of dedicated hardware. As a general rule, cycle stealing DMA is
not an option with MCS BASlC-52's "fake" DMA. Only "burst mode" DMA cycles can be implemented
without a significant time penalty. When "fake DMA" is implemented, the user must provide three-state
buffers on the PORT2, PORT0, and the address latch of the MCS BASIC-52 system.

- 101 -

intel
8.4 RUN TRAP OPTION (Version 1.1 Only)
Version 1.1 of MCS BASIC-52 permits the user to trap the interpreter in the RUN MODE. This option
is evoked by putting a 34H (52D) in external data memory location 5EH (94D). After a 34H (52D) isplaced in external data memory location 5EH (94D) the MCS BASIC-52 interpreter will be trapped in
the RUN mode forever or until the contents of external data memory location is changed to somethingother than 34H (52D). If no program is present when a 34H (52D) is placed in location 5EH (94D),MCS BASIC-52 will print the READY message forever and it will be time to RESET the device. The
RUN TRAP option can be employed with the other RESET options to permit the user to execute a
program from RAM on a RESET or power-up condition when some type of battery back-up memory
scheme is employed.

- 102 -

intel
8.5 ANOMALIES
Most dictionaries define an anomaly as a deviation from the normal or common order or as anirregularity anomalies to an extreme become "BUGS" or something that is wrong with the program.
Like all programs, MCS BASIC-52 contains some anomalies, hopefully, no bugs. The purpose ofmentioning the known anomalies here is that it may save the programmer some time, should strange
things happen during program execution. The known anomalies deal mainly with the way MCS
BASIC-52 compacts or tokenizes the BASIC program. The known anomalies and cautions are as
follows:
1) When using the variable H after a line number, make sure you put a space between the line number
and the H, or else BASIC will assume that the line number is a HEX number.
EXAMPLES:>20H=10 (WRONG)
>LIST
32
=10

>20 H=10 (RIGHT)
>LIST
20
H=10

2) When using the variable I before an ELSE statement, make sure you put a space between the I and
the ELSE statement, or else BASIC will assume that the IE portion of IELSE is the special function
operator IE.
EXAMPLES:>20 IF I>10 THEN PRINT IELSE 100>LIST20
IF I>10 THEN PRINT IELSE 100 (WRONG)>20 IF I>10 THEN PRINT I ELSE 100>LIST
20
IF 1>10 THEN PRINT I ELSE 100 (RIGHT)
3) A Space character may not be placed inside the ASC() operator. In other words, a statement like
PRINT ASC( ) will yield a BAD SYNTAX ERROR. Spaces may be placed in strings however, so astatement like LET $(1) = "HELLO, HOW ARE YOU" will work properly. The reason ASC( )yields an error is because MCS BASIC-52 eliminates all spaces when a line is processed. so ASC( )will be stored as ASC() and MCS BASIC-52 interprets this as an error.

- 103 -

intelCHAPTER 9
Assembly Language Linkage

9.1 OVERVIEW
NOTE: This section assumes that the designer has an understanding of the architecture and assembly
language of the MCS-51 Microcontroller family!!!
MCS BASIC-52 contains a complete library of routines that can easily be accessed with assembly language
CALL instructions. The advantage of using assembly language is that it offers a significant improvement inexecution speed relative to interpreted BASIC. In order to successfully interface MCS BASIC-52 with anassembly language program, the software designer must be aware of a few simple facts.
READ THIS CAREFULLY!!!
1. MCS BASIC-52 uses REGISTER BANKS 0, 1, and 2 (RB0, RB1, and RB2). REGISTER BANK 3(RB3) is never used except during a PGM statement. RB3 is designated the USER REGISTER BANK
and the users can do whatever they want to with REGISTER BANK 3 (RB3) and MCS BASIC-52 will
never alter the contents of this REGISTER BANK except during the execution of a PGM statement.
The contents of REGISTER BANK 3 (RB3) can be changed by executing a DBY ([expr]) = [0 to 255]
statement. Where the [expr] evaluates to a number between 24 (18H) and 31 (1FH) inclusive. In
addition, INTERNAL MEMORY LOCATIONS 32 (20H) and 33 (21 H) are also NEVER used by
MCS BASIC-52. These two BIT and/or BYTE addressable locations are specifically reserved for
assembly language programs.
2. MCS BASIC-52 uses REGISTER BANK 0 (RB0) as the WORKING REGISTER FILE. Wheneverassembly language is used to access MCS BASlC-52's routines, the WORKING REGISTER FILE,REGISTER BANK 0 (RB0) MUST BE SELECTED!!! This means that the USER MUST MAKE
SURE THAT REGISTER BANK 0 (RB0) IS SELECTED BEFORE CALLING ANY OF MCS
BASlC-52's ROUTINES. This is done simply by setting BITS 3 and 4 in the PSW equal to ZERO. If
this is not done, MCS BASIC-52 will "KICK OUT" the USER and NO operation will be performed.
When an ASSEMBLY LANGUAGE program is accessed by using the MCS BASlC-52's CALL
instruction, REGISTER BANK 0 (RB0) will always be selected. So unless the user selects REGISTERBANK 3 (RB3) in assembly language, it is NOT NECESSARY to change the designated REGISTER
BANK.
3. ALWAYS ASSUME THAT MCS BASIC-52 DESTROYS THE CONTENTS OF THE WORKINGREGISTER FILE AND THE DPTR, UNLESS OTHERWISE STATED IN FOLLOWING DOCUMENTATION.
4. Certain routines in MCS BASIC-52 require that REGISTERS be initialized BEFORE the user CALLS
that specific ROUTINE. These registers are ALWAYS in the WORKING REGISTER FILE,REGISTER BANK 0 (RB0).
5. Certain routines in MCS BASIC-52 return the result of an operation in a register or registers. The result
registers are ALWAYS in the WORKING REGISTER FILE, REGISTER BANK 0, (RB0).

- 104 -

intel
9.1 OVERVIEWREAD THIS CAREFULLY!!!
6. MCS BASIC-52 loads the INTERNAL STACK POINTER (SPECIAL FUNCTION REGISTERSP) with the value that is in INTERNAL MEMORY LOCATION 62 (3EH). MCS BASIC-52
initializes INTERNAL MEMORY LOCATION 62 (3EH) by writing a 77 (4DH) to this location
after a hardware RESET. MCS BASIC-52 does NOT use any memory beyond 77 (4DH) foranything EXCEPT STACK SPACE. If the user wants to ALLOCATE some additional internal
memory for their application, this is done by changing the contents of INTERNAL MEMORYLOCATION 62 (3EH) to a value that is GREATER than 77 (4DH). This will allocate the
INTERNAL MEMORY LOCATIONS from 77 (4DH) to the value that is placed in INTERNALMEMORY LOCATION 62 (3EH) to the user. As a guideline, it is a good idea to allow at least 48(30H) bytes of stack space for MCS BASIC-52. The bad news about reducing the stack space is that
it will reduce the amount of nested parentheses that MCS BASIC-52 can evaluate in an expression[expr]. This will either cause a I-STACK ERROR or will cause a fatal CPU "crash." Use caution
and DON'T allocate more memory than you need.
EXAMPLE OF THE EFFECTS OF ALTERING THE STACK ALLOCATION:
COMMENTS>PRINT DBY(62)77

AFTER RESET INTERNAL MEMORY LOCATION 6
CONTAINS A 77

>PRINT (1*(2*(3)))
6BASIC HAS NO PROBLEM EVALUATING 3 LEVELSOF NESTED PARENRHESIS>DBY(62)=230

NOW ALLOCATE 255-230 = 25 BYTES OF STACKSPACE TO BASIC, REMEMBER, THE STACK ON
THE 8052AH GROWS "UP"

>PRINT (1*(2*(3)))
ERROR: I-STACK
READY>DBY(62)=210>PRINT (1*(2*(3)))
6BASIC CANNOT EVALUATE THIS EXPRESSIONBECAUSE IT DOES NOT HAVE ENOUGH STACKNOW ALLOCATE 255-210 = 45 BYTES OF STACKSPACE TO BASIC
THE I-STACK ERROR GOES AWAY

7. Throughout this section a 16-BIT REGISTER PAIR is designated-Rx:Ry, where Rx is the most significant byte and Ry is the least significant byte.
EXAMPLE:
R3:R1 - R3=MOST SIGNIFICANT BYTE. R1=LEAST SIGNIFICANT BYTE

- 105 -

intel
9.2 GENERAL PURPOSE ROUTINES
Accessing MCS BASIC-52 routines with assembly language is easy. The user just loads the ACCUMULATOR with a specific value and CALLS LOCATION 48 (30H). The value placed in the
ACCUMULATOR determines what operation will be performed. Unless otherwise stated, the
CONTENTS of the DPTR and REGISTER BANK 0 (RB0) will ALWAYS be altered when callingthese routines. The generalized form for accessing MCS BASlC-52's routines is as follows:
ANL PSW,#11100111B
MOV A,#OPBYTECALL 30H; make sureRB0 isselected; load the
instruction; execute theinstruction

The value of OPBYTE determines what operation will be performed. The following operations can be
performed:
OPBYTE = 0 (00H) RETURN TO COMMAND MODE
This instruction causes MCS BASIC-52 to enter the COMMAND MODE. Control of the CPU ishanded back to the MCS BASIC-52 interpreter and BASIC will respond by outputting a READY and a
PROMPT character (>).
OPBYTE = 1 (01H) POP ARGUMENT STACK AND PUT VALUE IN R3:R1
This instruction converts the value that is on the ARGUMENT STACK to a 16 BIT BINARY
INTEGER and returns the BINARY INTEGER in registers R3 (high byte) and R1 (low byte) of
REGISTER BANK 0 (RB0). The ARGUMENT STACK gets popped after this instruction is executed.
If the value on the ARGUMENT STACK cannot be represented by a 16-BIT BINARY NUMBER (i.e.
it is NOT between 0 and 65535 (0FFFFH) inclusive), BASIC WILL TRAP THE ERROR and print a
BAD ARGUMENT ERROR MESSAGE. The BINARY VALUE returned is TRUNCATED, NOT
ROUNDED.
EXAMPLE:
BASIC PROGRAM -10 PUSH 20
20 CALL 5000HASSEMBLY LANGUAGE PROGRAM - ORG 5000H
MOV A.#01H; load opbyte
CALL 30H; RB0 still selected;; at this point R3 (of RB0) = 01H; and R1 (of RB0) = 04H; so, R3:R1 = 260, which was the value; that was placed on the ARGUGENT STACK- 106 -

intel
9.2 GENERAL PURPOSE ROUTINESCOMMENTS ON THE NEXT TWO INSTRUCTIONS:
The next two instructions permit the user to transfer floating point numbers between an assembly
language program and MCS BASIC-52. The user provides the address where a floating point number isstored or will be stored in a 16-bit REGISTER PAIR. Depending on the operation requested, thefloating point numbers are either PUSHED ON or POPPED OFF MCS BASlC-52's ARGUMENT
STACK. This instruction permits the user to keep track of variables in assembly language and bypass
the relatively slow
procedure BASIC must follow.
As mentioned earlier, when a floating point number is PUSHED onto the ARGUMENT STACK, the
ARGUMENT STACK POINTER is decremented by a count of 6. This is because a floating point
number requires 6 bytes of RAM storage. Although it may seem confusing to the novice, the LAST
value placed on the ARGUMENT STACK is referred to as the value on the TOP of the ARGUMENT
STACK, even though it is on the BOTTOM of the STACK relative to the sequential numbering of
memory addresses. No one knows why this is so.
The ARGUMENT STACK resides in EXTERNAL RAM MEMORY LOCATIONS 301 (12DH)
through 510 (1FEH). The lower BYTE of the ARGUMENT STACK POINTER resides in INTERNALMEMORY LOCATION 9 (09H). MCS BASIC-52 always assumes that the upper BYTE (higher orderaddress) of the ARGUMENT STACK POINTER is 1 (01H). The software designer can use thisinformation, along with the next two instructions to perform operations like copying the stack.
OPBYTE = 2 (02H) PUSH THE FLOATING POINT NUMBER ADDRESSED BY REGISTERPAIR R2:R0 ONTO THE ARGUMENT STACK.
R2 and R0 (in REGISTER BANK 0, RB0) contain the ADDRESS (R2 = high byte, R0 = low byte) of
the location where the floating point number is stored. After this instruction is executed the floating
point number that the REGISTER PAIR R2:R0 points to is PUSHED onto the TOP of the
ARGUMENT STACK. The ARGUMENT STACK POINTER automatically gets DECREMENTED,by a count of 6, when the value is placed on the stack. A floating point number in MCS BASIC-52
requires 6 BYTES of RAM storage. The register Pair R2:R0 points to the MOST SIGNIFICANTBYTE of the floating point number and is DECREMENTED BY 6 after the CALL instruction is
executed. So, if R2:R0 = 7F18H before this instruction was executed, it would equal 7F12H after this
instruction was executed.

- 107 -

intel
9.2 GENERAL PURPOSE ROUTINESOPBYTE = 3 (03H) POP THE ARGUMENT STACK AND SAVE THE FLOATING POINT
NUMBER IN THE LOCATION ADDRESSED BY R3:R1.
The TOP of the ARGUMENT STACK is moved to the location pointed to by the REGISTER PAIR
R3:R1(R3 = high byte, R1 = low byte, in REGISTER BANK 0, RB0). The ARGUMENT STACK
POINTER is automatically INCREMENTED BY 6. Just as in the previous PUSH instruction, the REGISTER PAIR R3:R1 points to the MOST SIGNIFICANT BYTE of the floating point number and is
DECREMENTED BY 6 after the CALL instruction is executed.
EXAMPLE OF USER PUSH AND POP:
BASIC PROGRAM:>5 REM PUT 100 AND 200 ON THE ARGUMENT STACK>10 PUSH 100,200>15 REM CALL THE USER ROUTINE TO SAVE NUMBERS>20 CALL 5000H>25 REM CLEAR THE STACK>30 CLEARS>35 REM USE ASM TO PUT NUMBERS BACK ON STACK>40 CALL 5010H>50 POP A,B>60 PRINT A,B>RUN
100 200READY

ASM PROGRAM:

ORG 5000H
MOV R3,#HIGH USER_SAVE
MOV R1,#LOW USER_SAVE
MOV A,#03H
CALL 30H
MOV A,#03H
CALL 30HRET;
ORG 5010H
MOV R2,#HIGH USER_SAVE
MOV R0,#LOW USER_SAVE
MOV A,#02H
CALL 30H
MOV A,#02H
CALL 30H
RET- 108 -; LOAD POINTERS TO WHERE; NUMBERS WILL BE SAVED.
; LOAD OPBYTE; SAVE ONE NUMBER; LOAD OPBYTE AGAIN; SAVE ANOTHER NUMBER; BACK TO BASIC;LOAD ADDRESS WHERE;NUMBERS ARE STORED;LOAD OPBYTE;PUT ONE NUMBER ON STACK;LOAD OPBYTE;NEXT NUMBER ON STACK;BACK TO BASICintel
9.2 GENERAL PURPOSE ROUTINESOPBYTE = 4 (04H) PROGRAM A PROM USING R3:R1 AS THE SOURCE ADDRESSPOINTER, R2:R0 AS THE DESTINATION (PROM) ADDRESS POINTER, AND R7:R6 AS
THE BYTE COUNTER.
This instruction assumes that the DATA to be programmed into a PROM is stored in external memory and that
the REGISTER PAIR R3:R1 (in RB0) contains the address of this external memory. REGISTER PAIR R7:R6
contains the total number of bytes that are to be programmed. The PROM is programmed sequentially and every
time a byte is programmed the REGISTER PAIR R7:R6 is decremented and the REGISTER PAIRS R3:R1 andR2:R0 are incremented. Programming continues until R7:R6 equals ZERO. The REGISTER PAIR R2:R0 must
be initialized with the desired ADDRESS of the EPROM to be programmed MINUS 1. This may sound strange,
but that is the way it works. So, if you wanted to program an EPROM starting at address 9000H, with the datastored in address 0D00H and you wanted to program 500 BYTES, then the registers would be set up as follows:
R2:R0 = 8FFFH, R3:R1 = 0D00H, and R7:R6 = 01F4H (500 decimal). You would then put a 4 (04H) in the
ACC and CALL location 30H.
NOTE: In Version 1.0, if an ERROR OCCURS DURING PROGRAMMING, MCS BASIC-52 WILL TRAP
THE ERROR AND ENTER THE COMMAND MODE. The user cannot handle errors that occur during the
EPROM programming operation!!!!
In Version 1.1, programming errors will only be trapped if the MCS BASIC-52 device is in the COMMAND
MODE. If the MCS BASIC-52 device is in the run mode, control will be passed back to the user. The user mustthen examine registers R6 and R7. If R6 = R7 = 0, then the programming operation was successfully completed,
if these registers do not equal zero then registers R2:R0 contain the address of the EPROM location that failed toprogram. This feature in Version 1.1 permits the user to program EPROMS in embedded applications andmanage errors, should they occur in the programming process, without trapping to the command mode.
In addition to setting up the pointers previously described, the user must also initialize the INTERNALMEMORY locations that control the width of the programming pulse. This gives the user complete control overthis critical prom programming parameter. The internal memory locations that must be initialized with thisinformation are 64 (40H) and 65 (41H). These locations are treated as a 16 bit register pair with location 64 (40H)
designated as the most significant byte and location 65 (41H) as the least significant byte. Locations 64 (40H) and65 (41H) are loaded into TH1 (TIMER1 high byte) and TL1 (TIMER1 low byte) respectively. The width of the
programming pulse, in microseconds is determined by the following equation:
WIDTH = (65536-256*DBY(64)+DBY(65))*12/XTAL microseconds
conversely;
DBY(64):DBY(65) = 65536-WIDTH*XTAL/12
The proper values for the "normal" 50 millisecond programming algorithm and the 1 millisecond "INTELligent"algorithm are calculated and stored by MCS BASIC-52 in external memory locations 296:297 ( 128H: 129H)
and 298:299 ( 12AH: 12BH) respectively. If the user wants to use the pre- calculated values the statements
DBY(64) = XBY(296) and DBY(65) = XBY(297) may be used to initialize the prom programming width for thenormal algorithm and the statements DBY(64) = XBY(298) and DBY(65) = XBY(299) can be used to initialize
for the INTELligent algorithm.

- 109 -

intel
9.2 GENERAL PURPOSE ROUTINESTo select the "INTELLIGENT" EPROM PROGRAMMING algorithm the directly addressable BIT 51(33H) MUST be set to 1 before the EPROM PROGRAMMING routine is called. The "STANDARD"
50 ms EPROM PROGRAMMING algorithm is selected by CLEARING BIT 51 (33H) (i.e. BIT 51 = 0)
before calling the EPROM PROGRAMMING routine. The directly addressable BIT 51 is located ininternal memory location 38.3 (26.3H) (BIT 3 of BYTE 38 (26H) in internal memory). This BIT can be
SET or CLEARED by the BASIC STATEMENTS DBY(38) = DBY(38).0R.08H to SET and
DBY(38)=DBY(38).AND.0F7H to CLEAR. Of course, the user can set or clear this bit in assembly
language with a SETB 51 or CLR 51 instruction.
The user must also turn on the EPROM PROGRAMMING voltage BEFORE calling the EPROM PROGRAMMING routine. This is done by CLEARING BIT P1.5, the fifth BIT on PORT 1. This too can bedone in BASIC with a PORT1 = PORT1.AND.0DFH instruction or in assembly language with a CLR
P1.5 instruction. The user must also set this bit when the PROM PROGRAMMING procedure iscomplete.
This instruction assumes that the hardware surrounding the MCS BASIC-52 device is the same as thesuggestions in the EPROM PROGRAMMING chapter of this manual.

- 110 -

intel
9.2 GENERAL PURPOSE ROUTINESOPBYTE = 5 (05H) INPUT A STRING OF CHARACTERS AND STORE IN THE BASICINPUT BUFFER.
This instruction inputs a line of text from the console device and saves the information in the MCS
BASIC-52's input buffer. MCS BASlC-52's input buffer begins at EXTERNAL MEMORYLOCATION 7 (0007H). All of the line editing features available in MCS BASIC-52 are implemented
in this instruction. If a control-C is typed during the input process, MCS BASIC-52 will trap back into
the command mode. A carriage return (cr) terminates the input procedure.
OPBYTE = 6 (06H) OUTPUT THE STRING OF CHARACTERS POINTED TO BY THE
REGISTER PAIR R3:R1 TO THE CONSOLE DEVICE.
This instruction is used to OUTPUT a string of characters to the console device. R3:R1 contains theinitial address of this string. The string can either be stored in PROGRAM MEMORY or EXTERNALDATA MEMORY. If BIT 52 (34H) (which is BIT 4 of internal RAM location 38 (26H)) is set, theoutput will be from PROGRAM MEMORY. If BIT 52 is cleared, the output will be from EXTERNALDATA MEMORY. The DATA stored in MEMORY is sent out to the console device one byte at a time
and the memory pointer is incremented. The output is stopped when a termination character is read. The
termination character for PROGRAM MEMORY and EXTERNAL DATA MEMORY are different.
The termination character for EXTERNAL DATA MEMORY is a (cr) 0DH. The termination character
for PROGRAM MEMORY is a " or 22H.
OPBYTE = 7 (07H) OUTPUT A CARRIAGE RETURN-LINE FEED SEQUENCE TO THE
CONSOLE DEVICE.
Enough said.
OPBYTE = 128 (80H) OUTPUT THE CHARACTER IN R5 (REGISTER BANK 0) TO THE
CONSOLE DEVICE.
This routine takes the character that is in R5 (register bank 0) and directs it to the console device. Any
console device may be selected (i.e. U0 or UI or the software serial port).

- 111 -

intel
9.2 GENERAL PURPOSE ROUTINESOPBYTE = 144 (90H) OUTPUT THE NUMBER ON THE TOP OF ARGUMENT STACK TOTHE CONSOLE DEVICE.
The floating point number that is on the top of the argument stack is outputted to the console device.
The FORMAT is determined by the USING statement. The argument stack is POPPED after the output
operation.
OPBYTE = 154 (9AH) THE 16 BIT NUMBER REPRESENTED BY REGISTER PAIR R2:R0
IS PUSHED ON THE ARGUMENT STACK.
This instruction converts the 16 bit register pair R2:R0 to a floating point number and pushes thisnumber onto the argument stack. This instruction is the converse of the OPBYTE = 1 instruction.

- 112 -

intel
9.3 UNARY OPERATORSThe next group of instructions perform an operation on the number that is on the TOP of the
ARGUMENT STACK. If the TOP of the ARGUMENT STACK is represented by the symbol [TOS],
then the following instructions would take the general form:[TOS] < OP [TOS]
Where OP is one of the following operators:
OPBYTE = 24 (18H) -- ABSOLUTE VALUE[TOS] < ABS([TOS]). The [TOS] is replaced by the absolute value of [TOS].
OPBYTE = 25 (19H) -- INTEGER[TOS] < INT([TOS]). The [TOS] is replaced by the integer portion of [TOS].
OPBYTE = 26 (1AH) -- SIGN[TOS] < SGN([TOS]). If [TOS] > 0 then [TOS] = 1, if [TOS] = 0 then [TOS] = 0, and if [TOS] < Othen [TOS] = -1.
OPBYTE = 27 (1BH) -- ONE'S COMPLEMENT[TOS] < NOT([TOS]). [TOS] must be a valid integer.
OPBYTE = 28 (1CH) -- COSINE OPERATOR[TOS] < COS([TOS]). [TOS] must be between +-200000.
OPBYTE = 29 (1DH) -- TANGENT OPERATOR[TOS] < TAN([TOS]). [TOS] must be between +- 200000 and [TOSl cannot equal PI/2,
3*PI/2, 5*PI/2..... (2*N+1)*PI/2.

- 113 -

intel
9.3 UNARY OPERATORSOPBYTE = 30 (1EH) -- SINE OPERATOR[TOSl < SIN([TOS]). [TOS] must be between +-200000.
OPBYTE = 31 (1FH) -- SQUARE ROOT[TOS] < SQR ([TOS]). [TOS] must be >= 0.
OPBYTE = 32 (20H) -- CBY OPERATOR[TOS] < CBY ([TOS]). [TOS] must be a valid integer.
OPBYTE = 33 (21H) -- E TO THE [TOS] OPERATOR[TOS] < e (2.7182818)**[TOS]. e is raised to the [TOS] power.
OPBYTE = 34 (22H) -- ATN OPERATOR[TOS] < ATN([TOS]). Arctangent, the value returned is between +- PI/2.
OPBYTE = 35 (23H) -- LOG OPERATOR (natural LOG)[TOS] < LOG([TOS]) -- [TOS] must be > 0.
OPBYTE = 36 (24H) -- DBY OPERATOR[TOS] < DBY([TOS]). [TOS] must be between 0 and 255 inclusive.
OPBYTE = 37 (25H) -- XBY OPERATOR[TOS] < XBY([TOS]). [TOS] must be a valid integer.

- 114 -

intel
9.4 SPECIAL OPERATORSThe next group of instructions place a value on the stack. The value placed on the stack is as follows:
OPBYTE = 38 (26H) -- PI[TOS] = PI. PI (3.1415926) is placed on the [TOS].
OPBYTE = 39 (27H) -- RND[TOS] = RND. A random number is placed on the [TOS].
OPBYTE = 40 (28H) -- GET[TOS] = GET. The value of the SPECIAL FUNCTION OPERATOR, GET is put on the [TOS].
OPBYTE = 41 (29H) -- FREE[TOS] = FREE. The value of the SYSTEM CONTROL VALUE, FREE is put on the [TOS].
OPBYTE = 42 (2AH) -- LEN[TOS] = LEN. The value of the SYSTEM CONTROL VALUE, LEN is put on the [TOS].
OPBYTE = 43 (2BH) -- XTAL[TOS] = XTAL. The value of the SPECIAL FUNCTION OPERATOR, XTAL is put on the [TOS].
OPBYTE = 44 (2CH) -- MTOP[TOS] = MTOP. The value of the SYSTEM CONTROL VALUE, MTOP is put on the [TOS].

- 115 -

intel
9.4 SPECIAL OPERATORSOPBYTE = 45 (2DH) -- TIME[TOS] = TIME. The value of the SPECIAL FUNCTION OPERATOR, TIME is put on the [TOS].
OPBYTE = 46 (2EH) -- IE
TOS] = IE The value of the IE register is put on the [TOS].
OPBYTE = 47 (2FH) -- IP[TOS] = IP. The value of the IP register is put on the [TOS].
OPBYTE = 48 (30H) -- TIMER0[TOS] = TIMER0. The value of TIMER0 (TH0:TL0) is put on the [TOS].
OPBYTE = 49(31H) -- TIMER1[TOS] = TIMERI. The value of TIMERI (TH1:TL1) is put on the [TOS].
OPBYTE = 50 (32H) -- TIMER2[TOS] = TIMER2. The value of TIMER2 (TH2:TL2) is put on the [TOS].
OPBYTE = 51 (33H) -- T2CON[TOS] = T2CON. The value of the T2CON register is put on the [TOS].
OPBYTE = 52 (34H) -- TCON[TOS] = TCON. The value of the TCON register is put on the [TOS].

- 116 -

intel
9.4 SPECIAL OPERATORSOPBYTE = 53 (35H) – TMOD
[TOS] = TMOD. The value of the TMOD register is put on the [TOS].
OPBYTE = 54 (36H) -- RCAP2[TOS] = RCAP2. The value of the RCAP2 registers (RCAP2H:RCAP2L) is put on the [TOS].
OPBYTE = 55 (37H) -- PORT1[TOS] = PORT1. The value of the PORT1 (P1) pins is placed on the [TOS].
OPBYTE = 56 (38H) -- PCON[TOS] = PCON. The value of the PCON register is put on the [TOS].

- 117 -

intel
9.5 DUAL OPERAND OPERATORSThe next group of instructions assume that TWO values are on the ARGUMENT STACK. If number on
the TOP of the ARGUMENT STACK is represented by the symbol [TOS] and the number NEXT to
TOP of the ARGUMENT STACK is represented by the symbol [NxTOS] and the ARGUMENT
STACK POINTER is represented by the symbol AGSP, then the following instructions would take thegeneral form:
TEMP1 = [TOS]
TEMP2 = [NxTOS]AGSP < AGSP + 6
RESULT = TEMP2 OP TEMP1[TOS] = RESULT
Where OP is one of the following operators to be described. NOTE that the group of instructions
ALWAYS POP the ARGUMENT STACK by one FLOATING POINT NUMBER SIZE (i.e. 6
BYTES).
ERRORS can be handled in two different ways with the ADD, SUBTRACT, MULTIPLY, and DIVIDE
routines. One option is to let MCS BASIC-52 trap ERRORS, should they occur during the operation.
With this option MCS BASIC-52 will print the appropriate error message to the console device. The
other option passes a STATUS CODE to the user. After the operation the Accumulator contains thestatus code information. The Status information is as follows:
ACC.0 -- ARITHMETIC UNDERFLOW
ACC.1 -- ARITHMETIC OVERFLOW
ACC.2 -- RESULT WAS ZERO (not an error, just a condition)
ACC.3 -- DIVIDE BY ZERO
ACC.4 -- NOT USED, ZERO RETURNED
ACC.5 -- NOT USED, ZERO RETURNED
ACC.6 -- NOT USED, ZERO RETURNED
ACC.7 -- NOT USED, ZERO RETURNED
If an ARITH. OVERFLOW or a DIVIDE BY ZERO ERROR occurs and the user is handling the error
condition, the floating point processor will return a result of +- 99999999E+ 127 to the argument stack.
The user can do what they want to with this result (i.e. use it or waste it). An ARITH. UNDERFLOWERROR will return to the argument stack a result of 0 (zero).

- 118 -

intel
9.5 DUAL OPERAND OPERATORSMCS BASIC-52 can perform the following DUAL OPERAND OPERATIONS:OPBYTE = 9 (09H) EXPONENTIATION -- The [NxTOS] value is raised to the [TOS] power.
RESULT = [NxTOS] ** [TOS]. NOTE -- [TOS] MUST BE LESS THAN 256.
OPBYTE = 10 (0AH) MULTIPLY
RESULT = [NxTOS] * [TOS]. If an ERROR occurs during this operation (i.e. ARITH. OVERFLOW or
UNDERFLOW) MCS BASIC-52 will trap the error and print the error message to the console device.
OPBYTE = 136 (88H) MULTIPLY
RESULT = [NxTOS] * [TOS]. If an error occurs during this operation, the status byte previously
discussed will be returned to the user.
OPBYTE = 11 (0BH) ADD
RESULT = [NxTOS] + [TOS]. BASIC handles errors.
OPBYTE = 130 (82H) ADD
RESULT = [NxTOS] + [TOS]. User handles errors.
OPBYTE = 12 (0CH) DIVIDERESULT = [NxTOS] / [TOS]. BASIC handles errors.
OPBYTE = 138 (8AH) DIVIDERESULT = [NxTOS] / [TOS]. User handles errors.
OPBYTE = 13 (0DH) SUBTRACTRESULT = [NxTOS] -- [TOS]. BASIC handles errors.

- 119 -

intel
9.5 DUAL OPERAND OPERATORSOPBYTE = 132 (84H) SUBTRACTRESULT = [NxTOS] - [TOS]. User handles errors.
OPBYTE = 14 (0EH) EXCLUSIVE OR
RESULT = [NxTOS] XOR [TOS], both values must be GREATER THAN OR EQUAL TO ZERO andLESS THAN OR EQUAL TO 65535 (0FFFFH).
OPBYTE = 15 (0FH) LOGICAL AND
RESULT = [NxTOS] and [TOS], both values must be GREATER THAN OR EQUAL TO ZERO andLESS THAN OR EQUAL TO 65535 (0FFFFH).
OPBYTE = 16 (10H) LOGICAL OR
RESULT = [NxTOS] OR [TOS], both values must be GREATER THAN OR EQUAL TO ZERO andLESS THAN OR EQUAL TO 65535 (0FFFFH).
OPBYTE = 18 (12H) TEST FOR EQUALITY
IF [NxTOS] = [TOS] then, RESULT = 65535 (0FFFFH), else RESULT = 0.
OPBYTE = 19 (13H) TEST FOR GREATER THAN OR EOUAL
IF [NxTOS] > = [TOS] then, RESULT = 65535 (0FFFFH), else RESULT = 0.
OPBYTE = 20 (14H) TEST FOR LESS THAN OR EQUAL
IF [NxTOS] < = [TOS] then, RESULT = 65535 (0FFFFH), else RESULT = 0.

- 120 -

intel
9.5 DUAL OPERAND OPERATORSOPBYTE = 21(15H) TEST FOR NOT EQUAL
IF [NxTOS] <> [TOS] then, RESULT = 65535 (0FFFFH), else RESULT = 0.
OPBYTE = 22(16H) TEST FOR LESS THAN
IF [NxTOS] < [TOS] then, RESULT = 65535 (0FFFFH), else RESULT = 0.
OPBYTE = 23(17H) TEST FOR GREATER THAN
IF [NxTOS] > [TOS] then, RESULT = 65535 (0FFFFH), else RESULT = 0.

- 121 -

intel
9.6 ADDED LINK ROUTINES TO VERSION 1.1
Version 1.1 of MCS BASIC-52 contains a number of useful assembly language link routines that were
not available in Version 1.0. Most of these routines were designed to be used in conjunction with the
new Command/Statement extensions that are described in Chapter 11 of this manual. The added linkroutines are as follows:
OPBYTE = 57 (39H) EVALUATE AN EXPRESSION WITHIN THE BASIC TEXT STRING
AND PLACE THE RESULT ON THE ARGUMENT STACK
This routine permits the user to evaluate a BASIC expression [expr] containing variables, operators andconstants. The result of the evaluated expression is placed on the floating point argument stack. This
lets the user evaluate expressions in "customized" statements and commands. An example of use of thisOPBYTE is given at the end of this section.
OPBYTE = 58 (3AH) PERFORM CRYSTAL DEPENDENT CALCULATIONS WITH THE
VALUE THAT IS ON THE ARGUMENT STACK
This routine is provided mainly to let the user write an assembly language RESET routine and perform
all of the crystal dependent calculations that are required by MCS BASIC-52. An example of acustomized RESET routine that uses this OPBYTE is presented in Chapter 11 of this manual.
OPBYTE = 63 (3FH) GET A CHARACTER OUT OF THE BASIC TEXT STRING
This routine permits the user to "pick" a character out of the BASIC program. For instance, in BASICthe user could have the following:
10

CALL 1000H A

If the user executed the following in assembly language at 1000H:
MOV
LCALL

A, #6330H

The character A would be returned in the accumulator. The Basic text pointer is located in location 8(8H) (low byte) and 10 (0AH) (high byte) of the internal ram on the MCS BASIC-52 device. If the user
were to implement the above function, the basic text pointer must be advanced to the carriage return atthe end of the statement before returning back to Basic. Failure to do this will cause a BAD SYNTAXERROR when the user returns back to Basic. The following OPBYTE can be used to advance the Basic
Text pointer.

- 122 -

intel
9.6 ADDED LINK ROUTINES TO VERSION 1.1
OPBYTE = 64 (40H) GET CHARACTER, THEN INCREMENT TEXT POINTER
This OPBYTE does the same thing as the previous one described, except that the BASIC text pointer is
INCREMENTED AFTER the character is read. An example of this OPBYTE is presented at the end of
this section.
OPBYTE = 65 (41H) INPUT A CHARACTER FROM THE CONSOLE DEVICE, PUT IT IN
THE ACCUMULATOR, THEN RETURN
This OPBYTE permits the user to input characters from MCS BASlC-52's console input routine. The
character is placed in the accumulator upon return.
OPBYTE = 66 (42H) ENTER THE RUN MODE
This OPBYTE permits the user to start the execution of an MCS BASIC-52 program from assembly
language. The user need only insure that locations 19 (13H) and 20 (14H) of internal data memory
contain the start address (high byte, low byte respectively) of the BASIC program.
OPBYTE = 129 (81H) INPUT AN ASCII FLOATING POINT NUMBER AND PLACE IT ON
THE ARGUMENT STACK. THE DPTR POINTS TO THE EXTERNALRAM LOCATION, WHERE THE ASCII TEXT STRING IS STOREDThis routine assumes that the user has placed an ASCII text string somewhere in memory and that thisASCII text string represents a valid floating point number. The user then puts the DPTR to the starting
address of this text string. After this OPBYTE is executed the text string will be converted to a valid
MCS BASIC-52 floating point number and placed on the argument stack and the DPTR will beadvanced to the end of the floating point number. If the DPTR does not point to a text string that
contains a valid floating point number, the accumulator will contain an 0FFH upon return.
OPBYTE = 152 (98) OUTPUT, IN HEX, TO THE CONSOLE OUTPUT DRIVER, THE
CONTENTS OF R3:R1
This routine is used to display HEX numbers, assuming that they are in registers R3:R1. If R3 = 0,
leading zeros can be suppressed by setting BIT 54 (36H) before calling this routine. If BIT 54 (36H) iscleared when this routine is called, the driver will always output four hex digits followed by thecharacter H. This routine always outputs a space character (20H) to the console device, before any hex
digits are output. BIT 54 (36H) is bit 6 of internal RAM location 38.

- 123 -

LOC

OBJ

1
2
3
4
5
6
7
8
9
1O
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

LINE
;*********************************************************************;; The following is an example of a program that uses the new OPBYTES; available in version 1.1 of MCS BASIC-52. This code is by no means; optimized but it is meant to demonstrate how the user can define; "customized" commands and statements in version 1.1 of MCS BASIC-52.
;; The new command defined here is DISPLAY. What it does is display a; region of external data memory to the console device. The syntax; for this statement is:;;
DISPLAY [expr], [expr];; Where the first expression is the starting address and the last
; expression is the ending address. In this example the DISPLAY is; treated like a command which means that it cannot be executed in; RUN mode.
;; The output for the DISPLAY command is as follows:;; ADDRESS then 16 Bytes of Characters i. e.
;; 1000H 00H 22H 33H 27H . . . . . . . .
;; Now on to the program.
;*********************************************************************SOURCE

ISIS-II MCS-51 MACRO ASSEMBLER V1.0
OBJECT MODULE PLACED IN:F4:DEMO HEXASSEMBLER INVOKED BY: ASM51 :F4:DEMO

MCS-51 MACRO ASSEMBLER

intel

9.6 ADDED LINK ROUTINES TO VERSION 1.1

EXAMPLE:- 124 -

OBJ

- 125 -

208A 7439
208C 120030

2087 302F63

207E 10207F 44495350
2083 4C4159
2086 FF

207C 2087

2078 90207E
2078 222078

2070 90207C
2073 222070

2048 D22D
204A 222048

2002 5A

2002

LOC

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

LINE
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
MOV
RET

ORG

MOV
RET

ORG

SETB
RET

ORG

DB

ORG

MOV
LCALL

JNP

DO DISPLAY:

DB

DB
DB

USER TABLE:DW

VECTOR TABLE:

SOURCE; Set up DPTR to Jump table; Set the bit that sags so; Tell basic that expansion option is; present; Set up DPTR to expansion table

A,#57
30H

47,DUMMY

0FFH

10H'DISPLAY'

DO_DISPLAY; Evaluate the first expression after; the keyword display, MCS BASIC-52; will handle any errors. The value; of the expression will be on the; Argument Stack.

; make sure that MCS BASIC-52 is in; the command mode. Bit 47 is set; if it is.

; End of table (short table); Token for Display; ASCII for display; This is the address of DISPLAY

DPTR,#USER_TABLE

2078H

DPTR,#VECTOR_TABLE

2070H

45

2048H

5AH

2002H

intel

9.6 ADDED LINK ROUTINES TO VERSION 1.1

- 126 -

105
106
107
108
109
110
111
112
113
114
115

20C6 C236

20C2 A91A
20C4 AB1B

20BD 7407
20BF 120030

20AE C3
20AF E518
20B1 951A
20B3 E519
20B5 951B
20B7 5004
20B9 E4

20AA 891A
20AC 8B1B

20A5 7401
20A7 120030

20A1 8918
20A3 8B19

209C 7401
209E 120030

2097 7439
2099 120030

2094 B42C6A

20BA 020030

LINE

71
72
73
74
79
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
9l
92
93
94
95
96
97
98
99
100
101
102
103
104

OBJ

208F 7440
2091 120030

LOC

LOOP2:

LOOP1:SOURCE

CLR

MOV
MOV

MOV
LCALL

LJMP

CLR
MOV
SUB8
MOV
SUBB
JNC
CLR

MOV
MOV

MOV
LCALL

MOV
MOV

MOV
LCALL

MOV
LCALL

CJNE

MOV
LCALL

; Get the character after the expression; and bump the BASIC text pointer

36H

R1.1AH
R3.18H

A,#7
30H

30H

C
A,18H
A,1AH
A,19H
A,1BH
LOOP2
A

1AH.R1
1BH.R3

A,#1
30H

18H,R1
19H,R3

A,#1
30H

A,#57
30H

; Don't suppress leading zeros; Output the Starting address; Do a carriage return, line feed

; (if display was a statement instead; of a command. this routine would; exit with a RET)

; If the carry is set, it's over
; Go to the command mode

; Check to make sure that the starting; or current address is <= the ending; address; Now everything is set up to loop

; Save the starting address in the user
; reserved locations 1AH and 18H; Convert the first expression (the; starting address) on the stack to; an Integer and put it in R3:R1; Save the ending address in the user
; reserved locations 18H and 19H. This
; is reserved as register bank 3; Convert the last expression (the; ending address) on the stack to; an integer and put it in R3:R1; Evaluate the next expression (the; ending address) and put it on the; Argument Stack

A,#',',C_ERROR; Make sure it is a comma, if not do an; error

A,#64
30H

intel

9.6 ADDED LINK ROUTINES TO VERSION 1.1

OBJ- 127 -

2106 7B21
2108 793B
210A D234
210C 7406
210E 120030

2101 7407
2103 120030

20F2 7B21
20F4 7915
20F6 D234
20F8 7406
20FA 120030
20FD E4
20FE 020030

20ED 7407
20EF 120030

20E5 E51A
20E7 540F
20E9 70E2
20EB 80C1

20DB F920DC 7B00
20DE D236
20E0 7498
20E2 120030

20D5 85821A
20D8 85831B

20D3 E0
20D4 A3

20CD 851A82
20D0 851B83

20C8 7498
20CA 120030

LOC

116
117
118
119
120
121
122
123
124
125
126
127
128
l29
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

LINE

C_ERROR:DUMMY:

LOOP3:SOURCE

MOV
MOV
SETB
MOV
LCALL

MOV
LCALL

MOV
MOV
SETB
MOV
LCALL
CLR
LJMP

MOV
LCALL

MOV
ANL
JNZ
SJMP

MOV
MOV
SETB
MOV
LCALL

MOV
MOV

MOVX
INC

MOV
MOV

MOV
LCALL

; Do a carriage return-line feed

; Loop until on a 16 Byte Boundary; Check to see if on a 16 byte boundary; Output the byte
; The high byte is always zero
; Suppress leading Zeros; Save the Address; Read the byte in external RAM; Bump to the next location; Now, set up to read 16 bytes; put address in DPTR

R3,#HIGH C_MSG
R1,#LOW C_MSG
52
A,#6
30H

A,#7
30H

; Do what we did before

R3,#HIGH D_MSG; Display the error message
R1,#LOW D_MSG
52; Print from ROM
A,#6
30H
A
; Go back to the command mode
30H

A,#7
30H

A,1AH
A,#0FH
LOOP3
LOOP1

R1,A
R3,#0
36H
A,#98H
30H

1AH,DPL
1BH,DPH

A,@DPTRDPTR

DPL,1AH
DPH,1BH

A,#98H30H

intel

9.6 ADDED LINK ROUTINES TO VERSION 1.1

OBJ- 128 -

C_MSG:

166
167

168
169

D_MSG:SOURCE

162
163
164
165

LINE

ASSEMBLY COMPLETE. NO ERRORS FOUND(that'- all it takes)

213B 594F5520
213F 4E454544
2143 20412043
2147 4F4D4D41
214B 20544F20
214F 4D414B45
2153 20444953
2157 504C4159
215B 20574F52
215F 4B22

2115 44495350
2119 4C415920
211D 49532041
2121 20434F4D
2125 4D414E44
2129 2C204E4F
212D 54204120
2131 53544154
2135 454D454E
2139 5422

2111 E42112 020030

LOC

END

DB

DB

CLR
LJMP

"YOU NEED A COMMA TO MAKE DISPLAY WORK""DISPLAY IS A COMMAND, NOT A STATEMENT"

A
30H

intel

9.6 ADDED LINK ROUTINES TO VERSION 1.1

intel9.7 INTERRUPTS
Interrupts can be handled by MCS BASIC-52 in two distinct ways. The first, which has already been
discussed, allows statements in an MCS BASIC-52 program to perform the required interrupt routine.
The ONTIME and ONEX1 statements enable this particular interrupt mode. Additionally, setting BIT
26.1H permits EXTERNAL INTERRUPT 0 to act as a "fake" DMA input and the details of this feature
are in the BELLS, WHISTLES, and ANOMALIES section of this manual. The second method ofhandling interrupts in MCS BASIC-52 allows the programmer to write assembly language routines toperform the interrupt task. This method yields a much faster interrupt response time, but, theprogrammer must exercise some caution.
All interrupt vectors on the MCS BASIC-52 device are "mirrored" to external PROGRAM MEMORYLOCATIONS 4003H through 402BH inclusive. The only MCS BASIC-52 STATEMENTS that enable
the interrupts on the 8052AH are the CLOCK1 and the ONEX1 STATEMENTS. If interrupts are NOT
enabled by these STATEMENTS, BASIC assumes that the USER is providing the interrupt routine inassembly language. The vectors for the various interrupts are as follows:
LOCATION---INTERRUPT
4003H------EXTERNAL INTERRUPT 0400BH------TIMER 0 OVERFLOW
4013H------EXTERNAL INTERRUPT 1
401BH------TIMER 1 OVERFLOW
4023H------SERIAL PORT
402BH------TIMER 2 OVERFLOW/EXTERNAL INTERRUPT 2
The programmer can enable interrupts in MCS BASIC-52 by using the statement IE = IE.OR.XXH,
where XX enables the appropriate interrupts. The bits in the interrupt register (IE) on the 8052AH are
defined as follows:
BIT

7

6

5

4

3

2

1

0

EA

X

ET2

ES

ET1

EX1

ET0

EX0

ENABLE UNDE- TIMER 2 SERIAL TIMER 1 EXT 1 TIMER 0 EXT 0
ALL
FINED
PORT

- 129 -

intel9.7 INTERRUPTS
Interrupts are enabled when the appropriate BITS in the IE register are set to a one. Details of the8052AH interrupt structure are available in the MICROCONTROLLER USERS MANUAL available
from INTEL.
IMPORTANT NOTE!!
Before MCS BASIC-52 vectors to the USER interrupt locations just described, the PROCESSORSTATUS WORD (PSW) is PUSHED onto the STACK. So, the USER does not have to save the PSW in
the assembly language interrupt routine!!! HOWEVER, THE USER MUST POP THE PSW BEFORE
RETURNING FROM THE INTERRUPT.
VERY IMPORTANT NOTE!!!
If the user is running some interrupt driven "background" routine while MCS BASIC-52 is running a
program, the user MUST NOT CALL any of the assembly language routines available in the MCS
BASIC-52 device. The only way the routines in the MCS BASIC-52 device can be accessed is when the
CALL statement in MCS BASIC-52 is used to transfer control to the users assembly language program.
The reason for this is that the MCS BASIC-52 interpreter must be in a "known" state before the user cancall the routines available in the MCS BASIC-52 device and a "random" interrupt does not guaranteethat the interpreter is in this known state. The user should use REGISTER BANK 3 to handle interrupt
routines in assembly language.

- 130 -

intel
9.8 RESOURCE ALLOCATION
Specific statements in MCS BASIC-52 require the use of certain hardware features on the device. If theuser wants to use these hardware features for interrupt driven routines, conflicts between BASIC and theassembly language routine may occur. To avoid these potential conflicts, the programmer needs to knowwhat hardware features are used by MCS BASIC-52. The following is a list of the COMMANDS and/or
STATEMENTS that use the hardware features on the 8052AH.
CLOCK1 -- uses TIMER/COUNTER 0 in the 13 bit 8048 mode.
PWM -- uses TIMER/COUNTER 1 in the 16 bit mode
LIST# -- uses TIMER/COUNTER 1 to generate baud rate in 16 bit mode
PRINT# -- same as LIST#PROG -- uses TIMER/COUNTER 1 for programming pulse
ONEX1 -- uses EXTERNAL INTERRUPT 1
In addition, TIMER/COUNTER 2 is used to generate the baud rate for the serial port. What thepreceding list means is that if CLOCK1, PWM, ONEX1, LIST#, PRINT#, and PROG
commands/statements are used by the programmer, the user MAY NOT use the associated
TIMER/COUNTER or EXTERNAL INTERRUPT pin for an assembly language routine.
MCS BASIC-52 initializes the TIMER/COUNTER modes by writing a 244 (0F4H), 16 (10H), and 52(34H) to the TCON, TMOD, and T2CON registers respectively. These registers are initialized only
during the RESET initialization sequence, and MCS BASIC-52 assumes that these registers are NEVER
changed.
So, if the user changes the contents of TCON, TMOD, or T2CON, something funny and/or disastrous isbound to happen if the Statements/Commands listed above are executed. If the user does not execute anyof the previously mentioned Statements or Commands, the user is free to use the interrupts in any way
suitable to the application.

- 131 -

intelCHAPTER 10
System Configuration

10.1 MEMORY / HARDWARE CONFIGURATIONMCS BASIC-52 always requires at least 1K bytes of external memory. After reset, MCS BASIC-52
sizes the external memory. If less than 1K bytes of external memory are available, MCS BASIC-52 willnot "sign-on." in fact, it will internally loop forever. This obviously is not too exciting, so it is wise tohang some external memory on the MCS BASIC-52 device.
MCS BASIC-52 sizes consecutative external memory locations from 0000H until a memory failure is
detected. The sizing operation is performed simply by writing a 5AH to an external memory location,then testing the location. If the particular memory location passes this test, BASIC then writes a 00H to
the location, then again, checks the location. MCS BASIC-52 only sizes the external memory fromlocations 0 through 0DFFFH. Memory locations 0E000H through 0FFFFH are reserved for user I/O
and/or assembly language programs.
The MCS BASIC-52 program resides in the 8K of ROM available in INTEL's 8052AH device and as aresult requires that external memory be "partitioned" in a specific manner. The architecture of the8052AH is NOT Von Neumann. This means that Data and Program Memory do not reside in the same
physical address space on the 8052AH. Specifically, the /RD (pin 17) and /WR (pin 16) pins on the
8052AH are used to enable DATA memory and /PSEN (pin 29) pin is used to enable PROGRAM
memory. Depending on the hardware configuration, MCS BASIC-52 operates in two distinct "memory"modes.
RAM ONLY MODE
In this mode of operation, Read/write memory is connected to the MCS BASIC-52 device starting at
memory address 0000H. Memory can be placed up to location 0FFFFH. In this mode of operation the
decoded addresses are used to generate the CHIP SELECT (/CS) signal for the RAM devices. The RD
pin on the 8052AH is used to generate the OUTPUT ENABLE (/OE) strobe and the /WR pin generates
the WRITE ENABLE (/WE or /WR) strobe. /PSEN is not used in the RAM only mode of operation. The
RAM only mode of operation offers the simplest hardware configuration available for the MCS BASIC52 device. An example of this configuration is shown in Figure 1. Since /PSEN is not used in the RAM
only mode, the user may not CALL assembly language routines. The RAM only also does not support
EPROM programming. In general, the RAM only mode will be used only to "check out" the device
during the initial system development stage.

- 132 -

intel10.1 MEMORY / HARDWARE CONFIGURATION
RAM / EPROM MODE
The RAM/EPROM mode of operation allows for the complete system implementation of MCS BASIC52. This mode of operation requires that external memory be mapped in a certain manner. The
RAM/EPROM memory configuration is as follows:
1) The /RD and the &WR pins on the MCS BASlC-52 device are used to enable RAM memory that isaddressed from 0000H to 7FFFH. Addresses are used to decode the chip select (/CS) for the RAM
devices and /RD and /WR are used to enable the /OE and /WE or (/WR) pins respectively.
2) The /PSEN pin on the MCS BASIC-52 device is used to enable EPROM memory that is addressedfrom 2000H to 7FFFH. Addresses are used to decode the chip select (/CS) for the EPROM devices
and /PSEN is used to enable the /OE pin.
3) For addresses between 8000H and 0FFFFH both the /RD and the /PSEN pin on the MCS BASIC-52
device are used to enable the memory. Either EPROM or RAM devices can be placed in this address
space. To permit both the /RD and the /PSEN pins to enable addresses in this address space, /RD and/PSEN must be logically "ANDED" together. This can be accomplished with a simple TTL gate suchas a 74LS08. The /WR pin on the MCS BASIC-52 device is used to write to RAM memory in thissame address space. The /PSEN and /RD signals do not have to be anded beyond address 7FFFH toenable MCS BASIC-52 to program an EPROM. This is only a suggestion since it will permit theuser to execute assembly language routines as well as MCS BASIC-52 programs that are located inthis address space.

- 133 -

intel10.1 MEMORY / HARDWARE CONFIGURATIONThis scheme of memory addressing actually permits MCS BASIC-52 to address 96K bytes of memory,
32K of RAM devices, 32K of EPROM/ROM devices and 32K of combined RAM/EPROM/ROM
devices. Since /RD and /PSEN are ANDED for addresses from 8000H through 0FFFFH, the 8052AH"looks like" a Von Neumann machine in this address space. The XBY and CBY special function
operators will yield the same value when their arguments are between 8000H and 0FFFFH.
When the EPROM programming feature in MCS BASIC-52 is used, BASIC assumes that the EPROM
to be programmed is addressed starting at location 8000H. MCS BASIC-52 can only program EPROMS
addressed between 8000H and 0FFFFH. When the PROG command is used for the first time, on anerased EPROM, MCS BASIC-52 stores this program beginning at address 8010H. Locations 8000H
through 800FH are used to save the baud rate information, plus configuration information. Somesuggestions for implementation of the RAM/EPROM mode are shown in figure 2.

- 134 -

intel10.2 EPROM PROGRAMMING CONFIGURATION / TIMING
With the proper hardware, the MCS BASIC-52 device can program just about any EPROM or EEPROM
device. The only requirement for EPROM programming is that the EPROM to be programmed isaddressed starting at location 8000H. MCS BASIC-52 requires very little external hardware to program
EPROMS. All of the critical EPROM programming timings are generated by three I/O port pins on the
MCS BASIC-52 device. These pins provide the following signals:
P1.3 -- ALE DISABLE
PORT 1, BIT 3 (pin 4 on the 8052AH) is used to DISABLE the ALE signal to the external latchedrequired by the 8052AH when external memory is addressed. This pin should be logically ANDED with
ALE. A simple TTL gate, such as a 74LS08 can be used to perform the ANDING function. Under
normal operation, P1.3 is in a logical high state (1). ONLY DURING EPROM PROGRAMMING IS
P1.3 PLACED IN A LOGICAL LOW STATE (0). Disabling the ALE signal to the external latch is
required to program EPROMS because of the way MCS BASIC-52 carries out the EPROM
programming process.
During programming, MCS BASIC-52 treats I/O PORT 0 and I/O PORT 2 as I/O ports, not as address/
data ports. MCS BASIC-52 first writes the low order address to be programmed to PORT 0. The data in
PORT 0 is then latched into the external address latch and then MCS BASIC-52 disables the ALE signal
to the latch by clearing bit P1.3. Thus, the low order address is "permanently" stored in the externallatch. MCS BASIC-52 then writes the high order address to PORT 2 and the DATA to be programmed
to PORT 0. So, the external address latch contains the low order address, PORT 2 contains the highorder address, and PORT 0 contains the DATA when EPROM programming occurs.
IMPORTANT NOTESWhen PORT 0 on the 8052AH is used as an I/O port, the output structure is an "open drain"
configuration. This requires that "pull-up" resistors be placed on PORT 0 to permit MCS B ASIC-52 toprogram EPROMS. Experimentally, 10K ohm pull-ups resistors on PORT 0 have yielded satisfactory
results.
In Version 1.1, INT0 must be kept high when programming EPROMs.

- 135 -

intel10.2 EPROM PROGRAMMING CONFIGURATION/TIMING
P1.4 -- PROGRAM PULSE WIDTHPORT 1, BIT 4 (pin 5 on the 8052AH) is used to provide the 50 millisecond or the 1 millisecond
programming pulse. The length of the programming pulse is determined by whether the "normal" or the"INTELligent" EPROM programming mode is selected. MCS BASIC-52 calculates the length of the
programming pulse from the assigned crystal value. So, be sure the proper XTAL has been assigned.
The accuracy of this pulse is within 10 CPU clock cycles. This pin is normally in a logical high (1) state.
It is asserted low (0) to program the EPROMS. Depending on the EPROM to be programmed this signal
will be used in different ways. More about this later.
P1.5 -- ENABLE PROGRAM VOLTAGE
PORT 1, BIT 5 (pin 6 on the 8052AH) is used to enable the EPROM programming voltage. This pin isnormally in a logical high (1) state. Prior to the EPROM programming operation, this pin is brought to alogical low (0) state. This pin is used to turn on or off the high voltage (12.5 volts to 25 volts, dependingon the EPROM) required to program the EPROMS.
The timing for the EPROM programming pins is shown in figure 3. The hardware required to program
different devices is shown in figure 4. Note that with very little external hardware the MCS BASIC-52
device can program virtually all commercially available EPROMS. Additionally, figure 5 suggests a
circuit using an INTEL 2816A EEPROM. This circuit also features a push button erase option.
IMPORTANT NOTEMCS BASIC-52 calculates the programming pulse width when the XTAL value is assigned. To insure
proper programming, make sure XTAL is assigned the proper value. MCS BASIC-52 performs the programming pulse width calculation to within 5 clock cycles, so the accuracy of the programming pulse iswell within the limits of any EPROM device.

10.3 SERIAL PORT IMPLEMENTATIONThe serial port I/O signals on the 8052AH are TTL compatible signals. They are typically notcompatible with most terminals. Figure 6 suggests hardware options for making the serial interface
compatible with terminals. The serial port is initialized by MCS BASIC-52 to the 8-bit uart mode. In thismode 8 data bits, plus one start and one stop bit are transmitted. Parity is not used.

- 136 -

+5V

10 µF

8.2k

30 pF

30 pF

- 137 -

17

16

15

14

13

12

11

10

31

9

18

19

PSEN
29

ALE
30

P3.7 (RD)P3.6 (WR)P3.5 (T1)

P3.4 (T0)P3.3 (INT1)

P3.2 (INT0)

P3.1 (TXD)

20

VSS

MCS
BASIC-

P3.0 (RXD)

EA

RESET

XTAL 2XTAL 1

VCC

40

+5V

P0.7

P0.6

P0.5

P0.4

P0.3

P0.2

P0.1

P0.0

P2.7

P2.6

P2.5

P2.4

P2.3

P2.2

P2.1

P2.0

P1.7

P1.6

P1.5

P1.4

P1.3

P1.2

P1.1

P1.0

DI4

8

32

33

34

35

36

37

38

39

28

27

26

25

24

DO7

DO6

DO5

DO4

Figure 1. Interface to 2K x 8 Static RAM

DI7

22
23

DI6

21

OE

DI3

7

E

DO2

DI2

6

DI5

DO1

DI1
DO3

DO0

DI0

GND

5

74
LS
373

VCC

4

3

2

1

+5V

O7

O6

O5

O4

O3

O2

O1

O0

A7

A6

A5

A4

A3

A2

A1

A0

VCC

+5V

OE

WE

A10

A9

A8

GND

2K
x
8
SRAM

CE

intel

100K

+5V

1489

4.7 µF- 138 -

1488

C1 = C2= 30 pF
FOR
XTALS
40 pF
FOR
CERAMICRESONATORS

C2

C1

4001

SERIALOUTEASERIAL
IN

XTAL2

XTAL1

RST

P1.4

P1.5

+5V

10K

4001

74LS08

CE

GND

+5

PSEN

Vcc

D0

A8

WE

A0

A0
OE

A2
A1

A2
A1

WE

A4
A3

6
A5

D0

A4
A3

6
A5

D0

CE

OE

WE

A0

A2
A1

A4
A3

6
A5

OE

2K
x8
STATIC
A7
RAM
A
D0

D1

D2

D3

D4

D5

D6

D7 A9 A10

RAM/EPROM from 32K to 64K on 8K boundaries

This system will decode: RAM from 0 to 16K on 2K boundaries, EPROM from 0 to 32K on 8K boundaries,

Figure 2A. Full system with EPROM power on protection (no DMA)GND OE

D1

+5

D2

RD

D3

D4

D5

WR

ALE

P1.3

P0.0

P0.1

Vcc

D1

D1

P0.2

D6

D2

D2

P0.3

74LS08

D3

D3

2K
x8
STATIC
A7
RAM
A

D4

D4

P0.5

2K
x8
STATIC
A7
RAM
A

D5

D5

P0.6

P0.4

D6

+5V

A8

D6

7 DO7
4 DO6
D
L DO5
O4
S DO3
3 DO2
7 DO1
3 DO0

CE
D7 A9 A10

A8+5V

D7 A9 A10

D7

A1

0
A0 E3

1

P0.7

P2.0

P2.1

P2.2

P2.3

+5V

E1
E2
A2

7407

A8

CE

WE

A0

A2
A1

A4
A3

6
A5

OE

2K
x8
STATIC
A7
RAM
A
D0

D1

D2

D3

D4

D5

D6

D7 A9 A10

74LS08

A1
A0

A3
A2

A5
A4

A7
A6

D0

D1

D2

D3

D4

D5

D6

D7

A8

A9

A10

A11

OE

2
7
1
2
8

10k

10k

PGM

A12

74LS082

3

ADDITIONAL CHIP ENABLES
8K-16K WITH 2K BOUNDARIES

CE

0

1

7
4
L
S
1
3
8

7
6
5
4

P2.4

A0

P2.6

7
4
L
S
1
3
8

7
6
5
4
3
2

1k

+5V

7407

4.7k

P2.5

A1

A2

E1

E2

E3

ADDITIONAL CHIP ENABLE 0-64K WITH 8K BOUNDARIES

P2.7

+5V

4001

PP

+5V

RD
ENABLE FOR
MEMORY ADDRESSEDFROM 32K TO 64K

V

2N44031

+21V

intel

1489

+5V

- 139 1488

C1 = C2= 30 pF
FOR
XTALS
40 pF
FOR
CERAMICRESONATORS

C2

C1

8.2k

10MFD

SERIALOUTEASERIAL
IN

XTAL2

XTAL1

INT0
DMA
RST REQUEST(P3.2)
P1.4

D7

D0

PSEN

7 DO7
4 DO6
D
L DO5
O4
S DO3
3 DO2
7 DO1
3 DO0
+5V

WE

A1
A0

A3
A2

OE

74LS08

A1
A0

A4
A3
A2

A7
A6
A5

A7
A6
A5
A4

D0

D1

D2

D3

D4

D5

D0

D1

D2

D3

D4

D5

D6

D7
D6

D7

OE

2
8
1
7
A

READY

Figure 2B. Programming 2817A’s with Version 1.1 of MCS BASIC-52

GND

+5

D1

Vcc

D2

D3

D4

D5

D6

RD

+5

74LS08

WR

ALE

P1.3

P0.0

P0.1

P0.2

P0.3

P0.4

P0.5

P0.6

P0.7

P2.0

A9
A8
A8

P2.1

A9

A10

CE

WR

A10

A12

8K
x8
STATIC
RAM
CE

P2.2

0

1

7
6
5
4
3
2

A11

7
4
L
S
1
3
8

P2.3

P2.4

+5V

A0

P2.6
P2.5

A1

A2

E1

E2

E3

P2.7

+5V

10k
+5

intel

1489

- 140 1488

C1 = C2= 30 pF
FOR
XTALS
40 pF
FOR
CERAMICRESONATORS

C2

C1

8.2k

10MFD

+5V

SERIALOUTEASERIAL
IN

XTAL2

XTAL1

INT0
DMA
RST REQUEST(P3.2)
P1.4

D7

7
4
L
S
3
7
3

DO7

Vcc

+5

Vcc

DO0

DO1

DO2

DO3

DO4

DO5

DO6

+5V

OE

74LS08

A7

WE

A1
A0

A3
A2

A0

A3
A2
A1

A6
A5
A4

A7
A6
A5
A4

D0

D1

D2

D3

D4

D5

D0

D1

D2

D3

D4

D5

OE

2
8
1
7
A

WR

Figure 2C. Programming 2817A’s with Version 1.0 of MCS BASIC-52

GND

GND OE

D0

PSEN

D2

D3

D4

D5

D1

+5

74LS08

RD

WR

ALE

P1.3

P0.0

P0.1

P0.2

P0.3

P0.4

P0.5

D6

P0.7

D6

D7

D7

P2.0

D6

A8

A8

P2.1

P0.6

A10
A9

A9

CE

A10

A12

8K
x8
STATICCE RAM

P2.2

0

3
2
1

7
6
5
4

A11

7
4
L
S
1
3
8

330 pF

+5
10k

P2.3

P2.4

+5V

A0

P2.6
P2.5

A1

A2

E1

E2

E3

P2.7

+5V

74LS08

10k

intel

P1.5

- 141 -PROGRAMMING PULSEPORT 1.4

PORT 2

ALE DISABLE

P1.3

PORT 0

EPROM VOLTAGE ENABLE

1 TCY

MIN

Figure 3A. EPROM Programming Timing Version

IF INTELLIGENT ALGORITHM USED

1 MS

50 MS

NOTE:

;

TCY – 1 us
XTAL
AT 12 MHz
WHEN USING THE INTELLIGENT ALGORITHM (FPROG)
THE LENGTH OF THE LAST PROGRAMMING PULSEIS THREE TIMES THE TOTAL NUMBER OF PULSESAFTER THE PROM IS PROGRAMMED.

12

HORIZONTAL TIME SCALE IS NOTTCY -40 TCYMIN
82 TCYMIN

VALID

HIGH ORDER ADDRESS

TCY

6

1 TCY

DATA VALIDLOW ORDER ADDRESS VALID

EPROM PROGRAMMING TIMINGintel

- 142 -

P3.7

READ

DON’T CARE

1 tcy

DON’T CARENOTE 2 tcy

30 tcyADDRESS LOW

1 tcy

36 tcyNOTE 1 tcy

DATA OUTADDRESS HIGH

30 tcyFLOAT

30 tcy1 tcy

SAMPLED tcy

DATA IN

VERIFY BYTE

Figure 3B. EPROM Programming Timing for Version 1.1

NOTE 2. When PROG command is executed, P1.5 goes low, and then the EPROM is read to see where to place the BASIC program.

NOTE 1. This pulse is either 1 millisecond (INTELigent algorithm) or 50 milliseconds (normal algorithm).

PORT 1.4

PROGRAMMING PULSE

P1.3

ALE DISABLE

PORT 0ADDRESS LOW/DATA

PORT 2ADDRESS HIGH

P1.5

EPROM VOLTAGE ENABLE

PROGRAM ONE BYTE

1 tcy

1 tcy

FLOAT

ADDRESS LOWNEXT

ADDRESS HIGHNEXT

PROGRAM NEXT
BYTE

intel

intel
21
VOLTS

21
VOLTS

10K Ω

(PIN 5)

10K Ω

74LS32

7407

P1.5
OF
8052AH

7407

P1.5

2N4403

2N4403P1.4

4.7K Ω

4.7K Ω

+5
1N270
1K

(PIN 6)
P1.4
OF
8052AH+5
VOLTS(PIN 1) Vpp

TO
ADDRESSDECODE(ACTIVE
LOW)

10K

PGM (PIN 27)
7407
2
7
6
4

TO
ADDRESSDECODE(ACTIVE LOW)

2
7
3
2
A

~

ANY NON-INVERTERTTL GATE MAY
BE USED

OE

CE

CE

2
7
1
2
8

OE
74LS0874LS08
PSEN

PSEN

RD

RD

Figure 4A. Programming 2764‘s/27128‘s

Figure 4B. Programming 2732A‘s+25 VOLTS

P1.5
OF
8052AH
P1.4
OF
8052AH

7407

7407

74LS3210K Ω
2N4403
4.7K Ω

74LS0474LS32
CE/ PGMTO
ADDRESSDECODE(ACTIVE
LOW)

1N270
Vpp

74LS08

10K
2
7
1
6

OE

74LS0874LS32PSEN
RD

Figure 4C. Programming 2716‘s- 143 -

+5V

4.7K

+5

intel+ 10 TO + 15 VOLTS

10K

+5
7407

2N4403

R
B

4.7K
+5

OE (PIN 20)

7407

15K Ω

+5

Rx/C

5uF

74LS08CE (PIN 18)
2
8
1
6
A

10K

15K

+5

5uF

Cx

7407
+5

A

Q

(PIN 1) Vpp
Rx/CTO
ADDRESSDECODELOGIC(ACTIVE LOW)

10K

R
B

A

Q

4.7K

RD
PSEN

+5

Cx

½ 74221½ 74221WE(PIN 21)7407

74LS08

P1.4
PROGRAM
PULSE
WIDTH

Figure 5. 2816A Circuit with Push Button Erase(Basic-52 should be „idle“ in the command mode when the Erase Button is pushed.)

+5

4.7K

SERIALOUTPUT

4.7K

TO RXD ON
BASIC-52
DEVICE

2N2222OR
EQUIV.

1N914
OR
EQUIV.

+5
TO RXD ON
BASIC-52
DEVICESERIALOUTPUT
1/4

1489

+5

4.7K

4.7K

+12
-12

TO TXD ON
BASIC-52
DEVICESERIALOUTPUTSERIALOUTPUT

1/4

TO TXD ON
BASIC-52
DEVICE

1488

1.8K

10uF
15V

Figure 6A.

Figure 6B.

TWO TRANSISTORS TO IMPLEMENT RS-232. THE “NEGATIVE”
SUPPLY FOR THE SERIAL OUTPUT LINE IS TAKEN FROM THE
SERIAL INPUT LINE. NO ±12 VOLT SUPPLY IS REQUIRED.

USING THE STANDARD 1489 AND1488 LINE RECEIVERS AND
DRIVERS. ±12 VOLT IS NEEDED WITH THIS IMPLEMENTATION.

- 144 -

intelCHAPTER 11
Reset Options (Version 1.1 Only)

Version 1.1 of MCS BASIC-52 contains numerous RESET options that were not available in Version
1.0. They are discussed in detail in chapters 3.2 through 3.5 of this manual. Briefly, they are as follows:
PROG1
Saves only the serial port baud rate for a power-up or RESET condition.
PROG2
Saves the serial port baud rate and automatically runs the first program that is saved in EPROM on a
power-up or RESET condition.
PROG3
Saves the serial port baud rate plus the assigned MTOP value. If RAM is available beyond the assigned
MTOP value, it will not be cleared during a power-up or RESET condition.
PROG4
Saves the serial port baud rate plus the assigned MTOP value, just like PROG3, but also automaticallyruns the first program that is saved in EPROM on a power-up or RESET condition.
PROG5
Does the same thing as PROG4, however, if external memory location 5FH contains the character
0A5H on a power-up or RESET condition, external memory will not be cleared. This mode assumes
that the user has employed some type of memory back-up.

- 145 -

intelRESET OPTIONS (VERSION 1.1 ONLY)
PROG6
Does the same thing as PROG5, but CALLS external program memory location 4039H during a
RESET or power-up sequence. This option also requires the user to put the character 0A5H in external
memory location 5FH to insure that external RAM will not be cleared during RESET or power-up. The
user must put an assembly language RESET routine in external memory location 4039H or else thisRESET mode will crash. When the user returns from the customized assembly language RESET
routine, three options exist:
OPTION 1 FOR PROG6
If the CARRY BIT is CLEARED (CARRY = 0) upon return from the user RESET routine MCS
BASIC- 52 will enter the auto-baud rate determining routine. The user must then type a space character(20H) on the terminal to complete the RESET routine and produce a RESET message on the terminal.
OPTION 2 FOR PROG6
If the CARRY BIT is SET (CARRY = 1) and BIT 0 of the ACCUMULATOR is CLEARED (ACC. 0 =0) MCS BASIC-52 will produce the standard sign-on message upon return from the user supplied
RESET routine. The baud rate will be the one that was saved when the PROG6 option was used.
OPTION 3 FOR PROG6
If the CARRY BIT is SET (CARRY = 1) and BIT 0 of the ACCUMULATOR is SET (ACC. 0 = 1),
MCS BASIC-52 will execute the first program stored by the user in EPROM (starting address of the
program is 8010H) upon return from the user supplied RESET routine.

- 146 -

intelRESET OPTIONS (VERSION 1.1 ONLY)
If these options are still not sufficient to address the needs of a specific application, one other option
exists and it functions as follows:
After RESET, MCS BASIC-52 initializes the SPECIAL FUNCTION REGISTERS SCON, TMOD,
TCON, and T2CON with the following respective values, 5AH, 10H, 54H, and 34H. If the user places
the character 0AAH in external CODE MEMORY location 2001H (remember CODE MEMORY isenabled by /PSEN), MCS BASIC-52 will CALL external CODE MEMORY location 2090H
immediately after these special function registers are initialized. No other registers or memory locations
will be altered except that the ACCUMULATOR will contain a 0AAH and the DPTR will contain a
2001H.
Since MCS BASIC-52 does not write to the above mentioned Special Function Registers at any time
except during the RESET or power-up sequence the user has the option of modifying any of the Special
Function Registers with this RESET option. Upon returning from this RESET mode, the MCS BASIC52 software package will clear the internal memory of the 8052AH and proceed with the RESET
routine. The PROG1 through PROG6 options will function as usual.
Now, suppose the user does not want to enter the normal RESET routines, or the user wants toimplement some type of "warm" start-up routine. This can be accomplished simple by initializing thenecessary Special Function Registers and then jumping back into either MCS BASIC-52's COMMAND
mode or RUN MODE. For a warm start-up or RESET (warm means that the MCS BASIC-52 device
was RESET, but power was not removed -- i.e. the user hit the RESET button) the following must be
initialized:
SCON, TMOD, TCON, T2CON, if the user does not want to use the values that MCS BASIC-52
supplies.
RCAP2H and RCAP2L must be loaded with the proper baud rate values. If the user has programmed an
EPROM with one of the PROG1 through PROG6 options, the proper baud rate value will be stored inexternal DATA MEMORY locations 8001H (RCAP2H) and 8002H (RCAP2L).
The STACK POINTER (Special Function Register SP) must be initialized with the contents of the
STACK POINTER SAVE location, which is in internal DATA MEMORY location 3EH. A MOV SP,
3EH assembly language instruction will accomplish the STACK POINTER initialization.

- 147 -

intelRESET OPTIONS (VERSION 1.1 ONLY)
After the above are initialized by the user supplied RESET routine, the user may enter MCS BASIC52's command mode by executing the following:
CLR A
LJMP 30H
Now, it is important to remember that the previous description applies only to a "warm" RESET withpower remaining to the MCS BASIC-52 system. This means that the user must also provide some wayof detecting the difference between a warm RESET and a power-on RESET. This usually involves
some type of flip-flop getting set with a power-on-clear signal from the users power supply. The details
of implementing this RESET detection mechanism will not be discussed here as the possible hardware
options vary depending upon the design.
The user may also implement a "cold start" reset option with the previously described reset mode. The
following code details what is necessary to implement a cold start option.
EXAMPLE:
ORG
;
DB
;
ORG
;

2001H
0AAH

; TELL BASIC THAT RESET IS EXTERNAL

2090H; LOCATION BASIC WILL CALL FOR RESET; AT THIS POINT BASIC HAS PLACED A 5AH IN; SCON, A 10H IN TMOD, A 54H IN TCON AND; A 34H IN T2CON

;
MOV
CLR

R0,#0FFH
A; FIRST CLEAR THE INTERNAL MEMORY; LOAD R0 WITH THE TOP OF INTERNAL MEMORY; SET ACCUMULATOR = 0

RESET1: MOV
DJNZ
;

@R0.A
R0.RESET1; LOOP UNTIL ALL THE INTERNAL RAM IS CLEARED

MOV
MOV

SP,#4DH
3EH,#4DH

;
MOV
MOV
MOV
;

; NOW SET UP THE STACK POINTER AND THE STACK; POINTER HOLDING REGISTER; 4DH IS THE INITIALIZED VALUE OF THE STACK; THIS IS THE SP HOLDING REGISTER; NOW CLEAR THE EXTERNAL RAM, IN THIS
; EXAMPLE ASSUME THAT 1FFFH BYTES OF RAM; ARE AVAILABLE; THE USER MUST CLEAR AT LEAST THE FIRST 512; BYTES OF RAM FOR A COLD START RESET

R3,#HIGH 1FFFH
R1,#LOW 1FFFH
DPTR,#0FFFFH- 148 -

intelRESET OPTIONS (VERSION 1.1 ONLY)RESET2:

INC
CLR
MOVX

DPTR
A
@DPTR,A; DPTR = 0 THE FIRST TIME THRU
; CLEAR THE RAM. A MEMORY TEST PROGRAM COULD; BE IN THIS LOOP
; NOW TEST FOR THE MEMORY LIMITS

MOV
A,R3
CJNE A,DPH-RESET2
MOV
A,R1
CJNE A,DPL.RESET2;; WHEN YOU GET HERE, YOU ARE DONE;
; NOW SET UP THE MEMORY POINTERS- FIRST MTOP;
MOV
DPTR.#10AH; LOCATION OF MTOP IN EXTERNAL RAM
MOV
A,#HIGH 1FFFH ; SAVE MTOP
MOVX @DPTR,A
INC
DPTR; NOW, SAVE THE LOW BYTE
MOV
A,#LOW 1FFFH
MOVX @DPTR,A;; NOW SET UP THE VARTOP POINTER, WITH NO STRINGS.
; VARTOP = MEMTOP;
MOVDPTR,#104H; LOCATION OF VARTOP IN EXTERNAL RAM
MOV
A,#HIGH 1FFFH
MOVX @DPTR,A
INC
DPTR
MOV
A,#LOW 1FFFH
MOVX @DPTR,A;;; NOW SAVE THE MATRIX POINTER "DIMUSE". THIS POINTER IS; DESCRIBED IN THE APPENDIX, WITH NO PROGRAM IN RAM.
; DIMUSE = 525 AFTER RESET;
MOV
DPTR,#108H; LOCATION OF DIMUSE IN EXTERNAL RAM
MOV
A,#HIGH 528
MOVX @DPTR,A
INC
DPTR
MOV
A,#LOW 528
MOVX @DPTR,A;; NOW SAVE THE VARIABLE POINTER "VARUSE" THIS POINTER IS; ALSO DESCRIBED IN THE APPENDIX. AFTER RESET VARUSE = VARTOP;
MOV
DPTR,#106H; LOCATION OF VARUSE IN EXTERNAL RAM
MOV
A,#HIGH 1FFFH
MOVX @DPTR,A
INC
DPTR
MOV
A,#LOW 1FFFH
MOVX @DPTR,A;; NOW SETUP BASICS CONTROL STACK AND ARGUMENT STACK;
MOV
9H,#0FEH; THIS INITIALIZES THE ARGUMENT STACK
MOV
11H,#0FEH; THIS INITIALIZES THE CONTROL STACK;; NOW TELL BASIC THAT NO PROGRAM IS IN RAM. THIS IS NOT NEEDED; IF THE USER HAS A PROGRAM IN RAM;
MOV
DPTR,#512; LOCATION OF THE START OF A USER PROGRAM
MOV
A,#01H; END OF FILE CHARACTER
MOVX @DPTR,A- 149 -

intelRESET OPTIONS (VERSION 1.1 ONLY)XTAL:RESET3:RESET4:; NOW PUSH THE CRYSTAL VALUE ON TO THE STACK AND LET BASIC; CALCULATE ALL CRYSTAL DEPENDENT PARAMETERS;SJMP RESET3;
DB
88H; THIS IS THE FLOATING POINT VALUE
DB
00H; FOR AN 11.0592 MHZ CRYSTAL
DB
00H
DB
92H
DB
05H
DB
11H
;
MOV
DPTR,#XTAL; SET UP TO PUSH CRYSTAL VALUE
MOV
A,9; GET THE ARG STACKCLRCSUBB A,#6; DECREMENT ARG STACK BY ONE FP NUMBER
MOV
9,A
MOV
R0,A
; SAVE THE CALCULATED ADDRESS IN R0
MOV
P2,#1
; THIS IS THE ARG STACK PAGE ADDRESS
MOV
R1,#6
; NUMBER OF BYTES TO TRANSFER;
CLR
A; TRANSFER ROM CRYSTAL VALUE TO THE
MOVC A,@A+DPTR; ARGUMENT STACK OF BASIC
MOVX @R0,A
INC
DPTR; BUMP THE POINTERSDEC
R0
DJNZ R1,RESET4; LOOP UNTIL THE TRANSFER IS COMPLETE;; NOW CALL BASIC TO DO ALL THE CRYSTAL CACULATIONS
;
MOV
A,#58
; OPBYTE FOR CRYSTAL CALCULATIONLCALL 30H
; DO THE CALCULATION;; NOW TELL BASIC WHERE START OF THE USER BASIC PROGRAM IS; BY LOADING THE START ADDRESS. IF THE PROGRAM IS IN EPROM; 13H WOULD = HIGH 8011H AND 14H = LOW 8011H. ANYWAY; ADDRESS 13H:14H MUST POINT TO THE START OF THE BASIC; PROGRAM;
MOV
13H,#HIGH 512 ; THIS TELLS BASIC THAT THE START OF
MOV
14H.#LOW 512 ; THE PROGRAM IS IN LOCATION 512;; NOW THE SERIAL PORT MUST BE INITIALIZED. THE USER
; CAN SET UP THE SERIAL PORT TO ANY DESIRED CONFIGURATION; HOWEVER, THIS DEMO CODE WILL SHOW THE AUTO BAUD
; ROUTINE
MOV
MOV
MOV
JB
;

R3,#00H
R1,#00H
R0,#04HRXD,$; INITIALIZE THE AUTO BAUD COUNTERS; LOOP UNTIL A START BIT IS RECEIVED- 150 -

intelRESET OPTIONS (VERSION 1.1 ONLY)RESET5:

MSG:

;
DJNZ

R0,$

; WASTE 8 CLOCKS INITIALLY. SIX CLOCKS; IN THE LOOP (16) TOTAL; 1 CLOCK (1); 1 CLOCK (2); 1 CLOCK (3); 1 CLOCK (4); 1 CLOCK (5); 1 CLOCK -- R3:R1 = R3:R1 - 1 (6); 1 CLOCK (7); 1 CLOCK (8); 2 CLOCKS (10), WAIT FOR END OF SPACE; WAIT FOR THE SPACE TO END (20H); WAIT FOR THE STOP BIT; LOAD THE TIMER 2 HOLDING REGISTERS

CLR
C
MOV
A,R1
SUBB A,#1
MOV
R1,A
MOV
A,R3
SUBB A,#00H
MOV
R3,A
MOV
R0,#3
JNB
RXD,RESET5
JB
RXD,$
JNB
RXD,$
MOV
RCAP2H.R3
MOV
RCAP2L.R1;; NOW YOU CAN ADD A CUSTOM SIGN ON MESSAGE;
MOV
R3,#HIHH MSG ; PUT ADDRESS OF MESSAGE IN R3:R1
MOV
R1,#LOW MSG
SETB 52; PRINT FROM ROM
MOV
A,#6; OP BYTE TO PRINT TEXT STRINGLCALL 30H;; NOW OUTPUT A CR LF
;
MOV
A,#7
; OP 8YTE FOR CRLF
LCALL 30H;
; GO TO THE COMMAND MODE
;
CLR
A
JMP
30H
;
DB
'CUSTOM SIGN ON MESSAGE'
DB
22H; TERMINATES MESSAGE;
END- 151 -

intelRESET OPTIONS (VERSION 1.1 ONLY)
To Summarize what the user must do to successfully implement a "COLD START" RESET:
1) The user must clear the internal RAM of the MCS BASIC-52 device and at least the first 512 bytes
of external RAM memory.
2) The user must initialize the stack pointer (special function register -- SP) and the stack pointerholding register (internal RAM location 3EH) with a value that is between 4DH and 0E0H. 4DH
gives MCS BASIC-52 the maximum stack size.
3) The user must initialize the following pointers in external RAM. MTOP at location 10AH (highbyte) and 10BH (low byte). VARTOP at locations 104H (high byte) and 105H (low byte). DIMUSEat locations 108H (high byte) and 109H (low byte). VARUSE at locations 106H (high byte) and107H (low byte). Details of what needs to be in these locations are presented in appendix 1.7 of thismanual.
4) The Control stack pointer (location 11H in internal memory) and the Argument stack pointer(location 09H in internal memory) must also be initialized with the value 0FEH. If the user is notgoing to assign the XTAL (crystal) value in BASIC, then the XTAL value must be pushed onto theargument stack and the user must Do an OPBYTE 58 call to MCS BASIC-52.
5) The User must also initialize the start address of a program. The start address is in locations 13H(high byte) and 14H (low byte) of internal data memory. If the user BASIC program is in RAM, then
13H: 14H = 512, if the user program is the the first program in EPROM, then 13H: 14H = 8011H.
6) The user must finally initialize the serial port. Any scheme can be used (as long as it works!!)
The added reset options should go a long way toward making MCS BASIC-52 configurable to any
custom application.

- 152 -

intelCHAPTER 12
Command/Statement Extensions (Version 1.1 Only)

MCS BASIC-52 V1.1 provides a simple, but yet effective way for the user to add COMMANDS and/or
STATEMENTS to the ones that are provided on the chip. All the user must do is write a few simple
programs that will reside in external code memory. The step by step approach is as follows:
STEP 1The user must first inform the MCS BASIC-52 device that the expansion options are available. This isdone by putting the character 5AH in CODE memory location 2002H. When MCS BASIC-52 enters thecommand mode it will examine CODE memory location 2002H. If a 5AH is in this location, MCS
BASIC-52 will CALL external CODE memory location 2048H. The user must then write a shortroutine to SET BIT 45 (2DH), which is bit 5 of internal memory location 37 (decimal) and place thisroutine at code memory location 2048H. Setting BIT 45 tells MCS BASIC-52 that the expansion option
is available. The following simple code will accomplish all that is stated above:
ORG 2002H
DB 5AH;
OG 2048HSETB 45
RETSTEP 2
With BIT 45 SET, MCS BASIC-52 will CALL external CODE memory location 2078H every time it
attempts to tokenize a line that has been entered. At location 2078H, the user must load the DPTR (Data
Pointer) with the address of the user supplied lookup table, complete with tokens.

- 153 -

intelCOMMAND/STATEMENT EXTENSIONS (VERSION 1.1 ONLY)
STEP 3The user needs the following information to generate a user token table:
1) THE USER TOKENS ARE THE NUMBRES 10H THROUGH 1FH (16 TOKENS AVAILABLE)
2) THE USER TOKEN TABLE BEGINS WITH THE TOKEN, FOLLOWED BY THE ASCII TEXTTHAT IS TO BE REPRESENTED BY THAT TOKEN, FOLLOWED BY A ZERO (00H)
INDICATING THE END OF THE ASCII, FOLLOWED BY THE NEXT TOKEN.
3) THE TABLE IS TERMINATED WITH THE CHARACTER 0FFH.
EXAMPLE:ORG 2078H;
MOV
DPTR,#USER_TABLE
RET
;
ORG
2200H
; THIS DOES NOT NEED TO BE;; IN THIS LOCATIONUSER_TABLE:;
DB
10H; FIRST TOKENDB'DISPLAY'; USER KEYWORD
DB
00H; KEYWORD TERMINATOR
DB
DB
DB

11H
'TRANSFER'
00H; SECOND TOKEN; SECOND USER KEYWORD; KEYWORD TERMINATOR

DB
DB
DB

12H
'ROTATE'
0FFH; THIRD TOKEN (UP TO 16); THIRD USER KEYWORD; END OF USER TABLE

This same user table is used when MCS BASIC-52 "de-tokenizes" a line during a LIST.

- 154 -

intelCOMMAND/STATEMENT EXTENSIONS (VERSION 1.1 ONLY)
STEP 4
Step 3 tokenizes the user keyword, this means that MC BASIC-52 translates the user keyword into theuser token. So, in the preceding example, the keyword TRANSFER would be replaced with the token
11H. When MCS BASIC-52 attempts to execute the user token, it first makes sure that the user
expansion option BIT is set (BIT 45), then CALLS location 2070H to get the address of the user vector
table. This address is placed in the DPTR. The user vector table consist of series of Data Words thatdefine the address of the user assembly language routines.
EXAMPLE:
ORG
;
MOV
RET
;
VECTOR_TABLE:
;
DW
DW
DW
;
ORG

2070H

; LOCATION BASIC CALLS TO; GET USER LOOKUPDPTR,#VECTOR_TABLE

RUN_DISPLAY; ADDRESS OF DISPLAY; ROUTINE, TOKEN (10H)
RUN_TRANSFER ; ADDRESS OF TRANSFER; ROUTINE, TOKEN (11H)
RUN_ROTATE; ADDRESS OF ROTATE; ROUTINE, TOKEN (12H)
2300H; AGAIN, THESE ROUTINES; MAY BE PLACED ANYWHERE;
RUN DISPLAY:;; USER ASM CODE FOR DISPLAY GOES HERE;RUN TRANSFER:;; USER ASM CODE FOR TRANSFER GOES HERE;
RUN_ROTATE:;; USER ASM CODE FOR ROTATE GOES HERE;

- 155 -

intelCOMMAND/STATEMENT EXTENSIONS (VERSION 1.1 ONLY)
Note that the ordinal position of the DATA WORDS in the user vector table must correspond to thetoken, so the user statement with the token 10H must be the first DW entry in the vector table, 11H, thesecond, 12H, the third, and so on. The order of the tokens in the user table is not important!! The
following user lookup table would function properly with the previous example:
EXAMPLE:;USER_TABLE:
;
DB
DB
DB

13H
'ROTATE'
00H

DB
DB
DB

10H
'DISPLAY'
00H

DB
DB
DB

12H
'TRANSFER'
0FFH; THE TOKENS DO NOT HAVE
; TO BE IN ORDER IN THE; USER LOOKUP TABLE; END OF TABLE- 156 -

intelCOMMAND/STATEMENT EXTENSIONS (VERSION 1.1 ONLY)
The user may also use the command/statement extension option to re-define the syntax of MCS BASIC52. This is done simply by placing your own syntax in the user table and placing the appropriate BASIC
token in front of your re-defined keyword. A complete listing of all MCS BASIC-52 tokens andkeywords are provided in the back of this chapter. MCS BASIC-52 will always list out the programusing the user defined syntax, but it will still accept the standard keyword as a valid instruction. As an
example, suppose that the user would like to substitute the keyword HEXOUT for PH1., then the user
would generate the following entry in the user table:
EXAMPLE:;USER_TABLE:
DB
DB
DB
;
DB
DB
DB

8FH
'HEXOUT'
00H; TOKEN FOR PH1.
; TO BE IN ORDER IN THE; USER LOOKUP TABLE;;
DBREST OF USER_TABLE

10H'DISPLAY'
00H
0FFH; END OF TABLEMCS BASIC-52 will now accept the keyword HEXOUT and it will function in a manner identical toPH1. PH1 will still function correctly, however HEXOUT will be displayed when the user LIST aprogram.

- 157 -

intelCOMMAND/STATEMENT EXTENSIONS (VERSION 1.1 ONLY)TOKEN

KEYWORDTOKEN

KEYWORD

80H
81H
82H
83H
84H
85H
86H
87H
88H
89H
89H
89H
8AH
88H
8CH
8DH
8EH
8FH
90H
91H
92H
93H
94H
95H
96H
97H
98H
99H
9AH
98H
9CH
9DH
9EH
9FH
0A0H
0A1H
0A2H
0A3H
0A4H
0A5H
0A6H
0A7H
0A8H
0A9H
0AAH
0A8H
0ACH
0ADH
0AEH
0AFH

LET
CLEAR
PUSH
GOTO
PWM
PH0.
UI
UO
POP
PRINT
P.
? (V1.1 ONLY)CALLDIMSTRING
BAUD
CLOCK
PH1.
STOP
ONTIME
ONEX1
RETI
DO
RESTORE
REM
NEXT
ONERR
ON
INPUT
READ
DATA
RETURN
IF
GOSUB
FOR
WHILE
UNTIL
END
TAB
THEN
TO
STEP
ELSE
SPC
CR
IDLE
ST@ (V1.1 ONLY)
LD@ (V1.1 ONLY)PGM (V1.1 ONLY)RROM(V1.1 ONLY)

080H
ABS
081H
INT
0B2H
SGN
083H
NOT
084H
COS
085H
TAN
0B6H
SIN
087H
SOR
088H
CBY
089H
EXP
08AH
ATN
088H
LOG
08CH
DBY
08DH
XBY
08EH
PI
08FH
RND
0C0H
GET
0C1H
FREE
0C2H
LEN
0C3H
XTAL
0C4H
MTOP
0C5H
TIME
0C6H
IE
0C7H
IP
0C8H
TIMER00C9H
TIMER10CAH
TIMER2
0C8H
T2CON
0CCH
TCON
0CDH
TMOD
0CEH
RCAP2
0CFH
PORT1
0D0H
PCON
0D1H
ASC(
0D2H
USING(
0D2H
U.(
0D3H
CHR(
0D4H-0DFH NOT USED
0E0H
(
0E1H
**
0E2H
*
0E3H
+
0E4H
/
0E5H
0E6H
.XOR.
0E7H
.AND.
0E8H
.OR.
0E9H
- (NEGATE)
0EAH
=
0EBH
>=

- 158 -TOKEN

KEYWORD

0ECH
<=
0EDH
<>
0EEH
<
0EFH
>
0FOH
RUN
0FlH
LIST
0F2H
NULL
0F3H
NEW
0F4H
CONT
0F3H
PROG
0F6H
XFER
0F7H
RAM
0F8H
ROM
0F9H
FPROG
0FAH-0FFH NOT USEDintelCHAPTER 13
Mapping User Code Memory

You might have noticed by now that some of external CODE memory locations that MCS BASIC-52
calls and uses are located around 2000H and some of the locations are located around 4000H.
Specifically, they are as follows:
LOCATIONFUNCTION

2001H

ON RESET, MCS BASIC-52 LOOKS FOR A 0AAH IN THIS LOCATION, IF
PRESENT, CALLS LOCATION 2090H

2002H

MCS BASIC-52 EXAMINES THIS LOCATION TO SEE IF THE USER WANTS TO
IMPLEMENT THE COMMAND/STATEMENT EXTENSION OPTION, A 05AH IS TO
BE PLACED IN THIS LOCATION TO EVOKE THE COMMAND/EXTENSIONOPTION

2048H

MCS BASIC-52 CALLS THE LOCATION IF THE USER WANTS TO IMPLEMENTTHE COMMAND/STATEMENT EXTENSION OPTION. THE USER WILL USUALLYSET BIT 45 THEN RETURN.

2070H

MCS BASIC-52 CALLS THIS LOCATION TO GET THE USER VECTOR TABLE
ADDRESS WHEN THE COMMAND/STATEMENT EXTENSION OPTION IS
EVOKED. THE ADDRESS OF THE VECTOR TABLE IS PUT IN THE DPTR BY
THE USER.

2078H

MCS BASIC-52 CALLS THIS LOCATION TO GET THE USER LOOKUP TABLE
ADDRESS WHEN THE COMMAND/STATEMENT EXTENSION OPTION IS
EVOKED. THE ADDRESS OF THE LOOKUP TABLE IS PUT IN THE DPTR BY
THE USER.

2090H

MCS BASIC-52 CALLS THIS LOCATION WHEN THE USER EVOKES THEASSEMBLY LANGUAGE RESET OPTION

4003H

EXTERNAL INTERRUPT 0400BH

TIMER0 INTERRUPT

4013H

EXTERNAL INTERRUPT 1

401BH

TIMER1 INTERRUPT

4023H

SERIAL PORT INTERRUPT

402BH

TIMER 2 INTERRUPT

4030H

USER CONSOLE OUTPUT

4033H

USER CONSOLE INPUT

4036H

USER CONSOLE STATUS

403CH

USER PRINT@ OR LIST@ VECTOR

4100H-41FFH USER CALLS FROM 0 TO 7FH- 159 -

intelMAPPING USER CODE MEMORYOther vectors between 2040H and 2090H also exist, but they are mainly for testing purposes, but foryour information they are:
LOCATIONFUNCTION

2040H

TRAP LOCATION FOR EXTERNAL INTERRUPT 0 IF BIT 26H OF INTERNAL RAM
IS SET AND THE DMA OPTION IS EVOKED. PSW IS NOT PUSHED ONTO
STACK. INTERRUPTS OF COURSE, MUST BE ENABLED. ALSO, THISLOCATION WILL BE CALLED FOR CONSOLE OUTPUT IF BIT 2CH OF
INTERNAL RAM IS SET.

2050H

TRAP LOCATION FOR SERIAL PORT INTERRUPT IF BIT 1FH OF INTERNALRAM IS SET. PSW IS PUSHED ONTO THE STACK.

2060H

CALLED FOR CONSOLE INPUT IF BIT 32H OF INTERNAL RAM IS SET.

2068H

CALLED FOR CONSOLE STATUS CHECK IF BIT 32H OF INTERNAL RAM IS
SET.

2088H

TIMER1 INTERRUPT TRAP IF BIT 1AH OF INTERNAL RAM IS SET. PSW ISPUSHED ONTO THE STACK.

Contrary to popular belief, these vectors were not chosen to force the user to buy bigger EPROMS.
They are chosen so that addresses 2000H and 4000H can be overlayed and create no conflicts. The
Overlayed addresses would appear as 2001H, 2002H, 4003H, 400BH, 4013H, 401BH, 4023H, 402BH,
4030H, 4033H, 4036H, 4039H, 2040H, 2048H, 2050H, 2060H, 2068H, 2070H, 2078H, 2088H, 2090H,
and 4100H thru 41FFH. The diagram on the next page illustrates how to implement overlapping
addresses for 2000H and 4000H. By using overlapping addresses, the user can implement all MCS
BASIC-52 user expansion options with only a few hundred bytes of EPROM.
The reason this type of addressing scheme was chosen is that it permits the designer to offer custom
versions of MCS BASIC-52, by using the vector locations in the 2000H region. And give the designers
OEM the ability to take advantage of the I/O vectors located in the 4000H region.
As an added note, the MCS-51 instruction set is object relocatable on 2K boundaries if no LCALL or
LJMP instructions are used. This means that it is possible for the designer to ORG a program for 2000H
and actually execute the program at 2800H, 3000H, 3800H, etc. If the user does not use the LCALL or
LJMP instructions.

- 160 -

1489

1488

C1 = C2= 30 pF
FOR
XTALS
40 pF
FOR
CERAMICRESONATORS

C2

C1

8.2k

10MFD

+5V

- 161 SERIALOUTEASERIAL
IN

XTAL2

XTAL1

RST

P1.4

P1.5

7
4
L
S
3
7
3

DO7

+5V

P0.0

D0

GND

Vcc

D1

RD
PSEN

WE

A1
A0

A4
A3
A2

OE

OE

A1
A0

A4
A3
A2

A7
A6
A5

D0

D1

D2

D3

D4

Overlapping user EPROM address space

S2 CLOSED PERMITS 27128 TO BE AT ADDRESS 8000H. A13 MUST BE CONNECTED ON 27128

Vcc

DO1
DO0

DO2

DO3

DO4

DO5

S1 CLOSED PRODUCES OVERLAPPING ADDRESSESGND OE

D2

D3

D4

D5

WR

ALE

74LS08

D0

P0.1

P1.3

D1

P0.2

A7
A6
A5

D2

P0.3

DO6

D3

P0.4

D6

D4

P0.5

74LS08

A1
A0

A3
A2

A5
A4

A7
A6

D0

D1

D2

D3

D4

D5

D6

D7
D5

D6

D7

D7

P0.7
D5

A8

P2.0

D6

A9
A8

A8

P2.1

P0.6

A10

CE
A12

OE

2
7
1
2
8

10k

10k

PGM

A9

CE
A12

USER
EXPANSION
EPROM

A9

CE
A12

8K
x8
STATIC
RAM

P2.2

D7

74LS08

+5V

A11

4.7k

7407

A10

S1

+5V

74LS08

A10

0

1

7
6
5
4
3
2

4.7k

A11

7
4
L
S
1
3
8

S2

A11

+5V

A0

A1

A2

E1

E2

E3

1k

4.7k

P2.3

P2.4

P2.5

P2.6

P2.7

+5V

+5V

7407
+5V

V

PP

2N44031

+21V

+5V

intel

intelAPPENDIX A
1.1 MEMORY USAGE (Version 1.0)
The following list specifies what locations in internal and external memory MCS BASIC-52 uses, andwhat these locations are used for. This information can largely be regarded as "for your information,"
but it can be used to do things like alter the pulse width of a EPROM programming pulse, etc.
INTERNAL MEMORY ALLOCATION:
LOCATION(S) IN HEXMCS BASIC-52 USAGE

00H THRU 07H
08H
09H
0AH
0BH THRU 0FH
10H
11H
12H
13H
14H
15H
16H
17H
18H THRU 21H"WORKING REGISTER BANK"BASIC TEXT POINTER -- LOW BYTE
ARGUMENT STACK POINTER
BASIC TEXT POINTER -- HIGH BYTETEMPORARY BASIC STORAGE
READ TEXT POINTER -- LOW BYTECONTROL STACK POINTERREAD TEXT POINTER -- HIGH BYTE
START ADDRESS OF BASIC PROGRAM -- HIGH BYTE
START ADDRESS OF BASIC PROGRAM -- LOW BYTENULL COUNTPRINT HEAD POSITION FOR OUTPUTFLOATING POINT OUTPUT FORMAT TYPENOT USED -- RESERVED FOR USER

22H

BITS USED SPECIFICALLY AS FOLLOWS

BIT 22.0H
BIT 22.1H
BIT 22.2H
BIT 22.3H
BIT 22.4H
BIT 22.5H
BIT 22.6H
BIT 22.7H

SET WHEN "ONTIME" STATEMENT IS EXECUTED
SET WHEN BASIC INTERRUPT IN PROGRESSSET WHEN "ONEX1" STATEMENT IS EXECUTED
SET WHEN "ONERR" STATEMENT IS EXECUTED
SET WHEN "ONTIME" INTERRUPT IS IN PROGRESSSET WHEN A LINE IS EDITEDSET WHEN EXTERNAL INTERRUPT IS PENDING
WHEN SET, CONT COMMAND WILL WORK

23H

BITS USED SPECIFICALLY AS FOLLOWS

BIT 23.0H
BIT 23.1H
BIT 23.2H
BIT 23.3H
BIT 23.4H
BIT 23.5H
BIT 23.6H
BIT 23.7H

USED AS FLAG FOR "GET" OPERATORSET WHEN INVALID INTEGER FOUND IN TEXTTEMPORARY BIT LOCATIONCONSOLE OUTPUT CONTROL, 1 = LINE PRINTERCONSOLE OUTPUT CONTROL, 1 = USER DEFINEDBASIC ARRAY INITIALIZATION BITCONSOLE INPUT CONTROL, 1 = USER DEFINEDRESERVED- 162 -

intel
1.1 MEMORY USAGE (Version 1.0)
INTERNAL MEMORY ALLOCATION:
LOCATION(S) IN HEXMCS BASIC-52 USAGE

24H

BITS USED SPECIFICALLY AS FOLLOWS

BIT 24.0H
BIT 24.1H
BIT 24.2H
BIT 24.3H
BIT 24.4H
BIT 24.5H
BIT 24.6H
BIT 24.7H

STOP STATEMENT OR CONTROL-C ENCOUNTERED
0 = HEX INPUT, 1 = FP INPUT
0 = RAM MODE, 1 = ROM MODE
ZERO FLAG FOR DOUBLE BYTE COMPARE
SET WHEN ARGUMENT STACK HAS A VALUERETI INSTRUCTION EXECUTEDRESERVEDRESERVED

25H

BITS USED SPECIFICALLY AS FOLLOWS

BIT 25.0H
BIT 25.1H
BIT 25.2H
BIT 25.3H
BIT 25.4H
BIT 25.5H
BIT 25.6H
BIT 25.7H

RESERVED, SOFTWARE TRAP TESTFIND THE END OF PROGRAM, IF SETRESERVEDINTERRUPT STATUS SAVE BIT
SET WHEN PROGRAM EXECUTION IS COMPLETE
RESERVED, EXTERNAL TRAP TESTSET WHEN CLOCK1 EXECUTED, ELSE CLEARED
SET WHEN BASIC IS IN THE COMMAND MODE

26H

BITS USED SPECIFICALLY AS FOLLOWS

BIT 26.0H
BIT 26.1H
BIT 26.2H
BIT 26.3H
BIT 26.4H
BIT 26.5H
BIT 26.6H
BIT 26.7H

SET TO DISABLE CONTROL-C
SET TO ENABLE "FAKE" DMARESERVEDSET TO EVOKE "INTELLIGENT" PROM PROGRAMMINGSET TO PRINT TEXT STRING FROM ROM
RESERVEDSET TO SUPPRESS ZEROS IN HEX MODE PRINTSET TO EVOKE HEX MODE PRINT- 163 -

intel
1.1 MEMORY USAGE
INTERNAL MEMORY ALLOCATION:
LOCATION(S) IN HEXMCS BASIC-52 USAGE

27H
28H THRU 3DH
3EH
3FH
40H
41H
42H
43H
44H
45H
46H
47H
48H
49H
4AH
4BH
4CH
4DH THRU 0FFH

"BIT" ADDRESSABLE BYTE COUNTER
BIT AND BYTE FLOATING POINT WORKING SPACEINTERNAL STACK POINTER HOLDING REGISTERLENGTH OF USER DEFINED STRING -- $TIMER 1 RELOAD LOCATION -- HIGH BYTETIMER 1 RELOAD LOCATION -- LOW BYTE
BASIC TEXT POINTER SAVE LOCATION -- HIGH BYTE
BASIC TEXT POINTER SAVE LOCATION -- LOW BYTERESERVED
TRANSCENDENTAL FUNCTION TEMP STORAGE
TRANSCENDENTAL FUNCTION TEMP STORAGEMILLI-SECOND COUNTER FOR REAL TIME CLOCKSECOND COUNTER FOR REAL TIME CLOCK -- HIGH BYTESECOND COUNTER FOR REAL TIME CLOCK -- LOW BYTETIMER 0 RELOAD FOR REAL TIME CLOCKSOFTWARE SERIAL PORT BAUD RATE -- HIGH BYTESOFTWARE SERIAL PORT BAUD RATE -- LOW BYTE
8052AH STACK SPACE AND USER WORKING SPACE- 164 -

intel
1.1 MEMORY USAGE
EXTERNAL MEMORY ALLOCATIONLOCATION(S) IN HEXMCS BASIC-52 USAGE

00H AND 01H
02H AND 03H
04H
05H AND 06H
07H THRU 49H
50H THRU 5FH
60H THRU 0FEH
0FFH
100H
101H
102H AND 103H
104H AND 105H
106H AND 107H
108H AND 109H
10AH AND 10BH
10CH AND 10DH
10EH THRU 113H
114H THRU 11FH
120H AND 121H
122H AND 123H
124H THRU 127H
128H AND 129H
12AH AND 12BH
12CH
12DH THRU 1FEH

"LAST" END OF FILE ADDRESS FOR RAM FILE (H-L)
CURRENT END OR FILE ADDRESS FOR RAM FILE (H-L)
LENGTH OF THE CURRENT EDITED LINELN NUM IN BINARY OF CURRENT EDITED LINE (H-L)
BASIC INPUT BUFFERFLOATING POINT OUTPUT TEMPCONTROL STACKCONTROL STACK OVERFLOWLOCATION TO SAVE "GET" CHARACTERLOCATION TO SAVE ERROR CHARACTER CODE
LOCATION TO GO TO ON USER "ONERR" (H-L)
TOP OF VARIABLE STORAGE (H-L)
FP STORAGE ALLOCATION (H-L)
MEMORY ALLOCATED FOR MATRICES (H-L)
TOP OF MEMORY ASSIGNED TO BASIC (H-L)
RANDOM NUMBER SEED (H-L)
CRYSTAL VALUEFLOATING POINT TEMPSLOCATION TO GO TO ON ONEX1 INTERRUPT (H-L)
NUMBER OF BYTES ALLOCATED FOR STRINGS (H-L)
ONTIME INTERRUPT AND LINE NUMBER (H-L)"NORMAL" PROM PROGRAMMER TIME OUT (H-L)"INTELLIGENT" PROM PROGRAMMER TIME OUT (H-L)
RESERVEDARGUMENT STACKNOTE: (H-L) means HIGH BYTE -- LOW BYTE. in external memory all 16 bit binary numbers are
stored with the HIGH BYTE in the first (lower order) address and the LOW BYTE in the next
sequential address.

- 165 -

intel
1.1 MEMORY USAGE (VERSION 1.1)
The following list specifies what locations in internal and external memory locations are used by
Version 1.1 of MCS BASIC-52. Any differences between V1.0 and V1.1 are in bold face type.
INTERNAL MEMORY ALLOCATION: (VERSION 1.1)
LOCATION(S) IN HEXMCS BASIC-52 USAGE

00H THRU 07H
08H
09H
0AH
0BH THRU 0FH
10H
11H
12H
13H
14H
15H
16H
17H
18H THRU 21H"WORKING REGISTER BANK"BASIC TEXT POINTER -- LOW BYTE
ARGUMENT STACK POINTER
BASIC TEXT POINTER -- HIGH BYTETEMPORARY BASIC STORAGE (Available to user in BASICCALLS to ASM routines)
READ TEXT POINTER -- LOW BYTECONTROL STACK POINTERREAD TEXT POINTER -- HIGH BYTE
START ADDRESS OF BASIC PROGRAM -- HIGH BYTE
START ADDRESS OF BASIC PROGRAM -- LOW BYTENULL COUNTPRINT HEAD POSITION FOR OUTPUTFLOATING POINT OUTPUT FORMAT TYPENOT USED -- RESERVED FOR USER

22H

BITS USED SPECIFICALLY AS FOLLOWS

BIT 22.0H
BIT 22.1H
BIT 22.2H
BIT 22.3H
BIT 22.4H
BIT 22.5H
BIT 22.6H
BIT 22.7H

SET WHEN "ONTIME" STATEMENT IS EXECUTED
SET WHEN BASIC INTERRUPT IN PROGRESSSET WHEN "ONEX1" STATEMENT IS EXECUTED
SET WHEN "ONERR" STATEMENT IS EXECUTED
SET WHEN "ONTIME" INTERRUPT IS IN PROGRESSSET WHEN A LINE IS EDITEDSET WHEN EXTERNAL INTERRUPT IS PENDING
WHEN SET, CONT COMMAND WILL WORK

23H

BITS USED SPECIFICALLY AS FOLLOWS

BIT 23.0H
BIT 23.1H
BIT 23.2H
BIT 23.3H
BIT 23.4H
BIT 23.5H
BIT 23.6H
BIT 23.7H

USED AS FLAG FOR "GET" OPERATORSET WHEN PRINT@ OR LIST@ IS EVOKED
RESERVED, TRAPS TIMER 1 INTERRUPTCONSOLE OUTPUT CONTROL, 1 = LINE PRINTERCONSOLE OUTPUT CONTROL, 1 = USER DEFINEDBASIC ARRAY INITIALIZATION BITCONSOLE INPUT CONTROL, 1 = USER DEFINEDRESERVED, USED TO TRAP SERIAL PORT INTERRUPT- 166 -

intelINTERNAL MEMORY ALLOCATION (VERSION 1.1)
LOCATION(S) IN HEXMCS BASIC-52 USAGE

24H

BITS USED SPECIFICALLY AS FOLLOWS

BIT 24.0H
BIT 24.1H
BIT 24.2H
BIT 24.3H
BIT 24.4H
BIT 24.5H
BIT 24.6H
BIT 24.7H

STOP STATEMENT OR CONTROL-C ENCOUNTEREDUSER IDLE BREAK BIT
SET DURING AN INPUT INSTRUCTIONRESERVEDSET WHEN ARGUMENT STACK HAS A VALUERETI INSTRUCTION EXECUTEDRESERVED, TRAPS EXTERNAL INTERRUPT 0
SET BY USER TO SIGNIFY THAT A VALID LIST@ OR
PRINT@ DRIVER IS PRESENT

25H

BITS USED SPECIFICALLY AS FOLLOWS

BIT 25.0H
BIT 25.1H
BIT 25.2H
BIT 25.3H
BIT 25.4H
BIT 25.5H
BIT 25.6H
BIT 25.7H

RESERVED, SOFTWARE TRAP TESTFIND THE END OF PROGRAM, IF SETSET DURING A DIM STATEMENT
INTERRUPT STATUS SAVE BIT
RESERVED, INPUT TRAPSET TO SIGNIFY EXPANSION IS PRESENTSET WHEN CLOCK1 EXECUTED, ELSE CLEARED
SET WHEN BASIC IS IN THE COMMAND MODE

26H

BITS USED SPECIFICALLY AS FOLLOWS

BIT 26.0H
BIT 26.1H
BIT 26.2H
BIT 26.3H
BIT 26.4H
BIT 26.5H
BIT 26.6H
BIT 26.7H

SET TO DISABLE CONTROL-C
SET TO ENABLE "FAKE" DMARESERVED, OUTPUT TRAPSET TO EVOKE "INTELLIGENT" PROM PROGRAMMINGSET TO PRINT TEXT STRING FROM ROM
SET WHEN CONTROL-S ENCOUNTEREDSET TO SUPPRESS ZEROS IN HEX MODE PRINTSET EVOKE HEX MODE PRINT- 167 -

intelINTERNAL MEMORY ALLOCATION (VERSION 1.1)
LOCATION(S) IN HEXMCS BASIC-52 USAGE

27H
28H THRU 3DH
3EH
3FH
40H
41H
42H
43H
44H
45H
46H
47H
48H

"BIT" ADDRESSABLE BYTE COUNTER
BIT AND BYTE FLOATING POINT WORKING SPACEINTERNAL STACK POINTER HOLDING REGISTERLENGTH OF USER DEFINED STRING -- $TIMER 1 RELOAD LOCATION -- HIGH BYTETIMER 1 RELOAD LOCATION -- LOW BYTE
BASIC TEXT POINTER SAVE LOCATION -- HIGH BYTE
BASIC TEXT POINTER SAVE LOCATION -- LOW BYTERESERVEDTRANCENDENTAL FUNCTION TEMP STORAGE
TRANCENDENTAL FUNCTION TEMP STORAGEMILLI-SECOND COUNTER FOR REAL TIME CLOCKSECOND COUNTER FOR REAL TIME CLOCK -- HIGH
BYTE
SECOND COUNTER FOR REAL TIME CLOCK -- LOW
BYTE
TIMER 0 RELOAD FOR REAL TIME CLOCK
USER ARGUMENT FOR ONTIME -- HIGH BYTE
USER ARGUMENT FOR ONTIME -- LOW BYTE
8052AH STACK SPACE AND USER WORKING SPACE

49H
4AH
4BH
4CH
4DH THRU 0FFH

- 168 -

intelEXTERNAL MEMORY ALLOCATION (VERSION 1.1)
LOCATION(S) IN HEXMCS BASIC-52 USAGE

00H THRU 03H04H
05H AND 06H
07H THRU 56H
56H THRU 5DH
5EH
5FH
60H THRU 0FEH
00FH
100H
101H
102H AND 103H
104H AND 105H
106H AND 107H
108H AND 109H
10AH AND 10BH
10CH AND 10DH
1OEH THRU 113H
114H THRU 11FH
120H AND 121H
122H AND 123H
124H AND 125H
126H AND 127H
128H AND 129H
12AH AND 12BH
12CH
12DH THRU 1FEHNOT USED, RESERVEDLENGTH OF THE CURRENT EDITED LINELN NUM IN BINARY OF CURRENT EDITED LINE (H-L)
BASIC INPUT BUFFER
BINARY TO INTEGER TEMPUSED FOR RUN TRAP MODE (= 34H)
USED FOR POWER-UP TRAP (= 0A5H)
CONTROL STACKCONTROL STACK OVERFLOWLOCATION TO SAVE "GET" CHARACTERLOCATION TO SAVE ERROR CHARACTER CODE
LOCATION TO GO TO ON USER "ONERR" (H-L)
TOP OF VARIABLE STORAGE (H-L)
FP STORAGE ALLOCATION (H-L)
MEMORY ALLOCATED FOR MATRICIES (H-L)
TOP OF MEMORY ASSIGNED TO BASIC (H-L)
RANDOM NUMBER SEED (H-L)
CRYSTAL VALUEFLOATING POINT TEMPSLOCATION TO GO TO ON ONEX1 INTERRUPT (H-L)
NUMBER OF BYTES ALLOCATED FOR STRINGS (H-L)
SOFTWARE SERIAL PORT BAUD RATE (H-L)
LINE NUMBER FOR ONTIME INTERRUPT (H-L)"NORMAL" PROM PROGRAMMER TIME OUT (H-L)"INTELLIGENT" PROM PROGRAMMER TIME OUT (H-L)
RESERVEDARGUMENT STACKNOTE: (H-L) still means HIGH BYTE -- LOW BYTE, in external memory all 16 bit binary numbers
are stored with the HIGH BYTE in the first (lower order) address and the LOW BYTE in the next
sequential address.

- 169 -

intel
1.2 USING THE PWM STATEMENTThe PWM statement can be used to generate quite accurate frequencies. The following table lists thereload values 8 octaves of an equal tempered chromatic scale. The reload values are for the first twoarguments of the PWM statement, so it is assumed that a square wave is being generated. The reload
values assume a 11.0592 MHz crystal.
NOTE

OCTAVE

C
C#
D
D#
E
F
F#
G
G#
A
A#
B
C
C#
D
D#
E
F
F#
G
G#
A
A#
B
C
C#
D
D#
E
F
F#
G
G#
A
A#
B

1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2
2
2
3
3
3
3
3
3
3
3
3
3
3
3

IDEAL
FREQUENCYACTUALFREQUENCY

32.70334.64836.70838.89141.20343.65446.24648.99951.91355.00058.27061.73565.40669.29673.41677.78282.40687.30892.49897.998103.826110.000
116.540
123.470130.812138.592
146.832
155.564
164.812174.616184.996195.996207.652
220.000
233.080
246.940

32.70434.64936.70838.88941.20243.65346.21549.00051.91555.00158.27061.73665.40869.29373.41177.78582.40387.30692.49398.000103.830
110.002116.540
123.472130.798
138.586
146.845155.570
164.807174.612184.986
196.001
207.661
219.952
233.080
246.946- 170 -RELOADHEXRELOAD

14090
13299
12553
11849
11184
10556
9963
9404
8876
8378
7908
7464
7045
6650
6277
5924
5592
5278
4982
4702
4438
4189
3954
3732
3523
3325
3138
2962
2796
2639
2491
2351
2219
2095
1977
1866

370AH
33F3H
3109H
2E49H
2BB0H
293CH
26EBH
24BCH
22ACH
20BAH
1EE4H
1D28H
1B85H
19FAH
1885H
1724H
15D8H
149EH
1376H
125EH
1156H
105DH
0F72H
0E94H
0DC3H
0CFDH
0C42H
0B92H
0AECH
0A4FH
09BBH
092FH
08ABH
082FH
07B9H
074AH

intel
1.2 USING THE PWM STATEMENTNOTEOCTAVE

C
C#
D
D#
E
F
F#
G
G#
A
A#
B
C
C#
D
D#
E
F
F#
G
G#
A
A#
B
C
C#
D
D#
E
F
F#
G
G#
A
A#
B

4
4
4
4
4
4
4
4
4
4
4
4
5
5
5
5
5
5
5
5
5
5
5
5
6
6
6
6
6
6
6
6
6
6
6
6

IDEAL
FREQUENCYACTUALFREQUENCY

261.624
277.184
293.664311.128
329.624
349.232
369.992
391.992
415.304
440.000
466.160
493.880523.248554.368
587.238622.256659.248698.464739.984783.984
830.608880.000
932.320987.760
1046.496
1108.736
1174.656
1244.512
1318.496
1396.928
1479.968
1567.968
1661.216
1760.000
1864.640
1975.520

261.669
277.256293.690311.141329.614349.355
370.120391.836415.135440.114465.925
493.890523.042554.512587.006
621.862659.228698.182739.647783.674
830.270879.389932.793986.724
1047.272
1107.692
1175.510
1245.405
1320.343
1396.364
1481.672
1567.347
1663.538
1758.779
1865.587
1977.682- 171 -RELOADHEXRELOAD

1761
1662
1569
1481
1398
1319
1245
1176
1110
1047
989
933
881
831
785
741
699
660
623
588
555
524
494
467
440
416
392
370
349
330
311
294
277
262
247
233

06E1H
067EH
0621H
05C9H
0576H
0527H
04DDH
0498H
0456H
0417H
03DDH
03A5H
0371H
033FH
0311H
02E5H
02BBH
0294H
026FH
024CH
022BH
020CH
01EEH
01D3H
01B8H
01A0H
0188H
0172H
015DH
014AH
0137H
0126H
0115H
0106H
00F7H
00E9H

intel
1.2 USING THE PWM STATEMENTNOTEOCTAVE

C
C#
D
D#
E
F
F#
G
G#
A
A#
B
C
C#
D
D#
E
F
F#
G
G#
A
A#
B

7
7
7
7
7
7
7
7
7
7
7
7
8
8
8
8
8
8
8
8
8
8
8
8

IDEAL
FREQUENCYACTUALFREQUCNCYRELOADHEXRELOAD

2092.992
2217.472
2349.312
2489.024
2636.992
2793.856
2959.936
3135.936
3322.432
3520.000
3729.280
3951.040
4185.984
4434.944
4698.624
4987.048
5273.984
5587.712
5919.872
6217.872
6644.864
7040.000
7458.560
7902.080

2094.545
2215.385
2351.020
2490.811
2633.143
2792.727
2953.846
3134.694
3315.1083517.557
3716.129
3938.362
4189.091
4430.770
4702.041
5008.695
5296.552
5619.512
5907.692
6227.027
6678.261
7089.231
7432.258
7944.827

220
208
196
185
175
165
156
147
139
131
124
117
110
104
98
92
87
82
78
74
69
65
62
58

00DCH
00DOH
00C4H
00B9H
00AFH
00A5H
009CH
0093H
008BH
0083H
007CH
0075H
006EH
0068H
0062H
005CH
0057H
0052H
004EH
004AH
0045H
0041H
003EH
003AH

- 172 -

intel
1.2 USING THE PWM STATEMENT
The following program generates the appropriate reload values for the PWM statement, using any
crystal. The user enters the desired frequency and the crystal and the program determined the reload
values and errors.
>10 INPUT "ENTER CRYSTAL FREQUENCY - ",X>20 T-12/X>30 INPUT "ENTER DESIRED FREQUENCY FOR PWM - ",F>40 F1=1/F>50 C=(F1/T)/2 : REM CALCULATE RELOAD VALUE>60 IF C<20 THEN 30>70 C1=C-INT(C) : REM CALCULATE FRACTION>80 IF C1<.5 THEN 90 : C=C+1>90 PRINT : PRINT "THE DESIRED FREQUENCY IS - ",X,"HZ">100 C=INT(C) : PRINT>110 PRINT "THE ACTUAL FREQUENCY IS - ",1/(2*C*T),"HZ">120 PRINT>130 PRINT "THE RELOAD VALUE FOR PWM IS - ",C," IN HEX - ",: PH1.C>140 INPUT "ANOTHER FREQUENCY, 1=YES. 0=N0 - ",Q>150 1F Q=1 THEN 20- 173 -

intel
1.3 BAUD RATES AND CRYSTALSThe 16 bit auto-reload timer/counter (TIMER2) that is used to generate baud rates for the MCS BASIC52 device is capable of generating accurate baud rates with a number of crystals. The following is a listof
crystals that will accurately generate 9600 baud on the MCS BASIC-52 device. Additionally, the crystal
values on the left hand side of the table will accurately generate 19200 baud.
XTAL

RCAP2 RELOAD

XTAL

RCAP2 RELOAD

3680400
4300800
4915200
5529600
6144000
6758400
7372800
7987200
8601600
9216000
9830400
10444800
11059200
11673600

65524
65522
65520
65518
65516
65514
65512
65510
65508
65506
65504
65502
65500
65498

3993600
4608000
5222400
5836800
6451200
7065600
7680000
8294400
8908800
9523200
10137600
10752000
11366400
11980800

65523
65521
65519
65517
65515
65513
65511
65509
65507
65505
65503
65501
65499
65497

With the crystals listed above. the accuracy of the baud rate generator and the REAL TIME CLOCK
will depend ONLY on the absolute accuracy of the crystal. Note that the baud rate generator for the8052AH is so accurate that any crystal above 10 MHz will generate 9600 baud to within 1.5%
accuracy.

- 174 -

intel
1.3 BAUD RATES AND CRYSTALS
The following program generates the appropriate TIMER2 reload values for a given baud rate. The user
supplies the system clock frequency and the desired baud rate and the program calculates the proper
TIMER2 reload value. Additionally, percent error, for both the baud rate generator and MCS BASlC52's REAL TIME CLOCK are calculated and displayed.
>10 INPUT"ENTER CRYSTAL - ",X>20 INPUT"ENTER BAUD RATE - ",B>30 R=X/(32*B) : T=X/76800>40 R1=R-INT(R) : T1=T-INT(T)>50 IF R1<.5 THEN 80>60 R1=1-R1>70 R=R+1>80 IF T1<.5 THEN 110>90 T1=1-T1>100 T=T+1>110 PRINT "TIMER2 RELOAD VALUE IS - ",USING(######),INT(65536-R)>120 PRINT "BAUD RATE ERROR IS - ",USING(## ###),(R1/R)*100,"%">130 PRINT "REAL TIME CLOCK ERROR IS - "(T1/T)*100,"/."- 175 -

intel1.4 QUICK REFERENCE
COMMANDS:COMMANDFUNCTION

EXAMPLE(S)RUN

Execute a program

RUN

CONT

CONTinue after a STOP or control-C

CONT

LIST

LIST program to the console device

LIST
LIST 10-50

LIST#

LIST program to serial printer

LIST#
LIST# 50

LIST@

LIST program to user driver (version 1.1 only)

LIST@
LIST@ 50

NEWerase the program stored in RAM

NEW

NULL

set NULL count after carriage return line feed

NULL
NULL 4RAM

evoke RAM mode, current program in
READ/WRITE memory

RAM

ROM

evoke ROM mode, current program in
ROM/EPROM memory

ROM
ROM 3

XFER

transfer a program from ROM/EPROM to RAM

XFER

PROG

save the current program in EPROM

PROG

PROG1

save baud rate information in EPROM

PROG1

PROG2

save baud rate information in EPROM and executeprogram after RESET

PROG2

PROG3

save baud rate and MTOP information in EPROM(version 1.1 only)

PROG3

PROG4

save baud rate and MTOP information in EPROM
and execute program after RESET (version 1.1 only)

PROG4- 176 -

intel1.4 QUICK REFERENCE
COMMANDS:COMMANDFUNCTION

EXAMPLE(S)PROG5same as PROG4 except that external RAM is notcleared on RESET or power up if external RAM
contains a 0A5H in location 5EH (version 1.1 only)

PROG5

PROG6

same as PROG6 except that external code location
4039H is CALLED after RESET (version 1.1 only)

PROG6

FPROG

save the current program in EPROM using the
INTELligent algorithm

FPROG

FPROG1save baud rate information in EPROM using the
INTELligent algorithm

FPROG1FPROG2save baud rate information in EPROM and executeprogram after RESET, use INTELligent algorithm

FPROG2FPROG3same as PROG3, except INTELligent programmingalgorithm is used (version 1.1 only)

FPROG3FPROG4same as PROG4, except INTELligent programmingalgorithm is used (version 1.1 only)

FPROG4FPROG5same as PROG5, except INTELligent programmingalgorithm is used (version 1.1 only)

FPROG5FPROG6

same as PROG6, except INTELligent programmingalgorithm is used (version 1.1 only)

FPROG6- 177 -

intel
1.4 OUICK REFERENCE
STATEMENTS:
STATEMENTFUNCTION

EXAMPLE(S)BAUD

set baud rate for line printer port

BAUD 1200

CALL

CALL assembly language program

CALL 9000H

CLEAR

CLEAR variables, interrupts and Strings

CLEAR

CLEARS

CLEAR Stacks

CLEARS

CLEARI

CLEAR Interrupts

CLEARI

CLOCK1enable REAL TIME CLOCK

CLOCK1

CLOCK0disable REAL TIME CLOCK

CLOCK0DATADATA to be read by READ statement

DATA 100

READ

READ data in DATA statement

READ ARESTORERESTORE READ pointerRESTORE

DIM

allocate memory for arrayed variables

DIM A(20)DO

set up loop for WHILE or UNTIL

DO

UNTIL

test DO loop condition (loop if false)

UNTIL A= 10

WHILE

test DO loop condition (loop if true)

WHILE A= B

END

terminate program execution

END

FOR-TO-{STEP}

set up FOR-NEXT loop

FOR A= 1 TO 5

NEXT

test FOR-NEXT loop condition

NEXT A- 178 -

intel1.4 QUICK REFERENCE
STATEMENTS:
STATEMENTFUNCTION

EXAMPLE(S)

GOSUB

execute subroutine

GOSUB 1000

RETURN

RETURN from subroutine

RETURN

GOTO

GOTO program line number

GOTO 500

ON GOTO

conditional GOTO

ON A GOTO 5,20

ON GOSUB

conditional GOSUB

ON A GOSUB 2,6

IF-THEN-{ELSE}

conditional test

IF A<B THEN A=0

INPUTINPUT a string or variable

INPUT A

LET

assign a variable or string a value

LET A= 10(LET is optional)

ONERR

ONERRor GOTO line number

ONERR 1000

ONTIME

generate an interrupt when TIME is equal to or
greater than ONTIME argument-line number isafter comma

ONTIME 10, 1000

ONEX1

GOSUB to line number following ONEX1 when
INT1 pin is pulled low

ONEX1 1000

PRINT

PRINT variables, strings or literals(P. is shorthand for PRINT)

PRINT APRINT#PRINT to software serial port

PRINT# A

PH0.

PRINT HEX mode with zero suppression

PH0. A

PH1.

PRINT HEX mode with no zero suppression

PH1. A

PH0.#

PH0. to line printerPH0.# A

PH1.#

PH1.# to line printerPH1.# A

- 179 -

intel1.4 QUICK REFERENCE
STATEMENTS:
STATEMENTFUNCTION

EXAMPLE(S)PRINT@PRINT to user defined driver (version 1.1 only)

PRINT@ 5*5

PH0.@

PH0. to user defined driver (version 1.1 only)

PH0. @XBY(5EH)

PH1.@

PH1. to user defined driver (version 1.1 only)

PH1.@ A

PGM

Program an EPROM (version 1.1 only)

PGM

PUSH

PUSH expressions on argument stack

PUSH 10, A

POP

POP argument stack to variables

POP A, B, C

PWM

PULSE WIDTH MODULATIONPWM 50, 50, 100

REM

REMarkREM DONE

RETI

RETurn from Interrupt

RETI

STOP

break program execution

STOP

STRING

allocate memory for STRlNGs

STRING 50, 10

UI1

evoke User console Input routine

UI1

UI0

evoke BASIC console Input routine

UI0

UO1

evoke User console Output routine

UO1

UO0

evoke BASIC console Output routine

UO0

ST@

store top of stack at user specified location(version 1.1 only)

ST@ 1000H
ST@ A

LD@

load top of stack from user specified location(version 1.1 only)

LD@ 1000H
LD@ A

IDLE

wait for interrupt (version 1.1 only)

IDLE

RROM

run a program in EP(ROM) (version 1.1 only)

RROM 3- 180 -

intel1.4 QUICK REFERENCEOPERATORS -- DUAL OPERAND:OPERATORFUNCTION

EXAMPLE(S)+ADDITION

1+1/

DIVISION

10 / 2**EXPONENTATION

2**4

*

MULTIPLICATION

4*4-

SUBTRACTION

8-4

.AND.

LOGICAL AND

10.AND.5.OR.

LOGICAL OR

2.0R.1.XOR.

LOGICAL EXCLUSIVE OR

3.XOR.2

OPERATORS -- SINGLE OPERAND:
ABS( )ABSOLUTE VALUE

ABS(-3)

NOT( )ONES COMPLEMENTNOT(0)

INT( )INTEGER

INT(3.2)

SGN( )SIGN

SGN( - 5)

SQR( )SQUARE ROOTSQR(100)

RND

RANDOM NUMBER

RND

LOG( )

NATURAL LOG

LOG(10)EXP( )"e" (2.7182818) TO THE X

EXP(10)

SIN( )

RETURNS THE SINE OF ARGUMENTSIN(3.14)

COS( )

RETURNS THE COSINE OF ARGUMENT

COS(0)TAN( )

RETURNS THE TANGENT OF ARGUMENT

TAN(.707)

ATN( )

RETURNS ARCTANGENT OF ARGUMENT

ATN(1)- 181 -

intel1.4 QUICK REFERENCEOPERATORS -- SPECIAL FUNCTION:
CBY( )READ PROGRAM MEMORY

P. CBY(4000)

DBY( )

READ/ASSIGN INTERNAL DATAMEMORY

DBY(99)=10

XBY( )

READ/ASSIGN EXTERNAL DATAMEMORYP. XBY(10)GETREAD CONSOLE

P. GET

IE

READ/ASSIGN IE REGISTER

IE=82H

IP

READ/ASSIGN IP REGISTER

IP=0

PORT1

READ/ASSIGN l/O PORT 1 (P1)

PORT1=0FFH

PCON

READ/ASSIGN PCON REGISTER

PCON=0RCAP2READ/ASSIGN RCAP2(RCAP2H:RCAP2L)

RCAP2=100

T2CON

READ/ASSIGN T2CON REGISTER

P. T2CON

TCON

READ/ASSIGN TCON REGISTER

TCON=10H

TMOD

READ/ASSIGN TMOD REGISTER

P. TMODTIME

READ/ASSIGN THE REAL TIME CLOCK

P. TIMETIMER0

READ/ASSIGN TIMER0 (TH0: TL0)

TIMER0=0

TIMER1

READ/ASSIGN TIMER1 (TH1: TL1)

P. TIMER1

TIMER2

READ/ASSIGN TIMER2 (TH2: TL2)

TIMER2=0FFH

STORED CONSTANT:PI

PI

PI -- 3.1415926- 182 -

intel
1.5 INSTRUCTION SET SUMMARYCOMMANDSSTATEMENTS

OPERATORS

RUN
CONT
LIST
LIST#
LIST@ (V1.1)
NEW
NULL
RAM
ROM
XFER
PROG
PROG1
PROG2
PROG3 (V1.1)
PROG4 (V1.1)
PROG5 (V1.1)
PROG6 (V1.1)
FPROG
FPROG1FPROG2FPROG3 (V1.1)
FPROG4 (V1.1)
FPROG5 (V1.1)
FPROG6 (V1.1)

BAUD
CALL
CLEAR
CLEAR(S&I)
CLOCK(1&0)
DATA
READ
RESTORE
DIM
DO-WHILE
DO-UNTIL
END
FOR-TO-STEP
NEXT
GOSUB
RETURNGOTO
ON-GOTO
ON-GOSUB
IF-THEN-ELSE
INPUT
LET
ONERR
ONEX1
ONTIMEPRINT
PRINT#PRINT@ (V1.1)
PH0.
PH0.#
PH0.@ (V1.1)
PH1.
PH1.#
PH1.(@ (V1.1)
PGM (V1.1 )
PUSH
POP
PWM
REM
RETI
STOP
STRING
UI(1&0)
U0(1&0)LD@ (V1.1 )ST@ (V1.1 )IDLE (V1.1)
RROM (V1.1 )ADD (+)
DIVIDE (/)
EXPONENTIATION (**)
MULTIPLY (*)
SUBTRACT (-)
LOGICAL AND (.AND.)LOGICAL OR (.OR.)
LOGICAL X-OR (.XOR.)LOGICAL NOT (.OR.)
ABS( )
INT( )
SGN( )
SQR( )RND
LOG( )
EXP( )
SIN( )
COS( )TAN( )
ATN( )=, >, >=, <, <=, <>ASC( )
CHR( )CBY( )DBY( )XBY( )
GET
IE
IP
PORT1
PCON
RCAP2
T2CON
TCON
TMOD
TIME
TIMER0TIMER1TIMER2
XTAL
MTOP
LEN
FREE
PI

- 183 -

intel
1.6 FLOATING POINT FORMATMCS BASIC-52 stores all floating point numbers in a normalized packed BCD format with an offset
binary exponent. The simplest way to demonstrate the floating point format is to use an example. If thenumber PI (3.1415926) was stored in location X, the following would appear in memory.
LOCATIONVALUEDESCRIPTIONX

81H

EXPONENT -- 81H = 10**1, 82H = 10**2,
80H = 10**0, 7FH = 10**-1 etc.
THE NUMBER ZERO IS REPRESENTED WITH A
ZERO EXPONENT

X-1

00H

SIGN BIT -- 00H = POSITIVE, 01H = NEGATIVE OTHER BITS AREUSED AS TEMPS ONLY DURING A CALCULATION

X-2

26H

LEAST SIGNIFICANT TWO DIGITS

X-3

59H

NEXT LEAST SIGNIFICANT TWO DIGITS

X-4

41H

NEXT MOST SIGNIFICANT TWO DIGITS

X-5

31H

MOST SIGNIFICANT TWO DIGITS

Because MCS BASIC-52 normalizes all numbers, the most significant digit is never a zero unless thenumber is zero.

- 184 -

intel
1.7 STORAGE ALLOCATIONThis section is intended to answer the question -- where does MCS BASIC-52 store its variables andstrings?
Two 16 bit pointers stored in external memory control the allocation of strings and variables and an
additional two pointers control the allocation of scalar variables and dimensioned variables. These
pointers are located and defined as follows:
LOCATION (H-L)

NAME

DESCRIPTION

10AH-10BH

MTOP

THE TOP OF RAM THAT IS ASSIGNED TO BASIC

104H-105H

VARTOP

VARTOP = MTOP - (THE NUMBER OF BYTES OF MEMORY THAT THE USER HAS ALLOCATED FOR STRINGS).
IF STRINGS ARE NOT USED, VARTOP = MTOP106H-107H

VARUSEAFTER A NEW, CLEAR, OR RUN IS EXECUTED, VARUSE =VARTOP, EVERYTIME THE USER ASSIGNS OR USES A
VARIABLE VARUSE IS DECREMENTED BY A COUNT OF 8.

108H-109H

DIMUSEAFTER A NEW, CLEAR, OR RUN IS EXECUTED, DIMUSE =[LENGTH OF THE USER PROGRAM THAT IS IN RAM MEMORY + STARTING ADDRESS OF THE USER PROGRAM IN
RAM (512) + THE LENGTH OF ONE FLOATING POINT
NUMBER (6)]. IF NO PROGRAM IS IN RAM MEMORY,DIMUSE = 518 AFTER A CLEAR IS EXECUTEDMCS BASIC-52 stores string variables between VARTOP and MTOP. $(0) is stored from VARTOP to
VARTOP + (user defined string length + I ), $(1 ) is stored from VARTOP + (user defined string length +I) + I to VARTOP + 2 * (user defined string length + 1) etc. If MCS BASIC-52 attempts to access a stringthat is outside the bounds established by MTOP, a MEMORY ALLOCATION ERROR is generated.
Now, Scalar variables are stored from VARTOP "down" and Dimensioned variables are stored fromDIMUSE "up." When the user dimensions a variable either implicity or explicity the value of DIMUSE
increases by the number of bytes required to store that dimensioned variable. For example, if the user
executes a DIM A(10) statement, DIMUSE would increase by 66. This is because the user is requestingstorage for 11 numbers (A(0) through A(10)) and each number requires 6 bytes for storage and 6 * 11 = 66.

- 185 -

intel
1.7 STORAGE ALLOCATIONAs mentioned in the previous example, every time the user defines a new variable the VARUSE pointerdecrements by a count of 8. Six of the eight counts are due to the memory required to store a floating
point number and the other two counts are the storage required for the variable name (i.e. A1, B7, etc).
The variable B7 would be stored as follows:
LOCATIONVALUEDESCRIPTIONX

37H

THE ASCII VALUE -- 7, IF B7 WAS A DIMENSIONED VARIABLE THE
MOST SIGNIFICANT BIT OF THIS LOCATION WOULD BE SET. IN
VERSION 1.1 THIS LOCATION ALWAYS CONTAINS THE ASCIIVALUE FOR THE LAST CHARACTER USED TO DEFINE A VARIABLE

X-1

42H

THE ASCII VALUE -- B, IN VERSION 1.1 OF MCS BASIC-52 THISLOCATION CONTAINS THE ASCII VALUE OF THE FIRST CHARACTER USED TO DEFINE A VARIABLE PLUS 26 * THE NUMBER OF
CHARACTERS USED TO DEFINE A VARIABLE, IF THE VARIABLE
CONTAINS MORE THAN 2 CHARACTERS.

X-2
THRU
X-7

??

THE NEXT SIX LOCATIONS WOULD CONTAIN THE FLOATING
POINT NUMBER THAT THE VARIABLE IS ASSIGNED TO, IF THE
VARIABLE WAS A SCALAR VARIABLE. IF THE VARIABLE WAS DIMENSIONED, X-2 WOULD CONTAIN THE LIMIT OF THE DIMENSION(I.E. THE MAX. NUMBER OF ELEMENTS IN THE ARRAY) AND
X-3: X-4 WOULD CONTAIN THE BASE ADDRESS OF THE ARRAY.
THIS ADDRESS IS EQUAL TO THE OLD VALUE OF THE DIMUSE
POINTER BEFORE THE ARRAY WAS CREATED

Whenever a new scalar or dimensioned variable is used in a program, MCS BASIC-52 checks both the
DIMUSE and VARUSE pointers to make sure that VARUSE > DIMUSE. If the relationship is not true,
a MEMORY ALLOCATION ERROR is generated.

- 186 -

intel
1.7 STORAGE ALLOCATIONTo Summarize:
Strings are stored from VARTOP to MTOP.
Scalar variables are stored from VARTOP "down" and VARUSE points to the next available scalar
location.
Dimensioned variables are stored from the end of the user program in RAM "up." If no program is in
RAM this location is 518 . DIMUSE keeps track of the number of bytes the user has allocated for
dimensioned variables.
If DIMUSE >= VARUSE a MEMORY ALLOCATION ERROR is generated- 187 -

intel
1.8 FORMAT OF AN MCS BASIC-52 PROGRAM
This section answers the question "How does MCS BASIC-52 store a program?"
LINE FORMAT
Each line of MCS BASIC-52 text consists of tokens and ASCII characters, plus 4 bytes of overhead.
Three of these four bytes are stored at the beginning of every line. The first byte contains the length of aline in binary and the second two bytes are the line number in binary. The fourth byte is stored at theend of the line and this byte is always a 0DH or a carriage return in ASCII. An example of a typical line
is shown below, assume that this is the first line of a program in RAM.
10 FOR I = I TO 10: PRINT 1: NEXT I
LOCATION

BYTE

DESCRIPTION

512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528

11H
00H
0AH
0A0H
49H
0EAH
31H
0A6H
31H
30H
3AH
89H
49H
3AH
97H
49H
0DH

THE LENGTH OF THE LINE IN BINARY (17D BYTES)HIGH BYTE OF THE LINE NUMBER
LOW BYTE OF THE LINE NUMBER
THE TOKEN FOR "FOR"THE ASCII CHARACTER "I"
THE TOKEN FOR "="THE ASCII FOR "1"
THE TOKEN FOR "TO"THE ASCII FOR "1"
THE ASCII FOR "0"
THE ASCII FOR ":"THE TOKEN FOR "PRINT"
THE ASCII FOR "I"
THE ASCII FOR ":"THE TOKEN FOR "NEXT"THE ASCII FOR "I"
END OF LINE (CARRIAGE RETURN)

TO FIND THE LOCATION OF THE NEXT LINE, THE LENGTH OF THE LINE IS ADDED TO
THE LOCATION WHERE THE LENGTH OF THE LINE IS STORED. IN THIS EXAMPLE, 512+ 17D = 529, WHICH IS WHERE THE NEXT LINE IS STORED.
The END of a program is designated by the value 01H. So, in the previous example if line 10 was the
only line in the program, location 529 would contain the value 01H. A program simply consists of anumber of lines packed together in one continuous block with the last line ending in a 0DH, 01H
sequence.

- 188 -

intel
1.8 FORMAT OF AN MCS BASIC-52 PROGRAM
EPROM FILE FORMATThe EPROM FILE format consists of the same line and program format, previously described exceptthat each program in the EPROM file begins with the value 55H. The value 55H is only used by MCS
BASIC-52 to determine if a valid program is present. If the user types ROM 6, MCS BASIC-52
actually goes through the first program stored in EPROM line by line until the END of PROGRAM(01H) is found, then it examines the next location to see if a 55H is stored in that location. It then goes
through that program line by line. This process is repeated 6 times. If the character 55H is not foundafter the end of a program, MCS BASIC-52 will return with the PROM MODE error message. This
would mean that less than six programs were stored in that EPROM.
The first program stored in EPROM (ROM 1) always begins at location 8010H and this location will
always contain a 55H. The actual user program will begin at location 8011H.
EPROM locations 8000H through 800FH are reserved by MCS BASIC-52. These locations contain initialization information when the PROGX options are used. Version 1.0 of MCS BASIC-52 only usedthefirst three bytes of this reserved EPROM area. The information stored in these bytes is as follows:
LOCATIONDESCRIPTION

8000H

CONTAINED A 31H IF PROG 1 WAS USED, CONTAINED A 32H IF PROG 2
WAS USED

8001H

BAUD RATE (RCAP2H)

8002H

BAUD RATE (RCAP2L)

Version 1.1 of MCS BASIC-52 uses the same locations as Version 1.0, but additionally locations
8003H and 8004H (high byte, low byte) are used to store the MTOP information for the PROG 3, 4, 5,
6 options.
IMPORTANT NOTE -The PROG X options simply store ASCII character following the PROG command in location 8000H.
That is why PROG 1 stores a 31H in location 8000H, PROG 2 a 32H, PROG 3 (Version 1.1 only) a
33H etc. If the user employs the user defined reset option defined in Chapter 11 of this manual, it would
be possible for the user to create unique PROG options. For example, PROG A would store a 41H inlocation 8000H and upon RESET the user could examine this location with an assembly language
routine and generate a unique PROG A reset routine for that particular application.

- 189 -

intel
1.9 ANSWERS TO A FEW QUESTIONSQUESTION
Why can't MCS BASIC-52 access the 8052's SPECIAL FUNCTION REGISTER SCON ?
ANSWER
The only time the user would likely change the contents of SCON is if the user is writing custom I/Odrivers in assembly language. If the user is writing assembly language I/O drivers, then the user canchange the contents of SCON in assembly language. Changing the contents of SCON can cause MCS
BASIC-52's console routines to crash.
QUESTION
I have written an upload/download routine using my computer, but when I download a program, MCS
BASIC-52 misses characters, why?
ANSWERMCS BASIC-52 is actually capable of accepting characters at 38,400 baud. The problem is that after
MCS BASIC-52 receives a carriage return (cr), it tokenizes the line of text that was just entered.
Depending on how complicated and how long the line is, MCS BASIC-52 can take up to a couple ofhundred milliseconds to tokenize the line. If the user keeps stuffing characters into the serial port while
MCS BASIC-52 is tokenizing the line, the characters will be lost. What the user must do in thedownload routine is wait until MCS BASIC-52 responds with the prompt character (>) after a carriage
return is sent to the MCS BASIC-52 device. The prompt (>) informs the user that MCS BASIC-52 isready to receive characters from the console device.
QUESTION
I am writing in assembly language and I notice that the 8052AH has no decrement DPTR instruction.
What is the easiest. shortest or simplest way to decrement the DPTR?
ANSWER
The shortest one we know is:

DECDP:

XCH
JNZ
DEC
DEC
XCH

A, DPL
DECDP
DPH
A
A, DPL; SWAPA<>DPL; DPH = DPH-1 IF DPL = O; DPL = DPL-1

This routine affects no flags or registers (except the DPTR) either!

- 190 -

intel
1.9 ANSWERS TO A FEW QUESTIONSQUESTION
After RESET or power-up, MCS BASIC-52 does not return the proper value for MTOP, what's the
problem?
ANSWER
Virtually every time this problem occurs it is because something is wrong with the decoding circuitry inthe system or one or more of the address lines to the RAM are open or shorted. The user should makesure that all of the address lines to the system RAM are connected properly!
A simple memory test can be implemented in the COMMAND MODE to verify the addressing to theRAM. First set XBY(1000H) = 55, then walk ones across the address (i.e. P. XBY(1001H) - P.
XBY(1002H) - P. XBY(1004H) -- P. XBY(1008H) P. XBY(1010H)) until all locations are tested. If for
instance, P. XBY(1008H) returns a result of 55, then address line 3 (A3) would probably be open or
shorted.

- 191 -

intel1.10 PIN-OUT LIST
The following is a pin-out list of the most common devices found in an MCS BASIC-52 system:

VEE

1

14

VCC

INPUT A

1

14

VCC

INPUT A2

13

INPUT D1RESPONSE
CONTROL A

2

13

INPUT DOUTPUT A

3

12

INPUT D2OUTPUT A

3

12

RESPONSEOUTPUT D

INPUT B1

4

11

OUTPUT D

INPUT B

4

11

OUTPUT D

INPUT B2

5

10

INPUT C1RESPONSE
CONTROL B

510

INPUT COUTPUT B

6

9

INPUT C2OUTPUT B

69RESPONSE
CONTROL C

GND

7

8

OUTPUT C

GROUND

7

8

OUTPUT C

1488

1

20

VCC

O0

2

19

O7

1

14

A6

3

18

D7

2

13

A5

4

17

D6
3

12

O1

5

16

O6

O2

6

15

O5

4

11

A7

7

14

D5

A4

5

10

8

13

D4

A3

9

12

O4

10

11

1

20

1489

VPP

GND

DIR

6

E

9

7

GND

74LS373

VCC

8

1

A0

VCC

16

2

A1

O0

15

3

A2

O1

14

4

E1

O2

13

5

E2

O3

12

6

E3

O4

11

7

O7

O5

10

8

GND

O6

9

A0

2

19

CE

A1

3

18

B0

A2

4

17

B1

A3

5

16

B2

A4

6

15

B3

A5

7

14

B4

A6

8

13

B5

A7

9

12

B6

GND

10

11

B7

74LS138

74LS32VCC

74LS245

P27128A

GND

1

14

2

13

3

12

4

11

5

10

6

9

7

8

GND

14

2

13

2764A

VPP
A12
A7
A6
A5
A4
A3
A2
A1
A0
O0
O1
O2
GND

VPP
A12
A7
A6
A5
A4
A3
A2
A1
A0
O0
O1
O2
GND

2732A

VPP

1

28

VCC

A12

2

27

PGM

A7
A6
A5
A4
A3
A2
A1
A0
O0
O1
O2
GND

A7

3

26

A13

A6

4

25

A8

A5

5

24

A9

A4

6

23

A11

A3

7

22

OE

A2

8

21

A10

A1

9

20

CE

A0

10

19

O7

O0

11

18

O6

O1

12

17

O5

O2

13

16

O4

GND

14

15

O3

2732A

VCC
A8
A9
A11
OE/VPP
A10
CE
O7
O6
O5
O4
O3

2764A

27256

VCC

VCC
A14
A13
A8
A9
A11
OE/VPP
A10
CE
O7
O6
O5
O4
O3

PGM
N.C.
A8
A9
A11
OE/VPP
A10
CE
O7
O6
O5
O4
O3

VCC

EPROMS

74LS0874LS00
INVERTING

1

27256

VCC

T2 / P1.0

1

40

VCC

PA3

1

40

PA4

T2EX / P1.1

2

39

AD0

PA2

2

39

PA5

PWM OUTPUT / P1.2

3

38

AD1

PA1

3

38

PA6

ALE DISABLE / P1.3

4

37

AD2

PA0

4

37

PA7

PROGRAM PULSE / P1.4

5

36

AD3

RD

5

36

WR

PROGRAM ENABLE / P1.5

6

35

AD4

CS

6

35

RESET

NC

1

28

VCC

DMA ACKNOLEDGE / P1.6

7

34

AD5

GND

7

34

D0

A12

2

27

WR

LINE PRINTER OUTPUT / P1.7

8

33

AD6

A1

8

33

D1

A7

3

26

NC

RESET

9

32

AD7

A0

9

32

D2

A6

4

25

A8

CONSOLE SERIAL INPUT

1031+ 5 VOLTS

PC7

10

31

D3

A5

5

24

A9

CONSOLE SERIAL OUTPUT

118052AH-

30

ALE

PC6

11

30

D4

A4

6

23

A11

INT0 / DMA REQUEST

12

BASIC

29

PSEN

PC5

12

29

D5

A3

7

22

OE

INT1

13

28

A15

PC4

13

28

D6

A2

8

21

A10

T2

14

27

A14

PC3

14

27

D7

8255A

3

12

A1

9

20

CE

T1

15

26

A13

PC2

15

26

VCC

4

11

A0

10

19

I/O7

WR

16

25

A12

PC1

16

25

PB7

5

10

I/O0

11

18

I/O6

RD

17

24

A11

PC0

17

24

PB6

I/O1

12

17

I/O5

XTAL2

18

23

A10

PB0

18

23

PB5

6

9

I/O2

13

16

I/O4

XTAL1

19

22

A9

PB1

19

22

PB4

7

8

GND

14

15

I/O3

GND

20

21

A8

PB2

20

21

PB3

7406/LS04/LS05
7407 NONINVERTING

8K x 8 SRAM

- 192 -

intel1.11 8052AH SPECIAL FUNCTION REGISTERS
The following details the operation of the special function registers on the 8052AH:
SYMBOL
NAME

NAME

ACC

Accumulator

B

B Register

PSW

Program Status WordSP

Stack Pointer

DPTR

Data Pointer 2 Bytes:

DPH

Low Byte

DPL

High Byte

P0Port 0

P1

Port 1

P2

Port 2

P3Port 3

IP

Interrupt Priority Control

IE

Interrupt Enable Control

TMOD

Timer/Counter Mode Control

TCON

Timer/Counter Control

T2CON

Timer/Counter 2 Control

TH0

Timer/Counter 0 High Byte

ADDRESSMCS BASIC-52

0E0H
0F0H
0D0H
81H

NOT ADDRESSABLE

82H
83H
80H
90H
0A0H
0B0H
0B8H
0A8H
89H
88H
0C8H
8CH

NOT ADDRESSABLE

NOT ADDRESSABLE
NOT ADDRESSABLE
NOT ADDRESSABLE

NOT ADDRESSABLE
NOT ADDRESSABLE
PORT1
NOT ADDRESSABLE
NOT ADDRESSABLE
IP
IE
TMOD
TCON
T2CON
} TIMER0

TL0

Timer/Counter 0 Low Byte

TH1Timer/Counter 1 High Byte

8AH
8DH
} TIMER1

TL1

Timer/Counter 1 Low Byte

TH2Timer/Counter 2 High Byte

8BH
0CDH
} TIMER2

TL2

Timer/Counter 2 Low Byte

RCAP2HT/C 2 Capture Reg. High Byte

0CCH
0CBH
} RCAP2

RCAP2L
SCON
SBUF
PCON

0CAH
98H
99H
87H

T/C 2 Capture Reg. Low Byte
Serial Control
Serial Data BufferPower Control- 193 -NOT ADDRESSABLE
NOT ADDRESSABLE
NOT ADDRESSABLE

intel1.11 8052AH SPECIAL FUNCTION REGISTERS
PSW: PROGRAM STATUS WORD. ADDRESS 0D0H

CY

AC

F0

RS1

RS0

OV

CY

PSW.7

Carry Flag.

AC

PSW.6

Auxiliary Carry Flag.

F0

PSW.5

Flag 0 available to the user for general purpose.

RS1

PSW.4

Register Bank selector bit 1.

RS0

PSW.3

Register Bank selector bit 0.

OV

PSW.2

Overflow Flag.

--

PSW.1

RESERVED FOR FUTURE USE.

P

PSW.0

PARITY FLAG.

--

P

--

--

PCON: POWER CONTROL REGISTER. NOT BIT ADDRESSABLE.

SMOD

--

--

--

--

--

SMOD Doubles the baud rate when TIMER 1 is used to generate the baud rate for the serial port.
The remaining bits of PCON are not implemented on the MCS BASIC-52 device.

- 194 -

intel1.11 8052AH SPECIAL FUNCTION REGISTERS

(MSB)
GAT
E

(LSB)
C/T

M1

M0

GAT
E

TIMER 1GATE Gating control When set. Timer/Counter "x"is enabled only while "INTx" pin is high and"TRx" control pin is set. When cleared
Timer "x" is enabled whenever "TRx"control bit is set
C/T

Timer or Counter Selector Cleared for Timer
operation (input from internal system clock).
Set for Counter operation (input from "Tx"
input pin).

C/T

M1

M0

TIMER 0
M1 M0 Operating Mode00

MCS-48 Timer "TLx" serves as fivebit prescaler.

0

1

16 bit Timer/Counter "THx" and "TLX"
are cascaded; there is no prescaler

108-bit auto-reload timer-counter "THx"holds a value which is to be reloaded into
"TLx" each time it overflows.

1

1(Timer 0) TL0 is an eight-bit timer
counter-controlled by thestandard Timer 0 control
bits TH0 is an eight-bit timer
only controlled by Timer1
control bits.

1

1(Timer 1) Timer-counter 1 stopped.

TMOD: Timer/Counter Mode Control Register- 195 -

intel1.11 8052AH SPECIAL FUNCTION REGISTERS

(MSB)

(LSB)

TF2

EXF2

SymbolPosition

Name and Significance

TF2

T2CON.7

Timer 2 overflow flag set by a Timer 2 overflow and must be clearedby software. TF2 will not be set when either RCLK = 1 or TCLK = 1.

EXF2

T2CON.6

Timer 2 external flag set when either a capture or reload is caused by anegative transition on T2EX and EXEN2 = 1. When Timer 2 interrupt
is enabled, EXF2 = 1 will cause the CPU to vector to the Timer 2interrupt routine. EXF2 must be cleared by software.

RCLK

T2CON.5

Receive clock flag. When set, causes the serial port to use Timer 2
overflow pulses for its receive clock in modes 1 and 3. RCLK = 0
causes Timer 1 overflow to be used for the receive clock.

TCLK

T2CON.4

Transmit clock flag. When set, causes the serial port to use Timer 2
overflow pulses for its transmit clock in modes 1 and 3. TCLK = 0
causes Timer 1 overflows to be used for the transmit clock.

EXEN2 T2CON.3

Timer 2 external enable flag. When set, allows a capture or reload tooccur as a result of a negative transition on T2EX if Timer 2 is not
being used to clock the serial port.
EXEN2 = 0 causes Timer 2 to ignore events at T2EX.

TR2

T2CON.2

Start/stop control for Timer 2. A logic 1 starts the timer.

C/T2

T2CON.1

Timer or counter select. (Timer 2)
0 = Internal timer (OSC/12)
1 = External event counter (falling edge triggered).

CP/RL2 T2CON.0

RCLK

TCLK

EXEN2

TR2

C/T2

CP/RLS2

Capture/Reload flag. When set, captures will occur on negativetransitions at T2EX if EXEN2 = 1. When cleared, auto reloads will
occur either with Timer 2 overflows or negative transitions at T2EX
when EXEN 2 = 1. When either RCLK = 1 or TCLK = 1, this bit isignored and the timer is forced to auto-reload on Timer 2 overflow.

Timer/Counter2 Control Register- 196 -

intel1.11 8052AH SPECIAL FUNCTION REGISTERS
(MSB)

(LSB)

SM0 SM1 SM2 REN

TB8

RB8

TI

RI

where SM0, SM1 specify the serial port mode, as follows:
SM0

SM1

Mode

0
0
1

0
1
0

0
1
2

1

1

3

• SM2

• REN

Description

Baud
Rate
shift
fosc /12
registervariable
8-bit UART fosc./64
9-bit UART or
fosc /32
9-bit UART variable• TB8 is the 9th data bit that will be transmitted inmodes 2 and 3. Set or clear by software as
desired.
• RB8enables the multiprocessor
communication feature in modes 2 and 3.
In mode 2 or 3, if SM2 is set to 1 then RI
will not be activated if the received 9th
data bit (RB8) is 0. In mode 1, if SM2 = 1
then RI will not be activated if a valid stop
bit was not received. In mode 0, SM2
should be 0.
enables serial reception. Set by software toenable reception. Clear by software to disable reception.

• TI

In modes 2 and 3, is the 9th data bit thatwas received. In mode 1, if SM2 = 0, RB8
is the stop bit that was received. In mode0, RB8 is not used.

is transmit interrupt flag. Set by hardware at
the end of the 8th bit time in mode 0, or at
the beginning of the stop bit in the othermodes, in any serial transmission. Must be
cleared by software.

• RI is receive interrupt flag. Set by hardware at
the end of the 8th bit time in mode 0, or
halfway through the stop bit time in theother modes, in any serial reception(except see SM2). Must be cleared bysoftware.

SCON: Serial Port Control Register
(MSB)

(LSB)

TF1

TR1

TF0

TR0

IE1

IT1

IE0

IT0

0

1

0

1

0

1

0

0

Symb. Position
Name and Significance
TF1
TCON.7 Timer 1 overflow Flag. Set by hardware on timer/counter overflow.
Cleared by hardware when processor
vectors to interrupt routine.
TR1 TCON.6 Timer 1 Run control bit. Set clearedby software to turn timer/ counter
on/off.
TF0
TCON.5 Timer 0 overflow flag. Set by hardware on timer/counter overflow.
Cleared by hardware when processor
vectors to interrupt routine.
TR0 TCON.4 Timer 0 Run control bit. Set
cleared by software to turn timer/counter on/off.

Symb. Position
Name and Significance
IE1
TCON.3 Interrupt 1 Edge flag. Set by hardware
when external interrupt edge detected.
Cleared when interrupt processed.
IT1
TCON.2 Interrupt 1 Type control bit. Set
cleared by software to specify fallingedge/low level triggered externalinterrupts.
IE0
TCON.1 Interrupt 0 Edge flag. Set by hardware
when external interrupt edge detected.
Cleared when interrupt processed.
IT0
TCON.0 Interrupt 0 Type control bit. Set/
cleared by software to specify fallingedge/low level triggered externalinterrupts.

TCON: Timer/Counter Control Register- 197 -

intel1.11 8052AH SPECIAL FUNCTION REGISTERS
(MSB)
X

X

(LSB)

(MSB)

PT
PT PX PT PX
PS
2
1
1
0
0

EA

Symbol Position Function

X

(LSB)
ET
ET EX ET EX
ES
2
1
1
0
0

Symbol Position Function

--

IP.7

reserved--

IP.6

reserved

PT2

IP.5

defines the Timer 2 interrupt
priority level. PT2 = 1 programs it
to the higher priority level.

EA

IE.7

disables all interrupts. If EA = 0, nointerrupt will be acknowledged. If
EA = 1, each interrupt source isindividually enabled or disabled by
setting or clearing its enable bit.

--

IE.6

reservedPS

IP.4

defines the Serial Port interrupt
priority level. PS = 1 programs it
to the higher priority level.

ET2

IE.5

enables or disables the Timer 2
overflow or capture interrupt. If ET2= 0, the Timer 2 interrupt is disabled.

PT1

IP.3

defines the Timer 1 interrupt
priority level. PT1 = 1 programs it
to the higher priority level.

ES

IE.4

enables or disables the Serial Port
interrupt. If ES = 0, the Serial Port
interrupt is disabled.

PX1

IP.2

defines the External Interrupt 1priority level. PX1 = 1 programs
it to the higher priority level.

ET1

IE.3

enables or disables the Timer 1
Overflow interrupt. If ET1 = 0, theTimer 1 interrupt is disabled.

PT0

IP.1

defines the Timer 0 interrupt
priority level. PT0 = 1 programs it
to the higher priority level.

EX1

IE.2

enables or disables External Interrupt
1. If EX1 = 0, External Interrupt 1 isdisabled.

PX0

IP.0

defines the External Interrupt 0
priority level. PX0 = 1 programs
it to the higher priority level.

ET0

IE.1

enables or disables the Timer 0
Overflow interrupt. If ET0 = 0, theTimer 0 Interrupt is disabled.

EX0

IE.0

enables or disables External Interrupt
0. If EX0 = 0, External Interrupt 0 is
disabled.

IP: Interrupt Priority Register

IP: Interrupt Enable Register- 198 -

intel1.12 REFERENCESREFERENCES
J. Sack and J: Meadows, Entering BASIC, Science Research Associates, 1973.
C. Pegels, BASIC: A Computer Programming Language, Holden-Day, Inc., 1973.
J. Kemeny and T. Kurtz, BASIC Programming, People Computer Company, 1967.
Albrecht, Finkle, and Brown, BASIC, People Computer Company, 1973.
T. Dwyer, A Guided Tour of Computer Programming in BASIC, Houghton Mifflin Co., 1973.
Eugene H. Barnett, Programming Time Shared Computers in BASIC, Wiley-Interscience, L/C 72-175789.
Programming Language #2, Digital Equipment Corp., Maynard, Mass. 01754.
101 BASIC Computer Games, Digital Equipment Corp., Maynard, Mass. 01754.
What to do After You Hit Return. People Computer Company.
BASIC-80 REFERENCE MANUAL, Intel Corp., Santa Clara, Calif- 199 -

intelAPPENDIX B
INSTRUCTION SET SUMMARY
This appendix contains two tables (see tables B-1 and B-2): the first identifies all of the 8052's
instructions in alphabetical order; the second table lists the instructions according to their hexadecimal
opcodes and lists the assembly language instructions that produced that opcode.
The alphabetical listing also includes documentation of the bit pattern, flags affected, number ofmachine cycles per execution and a description of the instructions operation and function. The list
below defines the conventions used to identify operation and bit patterns.
ABBREVIATIONS AND NOTATIONS USEDA
AB
B
bit address
page address

Accumulator
Register Pair
Multiplication Register8052 bit address
11-bit code address within
2K pagerelative offset 8-bit 2's complement offset
C
Carry Flag
code address Absolute code address
data
Immediate data
data address On-chip 8-bit RAM address
DPTR
Data pointerPC
Program Counter
Rr
Register (r = 0-7)
SP
Stack pointerHigh
High order byte
low
Low order byte
i-j
Bits i through j.n
Bit n
aaa aaaaaaaa Absolute page address
encoded in instruction
and operand byte
bbbbbbbb
Bit address encoded inoperand byte
dddddddd
Immediate data encoded inoperand byte

One byte of a 16-bit
address encoded inoperand byte
mmmmmmmm Data address encoded inoperand byte
oooooooo
Relative offset encoded inoperand byter or rrr
Register identifier encoded
in operand byte
AND
Logical AND
NOT
Logical complement
OR
Logical OR
XOR
Logical exclusive OR
+
Plus
Minus
/
Divide·Multiply(X)
The contents of X
((X))
The
memorylocationaddressed by (X) (Thecontents of X)=
Is equal to<>
Is not equal to<
Is less than>Is greater than<Is replaced byllllllll- 200 -

intelTable B-1. Instruction Set Summary
Mnemonic
Operation
ACALL code addr
(PC) <- (PC) + 2(SP) <- (SP) + 1((SP)) <- (PC) low(SP) <- (SP) + 1((SP)) <- (PC) high(PC) 0-10 <- page address
ADD A,#data(A) <- (A) + data
ADD A,@Rr(A) <- (A) + ((Rr))
ADD A,Rr(A) <- (A) + (Rr)ADD A,data addr(A) <- (A) + (data address)
ADDC A,#data(A) <- (A) + (C) + data
ADDC A,@Rr(A) <- (A) + (C) + ((Rr))
ADDC A,Rr(A) <- (A) + (C) + (Rr)
ADDC A,data addr(A) <- (A) + (C) + (data address)
AJMP code addr
(PC) 0-10 <- code address
ANL A,#data(A) <- (A)AND data
ANL A,@Rr(A) <- (A) AND ((Rr))
ANL A,Rr(A) <- (A) AND (Rr)
ANL A,data addr(A) <- (A) AND (data address)
ANL C,bit addr(C) <- (C) AND (bit address)
ANL C,lbit addr(C) <- (C) AND NOT (bit address)
ANL data addr, #data(data address) <(data address) AND data
ANL data addr,A(data address) <(data address) AND A

CyclesBinaryCodeFlags
P OV AC C

Function2

aaa10001
aaaaaaaa

1

P OV AC C

Add immediate data to A.

100100100
dddddddd
0010011r

P OV AC C

100101rrr

P OV AC C

Add contents of indirect
address to A.
Add register to A.

1

P OV AC C

100100101
mmmmmmmm
00110100
dddddddd
0011011r

100111rrr

P OV AC C

100110101
mmmmmmmm
aaa00001
aaaaaaaa

P OV AC C

P

101010100
dddddddd
0101011r

1

01011rrr

P

101010101
mmmmmmmm
10000010
bbbbbbbb
10110000
bbbbbbbb
01010011
mmmmmmmm
dddddddd
01010010
mmmmmmmm

P

1

2
1

2
2
2
1

- 201 -

Push PC on stack, andreplace low order 11 bitswith low order 11 bits ofcode address.

P OV AC CP OV AC CPCC

Add contents of data
address to A.
Add C and immediate data
to A
Add C and contents ofindirect address to A.
Add C and register to A
Add C and contents of data
address to A
Replace low order 11 bits of
PC with low order 11 bits
code address.
Logical AND immediate data
to A.
Logical AND contents ofindirect address to A
Logical AND register to A
Logical AND contents ofdata address to A.
Logical AND bit to C
Logical AND complement of
bit to C
Logical AND immediate datato contents of data address
Logical AND A to contents ofdata address.

intelTable B-1. Instruction Set Summary (Cont'd.)Mnemonic
Operation
CJNE @Rr,#data,code addr
(PC) <- (PC) + 3
IF ((Rr)) <> data
THEN(PC) <- (PC) + relative offset
IF ((Rr)) < data
THEN(C) <- 1
ELSE(C) <- 0
CJNE A,#data,code addr
(PC) <- (PC) + 3
IF(A) <> data
THEN(PC) <- (PC) + relative offset
IF (A) < data
THEN(C) <- 1
ELSE(C) <- 0
CJNE A,data addr,code addr
(PC) <- (PC) + 3
IF (A) <> (data address)
THEN(PC) <- (PC) + relative offset
IF (A) < (data address)
THEN(C) <- 1
ELSE(C) <- 0
CJNE Rr,#data,code addr
(PC) <- (PC) + 3
IF (Rr) <> data
THEN(PC) <- (PC) + relative offset
IF (Rr) < data
THEN(C) <- 1
ELSE(C) <- 0
CLR A(A) <- 0
CLR C(C) <- 0
CLR bit addr
(bit address) <- 0
CPL A(A) <- NOT (A)CPL C(C) <- NOT (C)CPL bit addr
(bitaddress) <NOT (bit address)
DA A
DEC @Rr((Rr)) <- ((Rr)) - 1
DEC A(A) <- (A) - 1
DEC Rr(Rr) <- (Rr) -1

CyclesBinaryCodeFlags
P OV AC CCFunction

2

1011011r
dddddddd
oooooooo210110100
dddddddd
ooooooooC

If immediate data and A are
not equal, jump to code
address.

2

10110101
mmmmmmmm
ooooooooC

If contents of data address
and A are not equal, jump tocode address.

210111rrr
dddddddd
ooooooooC

If immediate data andregister are not equal, jump
to code address.

1

11100100

1

11000011

1
111000010
bbbbbbbb
111101001

10110011

1

10110010
bbbbbbbb

1
111010100
0001011r

P

100010100P

100011rrr- 202 -

P

If immediate data andcontents of indirect address
are not equal, jump to code
address.

Set A to zero (0).
C

Set C to zero (0).
Set bit to zero (0).

P

Complements each bit in A.
C

Complement C.
Complement bit.

C

Adjust A after a BCD add.
Decrement contents ofindirect address.
Decrement A.
Decrement register.

intelTable B-1. Instruction Set Summary (Cont'd.)Mnemonic
Operation
DEC data addr
(data address) <(data address) - 1
DIV AB(AB) <- (AJ/(B)DJNZ Rr,code addr
(PC) <- (PC) + 2(Rr) <- (Rr) - 1
IF(Rr) <> 0
THEN(PC) <- (PC) + relative offset
DJNZ data addr,code addr
(PC), <- (PC) + 3(data address) <(data address) - 1
IF (data address) <> 0
THEN(PC) <- (PC) + relative offset
INC @Rr((Rr)) <- ((Rr)) + 1
INC A(A) <- (A) + 1
INC DPTR(DPTR) <- (DPTR) + 1
INC Rr((R) <- (Rr) + 1
INC data addr
(data address) <(data address) + 1
JB bit addr,code addr
(PC) <- (PC) + 3
IF (bit address) = 1
THEN(PC) <- (PC) + relative offset
JBC bit addr,code addr
(PC) <- (PC) + 3
IF (bit address) = 1
THEN(bit address) <- 0(PC) <- (PC) + relative offset
JC code addr
(PC) (PC) + 2
IF(C) = 1
THEN(PC) <- (PC) + relative offset
JMP @A + DPTR
(PC) <- (A) + (DPTR)JNB bit addr,code addr
(PC) <- (PC) + 3
IF (bit address) = 0
THEN(PC) <- (PC) + relative offset

CyclesBinaryCodeFlags
P OV AC C

Function

100010101
mmmmmmmm

4

10000100211011rrroooooooo211010101
mmmmmmmm
oooooooo

Decrement data address, if
zero (0), then jump to code
address.

1

000001 1r

100000100

Increment contents ofindirect address.
Increment A.

110100011

100001rrr

Increment 16-bit data
pointer.
Increment register.

200000101
mmmmmmmm

Increment contents of data
address.

200100000
bbbbbbbb
ooooooooIf bit is one, n jump to code
address.

200010000
bbbbbbbb
ooooooooIf bit is one, n clear bit andjump to code address.

2

01000000oooooooo

If C is one, then jump tocode address.

201110011

200110000
bbbbbbbb
oooooooo

Add A to data pointer andjump to that code address.
If bit is zero, n jump to code
address.

- 203 -

Decrement contents of data
address.
P OV

P

C

Divide A by B (multiplication
register).
Decrement register, if not
zero (0), then jump to code
address.

intelTable B-1. Instruction Set Summary (Cont'd.)Mnemonic
Operation
JNC code addr
(PC) + (PC) + 2
IF (C) = 0
THEN(PC) <- (PC) + relative offset
JNZ code addr
(PC) <- (PC) + 2
IF (A) <> 0
THEN(PC) <- (PC) + relative offset
JZ code addr
(PC) <- (PC) + 2
IF (A) = 0
THEN(PC) <- (PC) + relative offset
LCALL code addr
(PC) <- (PC) + 3(SP) <- (SP) + 1((SP)) <- ((PC)) low(SP) <- (SP) + 1((SP)) <- (PC) high(PC) <- code address
LJMP code addr
(PC) <- code address

CyclesBinaryCodeFlags
P OV AC C

Function

201010000oooooooo

If C is zero (0), n jump tocode address.

201110000oooooooo

If A is not zero (0), n jump tocode address.

201100000oooooooo

If A is zero (0), then jump tocode address.

200000010
1 1 1 1 1 1 1 1†
1 1 1 1 1 1 1 1†

Push PC on stack andreplace entire PC value withcode address.

200000010
1 1 1 1 1 1 1 1†
1 1 1 1 1 1 1 1†
0111011r
dddddddd
1111011r

Jump to code address.

MOV @Rr,#data((Rr)) <- data
MOV @Rr,A((Rr)) <- (A)MOV @Rr,data addr((Rr)) <- (data address)
MOV A,#data(A) <- data
MOV A,@Rr(A) <- ((Rr))
MOV A,Rr(A) <- (Rr)
MOV A,data addr(A) <- (data address)
MOV C,bit addr(C) <- (bitaddress)
MOV DPTR,#data(DPTR) <- data

1

MOV Rr,#data(Rr) <- data
MOV Rr,A(Rr) <- (A)11Move immediate data to
indirect address.
Move A to indirect address.

P

1

1010011r
mmmmmmmm
01110100
dddddddd
1110011r

1

11101rrr

P

111100101
mmmmmmmm
10100010
bbbbbbbb
10010000d d d d d d d d†
d d d d d d d d†
01111rrr
dddddddd
11111rrr

P

2
1

1
2

1

Move contents of data
address to indirect address.
Move immediate data to A.

P

Move contents of indirect
address to A.
Move register to A.

CMove contents of data
address to A.
Move bit to C.
Move two bytes ofimmediate data pointer.
Move immediate data toregister.
Move A to register.

† The high order byte of the 16-bit operand is in the first byte following the opcode. The low order byte is in thesecond byte following the opcode.

- 204 -

intelTable B-1. Instruction Set Summary (Cont'd.)Mnemonic
Operation
MOV Rr,data addr
(Rr) <- (data address)
MOV bit addr,C(bit address) <- (C)MOV data addr,#data(data address) <- data

CyclesBinaryCodeFlags
P OV AC C

210101rrr
mmmmmmmm
10010010
bbbbbbbb
01110101
mmmmmmmm
dddddddd
1000011r
mmmmmmmm
11110101
mmmmmmmm
10001rrr
mmmmmmmm
10000101m m m m m m m m•
m m m m m m m m•
10010011

P
P

2
2
2

Move contents of data
address to register.
Move C to bit.
Move immediate data to data
address.

MOV data addr,@Rr(data address) <- ((Rr))
MOV data addr,A(data address) <- (A)MOV data addr,Rr(data address) <- (Rr)
MOV data addr1,data addr2(data address1) <(data address2)
MOVC A,@A + DPTR
(PC) <- (PC) + 1(A) <- ((A) + (DPTR))
MOVC A,@A + PC
(A) <- ((A) + (PC))

2210000011

MOVX @DPTR,A
DPTR)) <- (A)211110000MOVX @Rr,A((Rr)) <- (A)21111001r

MOVX A,@DPTR(A) <- ((DPTR))

211100000PMOVX A,@Rr(A) <- ((Rr))

21110001r

P

MUL AB(AB) <- (A) * (B)NOPORL A,#data(A) <- (A)OR data
ORL A,@Rr(A) <- (A) OR ((Rr))
ORL A,Rr(A) <- (A) OR (Rr)
ORL A,data addr(A) <- (A) OR (data address)
ORL C,bit addr(C) <- (C) OR (bit address)

4

10100100

P OV

1
1
1

00000000
01000100
dddddddd
0100011r

1

01001rrr

P

1

01000101
mmmmmmmm
01110010
bbbbbbbb

P

1
2
2

2

Function

Move contents of indirect
address to data address.
Move A to data address.

C

P
P

C

Move register to data
address.
Move contents of second
data address to first data
address.
Add A to DPTR and movecontents of that code
address with A.
Add A to PC and movecontents of that code
address with A.
Move A to external data
location addressed by
DPTR.
Move A to external data
location addressed byregister.
Move contents of external
data location addressed by
DPTR to A
Move contents of external
data location addressed byregister to A.
Multiply A by B
(multiplication register).
Do nothing.
Logical OR immediate data
to A.
Logical OR contents ofindirect address to A.
Logical OR register to A.
Logical OR contents of data
address to A.
Logical OR bit to C.

• The source data address (second data address) is encoded in the first byte following the opcode. The
destination data address is encoded in the second byte following the opcode.

- 205 -

intelTable B-1. Instruction Set Summary (Cont'd.)Mnemonic
Operation
ORL C,/bit addr(C) <- (C) OR NOT (bit address)
ORL data addr,#data(data address) <(data address) OR data
ORL data addr,A(data address) <(data address) OR A
POP data addr
(data address) <- ((SP))(SP) <- (SP) – 1
PUSH data addr
(SP) <- (SP) + 1((SP)) <- (data address)
RET(PC)high <- ((SP))(SP) <- (SP) - 1(PC)low <- ((SP))(SP) <- (SP) - 1
RETI(PC)high <- ((SP))(SP) <- (SP) - 1(PC)low <- ((SP))(SP) <- (SP)
RL A
RLC A

Cycles2
2
1

BinaryCode

10100000
bbbbbbbb
01000011
mmmmmmmm
dddddddd
01000010
mmmmmmmm

Flags
P OV AC CCFunction
Logical OR complement of
bit to C.
Logical OR immediate data
to data address.
Logical OR A to data
address.

211010000
mmmmmmmm

Place top of stack at data
address and decrement SP.

2

11000000
mmmmmmmm

200100010

Increment SP and placecontents of data address at
top of stack.
Return from subroutine call.

200110010

Return from interrupt routine.

1
100100011
00110011

P

C

RR A
RRC A

1
1

00000011
00010011

P

C

SETB C(C) <- 1
SET8 bit addr
(bit address) <- 1
SJMP code addr
(PC) <- (PC) + 2(PC) <- (PC) + relative offset
SUBB A,#data(A) <- (A) - (C) - data
SUBB A,@Rr(A) <- (A) - (C) - ((Rr))
SUBB A,Rr(A) <- (A) - (C) - (Rr)
SUBB A, data addr(A) <- (A) - (C) - (data address)
SWAP A1

11010011

1

11010010
bbbbbbbb
10000000oooooooo2

C

Set bit to one (1).
Jump to code address.
P OV AC C

110010100
dddddddd
100l0l lr

1

10011rrr

P OV AC C

110010101
mmmmmmmm
11000100P OV AC C

1

1- 206 -

Rotate A left one position.
Rotate A through C left one
position.
Rotate A right one position.
Rotate A through C right one
position.
Set C to one (1).

P OV AC C

Subtract immediate datafrom A.
Subtract contents of indirect
address from A.
Subtract register from A.
Subtract contents of data
address from A
Exchange low order nibble
with high order nibble in A

intelTable B-1. Instruction Set Summary (Cont'd.)Mnemonic
Operation

CyclesBinaryCode

XCH A,@Rrtemp <- ((Rr))((Rr)) <- (A)(A) <- temp
XCH A,Rr
temp <- (Rr)
(Rr) <- (A)(A) <- temp
XCH A,data addr
temp <- (data address)(data address) <- (A)(A) <- temp
XCHD A,@Rr
temp <- ((Rr)) 0-3((Rr)) 0-3 <- (A) 0-3(A) 0-3 <- temp
XRL A,#data(A) <- (A) XOR dataXRL A,@Rr(A) <- (A) XOR ((Rr))

1

1100011r

Flags
P OV AC C
P

111001rrr

PMove A to register and viceversa.

1

11000101
mmmmmmmm

P

Move A to data address andvice versa.

10110011r

P

Move low order of A to loworder nibble of indirect
address and vice versa.

1

P

1

01100100
dddddddd
0110011r

XRL A,Rr(A) <- (A) XOR (Rr)XRL A,data addr(A) <- (A) XOR (data address)

101101rrr

P

101100101
mmmmmmmmP

XRL data addr,#data(data address) <(data address) XOR data
XRL data addr,A(data address) <(data addr as) XOR A

201100011
mmmmmmmm
dddddddd
01100010
mmmmmmmm

Logical exclusive OR
immediate data to A.
Logical exclusive OR
contents of indirect address
to A.
Logical exclusive OR register
to A.
Logical exclusive OR
contents of data address toA.
Logical exclusive OR
immediate data to data
address.
Logical exclusive OR A todata address.

1- 207 -

P

Function
Move A to indirect address
and vice versa.

intelTable B-2. Instruction Opcodes in Hexadecimal
Hex
Code

Numberof Bytes

00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
10
11
12
18
14
15
16
17
18
19
1A
1B
1C
1D
1E
1F
20
21
22
23
24
25
26
27
28
29
2A
2B
2C
2D
2E
2F
80
31
32
83
34
35
36
37
38
89
3A
3B

1
2
3
1
1
2
1
1
1
1
1
1
1
1
1
1
3
2
3
1
1
2
1
1
1
1
1
1
1
1
1
1
3
2
1
1
2
2
1
1
1
1
1
1
1
1
1
1
3
2
1
1
2
2
1
1
1
1
1
1

Mnemonic
NOP
AJMP
LJMP
RR
INC
INC
INC
INC
INC
INC
INC
INC
INC
INC
INC
INC
JBC
ACALL
LCALL
RRC
DEC
DEC
DEC
DEC
DEC
DEC
DEC
DEC
DEC
DEC
DEC
DEC
JB
AJMP
RET
RL
ADD
ADD
ADD
ADD
ADD
ADD
ADD
ADD
ADD
ADD
ADD
ADD
JNB
ACALL
RETI
RLC
ADDC
ADDC
ADDC
ADDC
ADDC
ADDC
ADDC
ADDC

- 208 -

Operands
code addr
code addr
AA
data addr
@R0
@R1
R0
R1
R2
R3
R4
R5
R6
R7
bit addr,code addr
code addr
code addr
AA
data addr
@R0
@R1
R0
R1
R2
R3
R4
R5
R6
R7
bit addr,code addr
code addr
AA,#dataA,data addr
A,@R0
A,@R1
A,R0
A,R1
A,R2
A,R3
A,R4
A,R5
A,R6
A,R7
bit addr,code addr
code addr
AA,#dataA,data addr
A,@R0
A,@R1
A,R0
A,R1
A,R2
A,R3

intelTable B-2. Instruction Opcodes in Hexadecimal
Hex
Code

Numberof Bytes

3C
3D
3E
3F
40
41
42
43
44
45
46
47
48
49
4A
4B
4C
4D
4E
4F
50
51
52
53
54
55
56
57
58
59
5A
5B
5C
5D
5E
5F
60
61
62
63
64
65
66
67
68
69
6A
6B
6C
6D
6E
6F
70
71
72
73
74
75
76
77

1
1
1
1
2
2
2
3
2
2
1
1
1
1
1
1
1
1
1
1
2
2
2
3
2
2
1
1
1
1
1
1
1
1
1
1
2
2
2
3
2
2
1
1
1
1
1
1
1
1
1
1
2
2
2
1
2
3
2
2

Mnemonic

Operands

ADDC
ADDC
ADDC
ADDC
JC
AJMP
ORL
ORL
ORL
ORL
ORL
ORL
ORL
ORL
ORL
ORL
ORL
ORL
ORL
ORL
JNC
ACALL
ANL
ANL
ANL
ANL
ANL
ANL
ANL
ANL
ANL
ANL
ANL
ANL
ANL
ANL
JZ
AJMP
XRL
XRL
XRL
XRL
XRL
XRL
XRL
XRL
XRL
XRL
XRL
XRL
XRL
XRL
JNZ
ACALL
ORL
JMP
MOV
MOV
MOV
MOV

A,R4
A,R5
A,R7
A,R7
code addr
code addr
data addr,Adata addr,#data
A,#dataA,data addr
A,@R0
A,@R1
A,R0
A,R1
A,R2
A,R3
A,R4
A,R5
A,R6
A,R7
code addr
code addr
data addr,Adata addr,#data
A,#dataA,data addr
A,@R0
A,@R1
A,R0
A,R1
A,R2
A,R3
A,R4
A,R5
A,R6
A,R7
code addr
code addr
data addr,Adata addr,#data
A,#dataA,data addr
A,@R0
A,@R1
A,R0
A,R1
A,R2
A,R3
A,R4
A,R5
A,R6
A,R7
code addr
code addr
C,bit addr
@A + DPTR
A,#datadata addr.#data@R0,#data@R1,#data- 209 -

intelTable B-2. Instruction Opcodes in Hexadecimal
Hex
Code

Numberof Bytes

78
79
7A
7B
7C
7D
7E
7F
80
81
82
83
84
85
86
87
88
89
8A
8B
8C
8D
8E
8F
90
91
92
93
94
95
96
97
98
99
9A
9B
9C
9D
9E
9F
A0
A1
A2
A3
A4
A5
A6
A7
A8
A9
AA
AB
AC
AD
AE
AF
B0
B1
B2
B3

2
2
2
2
2
2
2
2
2
2
2
1
1
3
2
2
2
2
2
2
2
2
2
2
3
2
2
1
2
2
1
1
1
1
1
1
1
1
1
1
2
2
2
1
1
2
2
2
2
2
2
2
2
2
2
2
2
2
1

Mnemonic

Operands

MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
SJMP
AJMP
ANL
MOVC
DIV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
ACALL
MOV
MOVC
SUBB
SUBB
SUBB
SUBB
SUBB
SUBB
SUBB
SUBB
SUBB
SUBB
SUBB
SUBB
ORL
AJMP
MOV
INC
MUL
reserved
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
ANL
ACALL
CPL
CPL

R0,#data
R1,#data
R2,#data
R3,#data
R4,#data
R5,#data
R6,#data
R7,#data
code addr
code addr
C,bit addr
A,@A + PC
ABdata addr, data addr
data addr,@R0
data addr,@R1
data addr,R0
data addr,R1
data addr,R2
data addr,R3
data addr,R4
data addr,R5
data addr,R6
data addr,R7
DPTR,#data
code addr
bit addr,C
A,@A + DPTR
A,#dataA,data addr
A,@R0
A,@R1
A,R0
A,R1
A,R2
A,R3
A,R4
A,R5
A,R6
A,R7
C,lbit addr
code addr
C,bit addr
DPTR
AB

- 210 -

@R0,data addr
@R1,data addr
R0,data addr
R1,data addr
R2,data addr
R3,data addr
R4,data addr
R5,data addr
R6,data addr
R7,data addr
C,lbit addr
code addr
bit addrCintelTable B-2. Instruction Opcodes in Hexadecimal
Hex
Code

Numberof Bytes

B4
B5
B6
B7
B8
B9
BA
BB
BC
BD
BE
BF
C0
C1
C2
C3
C4
C5
C6
C7
C8
C9
CA
CB
CC
CD
CE
CF
D0
D1
D2
D3
D4
D5
D6
D7
D8
D9
DA
DB
DC
DD
DE
DF
E0
E1
E2
E3
E4
E5
E6
E7
E8
E9
EA
EB
EC
ED
EE
EF

3
3
3
3
3
3
3
3
3
3
3
3
2
2
2
1
1
2
1
1
1
1
1
1
1
1
1
1
2
2
2
1
1
3
1
1
2
2
2
2
2
2
2
2
1
2
1
1
1
2
1
1
1
1
1
1
1
1
1
1

Mnemonic

Operands

CJNE
CJNE
CJNE
CJNE
CJNE
CJNE
CJNE
CJNE
CJNE
CJNE
CJNE
CJNE
PUSH
AJMP
CLR
CLR
SWAP
XCH
XCH
XCH
XCH
XCH
XCH
XCH
XCH
XCH
XCH
XCH
POP
ACALL
SETB
SETB
DA
DJNZ
XCHD
XCHD
DJNZ
DJNZ
DJNZ
DJNZ
DJNZ
DJNZ
DJNZ
DJNZ
MOVX
AJMP
MOVX
MOVX
CLR
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV

A,#data,code addr
A,data addr,code addr
@R0,#data,code addr
@R1,#data,code addr
RO,#data,code addr
R1,#data,code addr
R2,#data,code addr
R3,#data,code addr
R4,#data,code addr
R5,#data,code addr
R6,#data,code addr
R7,#data,code addr
data addr
code addr
bitaddrCA
A,data addr
A,@R0
A,@R1
A,R0
A,R1
A,R2
A,R3
A,R4
A,R5
A,R6
A,R7
data addr
code addr
bit addrCA
data addr,code addr
A,@R0
A,@R1
R0,code addr
R1,code addr
R2,code addr
R3,code addr
R4,code addr
R5,code addr
R6,code addr
R7,code addr
A,@DPTRcode addr
A,@R0
A,@R1
A
A,data addr
A,@R0
A,@R1
A,R0
A,R1
A, R2
A,R3
A,R4
A,R5
A,R6
A,R7

- 211 -

intelTable B-2. Instruction Opcodes in Hexadecimal
Hex
Code

Numberof Bytes

F0
F1
F2
F3
F4
F5
F6
F7
F8
F9
FA
FB
FC
FD
FE
FF

1
2
1
1
1
2
1
1
1
1
1
1
1
1
1
1

Mnemonic

Operands

MOVX
ACALL
MOVX
MOVX
CPL
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV

@DPTR,A
code addr
@R0,A
@R1,A
A
data addr,A
@R0,A
@R1,A
R0,A
R1,A
R2,A
R3,A
R4,A
R5,A
R6,A
R7,A

- 212 -

intelINDEXAABS, 5, 76, 113, 158, 181, 183
Accumulator, 27,1 06,1 23, 1 46,1 47,1 93
ADD, 5, 8, 74, 80, 118, 119, 181, 183
Argument Stack, 8, 31, 60, 61, 98,106-108,
112, 113, 118, 122, 123, 163, 165, 167,
169
Arithmetic Overflow, 97,118
Arithmetic Underflow, 97,1 1 8
Array Size, 99
ASC, 83-85, 103, 158, 183
Assembly Language Linkage, 29, 67, 99,104
ATN, 79, 114, 158, 181, 183
Auto_Baud, 2

B
BAUD Rate, 16, 24, 27, 28, 57, 89, 93, 94,
131, 145-147, 158, 164, 169, 174, 175,
178, 183, 189, 194

C
CALL, 12, 29, 104, 107, 108, 130, 132, 158,
178, 183
Carry Bit, 27,146
CBY, 86, 114, 158, 182, 183
CHR, 83, 85, 158, 183
CLEAR, 6, 30, 32, 35, 66158, 178
CLEARI, 31, 32, 53, 178, 183
CLEARS, 31, 178, 183
CLOCK0, 32, 53, 158, 178, 183
CLOCK1, 30-32, 52, 91, 92, 131, 158, 163,
167, 178, 183
Command Mode, 4, 12, 13, 24, 106, 109,
111, 167, 191
Command/Statement Extension, 10, 11, 122,
153-159
Constants, 5, 6, 122
CONT, 14, 38, 65, 158, 166, 176, 183
Control Stack, 8,11, 31, 42, 98,169
COS, 5, 77-79, 113, 158, 181, 183
CR, 4, 55, 158

D
DATA, 33, 34, 97, 158, 178, 183
Data Format, 5
DBY, 86, 114, 158, 182, 183

DIM, 6, 35, 99, 158, 167, 178, 183
DIMUSE, 185, 186
Direct Memory Access (DMA), 101, 129,
163,167
DIVIDE, 5, 8, 80, 118, 119, 181, 183
DO_UNTIL, 8, 31, 36, 37, 98, 158, 178, 183
DO_WHILE, 8, 31, 37, 98, 158, 178, 183
DPTR, 104, 106, 123, 147, 153, 155, 159,
190, 193

E
END, 38, 158, 178, 183, 188
EPROM Programming, 10, 20, 23, 72,109,
110, 132, 134-136, 141, 142, 162
EQUAL, 7, 80, 81, 120, 158, 183
Error Messages, 96-99
EXCLUSIVE OR, 120, 158
EXP, 78, 158, 181, 183
EXPONENT, 74, 80, 119, 181, 183
Expression, 6F
Floating Point Numbers, 55, 71,107, 108,
112, 118, 123, 184, 186
FOR_TO{STEP}_NEXT, 8, 11, 12, 31, 39,
40, 42, 98, 158, 178, 183
FPROG, 25, 94, 158, 177, 183
FPROG1, 25, 177, 183
FPROG2, 25, 177, 183
FPROG3, 26, 177, 183
FPROG4, 26, 177, 183
FPROG5, 27, 177, 183
FPROG6, 27, 177, 183
FREE, 7, 21, 95, 115, 158, 183

G
GET, 67, 86, 87, 100, 115, 122, 123, 158,
162,165,166, 169, 182, 183
GOSUB, 8, 11, 12, 41, 43, 44, 51, 52, 61,
98, 158, 179, 183
GOTO, 12, 13, 43, 44, 46, 158, 179, 183
GREATER THAN, 7, 80, 81, 121, 158, 183
GREATER THAN OR EQUAL, 7, 80, 81,
120, 158, 183- 213 -

intelIO

IDLE, 10, 69, 158, 167, 180, 183
IE, 31, 51, 88, 101, 103, 116, 129, 130, 158,
182, 183, 193, 198
IF_THEN_ELSE, 9, 45, 46, 97, 158, 179,
183
Illegal Direct, 97
INPUT, 47, 48, 82, 158, 179, 183
Input Buffer, 11 , 111
INT, 76, 113, 158, 181, 183
Integers, 5, 75, 76
INTELligent Algorithm, 25, 26, 72, 109, 110,
136, 141 , 163, 165, 167, 169,177
Internal Stack, 8, 99
Interrupts, 129, 130, 159, 160, 162, 163,
166, 167
IP, 88, 116, 158, 182, 183, 193, 198

ON GOSUB, 43, 44, 158, 179
ON GOTO, 43, 44158, 179
ONERR, 30, 50, 158, 162, 166, 169, 179,
183
ONEX1, 30, 31, 51, 53, 64, 69, 129, 131,
158, 162, 166, 169, 179, 183
ONTIME, 30, 31, 51, 52, 53, 64, 69, 129,
158, 162, 166, 168, 179, 183
ON_GOSUB, 183
ON_GOTO, 183
Opbyte, 11, 106-109, 111-124
Operators, 122

L
LD@, 10, 71, 158, 180, 183
LEN, 7, 95, 115, 158, 183
LESS THAN, 7, 80, 81, 121, 158, 183
LESS THAN OR EQUAL, 7, 80, 81, 120,
158, 183
LET, 49, 66, 82, 86, 91, 95, 158, 179, 183
Line Editor, 8
LIST, 4, 9, 10, 15-17, 21, 100, 158, 176,
183
LIST#, 16, 28, 94, 131, 176, 183
LlST@, 11, 17, 59, 159, 166, 167, 176, 183
LOG, 78, 114, 158, 181, 183
LOGICAL AND, 76, 80, 81, 120, 158, 181,
183
LOGICAL EXCLUSIVE OR, 75, 80, 81, 181,
183
LOGICAL OR, 75, 80, 81, 120, 158, 181,
183

M
MTOP, 2, 7, 21, 26, 27, 95,115, 145,152,
158, 176, 183, 185, 187, 189, 191
MULTIPLY, 8, 74, 80, 118, 119, 181, 183

N

P
PCON, 89, 117, 158, 182, 183, 193, 194
PGM, 10, 72, 73, 104, 158, 180, 183
PH0., 58, 158, 179, 183
PH0.#, 58, 179, 183
PH0.@, 59, 180, 183
PH1., 58, 157, 158, 179, 183
PH1.#, 58, 179, 183
PH1.@, 59, 180, 183
PI, 77, 79, 115, 158, 182, 183
POP, 60, 61, 98, 106, 108, 118, 130, 158,
180,183
PORT1, 88, 117, 158, 182, 183
PRINT, 4, 10, 11, 54, 55, 57-59, 63, 158,
179, 183
PRINT#, 28, 57, 94, 131, 179, 183
PRlNT@, 11, 59, 159, 166, 167, 180, 183
PROG, 23, 25, 94, 131, 134, 158, 176, 183,
189
PROG1, 10, 24, 25, 145, 176, 183, 189
PROG2, 10, 24, 25, 145, 176, 183, 189
PROG3, 10, 26, 145, 176, 183, 189
PROG4, 10, 26, 145, 176, 183
PROG5, 10, 27, 145, 177, 183
PROG6, 10, 27, 146, 177, 183
Programming Error, 98
PSW, 130, 160, 193, 194
PUSH, 60, 61, 98, 107, 130, 158, 180, 183
PWM, 62, 90, 94, 131, 158, 170-173, 180,
183

NEGATION, 80, 158
NEW, 18, 35, 66, 158, 176, 183
NOT, 76, 81, 113, 158, 181, 183
NOT EQUAL, 7, 80, 81, 121, 158, 183
NULL, 19, 95, 158, 166, 176, 183- 214 -

intelR

T

RAM, 21, 158, 176, 183
RAM Only Mode, 132
RAM/EPROM Mode, 133,134
RCAP2, 89, 117, 158, 182, 183
READ, 33, 34, 97, 158, 178, 183
REM, 12, 63, 158, 180, 183
Reset, 2, 3, 10, 24, 26, 27, 29,102, 122,
131, 145-152, 159, 176, 177, 191
RESTORE, 33, 158, 178, 183
RETI, 51, 53, 64, 158, 163, 180, 183
RETURN, 41, 42, 64, 98, 123, 158, 179, 183
RND, 77, 115, 158, 181, 183
ROM, 21, 158, 176, 183
RROM, 10, 70, 158, 180, 183
RUN, 13, 21, 24, 35, 43, 100, 158, 176, 183
Run Mode, 4, 13, 123
Run Trap, 10, 27, 102,169

T2CON, 2, 3, 89, 116, 131, 147, 158, 182,
183, 193
TAB, 4, 54, 158
TAN, 77, 79, 113, 158, 181, 183
TCON, 3, 90, 116, 131, 147, 158, 182, 183,
193, 197
Text Pointer, 122, 123,162, 164, 166
TIME, 7, 32, 52, 53, 91, 92, 116, 158, 182,
183
TIMER0, 90, 92, 116, 158, 182, 183
TIMER1, 89, 90, 92, 94, 116, 158, 182, 183
TIMER2, 89, 92, 94, 116, 158174, 175, 182
183, 196
TMOD, 3, 90, 117, 131, 147, 158, 182, 183
193, 195

S
SCON, 147, 190, 193, 197
Serial Port, 131 ,136, 159, 160, 166
SGN, 76, 113, 158, 181, 183
Sign-On, 2
SIN, 5, 77-79, 114, 158,181, 183
SMOD, 194
SPC, 4, 54, 158
SQR, 77, 114, 158, 181, 183
ST@, 10, 71, 158, 180, 183
Stack Pointer, 8, 31, 105, 147, 152, 193
STOP, 14, 65, 98, 158, 163, 176, 180, 183
STRING, 30, 49, 66, 82, 83, 99, 158, 164,
168, 180, 183, 185
SUBTRACT, 5, 8, 74, 80, 118-120, 181, 183

U
UI, 67, 158, 180, 183
UNTIL, 178
UO, 68, 158, 180, 183
USING, 4, 55, 56, 112, 158

V
Variables, 6, 11, 122, 185
VARTOP, 185, 187
VARUSE, 185-187

X
X-OFF, 10
X-ON, 10
XBY, 87, 114, 158, 182, 183
XFER, 21, 22, 158, 176, 183
XTAL, 2, 3, 7, 28, 32, 62, 89, 91, 93, 115,
136, 152, 158, 165, 169, 174, 175, 183- 215 -