:II CJD. mD LLI- MOS Microcomputer Division 464 Ellis Street, Mountain View, California 94042 :£ 1977 Fairchild Camera and Instrument Corporation/464 Ellis Street, Mountain View, California 94042/(415)962-5011/TWX 910-379-6435 TABLE OF CONTENTS SECTION 1.0 INTRODUCTION .............................................. . 1.1 ASSUMED READER BACKGROUND ....................... . 1.2 SUPPORTING DOCUMENTATION. . . . .. . . . .......... . PAGE 1-1 1-1 1-1 2.0 THE F8 MICROPROCESSOR SYSTEM ... ...... '" . . . . . . . . . .. . .... ... ..... ......... .. . .. .. . ..... . . . 2-1 2.1 WHAT IS A MICROPROCESSOR . . .. . . . .. . . . . . . . . . ... . . ... . . . . ........ ... ... . . .. .. . . .. ... 2-1 2.2 SOME BASIC CONCEPTS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-1 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 INSTRUCTIONS, PROGRAMS, DATA AND MEMORY .............................. . INTERRUPTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .......... . PROGRAMMABLE CLOCKS. . . . . . . . . . . . . . . . . . . . . . .............. . DIRECT MEMORY ACCESS .................................... . A COMPLETE MICROPROCESSOR SYSTEM ................. . 2-1 2-2 2-2 2-2 2-3 2.3 THE F8 SYSTEM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4 2.3.1 CHIP AND I/O PORT SELECTION. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4 2.4 THE 3850 CPU. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ............................. . 2-4 2.4.1 TIMING. . . . . . . .............................. . . ..................... 2-4 2.4.2 CPU REGISTERS .................................... . .................. 2-6 2.4.3 STATUS. . . . . . .. . . ................................ . 2.4.4 3850 INPUT/OUTPUT ............................... . 2.5 THE 3851 PSU....... . ......................... . 2.5.1 3851 TIMING ........................ _. . ............. . 2.5.2 3851 REGISTERS.. . ............................................. . 2.5.3 3851 INPUT/OUTPUT ............................. . 2.5.4 3851 LOCAL TIMER AND INTERRUPT. . . . . . . . . . . ........................ . 2-6 2-7 2-7 2-7 2-8 2-9 2-9 2.6 THE 3852 DYNAMIC MEMORY INTERFACE.. . . . ............ . 2.6.1 3852 TIMING. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......... . 2.6.2 3852 REGISTERS ....................................... . 2.6.3 3852 DIRECT MEMORY ACCESS AND MEMORY REFRESH 2-9 .. ...... 2-9 ........... 2-9 2-10 2.7 2.8 THE 3853 STATIC MEMORY INTERFACE ............................ . THE 3854 DIRECT MEMORY ACCESS ................................................ . 2-10 2-11 2.8.1 3854 REGISTERS. . . . . . . . . . . . . . ................... . ..... 2-11 2.8.2 DMA CONTROL CODES ......................... . 3.0 F8 PROGRAMS ................ . 3.1 FLOWCHARTING. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ........... . 3.2 ASSIGNING MEMORY. . . . . . . . . . . . . . . . . . . . . . . ........ . 3.3 SOURCE AND OBJECT PROGRAMS. . . .. . .. . . . . . . . . . . . . . .. . ................... . 4.0 ASSEMBLY LANGUAGE SyNTAX.......................................................... . 4.1 INSTRUCTION TYPES ........................................ . 4.1.1 COMMENTS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .............. . 4.1.2 EXECUTABLE INSTRUCTIONS.. .. .. .. .... .. .. . .. .. .. .. .. . .. ...... .. 4.1.3 ASSEMBLER DIRECTIVES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .......... . 4.2 INSTRUCTION FIELDS ................................................................... . 4.2.1 LABEL FIELD .................................. . . . . . . . . . . . . . .............. . 4.2.2 MNEMONIC FIELD .............................................................. . 4.2.3 OPERAND FIELD. . . . . . . . . . .. . . . . . . . . . . . . . . .. . . . . ..................... . 4.2.4 COMMENT FIELD. . ................................ . 4.2.5 ALIGNING FIELDS .................................................. . 4.3 LANGUAGE COMPONENTS. 4.3.1 VALID CHARACTERS .. . 4.3.2 CONSTANTS ........... . 4.3.3 SYMBOLS ................. . 4.3.4 EXPRESSIONS ................ . 5.0 ASSEMBLER DIRECTIVES . ...................................... . 5.1 BASE - SELECT LISTING NUMERIC BASE ............................... . 5.2 DC - DEFINE CONSTANT ............................................... . 2-12 3-1 3-1 3-1 3-2 4-1 4-1 4-1 4-1 4-1 4-1 4-1 4-2 4-2 4-2 4-3 4-4 4-4 4-4 4-5 4-5 5-1 5-1 5-1 SECTION 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5.10 5.11 TABLE OF CONTENTS (Cont'd). EJECT - EJECT CURRENT LISTING PAGE ......................... . END - END OF ASSEMBLY ....................................... . EQU - EQUATE A SYMBOL TO A NUMERIC VALUE .. 5.5.1 A COMPARISON OF THE EQU AND DC DIRECTIVES ....................... . MAXCPU - SPECIFY MAXIMUM CPU TIME ......................... . ORG - ORIGIN A PROGRAM ....................................... . SYMBOL - ASSEMBLER PROVIDE A SYMBOL TABLE .............. . TITLE - PRINT A TITLE AT THE HEAD OF THE ASSEMBLER LISTING ......... . XREF - ASSEMBLER PROVIDE A SYMBOL CROSS REFERENCE LISTING ........... . WHEN TO USE ASSEMBLER DIRECTIVES .......................... . 6.0 THE INSTRUCTION SET ... .............................. . 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 6.10 6.11 6.12 6.13 6.14 6.15 6.16 6.17 6.18 6.19 6.20 6.21 6.22 6.23 6.24 6.25 6.26 6.27 6.28 6.29 6.30 6.31 6.32 6.33 6.34 ADC - ADD ACCUMULATOR TO DATA COUNTER ..... AI - ADD IMMEDIATE TO ACCUMULATOR. AM - ADD (BINARY) MEMORY TO ACCUMULATOR .... AMD - DECIMAL ADD, MEMORY TO ACCUMULATOR AS - BINARY ADDITION, SCRATCHPAD MEMORY TO ACCUMULATOR .. ASD - DECIMAL ADD, SCRATCHPAD TO ACCUMULATOR ......................... . BRANCH INSTRUCTIONS ....................... . 6.7.1 BF - BRANCH PM FALSE........ . ........... . 6.7.2 BT - BRANCH ON TRUE. . . .......... . CI - COMPARE IMMEDIATE .................... . CLR - CLEAR ACCUMULATOR. ........... . ......... . CM - COMPARE MEMORY TO ACCUMULATOR ..... . COM - COMPLEMENT .... DCI - LOAD DC IMMEDIATE. 01 - DISABLE INTERRUPT. OS - DECREMENT SCRATCHPAD CONTENT .. EI - ENABLE INTERRUPT ............... . IN - INPUT LONG ADDRESS ...... . INC - INCREMENT ACCUMULATOR. INS - INPUT SHORT ADDRESS ....... . JMP - BRANCH IMMEDIATE .. . LI - LOAD IMMEDIATE. . ......... . LIS - LOAD IMMEDIATE SHORT ................. . LlSL - LOAD LOWER OCTAL DIGIT OF ISAR. LlSU - LOAD UPPER OCTAL DIGIT OF ISAR .... . LM - LOAD ACCUMULATOR FROM MEMORY ....... . LNK - LINK CARRY TO THE ACCUMULATOR ...................... . LR - LOAD REGISTER ........ . NI - AND IMMEDIATE. . ........... . NM - LOGICAL AND FROM MEMORY. . . . . . . . . . . . . ............ . NOP - NO OP... . ......................... . NS - LOGICAL AND FROM SCRATCHPAD MEMORY .. . 01 - OR IMMEDIATE ........................... . OM - LOGICAL "OR" FROM MEMORY ...... . OUT - OUTPUT LONG ADDRESS ................. . OUTS - OUTPUT SHORT ADDRESS .............. . 6.35 P! - CALL TO SUBROUT!NE !MMEDLl1.TE ....... . 6.36 PK - CALL TO SUBROUTINE DIRECT AND RETURN FROM SUBROUTINE DIRECT .. 6.37 6.38 6.39 6.40 6.41 6.42 6.43 6.44 POP - RETuRN ~HOiVl SUHHOU liN!:: ............. . SL - SHIFT LEFT. . . . ............. . SR - SHIFT RIGHT .......................... . ST - STORE TO MEMORY .............................. . XDC - EXCHANGE DATA COUNTERS .......... . XI - EXCLUSIVE OR IMMEDIATE .............. . XM - EXCLUSIVE OR FROM MEMORY .............. . XS - EXCLUSIVE OR FROM SCRATCHPAD ......... . .............. . PAGE 5-1 ... 5-2 5-2 5-2 . .. 5-2 5-3 5-3 5-3 5-3 5-3 6-1 6-2 6-2 6-3 6-3 6-4 6-4 6-5 6-7 6-7 6-7 6-7 6-7 6-8 6-8 6-8 6-8 6-8 6-8 6-9 6-10 6-10 6-10 6-10 6-10 6-11 6-11 6-11 6-11 6-11 6-12 6-13 6-13 6-13 6-13 6-14 6-14 61 A 6-14 6-15 6-15 6-15 6-15 6-16 6-16 6-16 ... 6-16 TABLE OF CONTENTS (Cont'd) SECTION PAGE 7-1 7-1 7-1 7-1 7-3 7-3 7.0 PROGRAMMING TECHNIQUES. 7.1 MANIPULATING DATA IN THE SCRATCHPAD .. 7.1.1 SIMPLE SCRATCHPAD BUFFER OPERATIONS 7.1.2 INCREMENTING UP, AND DECREMENTING DOWN SCRATCHPAD BUFFERS .. 7.1.3 USING SCRATCHPAD REGISTERS AS COUNTERS ... 71.4 USING SCRATCHPAD REGISTERS FOR SHORT DATA OPERATIONS. 7.2 ROM, RAM AND DATA TABLES .. 7.2.1 READING DATA OUT OF TABLES IN ROM 7.22 ACCESSING DATA TABLES IN RAM .... 7.3 SUBROUTINES ... 7.3.1 THE CONCEPT OF A SUBROUTINE ...... . 7.3.2 SUBROUTINE PROGRAM STEPS ...... . 7.3.3 SIMPLE SUBROUTINE CALLS AND RETURNS .... 7.3.4 NESTED SUBROUTINES 7.35 MULTIPLE SUBROUTINE RETURNS .. 7.3.6 PASSING PARAMETERS 7.4 MACROS.. . .......... . 74.1 DEFINING AND USING MACROS ... . 74.2 MACROS WITH PARAMETERS ... . 74.3 RULES FOR DEFINING AND USING MACROS .. 7.4.4 WHEN MACROS SHOULD BE USED .. 75 JUMP TABLES. 7.5.1 JUMP TABLE USING JUMP INSTRUCTIONS 7.52 JUMP TABLE USING ADDRESS CONSTANTS .. 7.5.3 JUMP TABLE USING DISPLACEMENT TABLES. 76 STATUS, BITS AND BOOLEAN LOGIC. 7.61 MANIPULATING INDIVIDUAL BITS .... 7.6.2 TESTING FOR STATUS. 7.7 POWERING UP AND STARTING PROGRAM EXECUTION ..... 8.0 INPUT/OUTPUT PROGRAMMING . ... 8.1 PROGRAMMED I/O . 8.1.1 POLLING ON STATUS 8.1.2 DATA. STATUS AND CONTROLS ..... . 8.1.3 PARALLEL DATA AND CONTROL PORTS 8.2 INTERRUPT 1j0 8.2.1 THE INTERRUPT SEQUENCE ... 8.2.2 ENABLING AND DISABLING INTERRUPTS 8.23 INTERRUPT PRIORITIES. . . .......... . 8.2.4 PROGRAM RESPONSE TO AN INTERRUPT................. . 8.2.5 MAKING 3851 PSU INTERRUPT ADDRESS PROGRAMMABLE 8.2.6 SIMPLE 1j0 INTERRUPTS ........... . 8.2.7 A SAMPLE PROGRAM .. . 8.3 LOCAL TIMERS (PROGRAMMABLE TIMERS) .... 8.3.1 LOCAL TIMER 1j0 PORTS ..... . 8.3.2 PROGRAMMING LOCAL TIMERS ......... . 8.3.3 A PROGRAMMING EXAMPLE - THE TIME OF DAY .... 8.4 DIRECT MEMORY ACCESS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ............. . 8.4.1 WHEN TO USE DMA . . . . . . . . . . . . . . . . . . . . . . . . . ....................... . 8.4.2 PROGRAMMING DMA ............................................................. . 8.4.3 CATCHING DMA ON THE FLy............................ . .................... . 7-4 7-4 7-4 7-6 7-6 7-7 7-7 7-8 7-12 7-13 7-14 7-14 7-14 7-15 7-15 7-15 7-16 7-16 7-16 7-17 7-17 7-18 7-18 8-1 8-1 8-1 8-2 8-3 8-3 8-3 8-4 8-4 8-5 8-5 8-5 8-6 8-7 8-7 8-8 8-8 8-9 8-9 8-10 8-11 9.0 PROGRAM OPTIMIZATION . .............................. " . . .. .. . . . . . . . . . . . .... . . . . . ... . . . . . . . 9-1 9.1 COUNTING CYCLES AND BYTES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-1 9.2 ELEMENTARY OPTIMIZATION TECHNIQUES ....... , ..................... '" . . . . . . . .. . . . . . . . . . . .. 9-1 9.2.1 SCRATCHPAD AND RAM MEMORy..................................................... 9-1 9.2.2 IMMEDIATE INSTRUCTIONS .................. , ...................... , ...... ... .. . . . . . . . 9-1 TABLE OF CONTENTS (Cont'd) SECTION 9.3 9.2.3 ~?4 9.2.5 SHORT INSTRUCTIONS ................ . USf OF OS INSTRUCT!ON TO DECREMENT AND TEST USE OF THE BR7 INSTRUCTION ................................ . PROGRAMMING FOR SPEED OR MEMORY ECONOMY .......... . 9.3.1 MACROS AND SUBROUTINES .... 9.3.2 TABLE LOOKUPS VERSUS DATA MANIPULATION 10.0 SOME USEFUL PROGRAMS .. ....... . 10.1 GENERATING TEXT ..... . 10.1.1 SIMPLE AND DEDICATED TEXT PROGRAMS .... 10.1.2 UNPACKING DECIMAL DIGITS .. 10.1.3 VARIABLE TEXT .................... . 10.2 MULTIBYTE ADDITION AND SUBTRACTION. 10.2.1 16-BIT, BINARY ADDITION AND SUBTRACTION ..... . 10.2.2 MULTIBYTE BINARY OR DECIMAL ADDITION AND SUBTRACTION 10.3 MULTIPLICATION. 10.4 DiViSiON ...... . APPENDIX A APPENDIX B APPENDIX C APPENDIX D BINARY NUMBER SYSTEM. ASCII CODES ... CONVERSION TABLES/TIMER COUNTS ... INSTRUCTION SUMMARY. FIGURE 2-1 2-2 2-3 2-4 2-5 2-6 2-7 2-8 2-9 2-10 2-11 2-12 3-1 3-2 3-3 4-1 4-2 4-3 4-4 4-5 4-6 4-7 5-1 6-1 6-2 LIST OF ILLUSTRATIONS Multifunction Logic Device. . ......... . Data and Instruction Paths In a Multifunction Logic Device. Program P Being Interrupted to Execute Program R . . . . . . ........ . Logical Components, Data Paths and Control Paths In any Microprocessor System. F8 Microprocessor System Configurations ............... . Logical Functions of the 3850 CPU .. Instruction Timing .. 3850 CPU Programmable Registers Logical Functions of the 3851 PSU .. Logical Functions of the 3852 DMI Device Logical Functions of the 3853 SMI Device ... . Logical Functions of the 3854 DMA Device ....... . Flowchart for a Program to Move Data from One RAM Buffer to Another .. Flowchart for Program to Add Two Multibyte Numbers and Output the Result. Source and Object Programs. . . . . . . . . . . . . . . . . . . . . . . . . . ........ . Four Comment Lines (Shaded; in a Source Program .......... . Label Fields (Shaded) in a Source Program. Mnemonic Field (Vertical Shaded) in a Source Program ........ . Operand Fields (Shaded) in a Source Program .... . Comment Fields (Shaded) in a Source Program .. . Source Program with Unaligned Fields ................... . Symbols in a Source Program ...... . Assembier Directives (Shadedj in a Source Program. Generation of a Displacement Object Program Byte in Response to a Forward Branch. Genel diiull UI d Di:;pidcelTlfli ObJeci Program Byte in Response to a Backward Branch PAGE 9-7 n "7 ;:..'-, 9-7 9-2 9-2 9-3 10-1 10-1 10-1 . .. 10-1 10-1 10-2 10-2 10-3 iO-3 10-5 A-l B-1 C-l D-l PAGE 2-1 2-1 2-2 2-3 2-4 2-5 2-5 2-6 2-8 2-10 2-11 2-12 3-1 3-2 3-3 4-1 4-2 4-2 4-3 4-3 4-3 4-5 5-1 6-5 0-0 FIGURE 7-1 7-2 7-3 8-1 8-2 8-3 8-4 9-1 TABLE 2-1 2-2 4-1 6-1 6-2 6-3 6-4 6-5 6-6 6-7 7-1 7-2 8-1 TABLE OF CONTENTS (Cont'd) Use of H, Q and DCl Registers to Hold Three Buffer Addresses Subroutine, as Compared to a Macro. . . . . . . . . . ........ . Scratchpad Stack .......... . Daisy Chaining and Interrupt Priority Determination ... Two Levels of Interrupt. .. . ............ . Two Devices Servicing a Keyboard to Cassette Application .. How BUFC and BUFD are used to Control DMA Operations Counting Cycles and Bytes ... LIST OF TABLES Summary of Status Bits. . . ......... . Hexadecimal Addresses of Four I/O Ports used as Registers by Four 3854 DMA Registers .. Summary of Restricted Characters Operand Symbols .............. . Operands Referencing Scratchpad Memory, as Specified by Symbol Sreg Branch Conditions Branch Conditions for BF Instruction Branch Conditions for BT Instruction I/O Port Address Assignments .. LR Instruction Operand Definitions ... Scratchpad Memory Utilization. . . . . . . . . . . . . . . . . . ......... . Use of a Memory Stack for Executing Multiple Level Subroutines ............ . Contents of Interrupt Control I/O Ports v PAGE 7-6 7-7 7-1C 8-4 8-4 8-6 8-lC 9-2 PAGE 2-6 2-11 4-4 6-1 6-1 6-6 6-6 6-7 6-9 6-1 ~ 7-1 7-1C 8-4 INTRODUCTION This manual explains how to write programs for the Fairchild F8 microprocessor system, and how these F8 programs cause a microprocessor system to function as a discrete logic replacement. The Fairchild F8 family of logic devices consists of a Central Processing Unit and a number of complementary devices, manufactured using n-channel Isoplanar MOS technology. Components of the F8 family include the following devices: 1) The 3850 Central Processing Unit (CPU) 2) The 3851 Program Storage Unit (PSU) 3) The 3852 Dynamic Memory Interface (DMI) 4) The 3853 Static Memory Interface (SMI) 5) The 3854 Direct Memory Access (DMA) Complete microprocessor based systems may vary in size and complexity from as little as two devices-the 3850 CPU and the 3851 PSU-to large systems incorporating the above five devices, plus any standard static and/or dynamic Random Access Memory (RAM) devices. The following are some general characteristics of this micro- processor device set: • 8-bit data organization • 2 #1s instruction cycle time • Over 70 microprocessor instructions • 64 general purpose registers in the CPU • Binary and decimal arithmetic, and logic functions • Up to 65,536bytes ofROM and RAM, in any combination • No need for special external interface devices • Internal, programmable real time clocks • Internal power on and reset logic • Multi-level interrupt handling • Clock and timing circuits 1-1 1.1 ASSUMED READER BACKGROUND This manual has been written for logic designers with little or no background in programming. The reader is assumed to understand the following: 1) Binary, octal, binary coded decimal and hexadecimal number systems 2) Signed and unsigned binary arithmetic 3) Boolean logic 4) ASCII and EBCDIC character codes For readers without the assumed background, a summary of this basic information is given in Appendix A. 1.2 SUPPORTING DOCUMENTATION The following manuals provide additional information on the F8 microprocessor: 1) F8 Circuit Data Book which provides electrical param- eter data for all Fairchild F8 Microprocessor devices. 2) F8 Timeshare Operating Systems Manual which ex- plains how to assemble and debug F8 Microprocessor programs on NCSS and GE Timeshare Networks. 3) F8 Circuit Reference Manual which describes the interactive timing and signal sequences which occur between devices in the F8 Microprocessor family. 4) F8S and F8SEM Users Manuals which describe how to assemble and debug microprocessor programs on the F8S and F8SEM hardware modules. 5) F8 Formulator Users and Reference Manuals which describe how to use and maintain Fairchild's F8 Formulator developmental hardware. THE Fa MICROPROCESSOR SYSTEM The purpose of a microprocessor system is to replace discrete logic; but in order to understand why a microprocessor system is effective as a logic design tool, it is first necessary to understand what is in a microprocessor system. 2.1 WHAT IS A MICROPROCESSOR? After a product has been fabricated using discrete logic com- ponents, it consists of one or more logic cards; each card may be visualized as generating a variety of signals output at the card edge, based on signals input at the card edge. The logic devices on the card are specifically selected and sequenced to generate the required product. If the same product is implemented using the F8 micropro- cessor, the F8 CPU and its five supporting devices can be made to function in the same way as anyone of many mil- lions of different discrete logic device combinations. In other words, the F8 CPU, optionally in conjunction with the sup- porting devices, has the capacity to duplicate the performance of any discrete logic design, limited only by speed consider- ations. F8 microprocessor systems have a 2 j.Ls instruction cycle time. The functions that will be performed by the F8 microprocessor system are established by a sequence of "instructions", stored in a memory device as a sequence of binary codes. Taken as a whole, the sequence of instruc- tions are referred to as a "stored program". 2.2 SOME BASIC CONCEPTS Any logic device may be reconstituted from some or all of the following basic functions: 1) 8inary addition 2) The logical operations AND, OR and EXCLUSIVE-OR 3) Shifts and rotates of binary digit sequences which are being interpreted as numerical entities (e.g., a byte = eight bits). A general purpose logic device can be created by implement- ing the basic functions listed above on a single chip. If the single chip is to duplicate the performance of other logic de- vices, it must be provided with a sequence of instructions that enable the required logic in the proper order, plus aa stream of data that is operated on by the specified logic. This is illustrated in Figure 2-1. INSTRUCTIONS IN I MULTIFUNCTION LOGIC DEVICE (CPUI Fig. 2-1. Multifunction Logic Device 2-1 In order to function, the multifunction logic device will need the following parts: A) An Arithmetic Logic Unit (ALU), containing the neces- sary basic logic functions. B) A control unit, which decodes instructions and enables elements of the ALU, as needed. C) Registers to hold instruction codes and data, as needed. D) Data paths within the CPU, and between the CPU and external devices. Parts A), B), C), and D) are the basic components of any Central Processing Unit (CPU). A CPU must be the focal point of any computer-maxi, mini or micro. Referring to Figure 2-1, where do "instructions" and "data in" come from, and where will "data out" go? There are two possibilities: memory or external devices. Refer to Figure 2-2. Memory is a passive depository of infor- mation where data or instruction codes may be stored. Memory must be divided into individually addressable loca- tions, each of which can store one element of instruction code or one element of data. In an F8 system, each individ- ually addressable location will be an 8-bit data unit (a byte), since the F8 is an 8-bit microprocessor. MEMORY MULTIFUNCTION lOGIC DEVICE (CPUI I/O PORT EXTERNAL DEVICES 1;0 PORT Fig. 2-2. Data and Instruction Paths in a Multifunction Logic Device "External devices" refer to any data source or destination beyond the perimeter of the microprocessor system. Drawing an analogy with a logic card, "'external devices" will refer to the world beyond the card edge connector. Data passes be- tween the microprocessor system and external devices via Input/Output (I/O) ports. 2.2.1 Instructions. Programs. Data and Memory For a microprocessor to perform any specified operation, it will receive and process a sequence of instructions. The se- quence may be very long-numbering even into the thousands of instructions. A sequence of instructions that can be taken as a unit is called a program; the purpose of this manual is to describe how a program is constructed out of a sequence of instructions. Data may (and usually will) be stored in memory. In fact, the 255 possible combinations of eight binary digits (Oi byte) may represent any of the following types of information: 1) An instruction code 2) Numeric or address data that is part of an instruction's code 3) Numeric or address data that is independent of instruction codes 4) A coded representation of a letter of the alphabet, digit or printable character It would be impossible to determine the content of any memory byte by random inspection. This does not cause prob- lems, since a program will occupy one or more segments of contiguous memory bytes, and data resides in blocks of memory as assigned by the programmer. 2.2.2 Interrupts The number of programs which may be stored in memory is limited only by the amount of memory available for program storage. If ten programs were stored in memory, by simply identifying one program, the same microprocessor system could be made to function in one of ten different ways. If a microprocessor has more than one program available for execution, how is the one program which is to be executed identified? There are two separate and distinct ways in which a program may be identified for execution: A) Program identification may itself be a programmed function; for example, each program, upon completing execution, may identify the next program to be exe- cuted. The key to this method of program identification is that it is internally controlled, within the logic of the microprocessor system. B) Programs may be called into execution by external devices; this may happen even if another program is in the middle of execution. For example, take the sim- ple case of a microprocessor that is recording data input by an external instrument; while receiving data from the external instrument, the microprocessor per- forms numerical operations on the collected data. Program executions are illustrated in Figure 2-3. Fig. 2·3. Program P Being Interrupted to Execute Program R In Figure 2-3, P represents the program performing numerical operations on the data. Data is collected by repeated execu- tion of program R. Events occur as follows: 1) Program P is executing. 2) When the external instrument has data which it is ready to transmit, it sends an interrupt signai iii to the microprocessor, along with the starting address of program R. 3) Upon receiving interrupt signal I, the microprocessor does some elementary "housekeeping"; for example, it saves the address of the program P instruction it was about to execute, plus any intermediatedata being held in temporary storage registers. 4) The microprocessor completely executes program R. 5) Upon completion of program R execution, the micro- processor restores values saved in step 3, then con- tinues program P execution from the point where interrupt I occurred. Thus execution of program P ap- pears to have gone into "suspended animation" for the duration of program R execution. The sequence of events illustrated in Figure 2-3 is quite com- mon in microprocessor applications, and is called an external interrupt. Interrupt programming is described in Section 8.2. 2.2.3 Programmable Clocks There are many microprocessor applications in which It IS important that the microprocessor system be synchronized with the real time of the outside world. Such synchronization is accomplished using programmable clocks, which are reg- isters that count at a known rate. When the shift register counts to zero, the event is marked by an interrupt (as de- cribed in Section 2.2.2); in this case the interrupt is defined as a "time out" interrupt. Since the rate at which the clock register counts will be known for any microprocessor system, setting a real time interval simply involves loading the register with the correct initial count. 2.2.4 Direct Memory Access Notice from Figure 2-2 that data may be input to the micro- processor from memory or from an external device, via an I/O port. It is easy to imagine how, in many applications, data will be transferred from an external device, via an I/O port and the CPU, to memory; the data will then be accessed from memory in the normal course of program execution. It makes little sense to tie up the logic of the CPU while shunt- ing data from an I/O port to memory; therefore, provisions are made for Direct Memory Access (DMA), whereby data is moved between memory and an "I/O port". bypassing the CPU entirely. The DMA "I/O port" is called a "DMAchannel". In order to implement DMA, the microprocessor system must have iogic (outside the CPU) which provides the follOWing three pieces of information: 1) A starting memory address for a data block. 2) A byte length for the data block. 3) The direction of the data movement. If the microprocessor has this logic, data may be transferred between memory and an I/O port independent of, and in parallel with, unrelated CPU-memory operations. 2.2.5 A Complete Microprocessor System To summarize, a complete microprocessor system will have the following logical components: 1) A CPU, which is the multifunction logic device of the system. 2) Memory (of various types and combinations), in which programs and data are stored. 3) Memory interface logic which identifies: a) the next memory location which must be accessed to fetch instruction codes for the CPU, and b) the memory location from which a byte of data will be read, or to which a byte of data will be written. / / I I I I \ \ \ \ / _OATAPATHS _---+_ CONTROL PATHS / / / / / / / REAL TIME ,- CLOCK ,/ ......._--'""- MEMORY CPU 4) 1/0 ports, through which bidirectional data pas- ses between the microproc'essor system and external devices. 5) DMA logic, which provides a direct data path between memory and external devices, bypassing the CPU. 6) Interrupt logic, which causes the CPU to temporarily suspend current program execution. Along with each interrupt request signal, interrupt logic identifies the program which is to implement operations required by the source of the interrupt. 7) Real time clock logic, which synchronizes the entire microprocessor system with the real outside world by generating interrupts at variably definable time intervals. Figure 2-4 illustrates these seven logical components, with associated data flow paths. ~ ~ I I ~'l- I Q I MICROPROCESSOR SYSTEM OUTSIDE WORLD Fig. 2·4. Logical Components. Data Paths and Control Paths in any Microprocessor System 2-3 2.3 THE F8 SYSTEM There is no one-for-one correspondence between the logical components of a microprocessor system, as illustrated in Figure 2-4, and the devices of the F8, or any other micro- processor product. In fact, it is counter-productive to extend the concept of isolating functions on separate devices because it reduces the flexibility of a microprocessor system to satisfy simple, as well as complex, applications needs. More than any other microprocessor product, the F8 combines many functions on single chips, thus allowing simple systems to be implemented with as few as two devices, and complex systems to be implemented using many devices. Figure 2-5 illustrates the way in which F8 microprocessor system devices interconnect to give a variety of system configurations. The simplest F8 system contains one 3850 CPU and one 3851 PSU. Another very simple F8 system consists of one 3850 CPU, plus either one 3852 OMI interfaced to a single dynamic memory, or one 3853 SMI interfaced to a single static memory device. A fully expanded F8 system may have one 3850 CPU, one 3852 OMI and one 3853 SMI device, up to four 3854 OMA devices, plus 3851 PSU and static or dynamic memory devices in any combination, providing not more than a combined total of 65,536 bytes of memory are directly addressed by the 3850 CPU. It is possible to address more than 65,536 bytes of memory using special techniques which are described in the F8 Circuit Reference Manual. I I 11,0 ,'" '7 I I ,{M LOAD CONTENTS OF MEMORY BYTE ADDRESSED BY DCO 0108 2C SIX DC EXCHANGE DCO AND DCl 010C 17 SEVEN ST~ STORE ACCUMULATOR IN MEMORY BYTE ADDRESSED BY DCO 0100 2C EIGHT XDC EXCHANGE DCa AND DC1 alOE 31 NINE DS ~ 1 DECREMENT SCRATCHPAD BYTE 1 010F 94 BNZ LOOP IF SCRATCHPAD BYTE 1 IS NOT ZERO, RETURN TO LOOP 0110 FA END Fig. 4-2. label Fields (Shaded) in a Source Program Assembler { BUFA EQU H'08OO' SET THE VALUE OF SYMBOl BUFA Directives BUFB EQU H'Q8AO' SET THE VALUE OF SYMBOL BUFB ORG H'0100' 0100 2A ONE DCI BUFA SET DCa TO BUFA STARTING ADDRESS 0101 08 0102 00 0103 2C lVVO XDC STORE IN DCl 0104 2A THREE DCI BUFB SET DCa TO BUFB STARTING ADDRESS 0105 08 0106 AO 0107 2C FOUR ..t H'8C' LOAD BUFFER LENGTH iNTO ACCUMULATOR 0108 so 0109 51 FIVE LR l.A SAVE BUFFER LENGTH IN SCRATCHPAD BYTE 1 010A 16 LOOP Lr..,1 LOAD CONTENTS OF MEIViORY BYTE ADDRESSED BY Dca 0108 2C SIX XDC EXCHANGE DCO AND DCl 010C 17 SEVEN ST STORE ACCUMULATOR IN MEMORY BYTE ADDRESSED BY DCO 0100 2C EIGHT XDr. FXr.HANGF nco AND DC1 alOE 31 NINE OS DECREMENT SCRATCHPAD BYTE 1 010F 94 BNZ LOOP IF SCRATCHPAD BYTE 1 IS NOT ZERO, RETURN TO LOOP 0110 FA Assembler Directive- - END ~ Object Program Fig. 4-3. Mnemonic Field (Vertical Shaded) in a Source Program field is ignored by the Assembler and generates no object code. The comment field must be separated from the operand field (or the mnemonic field if there is no operand field) by at least one blank; it continues to the end of the text line. Figure 4-5 identifies the comment fields of a program. 4.2.5 Aligning Fields BUFA EQU H'0800' SET THE VALUE OF SYMBOL BUFA BUFB EQU H'OBAO' SET THE VALUE OF SYMBOL BUFB ORG H'Ol00' ONE DCI BUFA SET DCO TO BUFA STARTING ADDRESS TWO XDC STORE IN DCl Figure 4-6 illustrates the source program of Figures 4-1 to 4-5, with a single space code separating each field of every instruction. THREE DCI BUFB SET DCO TO BUFB STARTING ADDRESS FOUR LI H'BO' LOAD BUFFER LENGTH INTO ACCUMULATOR FIVE LR l.A SAVE BUFFER LENGTH IN SCRATCHPAD BYTE 1 LOOP LM LOAD CONTENTS OF MEMORY BYTE ADDRESSED BY DCO SIX XDC EXCHANGE DCO AND DCl SEVEN ST STORE ACCUMULATOR IN MEMORY BYTE ADDRESSED BY DCO EIGHT XDC EXCHANGE DCO AND DCl NINE OS 1 DECREMENT SCRATCHPAD BYTE 1 Clearly the program in Figure 4-6 is hard to read. For clarity it is recommended that all fields be aligned within character positions of every line; here is one possibility: BNZ LOOP IF SCRATCHPAD BYTE 1 IS NOT ZERO. RETURN TO LOOP END Label field: Mnemonic field: Characters 1 to 6 Charcters 7 to 11 Characters 12 to 19 Characters 20 to 72 Operand field: Comment field: BUFA EQU BUFB EQU ORG 0100 2A ONE DCI 0101 OS 0102 00 0103 2C 1W0 XDC 0104 2A THREE DCI 0105 OS 0106 AO 0107 20 FOUR LI 010S SO 0109 51 FIVE LR 010A 16 LOOP LM 010B 2C SIX XDC 010C 17 SEVEN ST 010D 2C EIGJ.iT XDC 010E 31 NINE DS 010F 94 BNZ 0110 FA END H'0800' H'OSAO' H'01oo' BUFA BUFB H'SO' 1,A 1 lOOP Fig. 4-4. Operand Fields (Shaded) in a Source Program BUFA EQU H'08oo' BUFB EQU H'OBAO' ORG H'Oloo' 0100 2A ONE DCI BUFA 0101 08 0102 00 0103 2C 1W0 XDC 0104 2A THREE DCI BUFB 0105 08 0106 AO 0107 20 FOUR U W80' 0108 80 0109 51 FIVE LR l,A 010A 16 LOOP LM 0108 2C SIX XDC 010C 17 SEVEN ST 0100 2C EIGHT XDC OlOE 31 NINE OS 1 010F 94 BNZ LOOP 0110 FA END Fig. 4-5. Comment Fields (Shaded) in a Source Program Fig. 4-6. A Source Program with Unaligned Fields SET THE VALUE OF SYMBOL BUFA SET THE VALUE OF SYMBOL BUFB SET DCO TO BUFA STARTING ADDRESS STORE IN DCl SET DCO TO BUFB STARTING ADDRESS LOAD BUFFER LENGTH INTO ACCUMULATOR SAVE BUFFER LENGTH IN SCRATCHPAD BYTE 1 LOAD CONTENTS OF MEMORY BYTE ADDRESSED BY DCO EXCHANGE DCO AND DCl STORE ACCUMULATOR IN MEMORY BYTE ADDRESSED BY DCO EXCHANGE DCO AND DCl DECREMENT SCRATCHPAD BYTE 1 IF SCRATCHPAD BYTE 1 IS NOT ZERO, RETURN TO LOOP SET THE VALUE OF SYMBOL BUFA SET THE VALUE OF SYMBOl BUFB SET oeoTO BUFA STARTING ADDRESS STORE IN DC1 SET DCO TO BUFB STARTING ADDRESS LOAD BUFFER LENGTH INTO ACCUMULATOR SAVE BUFFER LENGTH IN SCRATCHPAD BYTE 1 LOAD CONTENTS OF MEMORY BYTE ADDRESSED BY oeo EXCHANGE DCO AND DCl STORE ACCUMULATOR IN MEMORY BYTE ADDRESSED BY DCO EXCHANGE DCO AND DCl DECREMENT SCRATCHPAD BYTE 1 IF SCRATCHPAD BYTE 1 IS NOT ZERO. RETURN TO LOOP 4-3 4.3 LANGUAGE COMPONENTS 4.3.1 Valid Characters The F8 Assembler accepts all characters available on an input terminal as valid characters. Alphabetic (A-Z), numeric (0-9), and special (aU other terminal characters) characters are va!:d when correctly used; in other words, there is no character which will always be invalid. Some characters have been assigned special meaning; the use of these special characters is therefore restricted, as described in the following sub-sections, and summarized in Table 4-1. Restricted Character Function Example D Specify decimal constants D'1234' H Specify hexadecimal constants H'123A' B Specify binary constants B'10011101 ' 0 Specify octal constants 0'23714' C Specify character constants C'VALID' T Specify timer counts T'123' * Current memory location *+3 * Multiplication sign (VAL*2) ** Exponentiation sign (VAL**2) + Addition sign I (VAL+2) - Subtraction sign (VAL-2) / Division sign (VALl2) ( Beginning of an expression (VAL+2) ) End of an expression (VAL+2) Separate operands A,l Table 4-1. A Summary of Restricted Characters Restricted characters may be used in any way that does not directly conflict with the restricted use. 4.3.2 Constants Constants represent quantities or data that do not vary in value during the execution of a program. The syntax for constants' representation is described below. DECIMAL A decimal number consists of a string of from one to five nu- meric characters. The number may be preceded by a minus "-" sign but no blanks are allowed within the number. The value of a decimal digit must fall in the range +32767 to -32768. Optionally, decimal numbers may be enclosed between single quotes. preceded by a D character. Examples: Valid Invalid Reason invalid 12 123456 Too many digits -123 123- Invalid character 12345 12.3 Invalid character -5432 12}S3 Invalid character 23456 65432 Above +32767 D'12' '12'D D does not precede number in quotes D'23456' D'65432' Above +32767 4-4 HEXADECIMAL A hexadecimal number consists of a string of from one to four numeric characters and/or alphabetic characters (A to F inclusive) enclosed in single quotes and preceded by an H. No blanks are allowed within the number or between the H a~d the ~umber. Hex:adeGimal nl.lmhers in the rclnge H'O' to H'FFFF' are valid. Signed hexadecimal numbers are invalid. Examples: Valid Invalid Reason invalid H'12' 'ABCD' No preceding H H'ABCD' H'-12' Invalid character H H'lAFO' H'12.A3' Invalid character (.) BINARY A binary number consists of a string of from 1 to 16 ones or zeroes, enclosed within a pair of quotes and preceded by a B. No blanks are allowed between the apostrophe symbols, or between the B and the number. If there are less than 16 binary digits, leading 0 digits are assumed. Examples: Valid B'101101' B'0010' OCTAL Invalid B1011101 B'10110111011100101 ' B'10021' Reason invalid No quotes Too many digits Invalid digit (2) An octal number consists of a string of from one to six nu- meric digits, excluding 8 or 9, enclosed between single quotes and preceded by an O. Octal numbers in the range 0'0' to 0'177777' are valid. Signed numbers are invalid. Examples: Valid 0'17243' 0'2462' 0'177272' 0'23714' CHARACTERS Invalid 017243 '2462' 0'277272' 0'23914' Reason invalid No quotes No preceding 0 Value exceeds maximum Invalid character (9) Any characters (other than the single quote character) may be enclosed in single quotes and preceded by a C, in which case the characters will be interpreted as ASCII characters (see Appendix B). Examples: Valid CVALID' C'12345' C'NAME' Invalid VALID' C12345' C"NAME" TIMER COUNTS Reason invalid No preceding C No initial single quote Double quotes As described in Section 2, the 3851 PSU and the 3853 Memory Interface device each have a timer which may be loaded under program control. Depending on the value loaded into the timer, variable delays may be programmed, at the end of which a timer interrupt is transmitted to the 3850 CPU. Timer counts may be entered, as decimal numbers between a and 255, enclosed in single quotes and preceded by a T. The assembler converts the timer count to the exact binary code which (based on the timer logic) will generate the re- quired time delay. Appendix C provides the exact codes that correspond to each timer count entered using 1'nn' format. Recall that the exact time delay is given by the equation: Delay =(timer counts) * 31 * Clock period Examples: Valid 1'25' 1'127' 1'254' 4.3.3 Invalid T25 1'12A' 1'264' Symbols Reason invalid No single quotes Invalid character (A) Count too high A symbol is a character string of from one to four characters, the first of which must be alphabetic (A-Z). A symbol may have any number of characters; however, only the first four characters are interpreted by the assembler. A symbol can- not have the exact appearance of a number, as specified in Section 4.3.2. Since a blank space acts as a field delimiter, it cannot be present as a character within a symbol. Examples: Valid Invalid Reason invalid ABCD ABt:SCD A blank present. AB is the assumed symbol AB12 12AB A numeric first character 012 0'12' Would be interpreted as decimal 12 SYMBOLA SYMBOLB Both symbols are SYMB Figure 4-7 illustrates a number of symbols in a source pro- gram. Observe that symbols may appear in the label field or the operand field of an instruction. When a symbol appears in the label field of an instruction, it is either assigned a value by that instruction (EQU) or it is assigned a value equal to the location of that instruction, depending on the nature of the instruction. Sections 5.5 and 5.7 describe how this is done. When a symbol appears in the operand field of an instruction, the assembler substitutes the assigned value for the symbol. For example, instruction THREE in Figure 4-7 causes the value associated with symbol BUFB to be loaded into the DCa reg- isters of all memory and memory interface devices. Instruc- tion THREE therefore generates the following object code: 2A 08 AO 4.3.4 Expressions Expressions may appear in the operand field of an instruction, and are evaluated by the assembler to generate a constant which is used in the object program. BUFA EQU H'OSOO' SET THE VALUE OF SYMBOL BUFA BUFB EQU H'OSAO' SET THE VALUE OF SYMBOL BUFB ORG H'0100' 0100 2A ONE DCI BUFA SET DCO TO BUFA STARTING ADDRESS 0101 OS 0102 00 0103 2C TWO XDC STORE IN DCl 0104 2A THREE DCI BUFB SET DCO TO BUFB STARTING ADDRESS 0105 OS 0106 AO 0107 20 FOUR LI H'SO' LOAD BUFFER LENGTH INTO ACCUMULATOR 010S SO 0109 51 FIVE LR l.A SAVE BUFFER LENGTH IN SCRATCHPAD BYTE 1 010A 16 LOOP LM LOAD CONTENTS OF MEMORY BYTE ADDRESSED BY DCO 010B 2C SIX XDC EXCHANGE DCO AND DCl 010C 17 SEVEN ST STORE ACCUMULATOR IN MEMORY BYTE ADDRESSED BY DCO 010D 2C EIGHT XDC EXCHANGE DCO AND DCl OlOE 31 NINE DS 1 DECREMENT SCRATCHPAD BYTE 1 010F 94 TEN BNZ LOOP IF SCRATCHPAD BYTE 1 IS NOT ZERO. RETURN TO LOOP 0110 FA TL END Object Program Hexadecimal memory address in which object code is stored. Fig. 4-7. Symbols in a Source Program 4-5 Unlike higher level languages, expressions do not represent equations to be resolved at execution time. By the time a program is executed, every expression in the source program will have been converted (by the assembler) to a constant in the object program. An expression can have three types of numeric value, linked by six types of algebraic symbol. These are the three types of numeric value: 1) Any symbol, as defined in Section 4.3.3. 2) Any constant numeric value, as defined in Section 4.3.2. 3) An asterisk (*), which will be interpreted as having the value of the memory address into which the first ob- ject program byte for this instruction will be stored. These are the six algebraic symbols that are recognized: 1) + for add 2) - for subtract 4-6 3) * for multiply 4) / for divide 5) ** for exponentiate 6) ( and) to enclose expression and subexpressions, which are to be evaluated as a constant. Expressions and subexpressions must be enclosed in brackets. An exception is the simple (and most frequently used expression: *±numeric constant Subexpressions may be nested ten deep. Use of complex expressions is pointless, since it is almost as simple to evaluate the expression and use the evaluated re- sult in the object program. The one time when expressions are useful is when calculating instruction addresses. Refer- ring to Figure 4-7, the following are substitutes for LOOP in the operand field of instruction TEN: *-5 (equals H'010F' - 5) (FOUR+3) (equals H'01 01' + 3) ASSEMBLER DIRECTIVES Assembler directives are instructions to the assembler; as such, they generate no object code. Assembler directives provide the assembler with the following three types of information: 1) Values of symbols 2) How memory is to be mapped 3) Assembly listings print options Assembler directives are described in alphabetic order on the following pages. A summary of the assembler directives which are necessary, versus those which are optional, is given in Section 5.11; hints on good programming practice are also provided. 5.1 BASE - SELECT LISTING NUMERIC BASE This is an optional directive which specifies the number sys- tem in which object program codes will be printed on the assembler printout. The following three options are provided: Label Mnemonic BASE BASE BASE Operand HEX OCT DEC Comment Select hexadecimal output Select octal output Select decimal output If no base is specified, decimal output will be selected by de- fault. If a base is specified, one BASE instruction should ap- pear at the beginning of the program, as illustrated in Figure 5-1. Since hexadecimal notation is the standard for the Fa micro- processor, it is strongly recommended that programmers use this numeric option. 5.2 DC - DEFINE CONSTANT This directive causes the assembler to generate a one or two byte constant. The DC directive is an exception in that it causes one or two bytes of object code to be generated- identical to the one or two byte constant specified. The DC directive will usually have a label, which becomes the symbol via which the constant is referenced. The general format of the DC directive is: Label Mnemonic LABEL DC Operand VALUE LABEL is any valid symbol. The label is optional. VALUE is any valid numeric value as described in Section 4.3.2. For examples of DC directive use see Section 7.2.1. See also Section 5.5.1 for a discussion of when DC directives are used and when EQU directives are used. 5.3 EJECT - EJECT CURRENT LISTING PAGE This directive has no effect on the program being assembled. It controls the line printer on which the assembler is printing out an assembly listing. When the assembler encounters EJECT in the mnemonicfield of an instruction, it immediately advances the line printer paper to the top of the next page. If the assembler is not printing out an assembly listing, it will ignore the EJECT directive. The format of the EJECT directive is: Label Mnemonic Operand EJECT nUE';'.':i~~~~,rtiMtlV~ti~~~a~,;~1·y;·;';(·,:'·l,:,1~,,·'" 'MAXcroso,itMilOF!;)pSECONDS'CN 'riME S~ECfFlEO:\ "'!<';'~'~;:'Y;; SYMBOL ',AS;f'MBOL,TA9.I.;E V\ln..lFOI.;I-C>WSOURCE PR~_,~, ,: 0100 2A 0101 OS 0102 00 0103 2C 0104 2A 0105 OS 0106 AO 0107 20 010S SO 0109 51 010A 16 0108 2C OlOC 17 0100 2C OlOE 31 alOF 94 0110 FA XREF,SYMBOLS'CRQSSLlSTlNG WtLLFQU.dVV'SOY'RC~ '~M:.' 'BASE HEX HEXADECIMAL NUMBERS SPECIFIED FORASSEMBLYHSllNG ,SOFA ebU H'o8oo'; SET THE VALUEOfSYMBQlBUFA BUFB 'EQU 'H,'QaAO' SET THEVALU,E OF SYMBOL BUFB ONE TWO THREE FOUR FIVE LOOP SIX SEVEN EIGHT NINE 'ORG H'0100' DCI' XDC DCI LI LR LM XDC ST XDC DS BNZ END BUFA SETDCD TO BUFA STARTING ADDRESS STORE IN DCl BUFB SET DCa TO BUFB STARTING ADDRESS H'SO' LOAD BUFFER LENGTH INTO ACCUMULATOR l,A SAVE BUFFER LENGTH IN SCRATCHPAD BYTE 1 LOAD CONTENTS OF MEMORY BYTE ADDRESSED BY Dca EXCHANGE DCO AND DCl STORE ACCUMULATOR IN MEMORY BYTE ADDRESSED BY DCO EXCHANGE Dca AND DCl 1 DECREMENT SCRATCHPAD BYTE 1 LOOP IF SCRATCHPAD BYTE 1 IS NOT ZERO, RETURN TO LOOP Fig. 5-1. Assembler Directives (Shaded) in a Source Program 5-1 , 5.4 END - END OF ASSEMBLY An END directive must terminate every source program. Upon encountering this directive, the assembler stops reading source program instructions, and starts to perform various post-assembly computations. Figure 5-1 illustrates use of an END directive. Note that an END directive cannot, and must not. have a label. The format of the END directive is: Label Must be blank Mnemonic END Operand 5.5 EQU - EQUATE A SYMBOL TO A NUMERIC VALUE Every symbol in a source program must be the label of an assembly language instruction or a DC directive, or the sym- bo! must be assigned a value by an EQU directive. The general format of an EQU directive is: Label Mnemonic LABEL EQU Operand VALUE LABEL is any valid symbol. VALUE is any valid numeric value as described in Section 4.2.2. Refer to Figure 5-1. The symbols BUFA and BUFB appear in instructions ONE and THREE, and are assigned values by two EQU directives. Therefore: BUFA EQU H'0800' ONE DCI BUFA is identical in its net effect to: ONE DCI H'0800' Why then are Equate directives used? In a real program, a symbol (such as BUFA) is likely to appear many times. If the value of the symbol changes, the progrram can be corrected by modifying one Equate directive, then re-assembling the program. If absolute values are used in instruction operands (instead of symbols), every instruction that references the absolute value must be changed in the source program if the absolute value changes; the source program nust be re-assembled. For example. suppose there are 24 instructions in a source program that reference the ~ymbol BUFA. The Equate direc- tive could be eliminated, in which case each of the 24 instruc- tions would have H'0800' where it had BUFA. However, if H'0800' had to be changed, instead of making the chan!;le in one Equate directive, the change would h(;I\ / ,.\ \i'~ \ / / " \j I, '\ \ I / \ 1\ \ '\ \ II 1/\1 '\ 1/ 1I 1 , II II \: -0 ¥ ,-. " I I I I (BI ROUTINE "5" APPEARS ONCE EXECUTION LOGIC (REPRESENTED BY ..... ~ I BRANCHES TO THE BEGINNING OF "5". THEN RETURNS, FROM THE END OF "5", TO THE BRANCH POINT Fig. 7-2. Subroutine, as Compared to a Macro 7.3.2 Subroutine Program Steps The instructions that implement any logic are the same within, or outside of, a subroutine. Compare the 16-bit addition pro- gram (AD16) in Section 7.3.3 with the equivalent program in Section 7.2.1; the only changes relate to entry and exit procedures. 7.3.3 Simple Subroutine Calls and Returns As described in Sections 6.35 and 6.36, there are two instructions used to call a subroutine into execution. Instruction PK saves the contents of the program counter (PCO) in the stack register (PC1). then loads the subroutine starting address from the K register (scratchpad registers 12 and 13) into the program counter. Instruction PI saves the contents of the program counter in the stack register; it then loads the subroutine starting ad- dress (which is in the two object program bytes following the Plop code byte) into the program counter. For straightforward returns from subroutines, the POP in- struction, described in Section 6.37, moves the contents of 7-7 the stack register back to PCO, thus effecting a return from a subroutine. PK may also be used to return from a subroutine by having the return address in the K registers. LR PO,Q likewise may be used to return by having the return address in the Q register. The starting address of a subroutine is identified by the sub- routine name, which is the label of the first instruction to be executed in the subroutine. Suppose the multibyte addition routine from Section 7.2.2 is to be named MADD, while the 16-bit addition routine from Section 7.1.4 is to be named AD16. These names are created by changing ONE LI CNT USE SCRATCHPAD REGISTER a to the following equivalent instruction: MADD LI CNT For AD16, change ONE LR A,O USE SCRATCHPAD REGISTER a LOAD LOW ORDER AUGEND BYTE to the following equivalent instruction: AD16 LR A.O LOAD LOW ORDER AUGEND BYTE Note that although the first sequential instruction is also the first executed instruction for MADD and AD 16, the first executed instruction may, in reality, be any instruction within the subroutine. The last instruction executed by a subroutine must be POP, PK or LR PO,Q. Therefore, if for the moment the AD 16 error return is ignored, subroutine AD16 becomes: AD16 TWO THREE FOUR FIVE SIX SEVN EIGHT NINE OUT LR AS LR LR LNK BNO POP AS LR POP A,O 2 2,A A,l EIGHT 3 3,A FIRST INSTRUCTION EXECUTED FOR AD16 IF THE RESULT IS TOO LARGE, RETURN RETURN AT END OF SUBROUTINE Notice that a subroutine may have more than one exit. Subroutine MADD becomes: MADD LI CNT TWO LR OA FIRST INSTRUCTION EXECUTED FOR MADD (rest of subroutine as in Section 7.2.2) TWTl DS BNZ POP o LOOP RETURN FOR MORE RETURN AT END OF SUBROUTINE Consider the very simple case of subroutineAD16 being called using a PI instruction. Instruction sequences, with arbitrarily selected memory addresses, might be as foiiows: Memory *MAIN PROGRAM SEGMENT Address H'102A' ONE PI AD16 H'102D' TWO INC *SUBROUTINE AD16 STARTS HERE H'2130' AD16 LR A,O H'213B' OUT POP Before instruction ONE is executed, PCO contains H'102A'. After instruction ONE has executed, PCO contains H'2130' and PCl contains H'102D'. Execution now proceeds from AD16, at H'2130'. Before instruction OUT is executed, PCO contains H'213B' and PCl still contains H'102D'. Instruction OUT moves H'102D' to PCO, thus returning execution to TWO. The following sequence illustrates PK being used to call AD16, and PI being used to enll MADD' *THIS ORIGIN FOR AD16 HAS BEEN ARBITRARILY SELECTED AD16 ORG H'0980' 10 1...11 '''' ... l""t. , ,,",,'AI ",....~,.._ LVMU LVVV vnucn AUGEND BYTE (rest of subroutine follows here) *THIS ORIGIN FOR MADD HAS BEEN ARBITRARILY SELEClED ORG H'09EO' MADD LI CNT USE SCRATCHPAD REGISTER 0 irest of subroutine follows here) *THIS ORIGIN FOR THE MAIN PROGRAM HAS BEEN *ARB!TRAR!LY SELECTED ORG H'1000' *BEFORE SUBROUTINE AD16 IS FIRST CALLED, LOAD *ITS STARTING ADDRESS INTO THE SCRATCHPAD K REGISTERS ONE LI TWO LR THREE LI LR FOUR PK FIVE PI SIX PK SEVEN PK etc H'09' KUA H'80' KLA MADD 7.3.4 Nested Subroutines LOAD STARTING ADDRESS OF SUBROUTINE AD161NTO K REGISTER FIRSTCALLTO SUBROUTINE AD16 FIRSTCALLTO SUBROUTINE MADD SECOND CALL TO SUB- ROUTINE AD16 THIRD CALL TO SUB- ROUTINE AD16 "Nesting" is the term applied to subroutines being called from within other subroutines. There is no reason why a subroutine should not, itself, call another subroutine. In fact, subroutines are such efficient programming tools, that it is not uncommon to find subroutines nested eight deep, or more, in large programs. Consider a very simple case, where creation of the correct carry status for multibyte addition is packaged into a sub- routine named CBIT. This subroutine is equivalent to instruc- t:C:1S E!GHT thivugh THRT of the addition program in Section 7.1.2. Subroutine CBIT appears as follows: CS~T In A " iviOVE STATUS FROM LNK 1...1"\ M,;;} ADDITION TO A LR J,W MOVE STATUS FROM BYTE ADD TO W XS 9 EXCLUSIVE-OR STATUSES LR 9,A RETURN STATUSTOJVIAW LR W,J POP I t I First try changing the addition program in Section 7.1.2 as follows: MADS COM INITIALLY CLEAR THE CARRY STATUS ONE LlSU 7 ADDRESS LOW ORDER BYTE OF EACH BUFFER LOOP LlSU 4 ADDRESS FIRST BUFFER TWO LR A,S LOAD FIRST BUFFER BYTE INTO A THREE LlSU 5 ADDRESS SECOND BUFFER FOUR LNK ADD ANY CARRY TO A FIVE LR J,W SAVE STATUS IN SCRATCH- PAD BYTE 9 SIX AS S ADD SAME BYTE OF SECOND BUFFER SEVEN LR D,A STORE ANSWER AND INC- REMENT BYTE POINTER EIGHT PI CBIT CALL C STATUS SUB- ROUTINE FORT BR7 LOOP RETURN IF NOT END FIFT POP RETURN FROM SUB- ROUTINE The addition routine has been converted into a subroutine named MADS. When subroutine MADS is called, the return address is stored in PC1 to be returned to PCO by POP instruction FIFT. Un- fortunately, when CBIT is called at EIGHT, the PI instruction will also store a return address, the address of instruction FORT, in PC1. The POP at FIFT will no longer work, since it will branch execution back to FORT, thus forming an endless execution loop. (This type of program error is handled by the MAXCPU directive.) When subroutines are nested one deep, (and this is often sufficient in simple F8 applications), the K registers in the scratchpad can be used to overcome the problem of wiping out PC1. For example, in Subroutine MADD, save PC1 in K upon entering MADS then use PK to return from MADD: MADS LR K,P COM EIGHT PI CBIT FORT BR7 LOOP FIFT PK SAVE RETURN ADDRESS INITIALLY CLEAR THE CARRY STATUS CALL C STATUS SUB- ROUTINE RETURN IF NOT END RETURN FROM SUB- ROUTINE FOR END When subroutines are nested more than two deep, a stack is created in RAM to hold subroutine return addresses. When creating such a memory stack, it is wise to use PC1 and K as address conduits to the stack, never actually retaining address permanently in PC1 or K. Consider the following simple, three-deep subroutine nest: Arbitrary Memory Addresses "MAIN PROGRAM 7-9 H'080A' ONE PI SUB1 H'080D' NXT1 ORG H'2073' CALLFIRST SUBROUTINE FIRST SUBROUTINE RETURNS HERE "START OF FIRST SUBROUTINE H'2073' SUB1 FIRST INSTRUCTION OF SUB1 H'2082' TWO PI SUB2 CALL TO SECOND SUB- ROUTINE H'2085' NXT2 SECOND SUBROUTINE RETURNS HERE H'2132' RET1 POP RETURN TO MAIN PROGRAM "START OF SECOND SUBROUTINE ORG H'12A4' H'12A4' SUB2 H'12B3' THRE PI H'12B6' NXR3 H'12E2' RET2 POP SUB3 FIRST INSTRUCTION OF SUB2 CALL TO THIRD SUB THIRD SUBROUTINE RETURNS HERE RETURN TO FIRST SUBROUTINE "START OF THIRD SUBROUTINE ORG H'1558' H'1558' SUB3 H'1596' RET3 POP FIRST INSTRUCTION OF SUB3 RETURN TO SECOND SUBROUTINE The sequence in which instructions are executed is given in Table 7-2, along with contents of PCO, PC1, and a "stack" in memory, where PC1 contents may be stored. Notice that the first return address, H'080D', is passed to SO, the first two bytes of the memory stack. Similarly the sec- ond (H'2085') and third (H'12B6') return addresses are stored in the second and third byte pairs of memory stack. At a" times, data in PC1 and K are merely the accidental result of logic needed to pass return addresses to the stack. A memory stack "pointer" must be maintained. After each return address is stored in the stack, the stack pointer wi" identify the next free stack byte. Return logic is the opposite of subroutine call logic. Before each call, the most recently stored return address (in the two stack bytes right behind the stack pointer) are moved to PC1, and the stack pointer address is decremented by 2. The memory stack may either be in scratchpad memory, or in RAM memory. r INSTRUCTIONiPCO REGISTERS/STACK CONTENTS PC1 JK First six bytes of Memory Stack L ........~~g~___-L.. I SO --L~--~.2_ J 1080A 0;00 I ? ? ONE i2073 7 ! 7 SU81 [2073 0800 ? I ? 1 2082 0800 !0800 ! 0800 TWO ,12A4 2085 ! 0800 ! 0800 1~_S~12A4 2085 0800 0800 12B3 2085 2085 0800 I THRE 1558 1286 2085 ! 0800 SU83 ! 1558 1286 2085 i 0800 I 1596 I 1286 1286 i 0800 RET3 1286 1286 1286, 0800 NXT3 ~86 1286 0800 12E 2 2085 2085 0800 i RET2 2085 2085 2085 0800 NXT2 12085 2085 2085 0800 [2132 0800 0800 0800 RET1 0800 0800 0800 0800 -- 7 ! 7 I 7 1 7 7 i 7 I 2085 2085 2085 2085 r 2085 2085 2085 2085 2085 1 2085 1 2085 ? r--- 7 !.-- ? ? ~ I: ) !-4-- 7 !.--- 7 ,--- 1286 ,--- 1286 - 1286 - 12B6 !~ 1286 ___ 12~_ - 12B6~--_ 12B6 r--- Before AftAr Before Before After Before Before After Before Before After Before Before After Before Before After I L~ Instructions are In order of execution Table 7-2. Use of a Memory Stack for Executing Multiple level Subroutines Consider first a stack in scratchpad memory. By assigning one 8-byte buffer to serve as a memory stack, subroutines may be nested four deep. One byte at the beginning of scratchpad memory will serve as the stack pointer. Subroutine CALL, described next, uses scratchpad bytes 0'77' to 0'70' as the memory stack, as illustrated in Figure 7-3. Scratchpad byte 8 is the stack pointer which mustbe initialized to H'7T. BYTE SCRATCHPAD NO. STACK / BEGINNING OF STACK 077' H1 IFIRST RETURN ADDRESS 076' L1 075' H2 } SECOND RETURN ADDRESS 074' l2 073' H3 f-------ij}THIRD RETURN ADDRESS I }FOURTH RETURN ADDRESS f-------l ~END OF STACK 0'72' I l3 071' H4 070' I l4 SUBROUTINE CAll lOAD ADDRESSES INTO THIS STACK SUBROUTINE RTRN FETCH ADDRESSES OUT OF THIS STACK Fig. 7-3. Scratchpad Stack Every subroutine must begin by saving PCl contents in K, then calling CALL. This is illustrated as follows for subroutine MADD, which is the addition program from Section 7.2.2, converted into a subroutine: MADD LR K,P SAVE RETURN ADDRESS IN K PI CALL SAVE RETURN ADDRESS IN STACK ONE LIS CNT USE SCRATCHPAD REGISTER 0 TWO LR OA AS A COUNTER Since the call to CALL is preceded by PC1 contents being saved in K, PCl is now free to hold the return address for CALL. Subroutine CALL has the following instructions: CALL LR A,8 MOVE THE STACK POINTER TOISAR Cl LR ISA C2 CI 0'67' CHECK FOR STACK OVERFLOW C3 BZ SFUL STACK HAS OVERFLOWED. MAKE ERROR EXIT. C5 LR A,KU MOVE KU TO STACK C6 LR DA C7 LR A,KL MOVE KL TO STACK C8 LR SA DO NOT DECREMENT ISAR C9 LR A.IS SAVE ISAR IN SCRATCH- PAD SYTE:3 C10 LR 8A Cll DS 8 DECREMENT VALUE SAVED FOR ISAR C12 POP RETURN The address of the next free stack byte is held in scratchpad byte 8. If this address is 0'67', it means that 0'70' is the address of the last filled stack byte and the stack is full. Therefore when CALL is called, the stack address is tested for overflow by checking ISAR. A value of 0'67' indicates the stack has overflowed. A value of 0'77' indicates the stack is empty. Subroutine CALL logic proceeds as follows: CALL Move the stack address from scratchpad byte 8 to ISAR C1 C2 Test stack address for 0'67'. C3 C5 to C8 C9 C10 C11 It is assumed that SFUL is the label of an instruction which will handle stack full errors in any way re- quired by program logic. This instruction branches execution to the instruction labeled SFUL. Move the contents of the K registers to the next two free bytes of the stack. The ISAR is onlydecremented once. The second decrement can be performed in the scratchpad, where 0'70' will decrement to 0'67' which is indicates stack full, rather than to 0'77' which would erroneously indicate stack empty. Return the new contents of ISAR to scratchpad register A. Decrement ISAR in scratchpad so that 0'70' will decrement to 0'67' which is full, not to 0'77', which is empty. C12 Return from subroutine CALL. A subroutine that uses CALL to save its return address on the stack will use another subroutine, named RTRN, to re- turn to the calling program. For example, subroutine MADD will now end with: lWT2 PI RTRN Since RTRN resets PCO, PI may be replaced with: lWT2 JMP RTRN Subroutine RTRN takes the address most recently stored in the stack and moves this address to PCO, effecting the desired return, as follows: RTRN LR A8 MOVE THE STACK POINTER TO ISAR R1 LR IS,A LR AI INCREMENT ADDRESS TO LAST FILLED STACK BYTE R2 LR AI MOVE THE ADDRESS R3 LR QL,A IDENTIFIED BY ISAR TO Q R4 LR AS R5 LR QU,A R6 LR AIS RESTORE ISAR R7 LR 8,A R8 LR PO,Q MOVE Q TO PCO Subroutine RTRN executes as follows: 7-11 RTRN R1 R2 to R5 R6 R7 R8 Move the stack pointer address from scratchpad register 8 to ISAR. Increment ISAR to move ad- dress from the first free stack byte to the last occupied stack byte. Move the address identified by ISAR to QL and QU. Increment ISAR to point to the prior address. Leave ISAR addressing what is now the first free byte. Save the new value of ISAR in scratchpad register 8. The subroutine that called RTRN now wishes to return to the address which RTRN has moved to the Q registers. RTRN can simply move this ad- dress from Q to PCO in order to effect the desired return. For large stacks, RAM memory may be used for the memory stack. Only minor logic modifications are required to CALL and RTRN if the stack is in RAM. Assuming that the stack pointer is maintained in scratchpad registers 8 (high) and 7 (low), subroutine CALR and RTRR perform the same func- tions as CALL and RTRN but, for a RAM stack, that may be more than 256 bytes long. As for the scratchpad stack, the RAM stack begins at a high RAM address, and the stack address is decremented as the stack gets filled. The end of the RAM stack is identified by a low address, represented using the symbols SPHI and SPLO for the high and low order bytes of the address. The stack pointer address identifies the last filled stack byte. *VERSION OF SUBROUTINE CALL FOR RAM STACKS, WITH *THE STACK POINTER IN SCRATCHPAD REGISTERS 8AND 7. CALR LR A,7 LOAD LOW ORDER BYTE OF LR CI LR LR BNE CI BEQ 11,A SPLO A8 10,A CA8 SPHI CA20 STACK ADDRESS MOVE TO HL COMPARE WITH END-OF- STACK L.O. BYTE LOAD HIGH ORDER BYTE OF STACK ADDRESS STORE IN HU IF LOW ORDER BYTE DOES NOT EQUAL STACK END, CONTINUE COMPARE HIGH ORDER BYTES IF EQUAL, STACK HAS OVERFLOWED CA8 LR DC,H MOVE H TO DC *SUBTRACT 2 FROM THE STACK ADDRESS, SINCE IT INCRE- *MENTS WHEN MEMORY IS ACCESSED. BY SUBTRACTING *2, DCO ADDRESSES THE SECOND FREE STACK BYTE. LI H'FE' ADC LR ST AKU MOVE KU TO STACK LR A,KL MOVE KL TO STACK ST *SUBTRACT 2 FROM STACK ADDRESS, SINCE IT HAS INCRE- *MENTED TO BEGINNING OF PREVIOUS ADDRESS. LI H'FE' ADC LR LR H,DC A 11 RESTORE STACK POINTER CA20 LR LR LR POP JMP 7.A A10 8.A SFUL RETURN STACK FULL ERROR The logic of CALR differs from ihe iogic of CALL oniy in the way stack overflow is handled. Rather than leaving the stack pointer addressing the next free byte of the stack, the stack pointer addresses the last used byte of the stack. Stack over- flow is tested for by comparing the contents of the stack pointer with an address that has been specified as the end of the stack. This end of stack address can be equated to any value that is convenient to program logic. Notice that whenever memory is accessed via the DC reg- isters the address in the DC registers is automatically incre- mented. The stack in RAM has arbitrarily been selected to begin at a high address and end at a low address, which is the opposite direction as seen by the DC registers. Since the DC registers address the last filled byte of the stack, two must be subtracted from this address so that two bytes of address data may be loaded into the stack without overloading the last filled byte. Also, after the two bytes of address have been loaded into the stack, two must again be subtracted from the address in the DC registers so that the address once again identifies the last filled byte of the stack. Although the sense of direction of the stack is inverted with regard to the DC registers when CALR is executed, stack direction will agree with the DC registers when RTRR is exe- cuted. Since stack access involves a forward and then a reverse direction, it makes no difference what is chosen to be forward and what reverse; either CALR or RTRR must access the stack by decrementing addresses. This is contrary to the sense of the DC registers which only increment addresses. *VERSION OF SUBROUTINE RTRN FOR RAM STACKS, WITH *THE STACK POINTER IN SCRATCHPAD REGISTERS 8 AND 7. RTRR LR A8 MOVE THE STACK POINTER TO H LR 10.A LR A7 LR 11.A LR DC,H MOVE THE STACK POINTER TO DC LM LOAD HIGH ORDER BYTE LR QU.A OF RETURN ADDRESS INTO QU LM LOAD LOW ORDER BYTE LR OLA OF RETURN ADDRESS INTO OL LR H,DC SAVE STACK POINTER IN LR A10 SCRATCHPAD BYTES 8 AND 7 LR S,A LR A,11 LR 7.A LR PO,Q MOVE Q TO PCO In F8 systems that have a 3852 and/or 3853 Memory Inter- face device, if DC1 is not used to address data buffers, it can be used effectively as a RAM stack pointer. /- I L 7.3.5 Multiple Subroutine Returns Observe that the 16-bit addition subroutine in Section 7.1.4 requires two returns, one for an overflow in the answer, the other for a valid execution. Frequent!y subrout:nes may execute with more than one possible outcome. The most efficient way of handling such logic is to build multiple returns into the calling program and into the called subroutine. Here are some examples. First. an error return: PI SUB1 BR ERR Next, mu!tiple valid returns: PI SUB2 BR PLUS BR ZERO CALL SUBROUTINE SUB1 ERROR RETURN FROM SUB1 NON-ERROR RETURN FROM SUB1 RESULT IS POSITIVE RESULT IS ZERO RESULT IS NEGATIVE Subroutines RTRN and RTRR can easily be rewritten to handle multiple returns. Instructions will be added that return, to PCO, the last address entered into the stack, plus any dis- placement that is in QL (scratchpad register 15) when the subroutine is called. RTRN will now appear as follows, renamed RTRD: RTRD R6 R7 Taking LR LR LR LR AS LR LR LNK LR LR LR LR advantage will become: AD16 LR Pi LR AS LR LR A,8 IS.A AI AQL I QL.A AS QU.A AIS 8A PO,Q of RTRD, K,P CALL AO 2 2.A A,1 MOVE STACK POINTER TO ISAR INCREMENT ADDRESS TO LAST FILLED BYTE LOAD LOW ORDER ADDRESS BYTE ADD DISPLACEMENT IN QL STORE RESULT IN QL LOAD HIGH ORDER ADDRESS BYTE ADD ANY CARRY FROM LO BYTE ADDITION STORE RESULT IN QU RESTORE ISAR MOVE Q TO PCO the i 6-bit addition subroutine SAVE RETURN ADDRESS IN K SAVE RETURN ADDRESS IN SCRATCHPAD STACK LOAD LOW ORDER AUGEND BYTE ADD ADDEND LOW ORDER BYTE SAVE THE RESULT LOAD HIGH ORDER AUGEND BYTE LNK ADD ANY CARRY FROM LOW ORDER BYTE ADD BNO EIGHT IF THERE IS A CARRY OR AN OVERFLOW, RETURN WITHOUT DISPLACEMENT BR ERR EIGHT AS 3 ADD THE HIGH ORDER ADDEND BYTE LR 3,A SAVE THE RESULT LIS 2 FOR A GOOD RETURN, ADD 2 TO RETURN ADDRESS BNO OK AGAIN IF THERE IS A CARRY OR OVERFLOW ERR CLR FOR AN ERROR RETURN, ADD 0 TO RETURN ADDRESS OK LR QL,A SAVE THE DISPLACEMENT IN QL PI RTRD Now AD16 will be called as follows: PI AD16 BR ERROR ERROR RETURN GOOD RETURN 7.3.6 Passing Parameters Subroutine MADD, as described so far, is of limited value, since the starting addresses of buffers BUFA, BUFB and BUFC are fixed. MADD will only add the contents of two fixed buffers and store the result in a third, fixed buffer. The subroutine would be far more useful if buffer locations and lengths could be specified at the time the subroutine is called. This can be done and is called parameter passing. The parameters to be passed to a subroutine are listed, in the calling program, after the subroutine call. For example, the call to MADD would appear as follows: VALA EQU VALB EQU VALC EQU COUNT EQU H'2080' H'2088' H'2800' H'08' *CALL TO MADD IN MAIN PROGRAM PI MADD CALL SUBROUTINE MADD DC VALA VALA IS A TWO BYTE AUGEND BASE ADDRESS DC VALB VALB IS A TWO BYTE ADDEND BASE ADDRESS DC VALC VALC IS A TWO BYTE ANSWER BASE ADDRESS DC COUNT COUNT IS A ONE BYTE BUFFER LENGTH BR ERROR RETURN HERE IF THERE IS AN ERROR RETURN HERE FOR SUCCESSFUL EXECUTION 7-13 Once MADD is entered, the return address in PC1 is in fact, the address where the augend buffer starting address will be found. Before entering into the body of the subroutine, MADD will load parameters into H, Q and DC1. This is illustrated below for subroutine MADD with parameters, renamed MADP. MADP LR K,P SAVE THE RETURN ADDRESS IN P PI CALL SAVE THE RETURN ADDRESS IN THE STACK LR A,KU MOVE THE RETURN ADDRESS FROM K LR 10,A TO DCO. DCO WILL NOW ADDRESS THE FIRST OF THE LR A,KL TWO BYTES IN WHICH VALA IS STORED, FOLLOW!NG LR 11,A THIS CALL TO MADP LR DC,H LOAD PARAMETER ADDRESS INTO DCO LM LOAD VALA INTO H LR 10,A LM LR 11,A XDC MOVE VALA FROM H TO DC1 BY LR DC,H EXCHANGING DCO WITH DC1 XDC MOVING VALA TO Dcd, THEN LM AGAIN EXCHANGING DCO AND DC1 LR 10,A NEXT LOAD VALB INTO H LM LR 11,A LM LOAD VALC INTO Q LR QU,A LM LR Ql,A LM LOAD COUNT iNTO SCRATCHPAD BYTE 0 LR O,A LR DC,H MOVE VALB TO DCO *THE MULTIBYTE ADD MAY NOW BEGIN COM INITIALLY CLEAR THE CARRY BIT LR J,W LOOP LM LOAD THE NEXT AUGEND • BYTE LR W,J LNK ADD ANY PREVIOUS CARRY LR J,W SAVE STATUS IN J XDC ADDRESS ADDEND BUFFER AM ADD CORRESPONDING ADDEND BYTE XDC READDRESS AUGEND BUFFER LR H,DC SAVE AUGEND ADDRESS IN H LR DC,Q LOAD ANSWER BUFFER ADDRESS ST STORE THE ANSWER LR Q,DC SAVE THE ANSWER BUFFER ADDRESS IN Q LR DC,H MOVE AUGEND ADDRESS BACK FROM H BNC TWT1 NO CARRY FOR NEXT BYTE LR J,W 1WT1 DS DECREMENT COUNTER BNZ lOOP RETURN FOR MORE lR W,J LIS 9 LOAD A FOR A GOOD RETURN BNC OUT TEST FOR A FINAL CARRY US 7 THERE is A CARRY, PREPARE FOR ERROR OUT lR Ql,A SAVE THE DISPLACEMENT IN QL PI RTRD RETURN FROM SUBROUTINE Parameter passing works as follows: A subroutine that expects to receive parameters will initiate execution with the return address pointing to the first byte of the parameter list, and not to the instruction which must be executed once program control returns to the calling pro- gram. In other words, after subroutine CALL has executed, the address saved on the stack is the address of the first parameter, not the address of the next instruction to be exe- cuted in the calling program. Initial subroutine iogic must therefore move the address of the first parameter to the DCO registers, and must then appropriately load parameters into registers where they will be needed for execution of the subroutine. This process is straightforward data movement and requires no special explanation. Observe that when subroutine RTRD is called to effect a return to the calling program (in this case the main program which called MADP) the return address, as stored in the stack, is still the address of the first parameter byte. There- fore, before RTRD is called, the value loaded into the accu- mulator is not zero or a displacement representing multiple returns. It is the number of bytes of parameters, or the num- ber of bytes of parameters plus the displacement of the multi- ple returns. For example, subroutine MADP requires seven bytes of parameter information to follow the call to MADP. Therefore, an error return from MADP requires the value 7 to be loaded into the accumulator before RTRD is called; a value of 9 must be loaded into the accumulator before RTRD if there is no error. 7.4 MACROS Observe in Figure 7-2(A) that an instruction sequence may reappear in a program each time it is reused. Such an instruc- tion sequence may be identified as a macro. Refer to Figure 7-2. If the instruction sequence represented by "s" is a subroutine (we will assume that it is named SUB1), then wherever the logic of SUB1 is required, a PI or PK instruction in the main program will cause execution to branch to one set of code, as illustrated in Figure 7-2(BI and described in Section 7.3. If,.on the other hand, the logic of SUB1 is to be treated as a macro, then the name SUB1 will appear in the mnemonic field of the source pro- gram as though SUB1 were the mnemonic for ali instruction. In the object program, the assembler will actually insert the sequence of instructions represented by SUB1 wherever SUBl appears in the source program, as illustrated in Figure 7-2(A). 7.4.1 Defining and Using Macros Beginning with a very simple example, suppose the instruc- tion sequence which creates the correct carry status in multi- byte addition routines is to be identified as a macro named CBIT, rather than as a subroutine named CBIT. The macro is defined in the source program by enclosing the instructions of the macro between assembler directives MACRO and MEND, as follows: MACRO CSIT lR A,9 NI H'02' LR J,W AS 9 LR 9,A LR W,J MEND MOVE STATUS FROM LNK ADDITION TO A MASK OUT All BUT C BIT MOVE STATUS FROM BYTE ADD TO W ADD STATUSES RETURN STATUS TO J VIA W In theory, a macro definition, as illustrated above, could ap- pear anywhere in a source program; the assembler simply takes everything between the MACRO and MEND directives and holds it to one side, inserting the instructions whenever it sees the macro name appear in the mnemonic field of an instruction. In practice, it is good programming to collect macro definitions either at the very beginning or at the very end of a source program. As an example of how a macro works, subroutine MADD could specify CBIT as a macro, rather than as a subroutine, as follows: (Body of subroutine MADD) EGTN LR DC,H MOVE AUGEND ADDRESS BACK TO H LR K,P SAVE PCl IN K NNTN CBIT INSERT INSTRUCTIONS FROM CBIT MACRO HERE 1WTl DS 0 DECREMENT COUNTER BNZ LOOP RETURN FOR MORE 1WT2 PK RETURN FROM SUBROUTINE FOR END When the assembler assembles the above instruction se- quence, instruction NNTN will be replaced directly by the six instructions listed between MACRO CBIT and MEND. For this reason, the programmer may look upon a macro simply as a short-hand method of writing source programs (i.e., a method of taking the tedium out of re-writing the same instruction seauence again and againl. 7.4.2 Macros with Parameters A simple macro, as illustrated for CBIT in Section 7.4.1, is of limited value; it makes an object program longer, but it makes vvr!t!ng the sot.!!"ce program eas!er. The p:-cgrlJm executes faster since the PI and POP instructions are not executed. Macros with parameters are more useful. Refer to subroutine MADP, in Section 7.3.6. In order to make the multibyte addition program MADD useful, it was modified so that the call to subroutine MADD could be followed by seven bytes I of parameter data, including three 2-byte addresses and a single byte buffer length counter. Instructions at the be- ginning of subroutine MADP transfer these parameters to the H, a and DCl registers before performing the multibyte addition, thus allowing subroutine MADP to perform muiti- byte additions on the contents of buffers that can have any length and can be anywhere in memory. The multibyte addition may also be specified as a macro, where the macro name is followed by a number of para- meters. In this case, the parameters would again be three addresses and a byte count. Now when the assembler sub- stitutes the instruction sequence of the multibyte add for the macro name appearing in an instruction mnemonic, it changes instructions within the sequence according to parameter specifications. When a macro is defined, macro parameters are listed after the macro name with an ampersand as the first character of each parameter and one space separating parameters. This is illustrated for macro MADP below: ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT LOOP NINE TEN ELEV TWEL THRT FRTN FFTN SXTN EGTN TWTY TWTl MACRO MADP LI LR DCI LR DCI XDC DCI COM LR LM LR LNK LR XDC AM XDC LR LR ST LR BNC LR DS BNZ MEND &VALA &VALB &VALC &CNT &CNT a,A &VALC a,DC &VALA &VALB J,W W,J J,W H,DC DC,a DC,H TWTl J,W a LOOP USE SCRATCHPAD REGISTER a AS A COUNTER SAVE THE ANSWER BUFFER STARTING ADDRESS IN a SAVE THE SOURCE BUFFER ADDRESSES IN Dca AND DCl INITIALLY CLEAR THE CARRY BIT LOAD THE NEXT AUGEND BYTE CARRY FROM PRIOR ADD TO STATUS ADD ANY PREVIOUS CARRY SAVE STATUS IN J ADDRESS ADDEND BUFFER ADD CORRESPONDING ADDEND BYTE READDRESS AUGEND BUFFER SAVE AUGEND ADDRESS IN H LOAD ANSWER BUFFER ADDRESS STORE THE ANSWER MOVE AUGEND ADDRESS BACK TO H NO CARRY FOR NEXT BYTE SAVE CARRY FROM AM INSTRUCTION DECREMENT COUNTER RETURN FOR MORE Any program can tell the assembler to insert the instruction sequence specified by macro MADP, changing the symbols &CNT, &VALA, &VALB and &VALC to any four symbols speci- fied in the operand field of the instruction that references macro MADP. For example, in order to reproduce the multi- byte addition instruction sequence as illustrated in Section 7.2.2, the following instruction would have to appear: MADP BUFA BUFB BUFC CNT 7-15 When the assembler encounters the above instruction, it will substitute all ofthe instructions listed between MACRO MADD and MEND; however wherever it finds &CNT it will replace it with CNT, wherever it finds &VALA, &VALB or &VALC it will substitute BUFA, BUFB or BUFC, respectively. 7.4.3 Rules for Defining and Using Macros The following few rules apply to the use and definition of macros: 1) No macro can be referenced in a program unless it has been defined as a macro, using the MACRO and MEND assembler directives. 2) When a macro is defined, it can reference another macro so long as the other macro is defined separately elsewhere. 3) If a macro is defined with parameters, then every time the macro is specified within the body of a pro- gram, the specification must have a valid symbol in the operand field, corresponding to every parameter in the macro definition. 7.4.4 When Macros Should be Used There are two circumstances when macros are more ef- ficient as a programming tool than subroutines. Short instruction sequences that are frequently used within a program are often better represented as macros, if sub- routine addresses are being maintained in a stack. It takes a certain amount of time to store a return address in a stack, then at the end of a subroutine to retrieve the ad- dress from the stack. If the body of the subroutine is quite short, the time taken to maintain the stack may become excessive. Under such circumstances it is better to repro- duce the instruction sequence as a macro wherever it is needed within a program. Subroutines which require a large number of parameters to be passed from the main subroutine are frequently bet- ter represented as macros; a considerable number of in- structions may be needed to move the parameters from the parameter list that follows the subroutine call, to the registers or memory locations out of which the subroutine will access the parameters. Consider subroutine MADP; if this subroutine is called only two or three times it is probably more efficient to represent it as a macro rather than as a subroutine. Macros always result in faster program execution than subroutines. Macros may result in longer programs than subroutines. Therefore, in an application where speed is important, macros should be used in preference to subroutines. 7.5 JUMP TABLES A jump table is a programming device which is particularly useful in microprocessor applications. A jump table allows an index number to be loaded into the accumulator after which program execution jumps to a memory location which is dedicated to that index number. Jump tables are commonly used in switching applications, where data may be received from, or control signals may have to be sent to, one of many external devices. 7.5.1 Jump Table Using Jump Instructions The F8 instruction set is well-suited to execution of jump tables. As illustrated, one jump table may serve an entire application of diverse operations. The jump table consists of nothing more than a large number of jump instructions. To execute the jUIT'P table, a pmgraIT' SiIT'ply loads an !D. number into the accumulator, then jumps to the table logic. The table logic adds the contents of the accumulator, three times, to the address of the first jump instruction, which is stored in the DCO registers. The sum is moved (via the a registers) to the program counter and the jump is effected. "JUMP TABLE PROGRAM. JUMP NUMBER IS ASSUMED TO "BE IN THE ACCUMULATOR. JUMP DCI JMPO LOAD THE FIRST JUMP ADC ADC ADC LR LR JMPO JMP JMP JMP JMP JMP etc. O,DC PO,O AO A1 A2 A3 A4 ADDRESS INTO DCO ADD THREE TIMES THE BRANCH INDEX TO DCO, FOR THE THREE BYTES OF A JMP INSTRUCTION MOVE DCO TO PCO JUMP OCCURS HERE 7.5.2 Jump Table Using Address Constants Another jump table technique uses a table of addresses which are indexed as in the previous example. However, instead of a JUMP (LR PO,O) to the jump table, the address is loaded from memory into Q. The LR PO,Q instruction then causes a direct jump to the address in Q. The major advan- tage of this technique is that the table is only two bytes per entry, as compared to three bytes in the previous example. It also executes using fewer instruction cycles. "THE JUMP NUMBER IS ASSUMED TO BE IN THE "ACCUMULATOR JUMP DCI ADC ADC LM LR LM LR LR JMPO DC DC DC DC DC etc. JMPO QH,A QL,A PO,Q AO A1 A2 A3 A4 LOAD THE FIRST JUMP ADDRESS INTO DCO ADD TWICE THE JUMP NUMBER. DCO NOW AD- DRESSES A JUMP ADDRESS LOAD FIRST BYTE OF JUMP ADDRESS STORE IN QH LOAD SECOND BYTE OF JUMP ADDRESS STORE IN QL BY iviOViNG Q TO PO, rOReE JUMP I-Hi 7.5.3 Jump Table Using Displacement Tables Under some circumstances the addresses of the jump table may all be within 256 bytes of each other. When this situa- tion exists, only a displacement need be created in the table. This displacement, when added to some base address, will produce the address requ~red for the Jump. Nct~ce that :n the following example, entry FOUR and FIVE go to the same lo- cation. This is a variation that is quite useful. Perhaps the values 4 and 5 are invalid and an error routine needs to be called. The jump table will satisfy this condition in a most efficient manner without a separate compare instruction for each invalid value. Also notice that the entry points need not be in any particular sequence; however, in this example A1 must be the first entry point encountered, and it must have the lowest address in order for the arithmetic to be valid. This displacement table is most efficient since the table values are only one byte each. If an entry is beyond the 256 range it is possible to treat it as a special case within the table. Notice that A6 is more than 256 bytes beyond the start of A1 and is too large to insert before A4. To include it insert a JMP A6 prior to the coding at A4. If this instruction is labeled A66, an entry in the table would be: SIXS DC (A66-A1-1 28) 82 The value of THRE would now become 85. "THE JUMP NUMBER IS ASSUMED TO BE IN THE "ACCUMULATOR JUMP DCI ZERO LOAD FIRST TABLE LOCATION INTO DCO ADC ADD VALUE FROM ACCUMULATOR LM LOAD TABLE VALUE TO ACCUMULATOR DCI (A1+128) LOAD FIRST ORIGIN ADC ADD DISPLACEMENT VALUE ADDED TO DC LR Q,DC RECALL DC TO Q LR PO,Q JUMP TO ROUTINE ZERO DC (Al-Al-128) VALUE =-128 ONE DC (A2-A1-128) VALUE =- 78 TWO DC (A5-A1-128) VALUE = 22 THRE DC (A4-A1-128) VALUE = 82 FOUR DC (ERR-A1-128) VALUE =- 53 F!VE DC (ERR-A1-128) VALUE = - 53 SIX DC (A3-A1-128) VALUE =- 28 SVEN DC (A6-A1-1 28) VALUE = 207 TOO LARGE! Values are displaced by -128 to take into account the fact that the DCI instruction points to the middle of the table (A1+128). Therefore, addresses are created as shown on the following page. ARBITRARY DECIMAL ~ ADDRESSES: - - 2100 A1 STARTS HERE 2150 A2 STARTS HERE w t:l :i 2175 ERR STARTS HERE a: ~ 2200 A3 STARTS HERE III 2356 2250 f--------i A5 STARTS HERE 2310 f---------1 A4 STARTS HERE 2435 f---------1 A6 STARTS HERE} OUT OF RANGE 2585 f---------1 A6 ENDS HERE 7.6 STATUS, BITS AND BOOLEAN LOGIC The F8 instruction set is rich in boolean logic instructions which are very useful in applications manipulating bits and control lines. Examples given in the following subsections demonstrate some elementary uses of boolean logic instructions, along with some less obvious but commonly needed routines. 7.6.1 Manipulating Individual Bits Immediate boolean instructions specify data in the operand of the instruction; they may be used to set or reset individual bits within the accumulator. To reset one or more bits within the accumulator, AND the accumulator contents with a mask which is the complement of the bits to be reset. For example, the following instructions will reset bit 3 of scratchpad byte 1: LR NI LR A,1 H'FT 1,A LOAD SCRATCHPAD BYTE 1 INTO A MASK OUT BIT 3 RETURN TO SCRATCHPAD BYTE 1 Similarly, individual bits can be set by ORing the accumu- lator with a mask which has a 1 in every bit position that is to be set. For example, bit 3 of scratchpad byte 1 contents can be set to 1 as follows: LR 01 LR A,1 H'04' 1,A LOAD SCRATCHPAD BYTE 1 INTO A . SET BIT 3 RETURN TO SCRATCHPAD BYTE 1 Masks may also be accessed out of RAM or scratchpad memory. The following instruction sequence takes every byte from a buffer CNT bytes long, starting at BUFA; it sets to 0 the bits specified by a mask stored in a memory byte addressed by MASK. BUFA, CNT and MASK are symbols which have been given arbitrary values below. BUFA MASK CNT EQU EQU EQU H'2380' H'08FF' 50 7-17 DCI MASK STORE THE MASK ADDRESS IN H LR H,DC DCI BUFA STORE THE BUFFER STARTING ADDRESS IN Q LR Q,DC LI CNT USE SCRATCHPAD BYTE OASA LR O,A COUNTER LOOP LM LOAD NEXT BYTE L1 LR DC,H LOAD MASK ADDRESS L2 NM AND ACCUMULATOR WITH MASK L3 LR DC,Q RELOAD BYTE ADDRESS L4 ST STORE MASKED BYTE IN ORIGINAL BYTE POSITION L5 LR Q,DC SAVE INCREMENTED BUFFER ADDRESS IN Q L6 DS 0 DECREMENT COUNTER L7 BNZ LOOP RETURN FOR MORE In addition to demonstrating use of the NM instruction, the above example shows how to process data in a single buffer, restoring a modified byte to its original byte position. The program proceeds as follows: The instructions preceding LOOP load the mask address into H and the beginning buffer address into Q. The buffer length is loaded into scratchpad byte A which is osed as a counter. LOOP The data counter holds the initial buffer address when this instruction is first executed and the next byte address on all subsequent executions of this instruction. This instruction therefore loads the next byte from BUFA. L1 Load the mask address from the H registers into the data counter, wiping out the incremented buffer address that resulted from instruction LOOP. L2 AND the contents of the accumulator with the mask byte. The fact that the AND with memory instruction will increment the address in the data counters is not consequential since this incremented address is not saved. On the next execution of this instruction, the original mask address stored in the H registers will be reused. L3 Reload the buffer address from the Q registers. This is the same address that was used by instruction LOOP. L4 Store the contents of the accumulator back in the buffer. Since the address loaded by L3 is the same address as was used by instruction LOOP, the masked byte will be stored back in the same memory location from which it was loaded. L5 This time save the incremented address in the data counters back in the Q registers. L6 Decrement the counter in scratchpad byte O. If L7 the result is zero, end. If the result is not zero process the next byte of the buffer. By using the NM instruction, the above example is resetting (to 0) selected bits from every byte in BUFA. By merely re- placing the NM instruction with an OM instruction, selected bits from every byte of BUFA could be set to 1. By storing the mask byte in a scratchpad register, the pro- gram can be greatly simplifip.n Thl;! illstruct!(I!"' seque'"!ce below is similar to the previous example, but the mask byte is stored in scratchpad register 1, and the DCl registerS are used to hold the buffer address, rather than the Q registers. Notice that at the LM instruction (LOOP), DCO is incremented; prior to the ST instruction, DCO and DC1 are exchanged. The ST instruction then increments DCO, thus both addresses remain synchronized. MASK EQU B'any binary value' BUFA CNT ONE TWO LOOP THREE EQU EQU LI LR DCI XDC DCI LI LR LM NS XDC ST OS BNZ H'2380' 50 MASK l.A BUFA BUFA CNT O.A o LOOP STORE BUFFER ADDRESS IN DCO AND IN DCl REGISTERS USE SCRATCHPAD BYTE 0 AS A COUNTER LOAD NEXT BYTE AND WITH MASK IN SCRATCHPAD BYTE 1 STORE IN ORIGINAL LOCATION DECREMENT COUNTER RETURN FOR MORE Again this routine can be simplified even further by deleting instructions ONE and TWO and changing instruction THREE to one of the following: NI 01 XI MASK MASK MASK AND WITH MASK OR WITH MASK EXCLUSIVE OR WITH MASK This change would result in saving two bytes. however the loop time would be increased by 1.5 cycles. 7.6.2 Testing for Status The EXCLUSIVE-OR instruction is very useful as a means of detecting changed statuses. There are many applications in which it will be necessary to keep a record of status for various control lines, and to detect when individual control line sta- tuses change and how they change. As illustrated in the instruction sequence below, eight control iines have their statuses maintained in scratchpad byte 3. When new sta- tuses are input from I/O port 0, they are temporarily saved in scratchpad byte 4. By EXCLUSIVE-ORing the new and old statuses, the accumulator identifies those status bits which have changed. By ANDing the changed status indicators with the old status, those indicators which went from "on" to "off" are identified. By EXCLUSIVE-ORing this result with the changed status indicators, those statuses which went from "off" to "on" are identified. IN 0 INPUT NEW STATUS S2 LR 4.A SAVE IN SCRATCHPAD BYTE 4 S3 XS 3 EXCLUSIVE-OR ACCUMU- LATOR WITH OLD STATUS S4 LR 5.A SAVE "CHANGED STATUSES" INDICATORS IN 5 S5 NS 3 AND WITH OLD STATUSES S6 LR 6.A SAVE "STATUSES TURNED OFF" IN 6 S7 XS 5 EXCLUSIVE-OR WITH "CHANGED STATUSES" S8 LR 7.A SAVE "STATUSES TURNED ON" IN 7 S9 LR A,4 NEW STATUS FROM SAVE S10 LR 3.A OLD STATUS FROM NEXT USAGE. Suppose the old status was: 7 6 5 432 1 0 Old Status = 1 0 1 1 1 0 0 0 Suppose the new status is: 7 6 543 2 1 0 New Status = 1 1 0 1 0 1 1 0 Bits 6, 2 and 1 have turned on. Bits 5 and 3 have turned off. Bits 6, 5, 3, 2 and 1 have changed. Here is the result of instruction S3: Old Status New Status Changed Statuses 7 6 543 2 1 0 101 1 100 0 11010110 01101110 Here is the result of instruction S5: Changed Status Old Status Turned Off 7 6 543 2 1 0 01101110 10111000 00101 000 Here is the result of instruction S7: Turned Off Changed Statuses Turned On 7 6 543 2 1 0 0010100 0 01101110 o 1 00 0 1 1 0 Bit No. Bit No. Bit No. Bit No. Bit No. 7.7 POWERING UP AND STARTING PROGRAM EXECUTION Whp.n power is turned on, an peo reg!sters !n an F8 micro processor system are set to O. Therefore the first instruction executed is located at memory byte O. Every F8 microprocessor system must, therefore, have a memory device (either a 3851 PSU, 3852 OMlor 3853 SMI). The first program to be executed must be originated at H'OO', as illustrated on the following page. I I I ORG START H'oo' FIRST INSTRUCTION EXECUTED The power on detect circuit for an F8 system is located in the CPU. This circuit insures that all critical control circuits and registers are in a valid operating condition when power is first applied. It performs the following functions: • Pushes previous contents of the program counter to the stack register • Resets the program counter to address "0000" 7-19 • Resets the Interrupt Control Bit (lCB) • Sets control block on the 3852 MI circuit When power is connected to the circuit or the reset line goes low, the CPU clears the program counter (PCO), pushing its previous contents into the stack register (PC1). Therefore, the instruction in location zero is executed first. The inter- rupt control bit is also cleared at this time. The rest of the F8 system is initialized under program control. The local in- terrupt block of the individual memory devices must be loaded before allowing any interrupts to occur. Output latches must be reset to zero before they may be used to input data. INPUT/OUTPUT PROGRAMMING Input/output programming covers program steps that cause data to be transferred between the F8 microprocessor system and the world beyond the microprocessor system. There are three separate and distinct types of input/output (I/O) programming: Programmed I/O, Interrupt I/O and Direct Memory Access (DMA). Programmed I/O is characterized by the 3850 CPU executing an instruction to initiate and control the I/O transfer of a single byte of data, via an I/O port. The key feature of programmed I/O is that it is initiated by the CPU, on a byte-by-byte basis. Interrupt I/O is characterized by an external device issuing an interrupt to the 3850 CPU; (this concept is discussed in Section 2.2.2). The interrupt does not itself cause any input or output data transfer to occur; rather it initiates execution of a program which performs any required programmed I/O DMA has been described conceptually in Sections 2.2.4, 2.6.3 and 2.8. DMA transfers data between a memory device within the microprocessor system and any device external to the microprocessor system, in parallel with other micropro- cessor operations. DMA is initiated using programmed I/O and, optionally, may terminate with an interrupt. The use of software clocks is also covered in this chapter. Even though software clocks have nothing to do with trans- fer of data between the microprocessor system and the out- side world, they do allow events within the microprocessor system to be synchronized with real time. 8.1 PROGRAMMED 1/0 A programmed input or output operation moves a byte of data from the 3850 CPU accumulator either to an I/O port (OUT), or from an I/O port to the accumulator (IN). Four instructions enable programmed I/O: INS and iN enable input, while OUTS and OUT enable output. (See Sections 6.16, 6.18, 6.33 and 6.34.) Note that a number of I/O ports are accessed by I/O instruc- tions, but transfer nodata between the microprocessor system and the outside world. These I/O ports hold control information used by interrupt I/O, DMA and real time clocks. Section 6.16 summarizes the I/O port addresses used by the F8, and indicates how the individual port addresses may be used. Programmed I/O is a very open ended subject, since it is de- pendent on how external circuitry accesses the I/O ports. The following subsections describe some general approaches to I/O programming as seen by the CPU. Actual applications will usually require modified versions of the given programming techniques. 8.1.1 Polling on Status A key feature of programmed I/O is that the microprocessor system and external devices operate at different speeds; the external device must transfer data at a rate which is slower than the I/O program's execution speed. The simplest way of handling programmed I/O, when external devices run slower than the microprocessor, is to have the ex- ternal device input a "status byte" to the I/O port when it is 8-1 ready to transmit or receive data. The 3850 CPU continuously inputs a byte of data from the port until the "ready status" appears. For example, suppose a 1 in the high order bit (bit 7) of the I/O port signifies a ready status; the following routine will input a byte of data via port 0: *ROUTINE TO INPUT A BYTE OF DATA VIA PORT 0, POLLING *ON STATUS TO SYNCHRONIZE WITH THE EXTERNAL *DEVICE. INa LIS a FIRST CLEAR THE PORT OUTS a LOOP INS a INPUT STATUS BP LOOP RETURN IF BIT 7 is a La INS a BIT 7 IS 1. INPUT A DATA BYTE ST STORE IN MEMORY BYTE ADDRESSED BY DCa L1 PI TEST BRANCH TO END OF INPUT TEST L2 BR LOOP RETURN FROM TEST FOR MORE INPUT L3 RETURN FROM TEST FOR END Three features of the above routine need to be explained. The first two instructions clear the output port. This is necessary because data being input at an I/O port is ORed with what- ever is already in the port. If, by chance, the high order bit of the last data byte input was 1, this would be interpreted as a ready status. The data which is input to the accumulator by instruction LOOP will be interpreted as a byte of status. While in this simple application only the high order bit of the status byte is being interrogated, in any real application all eight bits of the status byte could be assigned meaning. In this case, when bit 7 of the status byte is tested to be 1, a byte of data is input by instruction La to the accumulator. This routine assumes that the time delaybetween execution of instructions LOOP and La is sufficient for the external device to transmit a data byte. This routine assumes that an indeterminate number of char- acters are expected on input. A subroutine named TEST is called to determine if more bytes of data are expected. The operations performed by subroutine TEST are immaterial to the 1;0 routine. Subroutine TEST must have two returns; to instruction L2 if another byte of data is to be input, or to in- struction L3 if data input is complete. Each byte of data that is input to the accumulator in sub- routine INa must be stored in some read/write memory loca- tion. INa assumes that the DCa registers address a RAM byte into which the data must be stored. This assumes that before INa is called as a subroutine, the beginning address of a RAM data buffer is loaded into the DCa registers. Data bytes, as they are input, will be stored in ascending bytes of the ad- dressed RAM data buffer. Scratchpad bytes can also be used to hold data being input. Subroutine OUTO, described below, is avariation of subroutine INa. OUTO outputs data from a RAM buffer. The only dif- ference between subroutines INa and OUTO is that in OUTO, once a ready status has been detected, the data byte which is to be output must first be transferred from memory to the accumulator before being output to port O. Both subroutines INO and OUTO can address any port that the INS and OUTS instructions can address. In order to ad- dress other ports it is only necessary to replace the INS and OUTS instructions vv;th II\J and OUT instructions. *ROUTINE TO OUTPUT A BYTE OF DATA VIA PORT 0, POL- *LlNG ON STATUS TO SYNCHRONIZE WITH THE EXTERNAL *DEVICE OUTO LI OUTS INS o FIRST CLEAR THE PORT o LOOP1 o LOOP1 INPUT STATUS MO M1 M2 M3 BP LM OUTS PI BA 1 TEST LOOP1 RETURN IF BIT 7 is 0 BIT 7 IS 1. READ FROM MEMORY THE BYTE TO BE OUTPUT OUTPUT THE DATA BYTE BRANCH TO END OF INPUT TEST RETURN FROM TEST FOR MORE INPUT RETURN FROM TEST FOR END 8.1.2 Data, Status and Controls Observe that in Section 8.1.1, a byte input by an external device may be interpreted as status information or as data. Similarly, the 3850 CPU may output a byte which is to be interpreted as control signals or as data. To illustrate, consider an F8 microprocessor system being used to read data input from a keyboard, block the data into 256 byte records, then write the records out to a cassette. Events would proceed as follows: 1) Using a programmed input sequence such as INO, interpret a byte input from the keyboard as status. When a ready status is sensed, interpret the next byte arriving from the keyboard as data. 2) A subroutine such as TEST is called to create a 256 byte record in RAM, in the format needed for output to the cassette. 3.) When the microprocessor is ready to write a record to the cassette, it must first turn the cassette drive motor on, since the cassette drive will be stationary during the intervalswhen records are notbeing written out. The microprocessor will turn the cassette drive on by outputting an appropriate control byte whose bit pattern is determined by the specifications of the cassette drive controller. 4) The cassette drive will respond to the control byte. commanding the drive be turned on by transmitting back a status byte indicating that the command was successfully executed and the drive is now ready to receive data. 5) Upon receiving back the ready status from the cassette drive the microprocessor will output 256 bytes of data. Depending on the design of the cassette drive, the cassette drive controller may transmit a status byte back to the microprocessor after each individual data byte has been received. This status byte reports that the previous data byte has been recorded accu- rately, and the controller is ready to receive and record the next byte of data. 6) After the microprocessor has completed transmittal of an entire record of data, it must send a control signal to the cassette drive commanding the cassette drive to stop forward movement. 7) When all records have been written to the cassette drive, the microprocessor will issue athird control com- mand which causes the cassette drive to mechanically rewind. Observe that this simple application receives either data or status from the keyboard, then outputs either controls or data to the cassette drive; additional status information may come back from the cassette drive. Any external device may transmit two types of information to the microprocessor system: data or status. Any external device may receive two types of information from the microprocessor system: data or controls. Thus there are four types of information that may be trans- ferred between the microprocessor system and an external device. They are: a) data in b) status in c) data out d) control out An external device may communicate with the microproces- sor system using one, two, three or all four of the above types of information. For example, the keyboard uses "data in" and "status in" but does not use "data out" or "controls out". The cassette drive in the illustrated application uses "status in", "data out" and "controls out" but does not use "data in". Of course the cassette drive would be capable (at another time) of using "data in", when the data which was recorded on the cassette is subsequently read back into the microprocessor system. It is .feasible to use one port for all four of the information transfer types listed above when communicating with any one external device. For example, ane !/O port cou!d be used to receive status and data from the keyboard, and could also be used to receive status or data from the cassette drive and to output controls or data to the cassette drive. However, if more than one type of information is to go through one 1/0 port, exte!'"na~ ~og!C must have the means of mu!t:p!c;dng :n- formation in or out. A scheme that uses more 1/0 ports, but less external logic, allocates one port for data in or out and another port for status in or controls out. For example, 1/0 port 0 may be assigned to keyboard status in, 1/0 port 1 may be assigned to keyboard data in, 1/0 port 4 may be assigned to cassette status in and controls out and 1/0 port 5 may be assigned to cassette data in and out. I 8.1.3 Parallel Data and Control Ports Many applications will require data to be handled on paths that are more than eight bits wide. Sixteen-bit data, for example, is a common word size. Less frequently, it will be necessary to handle more than eight control lines at a time. Data paths that are more than eight bits wide can be handled in 8-bit units, sequentially through a single port. Alternatively, two or more ports may be assigned to one external data bus so that. whenever the microprocessor inputs data from an external device or outputs to the external device, it accesses each 110 port allocated to the data bus. This is illustrated be- low in subroutine IN16, which inputs data in 16-bit units via ports 4 (bits 0-7) and port 5 (bits 8-15). *ROUTINE TO INPUT 16 BITS OF DATA VIA PORTS 4 AND 5, *POLLING ON STATUS VIA PORT a TO SYNCHRONIZE WITH *THE EXTERNAL DEVICE IN16 LIS a FIRST CLEAR THE STATUS PORT TO REMOVE PREVIOUS OUTS a READY STATUS LOOP INS a INPUT STATUS BP LOOP RETURN IF BIT 7 IS a La INS 4 BIT 71S 1.INPUT FIRST DATA BYTE ST STORE IN MEMORY BYTE ADDRESSED BY DCa INS 5 INPUT SECOND DATA BYTE ST STORE IN NEXT MEMORY BYTE (ADDRESSED BY DCa) L1 PI TEST BRANCH TO END OF INPUT TEST L2 BR LOOP RETURN FROM TEST FOR MORE INPUT L3 RETURN FROM TEST FOR NO MORE INPUT 8.2 INTERRUPT I/O Two circumstances under which interrupts are commonly used to control 1;0 operations are: The programmed 110, described in Section 8.1, has the severe disadvantage that the microprocessor system spends a great deal of its time reading a status byte and waiting for the sta- tus byte to signal ··ready". If the external device operates at speeds close to that of the microprocessor, the wasted time may be unavoidable. For example, if the microprocessor can only execute ten instructions between each byte transmitted or received by the external device, it is probable that these ten instructions can be effectively used testing or processing each data byte as it is transferred. On the other hand, if the 3850 CPU can execute approximately one hundred instruc- tions between bytes of data being transmitted to or from the external device, there is sufficient time between data trans- fers for the microprocessor to be doing other useful work which mayor may not be related to the data transfer taking place. If instead of sending a ready status, the external de- vice transmits an interrupt request signal every time it is ready to transmit or receive a data byte, this signal can be used by the 3850 CPU to suspend executing whatever pro- gram was being executed, process a single byte of data, then return to the suspended program. The transfer of a sequence of data bytes at a known data rate constitutes a sequence of predictable events. In many ap- plications an external device's need for access to the micro- 8-3 processor system cannot be predicted. For example, an external device may only communicate to the microprocessor under distress circumstances, atwhich time the microprocessor must execute a program to compute and output needed cor- rection data. When the external device's need for access to the microprocessor system cannot be predicted, an inter- rupt is the only reasonable way in which the external device can gain control of the microprocessor system. 8.2.1 The Interrupt Sequence Each 3851 PSU in an F8 microprocessor system has an external interrupt line, as does the 3853 SMI device, ifpresent. The sequence of events surrounding an interrupt is as follows: 1) For interrupts to be processed, interrupts must be enabled within the 3850 CPU and at the device re- ceiving the interrupt request signal. At the 3850 CPU, all interrupts are enabled or disabled. At each 3851 or 3853 device, the individual interrupt line at that device is enabled or disabled. This is described in Section 8.2.2. 2) More than one device may simultaneously request to interrupt the 3850 CPU; that is, interrupt request sig- nals may be true, simultaneously, at more than one device. When this happens, priorities are arbitrated as described in Section 8.2.3. 3) When avalid interrupt request signal is detected bythe 3850 CPU, it ceases current program execution at the conclusion of the instruction currently being executed. (Certain instructions are exempt, as described below.) 4) The 3850 CPU sends out an interrupt acknowledge signal. The way in which this signal is trapped imple- ments interrupt priority when more than one interrupt request line is true, as described in Step 2. 5) When the 3850 CPU sends out an interrupt acknowl- edge signal. it clears the interrupt enable status within the 3850 CPU, thus disabling all subsequent interrupts. As described in Section 8.2.4, interrupts must be re-enabled, under program control, when such a step is appropriate to program logic. 6) Each device that has an interrupt request line also has a 16-bit address register which holds the address of the first instruction to be executed following the inter- rupt. The 3851 address register is a non-programmable mask option. The 3853 address register is made up of two I/O ports which are loaded with an address by appropriate I/O instructions. As described in Section 8.2.4, bit 7 of the interrupt address will always be 1 for an external interrupt. and will always be a for a local timer interrupt. The device that traps the interrupt acknowledge sig- nal output in step 5 responds by transmitting the contents of its interrupt address register as the next contents of PCO registers. 7) PSU and MI logic, under CPU control, moves the con- tents of PCO to PC1, then loads the addressfrom step 6 into PC0; thus a program dedicat,ed to the acknowl- edged interrupt request line is executed. An interrupt will not be acknowledged at the conclusion of any of the following instructions: PK PI POP JMP OUTS (if not port 0 or 1) OUT (if not port 0 or 1) EI LR W,J An instruction other than one of the above must be executed before an interrupt will be acknowledged. When power is first turned on, interrupts are disabled. 8.2.2 Enabling and Disabling Interrupts As described in Section 2.4.3, bit 4 of the 3850 CPU W reg- ister is an Interrupt Control bit. When this bit is set to 1, interrupt requests to the CPU are enabled; when this bit is reset to 0, no interrupt request to the CPU will be acknowl- edged. ICB is set to 1 by the EI instruction or by a LR W,J instruction; it is reset to 0 by the DI instruction or by a LR W,J instruction. Individual interrupt request lines are controlled at each de- vice via an I/O port which is set aside as an interrupt control buffer. For the 3851 PSU's, the interrupt control 1/0 port address is B'xxxxxxl0'; xxxxxx is the 1/0 port select code, which may be any number from 1 to H'3F'. The 3853 interrupt control 1/0 port address must be H'OE'. This address is also available on a 3851 PSU; when xxxxxx is H'03', the 3851 interrupt control 1/0 port address becomes H'OE'.: B'xxxxxxl0' =B'00001110' =H'OE' When a 3853 SMI device is present, a 3851 PSU with a chip select of H'03' cannot also be present. The following two instructions load the interrupt control 1/0 port: LI VAL OUT IPRT IPRT must be equated to the interrupt control 1/0 port address. VAL must be equated as shown in Table 8-1. Value VAL is I equated to I Effect H'OO' H'01' H'02' H'03' -I I IInterrupts disabled at this device. I External Interrupt enabled, timer interruptI disabled. Interrupts disabled at this device (same as H'OO'). External interrupts disabled, timer interrupt enabled. Table 8-1 _ Contents of Interrupt Control 1;0 Ports Timer interrupts are described in Section 8.4. 8.2.3 Interrupt Priorities When an F8 microprocessor system has more than one in- terrupt line, priorities are determined on the basis of "daisy chaining", as illustrated in Figure 8-1. 3850 CPU PIN 3851 psu POUT 3851 psu PIN POUT IREQ =COMMON INTERRUPT REQUEST LINE PIN =PRIORITY IN (INTERRUPT ACKNOWLEDGE) POUT =PRIORITY OUT (INTERRUPT ACKNOWLEDGE) 3853 SMI PIN EACH DEVICE RECEIVING PIN PASSES THE SIGNAL ON AS POUT, ETC. UNLESS IT IS REQUESTING AN INTERRUPT, IN WHICH CASE IT TRAPS PIN. Fig. 8-1. Daisy Chaining and Interrupt Priority Determination The daisychain sequence is a hardware feature of an F8 micro- processor system; when the system is configured, the inter- rupt acknowledge signal from the CPU is chained from one device to the next. This determines interrupt priorities. The only thing a programmer can do to modify interrupt pri- orities is to disable external interrupts at selected devices by appropriately loading the interrupt control 1/0 port at that device with some value other than H'Ol'. (See Section 8.2.2 and Table 8-1.) It should be clearly understood that interrupt priorities, as described in this section, apply only to interrupt request signals competing for the 3850 CPU's next interrupt service. There is nothing to prevent an interrupt from interrupting a previous interrupt; however, this type of nested priority is a function of how programs have been written. Once an in- terrupt has been acknowledged and is being serviced, and the ICB bit in the CPU is set to 1, the current interrupt service routine can itself be interrupted. In order to prevent an interrupt service routine from being itself interrupted, the ICB bit in the CPU W register must be left at zero until the interrupt service routine has completed execution. Figure 8-2 illustrates the concept of nested interrupts. FIRST INTERRUPT D = INTERRUPTS DISABLED (ICB = 0) I = INTERRUPTS ENABLED BY FIRST INTERRUPT SERVICE ROUTINE (ICB =1) Fig. 8-2. Two Levels of Interrupt The 3853 SMI device will not pass on an interrupt acknowl- edge signal; therefore, it must be at the end of the daisy chain, and will have lowest interrupt priority. 8.2.4 Program Response to an Interrupt There are three program steps which may be needed prior to an interrupt in order to prepare to receive interrupts. They are: 1) If a 3853 SMI device is present, the interrupt address register of the 3853 must be loaded with the address of the first instruction to be executed after an inter- rupt from the 3853 is acknowledged. As described in Sections 2.7 and 6.16, 1/0 port addresses H'OC' and WaD' have been reserved for the upper and lower interrupt address bytes, respectively; therefore the post-interrupt execution address can be loaded as follows: LI OUTS LI OUTS ADHI H'OC' ADLO WOO' ADHI and ADLO are symbols which must be equated to the high and low bytes of the selected execution address. Note that the 3851 PSU has the post- interrupt execution address as a permanent feature of the chip mask; therefore, each 3851 PSU has a fixed post-interrupt execution address associated with it. 2) Interrupts must be selectively enabled or disabled at 3851 and 3853 interrupt control ports, as described in Section 8.2.3. 3) The 3850 CPU master interrupt enable bit (ICB) must be set to 1, as described in Section 8.2.3. When an interrupt is acknowledged, events within the 3850 CPU proceed exactly as if a subroutine had just been called: the content of PCO is moved to PC1, and the content of the selected device's post-interrupt address register is moved to PCO. Interrupts should therefore be handled as though a subroutine had just been executed, as described in Section 7.3. For example, the first instructions executed following an interrupt might be: LR PI K,P CALL SAVE CONTINUATION ADDRESS IN K SAVE CONTINUATION ADDRESS IN STACK Returning from an interrupt to the interrupted program is identical to returning from a subroutine to the calling pro- gram; however, since a program may be interrupted any time interrupts have been enabled, parameter passing and multi- ple returns do not apply to post-interrupt programs and should not be used. Remember that the first interrupt service routine must enable ICB if second level interrupts are to be allowed (as illustrated in Figure 8-2). 8-5 8.2.5 Making 3851 PSU Interrupt Address Programmable The fact that the 3851 PSU's interrupt address is a permanent feature of the device is not a problem in applications where this address may have to be varied. Using a branch table (as described in Section 7.5), a number of possible post-interrupt service routine execution addresses may be maintained. The following routine shows how an external device may use a PSU 1/0 port to provide an index identifying the service rou- tine which must be executed following the interrupt. 1/0 port 4 has been arbitrarily selected as the 1/0 port address. The data byte at 1/0 port 4 selects an address from a branch table, as follows: *POST INTERRUPT SERVICE ROUTINE FOR PSU 1 RC11 LR K,P SAVE RETURN ADDRESS ON THE STACK PI INS LR PI CALL 4 RX BRANCH INPUT PROGRAM SELECT BYTE SAVE INDEX VALUE CALL BRANCH TABLE SUB- ROUTINE 8.2.6 Simple I/O Interrupts In Section 8.1.2, a simple application was described, where data is input at a keyboard and recorded in 256 byte records on a cassette. A cassette may record data at a rate of approximately 200 bytes/second. With time taken to start and stop the cassette, two or three seconds may elapse each time a record is out- put to the cassette. Preventing data from being input at the keyboard while it is being output to a cassette is both incon- venient and unnecessary. Simple 1/0 interrupts may be used to output data to the cassette, byte-by-byte. These few instructions are sufficient to service each interrupt. *PROGRAM TO WRITE ONE BYTE TO A CASSETIE, FOLLOW- *ING AN INTERRUPT CRW LM OUT EI POP CASS LOAD NEXT BYTE ADDRESSED BY DCa OUTPUT TO CASSETIE RETURN FROM INTERRUPT The key concept here is that the F8 is uniquely suited to pro- cessing a large number of simple interrupts. If the post- interrupt program will not itself be interrupted, and if it will call no subroutines, then merely ending it with a POP in- struction turns it into a complete interrupt service routine. Do not save the return address in the stack; do not call any starting or ending subroutines (e.g., CALL or RTRN). For example, see Section 2.8.7. 8.2.7 A Sample Program Figure 8-3 illustrates a configuration for the key to cassette application described in Section 8.1.2, except that 32 byte records are to be written to the cassette. STATUS FROM KEYBOARD 3850 °tETvAB~~~~-----.J 3851 PSU DEVICE SELECT ~ I INTERRUPT 1--_-r-_-+-~.....JtD j""""--CONTROL J 0 PORT SELECT '---.---'---r---' 8'000001' STATUS FADM CASSEITE CONTROLS TO CASSETTE ' - - - -.... OATA TO CASSETTE Fig. 8-3. Two Devices Servicing a Keyboard to Cassette Application *PROGRAM TO RECEIVE DATA FROM THE KEYBOARD USING *PROGRAMMED I/O *SCRATCHPAD BYTES 0'40'TO 0'77' MAKE UPTHE 32 BYTE *BUFFER. *SCRATCHPAD BYTES 0'20' TO 0'37' ARE USED AS A TEM- *PORARY BUFFER TO HOLD DATAWHILE THE MAIN BUFFER *IS BEING WRITTEN TO CASSETTE ORG H'OOOO' START L1SU 3 Sl L1SL 7 S2 LIS H'Ol' OUTS 6 INITIALIZE ISAR TO TEMPORARY BUFFER ENABLE EXTERNAL INTER- RUPTS AT PSU EI ENABLE INTERRUPTS S3 PI INKB INPUT NEXT EIGHT BYTES FROM KEYBOARD S5 L1SU 2 DECREMENT UPPER DIGIT OF ISAR S6 PI INKB INPUT NEXT EIGHT BYTES FROM KEYBOARD *AFTER INPUTTING 16 BYTES FROM THE KEYBOARD, IT IS *ASSUMED THAT ANY RECORD OUTPUT TO THE CASSETTE *IS COMPLETE. MOVE DATA FROM 0'37' - 0'20' TO 0'77'- 0'60'. S8 L1SL 7 LOAD FIRST SOURCE BYTE ADDRESS S9 S10 Sll S12 S13 S14 S15 S16 S17 L1SU LR L1SU LR 8R7 L1SU LR L1SU LR BR7 L1SU PI 3 A,S 7 D,A S9 2 A,S 6 D,A S14 5 INKB LOAD NEXT BYTE STORE NEXT BYTE iF NOT END OF BUFFER, RETURN FOR NEXT BYTE IF END OF F!RST BUFFER, MOVE SECOND BUFFER REPEAT MOVE FOR SECOND 8 BYTE BUFFER INPUT NEXT EIGHT BYTES FROM KEYBOARD TO SCRATCHPAD BUFFER 0'57' TO 0'50' 8-6 S19 L1SU S20 PI 4 INKB INPUT NEXT EIGHT BYTES FROM KEYBOARD TO SCRATCHPAD BUFFER 0'47' TO 0'40' ~8UFFER is NOW READY TO 8E OUTPUT TO CASSETTE. S21 LI H'3F' LOAD BUFFER INITIAL S22 LR O,A ADDRESS (0'77') INTO SCRATCHPAD BYTE 0 S23 LI ONC TURN CASSETTE ON S24 OUTS 5 S25 BR START RETURN FOR NEXT RECORD *INPUT SUBROUTINE INKB STORES A BYTE OF DATA INPUT *FROM KEYBOARD INTO SCRATCHPAD BYTE ADDRESSED "BY ISAR INKB LO LOOP Ll L2 L3 LR K,P CLR OUTS 0 INS 0 BP LOOP SAVE RETURN ADDRESS IN K CLEAR PORT 0 INPUT STATUS INS 1 INPUT DATA LR D,A STORE IN ISAR BUFFER BR7 LO RETURN IF NOT EIGHTH BYTE L4 PK RETURN *INTERRUPT SERVICE ROUTINE, EXECUTED TO WRITE ONE "BYTE TO CASSETTE. EO El E2 E3 E4 E5 E7 E8 E9 El0 Ell E12 E13 E14 E15 E16 E17 E18 FO ORG LR LR LR LR LR INS BZ LR OUTS LR AI CI BZ LI OUTS Li H'0280' l,A A,IS 2,A A,O IS,A 5 FO A,S 4 A,IS H'FF' 0'37' E17 STOP 4 O'77J LR O,A LR A,2 LR IS,A LR A,l EI POP LI REW OUTS 4 BR E18 SAVE ACCUMULATOR IN SCRATCHPAD BYTE 1 SAVE ISAR IN SCRATCHPAD BYTE 2 LOAD SCRATCHPAD BYTE 0 CONTENTS INTO ISAR RECEIVE STATUS FROM CASSETTE, INS SETS STATUS IF NOT END OF CASSETTE, OUTPUT NEXT BYTE MOVE ISAR TO A DECREMENT ALL 6 BITS OF ADDRESS TEST IF RESULT IS 0'37' RETURN IF NOT IF IT IS, ISSUE A STOP COMMAND RESET TO TOP FOR NEXT OUTPUT SAVE !SAR ADDRESS FOR NEXT BYTE BEFORE RETURNING, RESTORE ACCUMULATOR AND ISAR IF CASSETTE IS FULL, ISSUE REWIND COMMAND I I The logic of this program is relatively simple. Scratchpad bytes 0'77' to 0'40' constitute a 32-byte buffer, the contents of which is output as a record to the cassette. It is assumed that this record can be written to the cassette in less time than an operator takes to enter 16 digits at the keyboard. Therefore instructions START through S7, input 16 digits into the 16 scratchpad bytes addressed by 0'37' through 0'20'. Data is input from the keyboard using programmed 1/0 via subroutine INKB. Notice that subroutine INKB saves its return address in the K scratchpad registers and uses the PK in- struction to return; therefore a stack register is available for the interrupt. Subroutine INKB is almost identical to the input subroutine described in Section 8.1.1. The principle difference is that separate ports are being used for status and data. Observe that throughout this program data is input into scratchpad bytes, one scratchpad 8-byte buffer at a time. Once 16 digits have been input from the keyboard, they are moved from scratchpad bytes 0'37' - 0'20' to 0'77' - 0'60'. This entire data movement will take 208 microseconds which is not a noticeable delay to an operator entering data at the keyboard. The next 16 bytes of data entered at the keyboard go directly into scratchpad bytes 0'57' through 0'50' and 0'47' through 0'40'. After 32 bytes have been entered into the scratchpad buffer, a buffer counter is initialized in scratchpad byte 0 (instructions 21 et. seq.); then the cassette is turned on by instructions S23 and S24. ONC is used as a symbol representing the one byte code which will be recognized by the cassette control logic as a turn-on signal. Once the cassette has been turned on, program logic branches back to the start of data entry for the next record. Notice that nowhere in the main program has the interrupt service routine been mentioned. It is assumed that once the cassette has been turned on, cassette control logic will issue an interrupt request signal each time it is ready to receive another byte of data from the microprocessor. The interrupt service routine therefore may be executed at any time. It is as though there were a floating call to a subroutine that could randomly be executed at any point in the program where interrupts were being allowed. Observe that the interrupt service routine has to save the contents of the accumulator and the ISAR in scratchpad bytes because the accumulator and ISAR are going to be needed. The illustrated interrupt service routine is probably somewhat Simpler than most real interrupt service routines would be. Control logic associated with the cassette drive is assumed capable of sending status inputs to the microprocessor telling the microprocessor when to rewind the cassette. It is also assumed that housekeeping associated with the start and end of each record is handled by the cassette control logic. In all probability much of this housekeeping could be done by the microprocessor, but to include it in the example would detract from the purpose of the example, which is to show how an interrupt service routine is handled. The origins of the main program and interrupt service routine have been randomly selected. Note that since the origin of the interrupt service routine has been selected at H'0280', 8-7 this is the address which must be in the 3851 interrupt address register. The symbols STOP and REW in the interrupt service routine must be equated to the actual bit pattern that the cassette controller logic will interpret as stop and rewind commands, respectively. 8.3 LOCAL TIMERS (PROGRAMMABLE TIMERS) Programmable timers are a more useful microprocessor programming tool than is initially apparent to a programmer. Programmable timers are shift registers which, after being loaded with some initial value, count down to 0, then send an interrupt request signal to the CPU. (See Section 2.5.4.) The 3851 PSU and the 3853 SMI device both have program- mable timers. Here are some applications for which timers are useful: 1) In control applications, such as an operations monitor alarm, to insure that some maximum time interval is not exceeded between consecutive readings from sensitive data inputs. For example, suppose a tem- perature must be measured in a chemical reactor at least once every second to prevent runaway condi- tions. 253 maximum time intervals ·on a local timer approximate 1 second. Whenever a temperature is input, the local timer is reset to start counting tlown one second. If one second is counted down, the pro- gram can be written to output a signal that triggers an audible alarm. 2) To activate refresh logic for external devices. For ex- ample, a video display may need to be refreshed at fixed time intervals; the refresh sequence may be initiated by a local timer. 3) To maintain the real time of day in any system that has to generate clock times. Such devices include badge readers and numerous small office business systems. 8.3.1 Local Timer I/O Ports Local timer logic uses the local interrupt control 1/0 ports to enable local timer interrupts, as described in Section 8.2.2 and Table 8-1. The interrupt control 1/0 port must have the value H'03'loaded into it under program control in order to enable local timer interrupts at that one device. Therefore either external inter- rupts or local timer interrupts, but not both, may be enabled at one device. If interrupts have been disabled at the 3850 CPU, local timer interrupt requests will be ignored until a subsequent inter- rupt enable. At this time any interrupt request will still be active unless cleared prior to the interrupt enable. The timer 1/0 ports have 1/0 port addresses one higher than the local interrupt control 1/0 port. Therefore 3851 PSU port addresses are: B'xxxxxx10' for the local interruptcontrol 1/0 port S'xxxxxx11' for the iocai timer ilO port For the 3853 SMI, port addresses are: H'OE' H'OF' for the local interrupt control 1/0 port for the local timer I/O port 8.3.2 Programming Local Timers Programming a local timer requires the value H'03' to be loaded into the selected device's local interrupt control 1/0 port. A number between 0 and 254, identified as a timer constant, is loaded into the associated local timer 1/0 port. A value of 255 loaded into the local timer 1/0 port stops the clock. The value loaded into a local timer, as a timer constant, is converted (by the assembler) to a binary value, as given in Appendix C; that is why numbers should be entered as timer constants. A local timer interrupt will be generated after the time interval given by the product: (system clock pulse interval) * (local timer constant) * 31 For example, a value of T'200' loaded into a local timer 1/0 port will generate an interrupt after 3.1 ms if the system clock pulse interval is 500 ns. Instructions needed to enable a local timer are as follows: LI bUTS LIS OUTS EI T'200' 7 3 6 LOAD TIMER CONSTANT OUTPUTTO TIMER 1/0 PORT 7 LOAD TIMER INITIATION CONTROL OUTPUT TO CONTROL 1/0 PORT 6 ENABLE INTERRUPTS AT THE 3850 CPU !n the above eX3mp!e, the timer ccnst3nt T'200' has been arbitrarily selected. Any value from T'O' to T'256' could be used. T'256', remember, will stop the clock. The selection of 1/0 ports 7 and 6 is also arbitrary; any pair of 1/0 ports with addresses given in Section 8.3.1 could be used. Note, however, that the control 1/0 port number is always one less than the timer port number it controls. The value H'03' must be loaded into a local timer control 1/0 port if the associated timer port is to operate. When this value is loaded into the control 1/0 port any pending timer interrupt is cleared. Any subsequent zero value of the timer will set the timer interrupt. if the vaiue H'03' is in the controi i/O port before the timer constant is output to the timer 1/0 port, then the timer which is constantly running may interrupt before being set with a timer constant. Once the timer 1/0 port holds a zero value, an interrupt request signal will be generated once every 3.953 ms (for a 500 ns clock pulse). Providing the ICB bit is 1 within the 3850 CPU, every timer interrupt request will be acknowledged and serviced if the timer interrupt is enabled. The program that is executed after atimer interrupt is acknowl- edged is a service routine which, like the service routine il- lustrated in Section 8.2.7, is never called or referenced by any other program. The service routine must start executing at the memory address provided by the 3851 or 3853 device's interrupt address 1/0 ports; however, recall that the 7 bit of the address is automatically set to 0 for a timer interrupt, or to 1 for an external interrupt. If the external interrupt service routine is origined at H'0680', as illustrated in Section 8.2.7, then for the same device, the local timer interrupt service routine will be origined at H'0600'. 8.3.3 A Programming Example - The Tim.e of Day The program below creates the time of day by storing hours in scratchpad byte 8, minutes in scratchpad byte 7 and sec- onds in scratchpad byte 6. Scratchpad byte 5 is used as a counter. This program uses the maximum timer interval (3.953 ms between interrupts). The local timer must be initialized with the main program as follows: LIS LR LR LR LI LR LI OUTS LIS OUTS EI o 8,A 7,A 6,A 253 5,A T'O' 7 H'03' 6 ZERO HOURS, MINUTES AND SECONDS PORTS, ASSUM- ING THE DEVICE WILL BE SWITCHED ON EXACTLY AT MIDNIGHT INITIALIZE THE LOCAL COUNTER TO 253 CLEAR LOCAL TIMER PORT ENABLE THE LOCAL TIMER PORT INTERRUPTS ENABLE INTERRUPTS AT THE CPU The local timer interrupt service routine is assumed to be origined at H'0200'. It executes as follows: ORG H'Q?OO' OS 5 DECREMENT THE LOCAL COUNTER BNZ OUT CONTiNUE iF iT is NOT ZERO (ONE SEC). LI 253 IF IT IS ZERO, RESET TO 253 LR 5,A LR A,6 INCREMENT THE SECONDS COUNTER INC CI 60 TEST IF SECONDS EQUAL 60 I I OUT BZ LR EI POP T10 6,A IF THEY DO, ADJUST MINUTES IF THEY DO NOT, END *MINUTES ADJUST BEGINS HERE TlO US ° ZERO SECONDS LR 6,A LR A,7 INC CI BZ LR EI POP 60 T20 7,A LOAD MINUTES INCREMENT MINUTES TEST FOR 60 MINUTES AT 60 MINUTES, ADJUST HOURS OTHERWISE RETURN MINUTES *HOURS ADJUST BEGINS HERE T20 LIS ° ZERO MINUTES LR 7,A LI LR LR INC CI BNZ LIS T30 LR EI POP 153 CORRECT 0.392 SECOND ERROR EVERY HOUR 5,A A8 LOAD HOURS INCREMENT HOURS 24 TEST FOR 24 HOURS no AT 24 HOURS, RESET TO ° ° OTHERWISE RETURN HOURS 8,A 8.4 DIRECT MEMORY ACCESS Direct memory access (DMA) allows data to be transferred between any F8 microprocessor system memory and an ex- ternal device, bypassing the 3850 CPU. Data is transferred in parallel with any CPU operations. DMA has been described, as a concept, in Sections 2.6.3 and 2.8. One 3852 DMI device must be present in a microprocessor system that supports DMA. Up to four 3854 DMA devices may be present in the system; each 3854 DMA device provides one DMA channel. 8.4.1 When to Use DMA DMA is used to transfer data into, or out of, a microprocessor system that has heavy 1/0 requirements. For example, using programmed 1/0, the theoretically maximum data transfer rate is implemented by the following instruction sequence for data input: LOOP INS ST DS BNZ ° LOOP INPUT A DATA BYTE VIA PORT ° STORE IN RAM MEMORY TEST FOR END OF TRANS- MISSION RETURN FOR NEXT CHAR- ACTER Scratchpad register 1 is assumed to hold the initial character count. These four instructions execute in 9.5 instruction cycles, equal to 19 /.1S, using a 500 ns clock pulse. Assuming that external logic is synchronized to input one byte of data every 19 /.1S. the maximum data transfer rate is approximately 50,000 byteslsecond. 8-9 The maximum data transfer rate supported by programmed I/O is not of itself a limiting factor. A 256 byte buffer, for example, can be transferred in 4.86 ms. The problem is that this maximum data transfer rate requires external logic that processes data at a rate of one byte every 19 J.lS. Most ap- plications will not meet this requirement, usually because data transfer rates are set by logic considerations beyond the microprocessor system; that is, external logic determines data transfer rates, not the microprocessor system. Suppose external logic is inputting data to the microprocessor system at some rate, which we will label R bytes/second. The time that elapses between each byte transferred will be (l,OOO,OOO/R) J.lS. The local timer can be used to generate an interrupt shortly before each byte of data is due, in which case the local timer interrupt service routine will input the data byte. Assuming that data will always be in the 1/0 port before the local timer interrupt service routine is executed, the following service routine will input data bytes from an 1/0 port: ISRI LR O,A SAVE ACCUMULATOR CONTENTS IN ° XDC SWITCH DCO AND DCl LI TCNT RESTART TIMER OUTS 7 INS ° INPUT DATA BYTE ST SAVE IN MEMORY XDC SWITCH DCO AND DCl LR A,O RESTORE ACCUMULATOR FROM ° EI ENABLE INTERRUPTS POP RETURN TCNT is a symbol defined by the equate directive: TNCT EQU T'VAL' where VAL is a number between °and 255. Each count represents 31 clock periods and the total time is equal to (l,OOO,OOO/R) but less than 3.953 ms. It will take approximately 38 J.lS for interrupt service routine ISRI to execute; this means that approximately 9.7 ms will be required to input 256 bytes of data. This 9.7 ms will be spread over whatever time interval the external device re- quires to transfer 256 bytes of data. But there are some problems associated with the method of inputting data: 1) Recall that there are certain privileged instructions which inhibit acknowledgement of an interrupt. It is quite feasible for a 2 to 4 J.lS delay to randor;lly get inserted between each execution of ISRI if, by chance, a privileged instruction is being executed at the instant the local timer times out. Over 256 bytes of data trans- fer, this means that it is feasible for a 500 J.lS slew to develop, which will result in the loss of a byte of data, if the data transfer rate exceeds 2,000 bytes/so 2) If the microprocessor is handling interrupts other than the local timer, clearly other interrupts must be serviced by routines which are themselves interrupt- able, since one interrupt service routine blocking out ISRI for any significant period of time would almost c~rtainly create irrecoverable timing errors. 3) Observe that ISRI uses the DCl register and uses one scratchpad register to store accumulator contents. This means that the DCl register and the scratchpad regis!er cannot be used by any other program that is being executed during the same time period. If subroutine ISRI is expanded to include a status test plus logic to compute the timer constant that will compensate for timing slews, the new expanded version of ISRI might easily take 200 J.LS to execute. Under these circumstances the microprocessor system would spend a significant amount of its time merely moving data between memory and an I/O port. In all but the simplest I/O transfer applications, therefore, DMA becomes the preferable way of moving data between memory and external devices. 8.4.2 Programming DMA The actual programming steps required in order to initiate a DMA operation are simple, as follows: LI ADLO LOAD BUFFER STARTING OUT BUFA ADDRESS INTO ADDRESS 110 PORTS LI ADHI OUT BUFB LI CTLO LOAD LOW ORDER BYTE OF BYTE COUNT OUT BUFC LI CTRL LOAD HIGH ORDER 4 BITS OF BYTE COUNT OUT BUFD PLUS CONTROL BITS Symbols must be equated as follows: 1) The I/O port addresses, BUFA. BUFB, BUFC and BUFD 1/0 PORT BUFD 7 6 5 4 3 2 o I I I I I I I I I are given in Table 2-2 for the four 3854 DMA devices that may be present in an F8 microprocessor system. Whether a DMA device uses the first, second, third or fourth set of addresses is a function of device hard- ware configuration and of no concern to the program- mer, so long as the correct port addresses are used. 2) ADLO and ADHI represent the low order and high order bytes of the beginning address of the memory buffer into which data will be written, or from which data will be read. 3) Data buffers may be up to 4,096 bytes long. CTLO represents the low order eight bits of the buffer length, as illustrated in Figure 8-4. CTRL provides the controls which select DMA options and also the high order four bits of the buffer length, as illustrated in Figure 8-4. The following instructions will initiate 256 bytes of data being written into a memory buffer, where the data rate is con- troiled by the externai device. The memory buffer starting address is H'A280'. The first DMA channel is used. LI H'80' OUT H'FO' LI H'A2' OUT H'Fl ' LI H'OO' OUT H'F2' LI H'Cl' OUT H'F3' 1/0 PORT BUFC 7 6 5 4 3 2 OUTPUT LOW ORDER BYTE OF ADDRESS OUTPUT HIGH ORDER BYTE OF ADDRESS OUTPUT LOW ORDER BYTE OF COUNT OUTPUT HIGH ORDER 4 DIGITS OF COUNT (1) AND CONTROL DIGIT IC). o Bit No I I I I I I I I I tl III \..'-----'./ Buffer Length '----- 0 External device c6ntrols data transfer rate. \ I I A byte of data will be transferred every available I I 11....-____ 0 ~~:t~~O~sfer halts when the byte count register II 1,_______ decrements to 0 1 Data transfer continues until bit 7 is reset to O. o Data is transf",rre<1 from memory to an external device ~ Data is transferred from an external device to main memory ---- 0 Ha!! D~.~.~. opere!!0!: Start DMA operation Fig. 8-4. How BUFC and BUFD are used to Control DMA Operations I I 8.4.3 Catching DMA on the Fly There are many applications in which data will be transferred via DMA at unpredictable rates. For example, in communi- cations applications, data may come over a telephone line at a fixed baud rate, but the length of messages and the period when no data is being transferred may be completely random. Under such circumstances it is very useful if a program can start and stop DMA operations or interrogatethe buffer counter to find out how much data has been transferred via DMA since the last interrogation. The following program sequence catches DMA on the fly, in a way that would be well suited to random data transfer rates in communications applications: *SUBROUTINE TO INITIALIZE DMA WITH H'FF' IN THE BYTE *COUNTER. THE DATA BUFFER STARTS AT H'2000' DMA LI H'oo' OUTPUT BUFFER STARTING ADDRESS S2 OUT LI OUT LI OUT LI OUT POP H'FO' H'20' H'F1 ' H'FF' H'F2' H'CO' H'F3' OUTPUT BYTE COUNTER "MAIN PROGRAM TO HANDLE COMMUNICATIONS DATA *TRANSFERRED VIA DMA PI DMA INITIALIZE DMA M1 LIS 0 STOP DMA DATA TRANSFER M2 OUT H'F3' M3 IN H'F2' LOAD BYTE COUNT INTO COM SCRATCHPAD BYTE 0 M4 LR O,A (instructions to process data follow here) 8-11 Instruction steps to initiate DMA are pClckaged as a subrou- tine labeled DMA. The buffer length output is H'FF'. As this buffer length is counted down, the number of bytes trans- ferred via DMA can, at any time, be determined by reading the contents of I/O port F2 into the accumulator and com~ plementing. The control digit C starts data flow via DMA from the external device (assumed to be a communications interface) to the memory buffer, beginning at H'2000'. The main program starts by initializing DMA via a call to subroutine DMA. At some later point in the program, in- structions M1 and M2 are executed in order t6 load the code digit 0 into I/O port F3 and thus stop DMA transfers. Instruc- tions M3 through M4 determine the number of bytes that have been transferred via DMA, since DMA was initiated, and loads this byte count into scratchpad register O. Instruc- tions will now follow to move the number of bytes received to some other memory location where the data can be pro- cessed. Subroutine DMA will then be recalled to re-initialize DMA data transfers. After data has been processed execution will branch back to instruction M1 and so the program will continue processing whatever data has been transferred in each time interval. I ·1 I PROGRAM OPTIMIZATION Optimizing a program is not a routine mechanical task; rather, it is a function of application requirements and hardware configuration. Most microprocessor programs are written either to maximize execution speed, or to minimize the amount of memory used. Consider a simple example. A microprocessor has 1024 bytes of program memory. An application may only use half of the available memory, but may be too slow to meet product speci- fications. Converting every subroutine to a macro will speed up program execution time, but may double the size of the program. Since program memory comes in finite increments, economizing on program storage requirements is only mean- ingful when it reduces the number of devices required by a microprocessor system; therefore, increasing program stor- age requirements from 500 bytes to 1000 bytes carries no penalty. In practice, programming for minimum use of program stor- age should be the goal of microprocessor programmers. Microprocessor instruction sets are very versatile. Many variations of a program can be written to implement any problem; but some programs will be more efficient than others. A novice microprocessor programmer may well write programs that occupy 50% more memory than is really neces- sary. Inefficiencies of this type are not important in mini- computer systems, which usually include bulk storage devices such as disk units. The only penalty paid for having unneces- sarily long programs is a few extra milliseconds, making otherwise unnecessary transfers of program segments be- tween disk and memory. Unnecessarily long programs are very uneconomical in microprocessor systems, where the entire program sits in one or more memory devices. If a microprocessor system has two more memory devices than the most compact program would require, these two memory devices can become 20,000 memory devices, if the micro- processor system is to be reproduced 10,000 times. In many ways, the logic designer will find it easier to be- come an efficient microprocessor programmer than will a systems analyst. who has gained experience programming minicomputers and larger systems. The systems analyst has continuously striven to write programs which are general purpose. For example, a subroutine that performs multibyte addition must be able to add two number buffers of any length, located anywhere in memory, storing the result in a third number buffer. Such a multibyte addition subroutine, once written, could be frequently reused in almost any applica- tion, thus reducing future programming expenses. This is economical thinking in the world of minicomputers, but it is very uneconomical thinking in the world of microprocessors. A microprocessor application may be able to define two number buffers of specific length, in specific areas of memory, as the only number buffers which will ever be involved in mathematical operations. A multibyte addition subroutine, working within these restrictions, may have to be rewritten for every new microprocessor application, but the subroutine that results may use less than half of the memory storage requirements demanded by the equivalent g~neral purpose routine. When microprocessor systems are likely to be re- produced tens of thousands of times, extra front-end pro- gramming expense becomes trivial compared to the cost of extra memory devices, multiplied ten thousand fold. 9-1 In the following sub-sections, program optimization informa- tion is presented in the following sequence: 1) The concept of counting memory bytes and execution cycles is described. 2) Some basic techniques that will always make Fa programs more efficient are listed. 3) Some examples of execution speed versus memory utilization tradeoffs are given. 9.1 COUNTING CYCLES AND BYTES The Fa instruction set is summarized in Appendix D, where the number of object program bytes is listed for every instruction. Consider the data movement program described in Figure 5-1. This program is reproduced in Figure 9-1, along with number of execution cycles and memory bytes required by each instruction. Counting bytes is usually unnecessary, since the assembler listing prints the memory location where each object pro- gram byte will be stored. Thus subtracting memory addresses yields the length of any program, program segment or subroutine. 9.2 ELEMENTARY OPTIMIZATION TECHNIQUES There are a number of instruction choices where one selec- tion is always preferable. These obvious instruction choices are described in the following sub-sections. 9.2.1 Scratchpad and RAM Memory Always fill up the scratchpad before using RAM memory to store constants or data buffers. It takes one cycle to move a byte of data between the accumulator and a scratchpad byte; it takes 2.5 cycles to move a byte of data between the accu- mulator and external RAM. Both sets of instructions generate one byte of object code. 9.2.2 Immediate Instructions Immediate instructions are 2 or 3-byte instructions that spec- ify data in the instruction operand. Consider the 2-byte immediate instructions; these instruc- tions specify a 1-byte operand, which is combined with the contents of the accumulator in some way. An instruction such as: 1M LI CNT LOAD COUNTER INTO ACCUMULATOR executes in 2.5 cycles and occupies two bytes of memory. If this instruction occurs identically (with the same operand) many times in a program, consider loading CNT into a scratch- pad register, as follows: ONE TWO THRE LI LR LR CNT 1,A A,1 LOAD COUNTER INTO ACCUMULATOR Cycles Bytes a a TITLE a a MAXC:Pl.I 0 0 SYMBOL a a XREF a a BASE a a BUFA EQU a a BUFB EQU a a ORG 6 3 ONE DCI 2 1 TWO XDC 6 3 THREE DCI 2.5 2 FOUR LI 1 1 FIVE LR 2.5 LOOP LM 2 SIX XDC 2.5 SEVEN ST 2 1 EIGHT XDC 1.5 1 NINE DS 3.5 2 BNZ a a END ill 17 * BNZ will usually return to LOOP Total Bytes = 17 Total Cycles = 31.5 Total Cycles within iterative loop = 14 "SAMPLE PROGRAM TO MOVE DATA BETWEEN BUFFERS" 50 liMIT OF 50 S!:CONDS CPt.! T!ME SPEC!!=!ED A SYMBOL TABLE WILL FOLLOW SOURCE PROGRAM SYMBOLS CROSS LISTING WILL FOLLOW SOURCE PROGRAM HEX HEXADECIMAL NUMBERS SPECIFIED FOR ASSEMBLY LISTING H'0800' SET THE VALUE OF SYMBOL BUFA H'08AO' SET THE VALUE OF SYMBOL BUFB H'Ol00' BUFA SET DCa TO BUFA STARTING ADDRESS STORE IN DCl BUFB SET DCa TO BUFB STARTING ADDRESS H'80' LOAD BUFFER LENGTH INTO ACCUMULATOR 1,A SAVE BUFFER LENGTH IN SCRATCHPAD BYTE 1 LOAD CONTENTS OF MEMORY BYTE ADDRESSED BY DCa EXCHANGE DCa AND DCl STORE ACCUMULATOR IN MEMORY BYTE ADDRESSED BY DCa EXCHANGE DCa AND DCl DECREMENT SCRATCHPAD BYTE 1 LOOP IF SCRATCHPAD BYTE 1 !S NOT ZERO, RnURN TO LOOP Assuming 2 /-IS cycle time, time to move 128 bytes = 2*(14*128+17.5) =3619/-1s Fig. 9-1. Counting Cycles and Bytes Instructions ONE and TWO execute in 3.5 cycles and occupy three bytes of memory. Instruction THRE executes in one cycle, occupies one byte of memory and replaces instruction 1M. Clearly instruction 1M is better than ONE, TWO and THRE, if 1M occurs just once; however, if instruction 1M occurs iden- tically n times, then it accumulates 2.5n cycles and 2n bytes of memory, whereas ONE, TWO and THRE accumulate (3.5+n) cycles and (3+n) bytes of memory, respectively. Therefore ONE, TWO and THRE will execute faster when: 2.5n > 3.5 + n or 1.5n > 3.5 or n > 2.33 ONE, TWO and THRE occupy less memory when: 2n > (3 + n) or n> 3 In conclusion, if a 2-byte immediate instruction occurs iden- tically (same operand) three or more times in a program, it is more efficient to load the immed!ate opera!""!d !!""!!c ascra!ch- pad byte out of which it is referenced (providing a scratchpad byte is available). 9.2.3 Short Instructions Always go over a source program, making sure that the short instructions US, INS and OUTS have been used wherever the operand is small enough. 9.2.4 Use of OS Instruction to Decrement and Test Recall that when a OS instruction is used, the decremented scratchpad byte may be tested for '·decrement-from-zero". Since the OS instruction adds H'FF' to the designated scratch- pad byte contents, the carry status will always be set unless the scratchpad byte contained °before it was decremented. Therefore the instruction sequence: OS n BC BACK will decrement scratchpad byte n, return to BACK if byte n did not contain 0, but continue if byte n did contain 0. 9.2.5 Use of the BR7 Instruction The BR7 instruction is very useful whe!""! manipulating data buffers in scratchpad memory, as described in Section 7.1. 9.3 PROGRAMMING FOR SPEED OR MEMORY ECONOMY In the foHo'..I'!!!1g subsections, progr~mm;ng techniques that tradeoff between execution speed and the amount of memory used are described. 9.3.1 Macros and Subroutines To gain execution speed, possibly with a heavy increase in the amount of memory required, convert subroutines into macros as described in Section 7.4. I Always carefully examine subroutines, particularly those which are infrequently called or receive parameters from the calling program, to see if converting the subroutine into a macro would save memory bytes and, at the same time, increase execution speed. As described in Section 7.3, programs can be made much faster and will require less memory if subroutine nesting is limited to a first level. If a main program calls a subroutine, the subroutine can then call another subroutine. However, a subroutine cannot call another subroutine if it was, itself, called by a subroutine. Limiting subroutine nesting to a level of one means that return addresses can be stored in the stack register (PC1) and in the K registers of the scratchpad, eliminating the need for memory stacks. 9.3.2 Table Lookups Versus Data Manipulation Program execution speed can frequently be increased by looking up data out of tables in ROM. The concept is illustrated below, for the simple case of a 1- of-8 decoder. An octal digit is input into the low order three bits of I/O port O. The CPU must output, via I/O port 1, a data byte as follows: Input From Port 0 00000001 00000010 00000011 00000100 00000110 00000111 OOOOOOOO Output At Port 1 00000001 00000010 00000100 00001000 00100000 01000000 10000000 *ONE OF EIGHT DECODER PROGRAM, NOT USING TABLE *LOOKUP 110 LOOP OUT INS BNZ LIS LR LIS DS BZ SL BR OUTS o 110 8 O.A 1 o OUT 1 LOOP 1 INPUT OCTAL CODE INPUT IS NOT ZERO LOOP COUNTER TO LOOP COUNTER LOAD OUTPUT FOR 1 DECREMENT INPUT BRANCH OUT IF END SHIFT LEFT ONE BIT IF NOT END OUTPUT RESULT 9-3 *ONE OF EIGHT DECODER *LOOKUPS PROGRAM USING TABLE LKUP DC 0 DC 2 DC 4 DC 8 DC 16 DC 32 DC 64 DC 128 INS 0 DCI LKUP ADC LM OUTS INPUT OCTAL CODE LOAD TABLE BASE ADDRESS ADD INPUT CODE TO BASE ADDRESS LOAD OUTPUT OUTPUT RESULT Efficiencies compare as follows: Instructions Memory bytes Execution cycles Non-Table Lookup 10 13 min: 15 max: 69.5 Table Lookup 5 15 15 15 I I I SOME USEFUL PROGRAMS Some generally useful programs are given in this section. Programs are not shown as subroutines or as macros. The instructions implementing required logic are given, making it easy to incorporate an example into a program as a sub- routine, a macro or directly as a section of main memory. These programs are intended to showprogramming techniques, rather than to demonstrate optimum program efficiency. 10.1 GENERATING TEXT 10.1.1 Simple and Dedicated Text Programs The simplest text generation logic takes characters out of a memory buffer and outputs them via an 1/0 port. The 1/0 operation may be under program control, or interrupt 1/0 may be used. In each case, text is fetched via an elementary instruction sequence such as: DCI TEXT LOAD TEXT BUFFER STARTING ADDRESS LOOP LM LOAD NEXT TEXT BYTE *TEST FOR END-OF-RECORD CHARACTER. INSTRUCTIONS *FOR THIS TEXTARE NOTSHOWN, SINCETHEYARE A FUNe- *TION OF THE APPLICATION. OUT PRTN BR LOOP OUTPUT CHARACTER VIA PORT N RETURN FOR NEXT CHARACTER 10.1.2 Unpacking Decimal Digits A byte containing two BCD digits is converted into two ASCII digits as follows: LM LR O,A SL 4 LOAD BYTE WITH TWO BCD DIGITS SAVE BYTE IN SCRATCHPAD BYTE 0 SR 4 ISOLATE LOW ORDER DIGIT AS 1 ADD HIGH ORDER FOUR ASCII BITS LR 2,A SAVE IN SCRATCHPAD BYTE 2 LR AO LOAD TWO BCD DIGITS SR 4 ISOLATE HIGH ORDER DIGIT AS 1 ADD HIGH ORDER FOUR BITS *CHARACTER OUTPUT SEQUENCE FOLLOWS HERE This instruction sequence assumes that scratchpad byte 0 is available for temporary storage and that scratchpad byte 1 contains H'30'. Refer to Appendix B. A decimal digit becomes an ASCII character as follows: 7 6 5 4 3 2 1 0 Bit No. 0011XXXX -"--"-L r L Decimal digit, 0000 through 1001 This code identifies an ASCII decimal digit 10-1 If scratchpad byte 0 is not, available, any other byte may be used for data storage. If scratchpad byte 1 is not available, any other scratchpad byte, or the immediate instruction: AI H'30' may be used. 10.1.3 VariableText It is possible to have a text generation program in ROM that outputs variable text, temporarily stored in RAM. In other words, a fixed ROM program outputs messages of variable length and content. This is useful in word processing or human dialog applications. For example, an Fa microprocessor may drive a CRT used to collect data from convention attendees; the text program described below allows the dialog that will be displayed to be changed at any time, without changing the text generation program. The text table (labeled TEXT below) contains characters in any mixed sequence. The index table (labeled TIND below) consists of the following 3-byte sequence: This variable text generation program uses two data tables: a text table and an index table. Bytes 1 and 2 - Displacement from TEXT to first character to be output. If Byte 1 =H'FF', end of mes- sage is indicated. Byte 1 displacement cannot be H'FF'. Byte 3 - Number of characters to be printed. Messages are identified by number, starting at 1. A mes- sage's number is its sequential location, as identified by H'FF' codes in TIND. Consider the following very simple example. The following four messages are to be generated: 1) ENTER PRODUCT NUMBER: 2) NO SUCH PRODUCT RE-ENTER: 3) NUMBER OF UNITS: 4) PRODUCT SHIP DATE: The following TEXT table will be needed: RE-ENTERkSPRODUCT kSNUMBER:kSNOkSSUCH kSOFkSUNITS:SHIPkSD ATE:kS The following TrIO table will be needed: LR 10,A LR OC,H Byte No. Contents L3 LM LOAD NEXT CHARACTER TO (Hexadecimal) (Hexadecimal) BE OUTPUT 0 ~\ Message 1 is 20 PI COUT OUTPUT CHARACTER 1 characters, starting at *ANY OUTPUT CODE MAY REPLACE THE CALL TO SUB- Z 14 ; character 4 "ROUTINE COUT 3 FF End of message 1 DS 2 DECREMENT CHARACTER 4 ~l COUNTER 5 NO SUCH BNZ L3 RETURN FOR MORE 6 08 CHARACTERS 7 ~l XDC AT END OF MESSAGE 8 PRODUCT BR T10 SEGMENT, RESTORE TIND 9 08 ADDRESS TO DCO A ~l OUT END OF PROGRAM. ANY B RE-ENTER OTHER INSTRUCTIONS MAY C FOLLOW HERE 0 FF End of message 2 E ~l F NUMBER 10 06 TINO ORG X'08OO' ORIGIN ARBITRARILY 11 ~l SELECTED 12 OF UNITS DC H'oo' DISPLACEMENT TO HIGH 13 OA BYTE 14 FF End of message 3 DC H'03' DISPLACEMENT TO LOW 15 ~l BYTE 16 PRODUCT DC H'16' NUMBER OF CHARACTERS 17 08 IN THIS SEGM.ENT 18 ~l DC H'FF' END OF MESSAGE 1 19 SHIP DATE: 1A OB 1B FF End of message 4 DC H'OB' The following program assumes that the message number DC H'FF' is in the accumulator. The program generates the specified message. TGEN LR O,A SAVE MESSAGE NUMBER 10.2 MULTIBYTE ADDITION AND IN BYTE 0 SUBTRACTION DCI TIND LOAD TEXT INDEX STARTING ADDRESS 10.2.1 16-Bit. Binary Addition and Subtraction L1 DS 0 DECREMENT MESSAGE COUNTER The following program adds a 16-bit value in scratchpad BZ T10 MESSAGE FOUND bytes 1 (high) and 0 (low) to another 16-bit value in scratch- L2 LM SEEK NEXT H'FF' BYTE IN pad bytes 3 (high) and 2 (low), as follows: TIND COM BNZ L2 BYTE LOADED IS NOT H'FF' BR L1 BYTE LOADED IS H'FF' LR A.O LOAD LOW ORDER T10 LM MESSAGE FOUND. LOAD AUGEND BYTE LR O,A NEXT THREE BYTES OF TIND AS 2 ADD LOW ORDER COM AND SAVE IN SCRATCHPAD ADDEND BYTE BZ OUT BYTES 0, 1 AND 2. TEST LR 2,A SAVE ANSWER LM FIRST BYTE FOR H'FF' LR A,1 LOAD HIGH ORDER SIGNIFYING END OF AUGEND BYTE MESSAGE LNK ADD ANY CARRY LR 1,A BNO A1 IF NO OVERFLOW, LM CONTINUE LR 2,A BR ERROR MAKE ERROR EXIT FOR XDC SAVE T!ND ADDRESS!N DC1 CARRY DCI TEXT LOAD TEXT ADDRESS INTO A1 AS 3 ADD HIGH ORDER DCO ADDEND BYTE LR A,O ADD SCRATCHPAD BYTES LR 3,A SAVE ANSWER ADC 1 AND 0 TO DCO BNO NEXT IF NO OVERFLOW, LR H,DC CONTINUE LR A,10 BR ERROR MAKE ERROR EXIT FOR AS 1 CARRY I To perform 16-bit binary subtraction, the two's complement To perform multibyte decimal subtraction, the routine should of the 16-bit value in scratchpad bytes 1 and 0 is added to be changed as follows: the 16-bit value in H. Instructions required are as follows: LR DC,H MOVE SUBTRAHEND TO DC BUFA EQU H'0838' THE CONTENTS OF BUFA LR AO LOAD LOW ORDER BYTE BUFB EQU H'0920' AND BUFB ARE ADDED. THE OF MINUEND BUFC EQU H'077C' RESllLT IS STORED IN BUFC. COM COMPLEMENT IT CNT H'OA' 10 B','TE BUFFERS ARE ADC ADD TO SUBTRAHEND ASSUMED. LIS ADD 1 TO SUBTRAHEND ADC LR H,DC RESTORE PARTIAL SUM TO H LR A1 LOAD HIGH ORDER BYTE ONE LIS CNT USE SCRATCHPAD OF MINUEND TWO LR O,A REGISTER 0 AS A COUNTER COM COMPLEMENT THREE DCI BUFC SAVE THE ANSWER BUFFER AS 10 ADD HU TO ACCUMULATOR FOUR LR Q,DC STARTING ADDRESS IN Q LR 10,A STORE ANSWER BACK FIVE DCI BUFA SAVE THE SOURCE BUFFER SIX XDC ADDRESSES IN DCO AND DC1 10.2.2 Multibyte Binary or Decimal Addition and Subtraction SEVEN DCI BUFB Subroutine MADD, in any of the forms and variations de- EIGHT LI H'66' LOAD IMMEDIATE H'66' scribed in Section 7, performs multibyte binary addition. LR 2,A AND SAVE FOR LATER USE LIS 1 INITIALLY SET CARRY TO 1 LOOP LR 8,A SCRATCHPAD BYTE 8 USED To perform multibyte binary subtraction make changes as TO SAVE CARRY follows. (Refer to the program version in Section 7.2.2): LM LOAD SUBTRAHEND INTO ACCUMULATOR Replace COM ELEV XDC ADDRESS MINUEND EIGHT COM INITIALLY CLEAR THE AMD ADD MINUEND CARRY BIT LR J,W SAVE STATUS LR J,W AS 8 ADD PRIOR BYTE'S CARRY LOOP LM ASD 2 DECIMAL CORRECT BY LR W,J ADDING H'66' NINE LNK NNTN BNC TWTY+1 TEST IF DECIMAL CORRECT CREATES A CARRY with: TWTY LR J,W IF IT DOES, SAVE CARRY THRT XDC READDRESS AUGEND EIGHT LI H'FF' INITIALLY SET THE CARRY BUFFER INC BIT BY LOADING H'FF' INTO A, THEN INCREMENTING FRTN LR H,DC SAVE AUGEND ADDRESS LR J,W SAVE STATUS TO FORCE IN H TWOS COMPLEMENT FFTN LR DC,Q LOAD ANSWER BUFFER LOOP LM LOAD NEXT BYTE ADDRESS COM COMPLEMENT THE SXTN ST STORE THE ANSWER ACCUMULATOR SVTN LR Q,DC SAVE ANSWER BUFFER LR W,J RESTORE STATUS ADDRESS IN Q NINE LNK ADD CARRY, IF PRESENT EGTN LR DC,H MOVE AUGEND ADDRESS BACK TO H LIS 2 LOAD CARRY FROM AMD NS 9 OR ASD AND WITH SAVED To perform multibyte decimal addition, referring again to the STATUS IN J multibyte addition program as described in Section 7.2.2, SR SAVE IN SCRATCHPAD replace BYTE 1 TWT1 DS 0 DECREMENT COUNTER BNZ LOOP RETURN FOR MORE TWEL AM ADD CORRESPONDING ADDEND BYTE 10.3 MULTIPLICATION There are a number of possible multiplication routines. with: Consider first the binary multiplication of two 8-bit, positive TWEL AI H'66' PRIME AUGEND FOR numbers (in scratchpad bytes 0 and 1) to give a 16-bit prod- DECIMAL ADDITION duct in scratchpad bytes 7 (high) and 6 (low). The following AMD ADD ADDEND DECIMAL program performs the required multiplication: 10-3 *BINARY MULTIPLY SUBROUTINE *SCRATCH REG 1 CONTAINS MULTIPLIER *SCRATCH REG 2 CONTAINS MULTIPLICAND "SCRATCH REGS 6 AND 7 CONTAIN PRODUCT (SR7=MSB) BMPY LIS 8 INITIALIZE COUNTER TO 8 BMP1 BMP2 LR 5,A US 0 LR 6,A LR 7,A LR A6 AS 6 LR 6,A LR A7 LNK AS LR LR AS LR BNC LR AS LR LR LNK LR OS BNZ 7 7,A A1 1 1,A BMP2 A2 6 6,A A7 7,A 5 BMP1 ZERO PRODUCT SHIFT PARTIAL PRODUCT LEFT 1 SHIFT MULTIPLIER LEFT 1, BY ADD IF CARRY IF SET ADD MULTIPLICAND TO PRODUCT ADD MULTIPLICAND TO PRODUCT DECREMENT COUNT NOT FINI, REPEAT The above program occupies 26 bytes and executes in a maxi- mum of 373 MS. Contrast this with the program in Section 9.3.3 which occupies just 12 bytes, but executes in between 20Msand 1800.5 MS. Very fast decimal multiplication can be achieved using table lookups. Consider a 2-digit decimal number in scratchpad byte 0, multiplied by a 2-digit decimal number in scratchpad byte 1, to give a 4-digit answer in scratchpad bytes 7 (high) and 6 (low). The routine uses 100 bytes of ROM, to hold the following table: TABX+OO 01 0203040506070809 OA OB OC 00 OE OF holds: 00 00 00 00 00 00 00 00 00 00 Not Used TABX+10 11 12 13 14 15 16 17 18 19 1A 1B 1C 10 1E 1F holds: 00 01 02 03 04 05 06 07 08 09 Not Used TABX+20 21 2223242526272829 2A 2B 2C 20 2E 2F holds: 00 02 04 06 08 10 12 14 16 18 Not Used TABX+30 31 3233343536373839 3A 3B 3C 3D 3E 3F. holds: 00 03 06 09 12 15 18 21 24 27 Not Used TABX+40 41 4243444546474849 4A 4B 4C 40 4E 4F holds: 00 04 08 12 16 20 24 28 32 36 Not Used TABX+fiO 51 52 53 54 55 56 57 58 59 5,6, 58 5C 5D 5E 5F holds: 00 05 10 15 20 25 30 35 40 45 Not Used TABX+60 61 6263 64656667 68 69 6A 6B 6C 6D 6E 6F holds: 00 06 12 18 24 30 36 42 48 54 Not Used TABX+70 71 72 73 74757677 78 79 7A 7B 7C 7D 7E 7F holds: 00 07 14 21 28 35 42 49 56 63 Not Used IVA TABX+80 81 8283848586878889 8A 8B 8C 80 8E 8F holds: 00 08 16 24 32 40 48 56 64 72 Not Used TABX+90 91 9293949596979899 9A 9B 9C 90 9E 9F holds: 00 09 1827 3645 5463 72 81 Not Used All numbers above are hexadecimal. SupposeTABX is equated to H'2000'; then byte H'2oo8' contains H'OO'; byte H'2024' contains H'08'; byte H'2094' contains H'36'; etc. The table lookup proceeds as follows: LR SL SR LR LR SL AS DCI LR ADC LM LR LR SR LR LR SR SL AS LR ADC . LM LR LR SL AS LR ADC LM LR SL AI ASD 'n ...... LR LR SR LR LNK A,O 4 4 2,A A1 4 2 TABX H,DC 6,A A,O 4 3,A A,1 4 4 3 DC,H 7,A A,1 4 3 DC,H 3,A 4 H'66' 6 1'''1 ..J,vv 6,A A3 4 W,J ISOLATE MULTIPLIER LOW ORDER DIGIT STORE IN BYTE 2 LOAD MULTIPLICAND ISOLATE LOW ORDER DIGIT ADD MULTIPLIER LOW ORDER DIGIT X16 LOAD TABLE BASE ADDRESS SAVE BASE FOR FURTHER USE ADD ACCUMULATOR INDEX LOAD PRODUCT FROM TABLE STORE IN LOW ORDER BYTE OF ANSWER LOAD MULTIPLIER ISOLATE HIGH ORDER DIGIT SAVE IN BYTE 3 LOAD MULTIPLICAND ISOLATE HIGH ORDER DIGIT ADD MULTIPLIER HIGH ORDER DIGIT LOAD TABLE BASE ADDRESS ADD ACCUMULATOR INDEX LOAD PRODUCT FROM TABLE STORE IN HIGH ORDER BYTE OF ANSWER LOAD LOW ORDER DIGIT OF MULTIPLICAND ADD HIGH ORDER DIGIT OF MULTIPLIER OBTAIN PRODUCT SAVE iN BYTE 3 ADD LOW ORDER DIGIT TO HIGH ORDER DIGIT OF BYTE 6 ISOLATE HIGH ORDER DIGIT OF PRODUCT IN LOW ORDER POSITION OF ACCUMULATOR. ADD LINK I I I AI H'66' LR W,J ASD 7 ADD HIGH ORDER BYTE OF LNK OF ACCUMULATOR. ANSWER ADD LINK LR 7,A RESTORE HIGH ORDER BYTE AI H'66' OF ANSWER ASD 7 ADD HIGH ORDER BYTE LR A,1 LOAD HIGH ORDER DIGIT OF ANSWER SR 4 OF MULTIPLICAND LR 7,A RESTORE HIGH ORDER SL 4 BYTE OF ANSWER AS 2 Al - 'c-OW ORDER DIGIT OF MULTIPLIER More compact versions of this program could be written, but LR DC,H OBTAIN PRODUCT they would take longer to execute. ADC LM SAVE IN BYTE 3 10.4 DIVISION LR 3,A Division of positive numbers can be performed by a program SL 4 ADD LOW ORDER DIGIT TO using successive subtraction as follows: AI H'66' ASD 6 HIGH ORDER DIGIT OF 1) Zero the answer BYTE 6 2) Subtract the divisor from the dividend LR J,W 3) Test for a negative result LR 6,A 4) For a positive result, increment the answer and return LR A,3 ISOLATE HIGH ORDER DIGIT to 2 SR 4 OF PRODUCT IN LOW ORDER 5) For a negative result, the division is finished. Add the POSITION divisor to the dividend to obtain remainder. 10-5 I I I APPENDIX A - BINARY NUMBER SYSTEM The binary number system is a system of counting which utilizes the digits 1 and a to represent numeric quantities. The binary digits, referred to as BITs, are arranged in a se- quence of decreasing significance based upon powers of two. Each bit is numbered. By convention, the most significant bit is on the left, and the least significant bit is on the right. For example, consider the binary number: Binary number Bit number Power of base two Significance a 1 1 a a 1 5 4 3 2 1 a 25 24 23 22 2' 20 32 16 8 4 2 1 As in any number system, the quantity represented by a bi- nary number is calculated by multiplying each digit by its significance, then summing products. The binary number example is evaluated as follows: Quantity = 0*25+1 *24+1 *23+0*22+0*2'+1 *20 = a + 16 + 8 + a + a + 1 =25 Binary numbers may be used to represent any real number positive or negative. Non-integer numbers are represented in the same binary format shown above except that the significance of the bits changes. To indicate the correct interpretation of a binary number, a "binary point" (which is analogous to a decimal point in the decimal number system) is inserted. Consider the binary number below: Binary number Bit number Power of base two Significance a 1 6 5 1 a a 1 1 4 3 2 1 a 25 24 23 22 2' 20 2-' 32 16 8 4 2 1 V2 The number is evaluated as follows: Quantity = 0*25+1 *24+1 *23+0*22+0*2'+1 *20+1 *2-' = a + 16 + 8 + a + a + a + .5 =25.5 The bits of a binary number may be grouped in fours and trans- posed into the hexadecimal number system which includes the following digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. The following example illustrates the procedure: 10011100 Binary number 10011100 -.--- 9 A Hexadecimal number In this manual, hexadecimal numbers are written within quo- tation marks and preceded by an H. Consider the example: H'27', H'AE1 0', H'F' A-l The octal number system includes the following digits: 0, 1, 2, 3, 4, 5, 6, 7. Binary numbers are transposed into octal numbers by arrang- ing the bits into groups of three as illustrated below: 101001 Binary number, 101 001 -.- -.- 5 Octal number The Indirect Scratchpad Address Register (ISAR) uses two octal digits to address 64 scratchpad registers. Octal numbers are written within quotation marks preceded by an 0 as follows: 0'27', 0'3', 0'3270' Table A-1 illustrates the relationship between binary, decimal. hexadecimal and octal numbers. BINARY DECIMAL HEXADECIMAL OCTAL 0000 a a a a a a 1 1 1 1 a a 1 a 2 2 2 a a 1 1 3 3 3 a 1 a a 4 4 4 a 1 a 1 5 5 5 a 1 1 a 6 6 6 a 1 1 1 7 7 7 1 000 8 8 10 1 a a 1 9 9 11 1 a 1 a 10 A 12 1 011 11 B 13 1 1 a a 12 C 14 1 1 a 1 13 D 15 1 1 1 a 14 E 16 1 1 1 1 15 F 17 Table A-1. Binary, Decimal, Hexadecimal and Octal Numbers THE BYTE The Fairchild F8 microprocessor is an 8-bit device, which means that data is handled in eight binary digit (or one byte) units. An 8-bit byte may represent 256 (28) possible permu- tations of eight digits. When referencing the 8-bit byte, this manual has established the following conventions. The bits are numbered from right to left with numbers a through 7. The most significant bit is on the left; the least significant bit is on the right. Bit Number 7 6 543 2 1 a I I I I I I I I I t t Most significant bit Least significant bit An 8-bit byte may represent an instruction object code, an ASCII code or a data word. An 8-bit data word may be interpreted as a signed binary number with a value of from 127 to -128 as illustrated in Tabl~ A-2 It will become clear after reading the sections which follow on binary arithmetic, that the signed binary number system is a natural fallout of two's complement subtraction. BINARY DECIMAL HEXADECIMAL 10000000 -128 80 10000001 -127 81 10000010 -126 82 - - - - - - 11111110 -2 FE 11111111 -1 FF OOOOOOOO 0 0 00000001 1 1 00000010 2 2 - - - - - - 01111101 +125 70 01111110 +126 7E 01111111 +127 7F Table A-2. Signed Binary Numeric Interpretations Binary Number Addition Addition of binary numbers is accomplished by following three rules. 1) 1 bit + 1 bit 0 bit + a carry bit to the next significant bit 2) bit + 0 bit ,- bit 3) 0 bit + 0 bit 0 bit Consider the addition of two positive 8-bit binary numbers: Bit Number H'93' 7 6 5 432 1 0 100 1 001 1 "'H'AS' 1 0 0 0 0 0 H'3B' [!] 0 0 1 1 1 0 1 1 • Carry Bit The carry bit is set to indicate that the results of the addition cannot be represented in the existing 8-bits. However, if the carry bit represents the next higher significant bit. the results are valid. In a multiple byte addition, the carry bit from the most signif- icant bit position of a byte is added to the least significant A,-2 bit of the next (higher order) byte as follows: H'13E2' +H'4747' H'5B29' 000 1 001 1 o 1 000 1 1 1 o 1 0 1 1 0 1 ~/1 o"fo--T'~r-o~lT Binary Number Subtraction 11100010 o 1 000 1 1 1 0010100 1 Subtracting a binary number is the same as adding the two'~ complement of the number. The two's complement of a number is generated by comple- menting the number (replacing 0 with 1 and 1 with 0) and adding one to the complement. Here is an example: H'3C' 0 0 1 1 1 1 0 0 one's complement 1 1 0 0 0 0 1 1 1 two'scomplement 0 0 0 1 0 0 Observe that negative numbers in Table A-2 are the two's complement of their positive equivalents. In this fashion, an 8-bit number can contain sign and value information for numbers between 128 and -127. When adding signed binary numbers, care must be taken to indicate when the result exceeds the boundaries of the two's complement notation. " To exemplify the need for such indicators, consider some simple examples using the set Of 3-bit signed binarynumbers from 3 to -4. Signed Binary Numbers 011 010 001 000 111 110 101 100 Decimal 3 2 1 o -1 -2 -3 -4 Any number greater than 3 or less than -4 is outside the boundaries of the set of 3-bit signed binary numbers. -fhe addition of two numbers within this set may result in a number which is not defined as part of the set. Consider the addition of two numbers with like signs: 1) Bit No. 210 2) Bit No. 210 2 010 3 011 + 1 001 + 1 001 3 01T 4 /100 no carry carry 3) Bit No. 210 4) Bit No. 210 -3 101 -3 101 -1 111 -2 110 =4 1~0 -5 1-..011 two carries carry In example 1, no carry out of the two high order bits occured. The result is defined and valid. I I I , In example 2, carry out from the bit which precedes the sign bit (carry out from bit 2) occurred. The result is undefined and therefore invalid. In example 3, a carry from bit 2 and 3 occurred. The result is defined and valid. In example 4, a carry from the sign bit occurred. The result is undefined and invalid. The explanation of the four examples illustrates the rules which govern the error indication mechanism in the Fairchild F8 microprocessor. If the addition of two 8-bit numbers causes a result which is outside the boundary defined for 8-bit signed binary numbers, (illustrated in Table A-1), an overflow status bit is set. The overflow status bit is defined as the EXCLUSIVE-OR ofthe carry out of bit 6 and the carry out of bit 7. (EXCLUSIVE-OR is defined later in this appendix.) Consider binary number subtraction, (the addition of a binary number to a two's complement number). 1) Bit No. 7 6 543 2 1 a H'52' a 1 a 1 a a 1 a two's complement 1010111 a H'34' a a 1 1 a 1 a a -H'52' 1 a 1 a 1 1 1 a -H'18' @] 1 1 1 a a a 1 a two's complement H'18' a a a 1 1 a a a 2) Bit No. 7 6 543 2 1 a H'2A' a a 1 a 1 a 1 a two's complement 1101011 a H'B6' a 1 1 a 1 a -H'2A' 1 a 1 a 1 1 a H'8A' IT] a a a 1 1 a a In example 1, the subtrahend is larger than the minuend, indicating a negative answer. In unsigned binary number arithmetic, a negative result is indicated by no carry out of the most significant bit and is in two's complement form. There is no overflow because there is no carry out of either bit 6 or bit 7. In example 2, the subtrahend is smaller than the minuend indicating a positive answer. In unsigned binary arithmetic, a positive result is indicated by a carry from the most signif- icant bit pOSition and is in straight binary form. There is no overflow because there is a carry out of both bit 6 and bit 7. Multiplication of binary numbers may be performed in two ways: repetitive addition or in the fashion illustrated below, which is similar to the long hand method for multiplying decimal numbers: Decimal 91 x 5 455 Binary a 1 a 1 1 1 a 1 1 a 1 1 a 1 1 0000000 a 1 1 a 1 1 11000111 A-3 Division of binary numbers may be accomplished by repetitive subtraction of one operand from another, or by an operation similar to long hand division: COMPUTER LOGIC 7 3~ 111 11) 10101 11 100 11 11 11 --0 Assembly language instructions exist which perform logical operations on operands. Three such logical operations are described below (logical-OR, AND, and EXCLUSIVE-OR). The logical-OR operation is illustrated for the two operands I and J with the statement: If I or J equals 1, then the result is 1. Otherwise, the result is zero. The symbol used to indicate the logical-OR operation is the sign (V). Consider the logical-OR of two binary numbers: A V B =C (read A "or" B equals C) A 11010 B 01100 C 11110 The logical AND operation is illustrated for the two operands I and J with the following statement: If both I and J are 1, then the result is 1. Otherwise, the result is zero. The symbol used for the logical AND operation is (A). Consider the logical AND of two binary numbers: A A B = C (read A "and" B equals C) A 11010 B 01100 C 01000 The logical EXCLUSIVE-OR operation is illustrated for the op- erands I and J with the following statement: If both I and J equal 1 or both I and J equal 0, the result is zero, otherwise the result is 1. The symbol used to indicate the logical EXCLUSIVE-OR operation is a circled sign ( CD ). Consider the logical EXCLUSIVE-OR of two binary numbers: A CD B = C (read A "EXCLUSIVE-OR" with B equals C) A 11010 B 01100 C 10110 I APPENDIX B - ASCII CODES GRAPHIC OR ASCII GRAPHIC OR ASCII GRAPHIC OR ASCII CONTROL (HEXADECIMAL) CONTROL (HEXADECIMAL) CONTROL (HEXADECIMAL) NULL 00 ACK 7C 1 31 SaM 01 Alt. Mode 7D 2 32 EOA 02 Rubout 7F 3 33 EOM 03 21 4 34 EaT 04 22 5 35 WRU 05 # 23 6 36 RU 06 $ 24 7 37 BELL 07 % 25 8 38 FE 08 & 26 9 39 H. Tab 09 27 A 41 Line Feed OA 28 B 42 v. Tab OB 29 C 43 Form OC 2A D 44 Return OD + 2B E 45 SO OE 2C F 46 SI OF 2D G 47 DCa 10 2E H 48 X-On 11 / 2F I 49 Tape Aux. On 12 3A J 4A X-Off 13 3B K 4B Tape Aux. Off 14 < 3C L 4C Error 15 3D M 4D Sync 16 > 3F N 4E LEM 17 3F a 4F SO 18 [ 5B p 50 S1 19 "- 5C Q 51 S2 1A ] 5D R 52 S3 1B t 5E S 53 S4 1C 5F T 54 S5 1D @ 40 U 55 S6 1E blank 20 V 56 S7 1F 0 30 W 57 X 58 Y 59 Z 5A 8-1 I APPENDIX C - CONVERSION TABLESjTlMER COUNTS POWERS 0 F TWO 1 0 1.0 2 1 0.5 4 2 0.25 8 3 0.125 16 4 0.062 5 32 5 0.031 25 64 6 0.015 625 128 7 0.007 812 5 256 8 0.003 906 25 512 9 0.001 953 125 1 024 10 0.000 976 562 5 2 048 11 0.000 488 281 25 4 096 12 0.000 244 140 625 8 192 13 0.000 122 070 312 5 16 384 14 0.000 061 035 156 25 32 768 15 0.000 030 517 578 125 65 536 16 0.000 015 258 789 062 5 131 072 17 0.000 007 629 394 531 25 262 144 18 0.000 003 814 697 265 625 524 288 19 0.000 001 907 348 632 812 1 048 576 20 0.000 000 953 674 316 406 25 2 097 152 21 0.000 000 476 837 158 203 125 4 194 304 22 0.000 000 238 418 579 101 562 5 8 388 608 23 0.000 000 119 209 289 550 781 25 16 777 216 24 0.000 000 059 604 644 775 390 625 33 554 432 25 0.000 000 029 802 322 387 695 312 5 67 108 864 26 0.000 000 014 901 161 193 847 656 25 134 217 728 27 0.000 000 007 450 580 596 923 828 125 268 435 456 28 0.000 000 003 725 290 298 461 914 062 536 870 912 29 0.000 000001 862 645 149 230 957 031 25 1 073 741 824 30 0.000 000 000 931 322 574 615 478 515 625 2 147 483 648 31 0.000 000 000 465 661 287 307 739 257 812 5 4 294 967 296 32 0.000 000 000 232 830 643 653 869 628 906 25 8 589 934 592 33 0.000 000 000 116 415 321 826 934 814 453 125 17 179 869 184 34 0.000 000 000 058 207 660 913 467 407 226 562 5 34 359 738 368 35 0.000 000 000 029 103 830 456 733 703 613 281 25 68 719 476 736 36 0.000 000 000 014 551 915 228 366 851 806 640 625 137 438 953 472 37 0.000 000 000 007 275 957 614 183 425 903 320 312 5 274 877 906 944 38 0.000 000 000 003 637 978 807 091 712 951 660 156 25 549 755 813 888 39 0.000 000 000 001 818 989 403 545 856 475 830 078 125 1 099 511 627 776 40 0.000 000 000 000 909 494 701 772 928 237 915 039 062 5 2 199 023 255 552 41 0.000 000 000 000 454 747 350 886 464 118 957 519 531 25 4 398 046 511 104 42 0.000 000 000 000 227 373 675 443 232 059 478 759 765 625 8 796 093 022 208 43 0.000 000 000 000 113 686 837 721 616 029 739 379 882 812 5 17 592 186 044 416 44 0.000 000 000 000 056 843 418 860 8D8 014 869 689 941 406 25 35 184 372 088 832 45 0.000 000 000 000 028 421 709 430 404 007 434 844 970 703 125 70 368 744 177 664 46 0.000 000 000 000 014 210 854 715 202 003 717 422 485 351 562 5 140 737 488 355 328 47 0.000 000 000 000 007 105 427 357 601 001 858 711 242 675 781 25 281 474 976 710 656 48 0.000 000 000 000 003 552 713 678 800 500 929 355 621 337 890 625 562 949 953 421 31 2 49 0.000 000 000 000 001 776 356 839 400 250 464 677 810 668 945 312 5 1 125 899 906 842 624 50 0.000 000 000 000 000 888 178 419 700 125 232 338 905 334 472 656 25 2 251 799 813 685 248 51 0.000 000 000 000 000 444 089 209 850 062 616 169 452 667 236 328 125 4 503 599 627 370 496 52 0.000 000 000 000 000 222 044 604 925 031 308 084 726 333 618 164 062 5 9 007 199 254 740 992 53 0.000 000 000 000 000 111 022 302 462 515 654 042 363 166 809 082 031 25 18 014 398 509 481 984 54 0.000 000 000 000 000 055 511 151 231 257 827 021 181 583 404 541 015 625 36 028 797 018 963 968 55 0.000 000 000 000 000 027 755 575 615 628 913 510 590 791 702 270 507 812 5 72 057 594 037 927 936 56 0.000 000 000 000 000 013 877 787 807 814 456 755 295 395 851 135 253 906 25 144 115 188 075 855 872 57 0.000 000 000 000 000 006 938 893 903 907 228 377 647 697 925 567 676 950 125 288 230 376 151 711 744 58 0.000 000 000 000 000 003 469 446 951 953 614 188 823 848 962 783 813 476 562 5 576 460 752 303 423 488 59 0.000 000 000 000 000 001 734 723 475 976 807 094 411 924 481 391 906 738 281 25 1 152 921 504 606 846 976 60 0.000 000 000 000 000 000 867 361 737 988 403 547 205 962 240 695 953 369 140 625 2 305 843 009 213 693 952 61 0.000 000 000 000 000 000 433 680 868 994 201 773 602 981 120 347 976 684 570 312 5 4611686018427387904 62 0.000000000000 000 000 216 840 434 49710088680149056017398834228515625 9 223 372 036 854 775 808 63 0.000 000 000 000 000 000 108 420 217 248 550 443 400 745 280 086 994 171 142 578 125 C-l TABLE OF POWERS OF SIXTEEN IO 16 n 1 16 256 4 096 65 536 1 048 576 16 777 216 268 435 456 4 294 967 296 68 719 476 736 099 511 627 776 17 592 186 044 416 281 474 976 710 656 4 503 599 627 370 496 72 057 594 037 927 936 152 921 504 606 846 976 n o 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0.10000 0.62500 0.39062 0.24414 0.15258 0.95367 0.59604 0.37252 0.23283 0.14551 0.90949 0.56843 0.35527 0.22204 0.13877 0.86736 00000 00000 00000 x 10 00000 00000 00000 x 10-1 50000 00000 00000 X 10-2 06250 00000 00000 x 10-3 78906 25000 00000 x 10-4 43164 06250 00000 x 10-6 64477 53906 25000 x 10- 7 90298 46191 40625 x 10-8 06436 53869 62891 x 10-9 91522 83668 51807 x 10-10 47017 72928 23792 x 10-12 41886 08080 14870 x 10-\3 13678 80050 09294 x 10-14 46049 25031 30808 x 10-15 78780 78144 56755 x 10-16 17379 88403 54721 x 10-18 TABLE OF POWERS OF 1016 3 23 163 OEO 8AC7 A 64 o 1 2 3E8 3 2710 4 86AO 5 F 4240 6 98 9680 7 5F5 El00 8 3B9A 2 540B 17 4876 E8 04A5 918 4E72 5AF3 807E 8652 4578 B6B3 2304 107A A4C6 6FCl 508A A764 89E8 CAOO 9 E400 10 E800 11 1000 12 AOOO 13 4000 14 8000 15 0000 16 0000 17 0000 18 0000 19 1.0000 0.1999 0.28F5 0.4189 0.680B 0.A7C5 0.10C6 0.1.AD7 0.2AF3 0.44B8 0.60F3 O.AFEB 0.1197 0.lC25 0.2009 0.480E 0.734A 0.B877 0.1272 0.1083 0000 0000 0000 9999 C28F 9999 5C28 999A F5C3 x 16- 1 374B C6A7 EF9E x 16-2 8BAC 710C B296 X 16-3 AC47 lB47 8423 X 16-4 F7AO B5EO 8037 X 16-4 F29A BCAF 4858 X 16-5 10C4 6118 73BF X 16-6 2FAO 7F67 FFOB 9981 C268 3700 BE7B CA5F AA32 5001 C94F l.-L 9B5A 52CC X 16-7 SEF6 EAOF X 16 8 CB24 AAFF X 16-9 20EA 1119 X 16-9 4976 81C2 X 16-10 4257 9058 6226 36A4 0243 B602 3604 5660 FOAE B449 ABAl AC35 X 16-11 X 16-12 X 16-13 X 16-14 X 16-14 X 16-15 HEXADECIMAL-DECIMAL INTEGER CONVERSION The table below provides for direct conversions between hexadecimal integers in the range O-FFF and decimal integers in the range 0-4095. For conversion of larger integers, the table values may be added to the following figures: Hexadecimal Decimal. Hexadecimal Decimal 01000 4096 20000 131072 02 000 8192 30 000 196608 03 000 12288 40 000 262 144 04000 16384 50 000 327680 05000 20 480 60 000 393216 06 000 24576 70 000 458752 07 000 28672 80 000 524288 08 000 32768 90 000 589824 09 000 36864 AO 000 655360 OAOOO 40 960 BO 000 720 896 OB 000 45 056 CO 000 786432 OCOOO 49152 DO 000 851 968 00 000 53248 EO 000 917504 OE 000 57344 Fa 000 983040 OF 000 61440 100 000 1048576 10 000 65536 200 000 2 097 152 11000 69632 300 000 3 145728 12000 73728 400 000 4194304 13 000 77 824 500 000 5242880 14000 81 920 600000 6291 456 15000 86 016 700000 7340032 16000 90112 800 000 8388608 17000 94208 900000 9437 184 18000 98304 AOO 000 10 485760 19000 102400 BOO 000 11 534336 lAOOO 106496 COO 000 12582912 lB 000 110592 000 000 13631 488 lC 000 114688 EOO 000 14680064 10 000 118784 Faa 000 15728640 1E000 122880 1000 000 16777216 1F000 126976 2000 000 33554432 a 1 2 3 4 5 6 7 8 9 A B C 0 E F 000 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 010 0016 0017 0018 0019 0020 0021 0022 0023 0024 0025 0026 0027 0028 0029 0030 0031 020 0032 0033 0034 0035 0036 0037 0038 0039 0040 0041 0042 0043 0044 0045 0046 0047 030 0048 0049 0050 0051 0052 0053 0054 0055 0056 0057 0058 0059 0060 0061 0062 0063 040 0064 0065 0066 0067 0068 0069 0070 0071 0072 0073 0074 0075 0076 0077 0078 0079 050 0080 0081 0082 0083 0084 0085 0086 0087 0088 0089 0090 0091 0092 0093 0094 0095 060 0096 0097 0098 0099 0100 0101 0102 0103 0104 0105 0106 0107 0108 0109 0110 0111 070 0112 0113 0114 0115 0116 0117 0118 0119 0120 0121 0122 0123 0124 0125 0126 0127 080 0128 0129 0130 0131 0132 0133 0134 0135 0136 0137 0138 0139 0140 0141 0142 0143 090 0144 0145 0146 0147 0148 0149 0150 0151 0152 0153 0154 0155 0156 0157 0158 0159 OAO 0160 0161 0162 0163 0164 0165 0166 0167 0168 0169 0170 0171 0172 0173 0174 0175 OBO 0176 0177 0178 0179 0180 0181 0182 0183 0184 0185 0186 0187 0188 0189 0190 0191 OCO 0192 0193 0194 0195 0196 0197 0198 0199 0200 0201 0202 0203 0204 0205 0206 0207 000 0208 0209 0210 0211 0212 0213 0214 0215 0216 0217 0218 0219 0220 0221 0222 0223 OEO 0224 0225 0226 0227 0228 0229 0230 0231 0232 0233 0234 0235 0236 0237 0238 0239 OFO 0240 0241 0242 0243 0244 0245 0246 0247 0248 0249 0250 0251 0252 0253 0254 0255 C·3 HEXADECIMAL-DECIMAL INTEGER CONVERSION (Cont'd) o 1 2 3 100 0256 0257 0258 0259 110 0272 0273 0274 0275 120 0288 0289 0290 0291 I 130 I 0304 0305 0306 0307 140 0320 0321 0322 0323 150 0336 0337 0338 0339 160 0352 0353 0354 0355 170 0368 0369 0370 0371 180 0384 0385 0386 0387 190 0400 0401 0402 0403 lAO 0416 0417 0418 0419 180 0432 0433 0434 0435 lCO 0448 0449 0450 0451 100 0464 0465 0466 0467 1EO 0480 0481 0482 0483 lFO 0496 0497 0498 0499 200 0512 0513 0514 0515 210 0528 0529 0530 0531 220 0544 0545 0546 0547 230 0560 0561 0562 0563 240 0576 0577 0578 0579 250 0592 0593 0594 0595 260 0608 0609 0610 0611 270 0624 0625 0626 0627 280 0640 0641 0642 0643 290 0656 0657 0658 0659 2AO 0672 0673 0674 0675 1 280 0688 0689 0690 0691 1 2CO 0704 0705 0706 0707 200 0720 0721 0722 0723 2EO 0736 0737 0738 0739 2FO 0752 0753 0754 0755 300 0768 0769 0770 0771 310 0784 0785 0786 0787 320 0800 0301 0802 0803 330 0816 0817 0818 0819 340 0832 0833 0834 0835 /350 / 0848 0849 0850 0851 360 0864 0865 0866 0867 1370 1 0880 0881 0882 0883 1380 1 0896 0897 0898 0899 I;:1 ~~; 0913 0914 09i5 0929 0930 0931 380 0944 0945 0946 0947 3CO 0960 0961 0962 0963 300 0976 0977 0978 0979 3EO 0992 0993 0994 0995 3FO 1008 1009 1010 1011 4 5 6 7 0260 0261 0262 0263 0276 0277 0278 0279 0292 0293 0794 07% ~.- 0308 0309 0310 0311 0324 0325 0326 0327 0340 0341 0342 0343 0356 0357 0358 0359 0372 0373 0374 0375 0388 0389 0390 0391 0404 0405 0406 0407 0420 0421 0422 0423 0436 0437 0438 0439 0452 0453 0454 0455 0468 0469 0470 0471 0484 0485 0486 0487 0500 0501 0502 0503 0516 0517 0518 0519 0532 0533 0534 0535 0548 0549 0550 0551 0564 0565 0566 0567 0580 0581 0582 0583 0596 0597 0598 0599 0612 0613 0614 0615 0628 0629 0630 0631 0644 0645 0646 0647 0660 0661 0662 0663 0676 0677 0678 0679 0692 0693 0694 0695 0708 0709 0710 0711 0724 0725 0726 0727 0740 0741 0742 0743 0756 0757 0758 0759 0772 0773 0774 0775 0788 0789 0790 0791 0804 0805 0806 0807 0820 0821 0822 0823 0836 0837 0838 0839 0852 0853 0854 0855 0868 0869 0870 0871 0884 0885 0886 0887 0900 0901 0902 0903 0916 0917 0918 0919 0932 0933 0934 0935 0948 0949 0950 0951 0964 0965 0966 0967 0980 0981 0982 0983 0996 0997 0998 0999 1012 1013 1014 1015 r_d 8 9 A 8 -- - 0264 0265 0266 0267 0280 0281 0282 0283 0?Q6 n297 0298 0299 0312 0313 0314 0315 0328 0329 0330 0331 0344 0345 0346 0347 0360 0361 0362 0363 0376 0377 0378 0379 0392 0393 0394 0395 0408 0409 0410 0411 0424 0425 0426 0427 0440 0441 0442 0443 0456 0457 0458 0459 0472 0473 0474 0475 0488 0489 0490 0491 0504 0505 0506 0507 0520 0521 0522 0523 0536 0537 0538 0539 0552 0553 0554 0555 0568 0569 0570 0571 0584 0585 0586 0587 0600 0601 0602 0603 0616 0617 0618 0619 0632 0633 0634 0635 0648 0649 0650 0651 0664 0665 0666 0667 0680 0681 0682 0683 0696 0697 0698 0699 0712 0713 0714 0715 0728 0729 0730 0731 0744 0745 0746 0747 0760 0761 0762 0763 0776 0777 0778 0779 0792 0793 0794 0795 0808 0809 0810 0811 0824 0825 0826 0827 0840 0841 0842 0843 0856 0857 0858 0859 0872 0873 0874 0875 0888 0889 0890 0891 0904 0905 0906 0907 0920 0921 0922 0923 0936 0937 0938 0939 0952 0953 0954 0955 0968 0969 0970 0971 0984 0985 0986 0987 1000 1001 1002 1003 1016 1017 1018 1019 C o E F 0268 0269 0270 0271 0284 0285 0286 0287 0300 0316 0331 0348 0364 0380 0396 0412 0428 0444 0460 0476 0492 0508 0524 0540 0556 0572 0588 0604 0620 0636 0652 0668 0684 0700 0716 0732 0748 0764 0780 0796 0812 0828 0844 0860 0876 0892 0908 0924 0940 nncc V;;;J;"JU n')n1 V~Vf 0317 0333 0349 0365 0381 0397 0413 0429 0445 0461 0477 0493 0509 0525 0541 0557 0573 0589 0605 0621 0637 0653 0669 0685 0701 0717 0733 0749 0765 0781 0797 0813 0829 0845 0861 0877 0893 0909 0925 0941 nnl:"l' U::J;.JI 0302 0318 0334 0350 0366 0382 0398 0414 0430 0446 0462 0478 0494 0510 0526 0542 0558 0574 0590 0606 0622 0638 0654 0670 0686 0702 0718 0734 0750 0766 0782 0798 0814 0830 0846 0862 0878 0894 0910 0926 0942 U::KIO I"\~.n~ VJUJ I 0319 0335 0351 0367 0383 0399 0415 0431 0447 0463 0479 0495 0511 0527 0543 0559 0575 0591 0607 0623 0639 0655 0671 0687 0703 0719 0735 0751 0767 0783 0799 0815 0831 0847 0863/ 0879 1 0895 1 0911 0927 1 0943 0959 0972 0973 0974 0975 0988 0989 0990 0991 1004 1005 1006 1007 1020 1021 1022 1023 HEXADECIMAL-DECIMAL INTEGER CONVERSION (Cont'd) 0 1 2 3 4 5 6 7 8 9 A 8 C D E F 400 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 410 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 420 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 430 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 440 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 450 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 460 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 470 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 480 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 490 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 4AO 1184 1H~5 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 480 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 4CO 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 4DO 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 4EO 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 4FO 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 500 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 510 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 520 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 530 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 540 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 550 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 560 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 570 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 580 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 590 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 5AO 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 580 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 5CO 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 5DO 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 5EO 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 5FO 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 600 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 610 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 620 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 630 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 640 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 650 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 660 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 670 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 680 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 690 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 6AO 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 680 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 6CO 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 6DO 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 6EO 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 6FO 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 C-5 I 700 710 720 730 740 750 760 770 780 790 7AO 780 7CO 7DO 7EO 7FO 800 810 820 830 840 850 860 87(1 880 890 8AO 8BO 8CO 8DO 8EO 8FO 900 910 920 930 940 950 960 970 980 990 9AO 980 9CO 9DO 9EO 9FO HEXADECIMAL-DECIMAL INTEGER CONVERSION (Cont'd) o 2 3 1792 1793 1794 1795 1808 1809 1810 1811 1824 1825 1826 1827 I 1842 1843 1840 1841 1856 1857 1858 1859 1872 1873 1874 1875 1888 1889 1890 1891 1904 1905 1906 1907 1920 1921 1922 1923 1936 1937 1938 1939 1952 1953 1954 1955 1968 1969 1970 1971 1984 1985 1986 1987 2000 2001 2002 2003 2016 2017 2018 2019 2032 2033 2034 2035 2048 2049 2050 2051 2064 2065 2066 2067 2080 2081 2082 2083 2096 2097 2098 2099 2112 2113 2114 2115 2128 2129 2130 2131 2144 2145 2146 2147 2160 2161 2162 2163 2176 2177 2178 2179 2192 2193 2194 2195 2208 2209 2210 2211 2224 2225 2226 2227 2240 2241 2242 2243 2256 2257 2258 2259 2272 2273 2274 2275 2288 2289 2290 2291 2304 2305 2306 2307 2320 2321 2322 2323 2336 2337 2338 2339 2352 2353 2354 2355 2368 2369 2370 2371 1 2384 2385 2386 2387 2400 2401 2402 2403 2416 2417 2418 2419 1 2432 2433 2434 2435 1 2448 2449 2450 245j 2464 2465 2466 2467 2480 74Rl 2482 2483 2496 2497 2498 2499 2512 2513 2514 2515 2528 2529 2530 2531 2544 2545 2546 2547 4 5 6 7 1796 1797 1798 1799 1812 1813 1814 1815 1828 1829 1830 lR~l 1844 1845 1846 1847 1860 1861 1862 1863 1876 1877 1878 1879 1892 1893 1894 1895 1908 1909 1910 1911 1924 1925 1926 1927 1940 1941 1942 1943 1956 1957 1958 1959 1972 1973 1974 1975 1988 1989 1990 1991 2004 2005 2006 2007 2020 2021 2022 2023 2036 2037 2038 2039 2052 2053 2054 2055 2068 2069 2070 2071 2084 2085 2086 2087 2100 2101 2102 2103 2116 2117 2118 2119 2132 2133 2134 2135 2148 2149 2150 2151 2164 2165 2166 2167 2180 2181 2182 2183 2196 2197 2198 2199 2212 2213 2214 2215 2228 2229 2230 2231 2244 2245 2246 2247 2260 2261 2262 2263 2276 2277 2278 2279 2292 2293 2294 2295 2308 2309 2310 2311 2324 2325 2326 2327 2340 2341 2342 2343 2356 2357 2358 2359 2372 2373 2374 2375 2388 2389 2390 2391 2404 2405 2406 2407 2420 2421 2422 2423 2436 2437 2438 2439 2452 2453 2454 2455 2468 2469 2470 2471 2484 2485 7486 2487 2500 2501 2502 2503 2516 2517 2518 2519 2532 2533 2534 2535 2548 2549 2550 2551 r:-o 8 9 A 8 1800 1801 1802 1803 1816 1817 1818 1819 1R3? 1B33 1834 1835 ,- - 1848 1849 1850 1851 1864 1865 1866 1867 1880 1881 1882 1883 1896 1897 1898 1899 1912 1913 1914 1915 1928 1929 1930 1931 1944 1945 1946 1947 1960 1961 1962 1963 1976 1977 1978 1979 1992 1993 1994 1995 2008 2009 2010 2011 2024 2025 2026 2027 2040 2041 2042 2043 2056 2057 2058 2059 2072 2073 2074 2075 2088 2089 2090 2091 2104 2105 2106 2107 2120 2121 2122 2123 2136 2137 2138 2139 2152 2153 2154 2155 2168 2169 2170 2171 2184 2185 2186 2187 2200 2201 2202 2203 2216 2217 2218 2219 2232 2233 2234 2235 2248 2249 2250 2251 2264 2265 2266 2267 2280 2281 2282 2283 2296 2297 2298 2299 2312 2313 2314 2315 2328 2329 2330 2331 2344 2345 2346 2347 2360 2361 2362 2363 2376 2377 2378 2379 2392 2393 2394 2395 2408 2409 2410 2411 2424 2425 2471) 2427 2440 2441 2442 2443 2456 2457 2458 2459 2472 2473 2474 2475 2488 2489 2490 " ... n1 L'"+::>I 2504 2505 2506 2507 2520 2521 2522 2523 2536 2537 2538 2539 2552 2553 2554 2555 C D E F 1804 1805 1806 18071 1820 1821 1822 1823 1836 1837 1838 iR1y, -_........ - 1852 1853 1854 1855 1868 1869 1870 1871 1884 1885 1886 1887 1900 1901 1902 1903 1916 1917 1918 1919 1932 1933 1934 1935 1948 1949 1950 1951 1964 1965 1966 1967 1980 1981 1982 1983 1996 1997 1998 1999 2012 2013 2014 2015 2028 2029 2030 2031 2044 2045 2046 2047 2060 2061 2062 2063 2076 2077 2078 2079 2092 2093 2094 2095 2108 2109 2110 2111 2124 2125 2126 2127 2140 2141 2142 2143 2156 2157 2158 2159 2172 2173 2174 2175 2188 2189 2190 2191 2204 2205 2206 2207 2220 2221 2222 2223 2236 2237 2238 2239 2252 2253 2254 2255 2268 2269 2270 2271 2284 2285 2286 2287 2300 2301 2302 2303 2316 2317 2318 2319 2332 2333 2334 2335 2348 2349 2350 2351 2364 2365 2366 2367 2380 2381 2382 2383 2396 2397 2398 2399 2412 2413 2414 2415 2428 2429 2430 2431 2444 2445 2446 2447 2460 2461 2462 2463 2476 2477 2478 2479 2492 2493 2494 249b 2508 2509 2510 2511 2524 2525 2526 2527 2540 2541 2542 2543 2556 2557 2558 2559 HEXADECIMAL-DECIMAL INTEGER CONVERSION (Cont'd) 0 1 2 3 4 5 6 7 8 9 A B C D E F AOO 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 Al0 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 A20 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 A30 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 A40 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 A50 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 A60 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 A70 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 A80 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 A90 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 AAO 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 ABO 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 ACO 2752 2753 2754 2755 2756 2757 2758 2759 2760 4761 2762 2763 2764 2765 2766 2767 ADO 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 AEO 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 AFO 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 BOO 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 Bl0 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 B20 2848 2849 2850 3851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 B30 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 B40 2880 2881 2882 2883 2884 2885 2866 2887 2888 2889 2890 2891 2892 2893 2894 2895 B50 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 B60 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 B70 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 B80 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 B90 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 BAO 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 B80 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 BCO 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 BDO 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 BEO 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 t3FO 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 COO 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 Cl0 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 C20 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 C30 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 C40 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 C50 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 C60 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 C70 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 C80 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 C90 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 CAO 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 CBO 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 CCO 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 CDO 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 CEO 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 CFO 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 C-7 0 DOO 3328 Dl0 3344 D20 3360 , D30 13376 D40 3392 D50 3408 D60 3424 D7e 3440 D80 3456 D90 3472 DAO 3488 DBO 3504 DCO 3520 CCO 3536 OEO 3552 DFO 3568 EOO 3584 El0 3600 E20 3616 E30 3632 E40 3648 E50 3664 E60 3680 E70 3696 E80 3712 E90 3728 EAO 3744 EBO 3760 1 ECO 3776 EDO 3792 EEO 3808 EFO 3824 FOO 3840 FlO 3856 F20 3872 F30 3888 F40 3904 1= 1 ,50 I3920 F60 I 3936 I, F70 13952 F80 13968 F90 3984 FAO 14000 FBO 4016 FCO 4032 FDO 4048 FEO 4064 FFO 4080 HEXADECIMAL-DECIMAL INTEGER CONVERSION (Cont'd) 1 2 3 3329 3330 3331 3345 3346 3347 3361 3362 3363 ""1"\""'''' 3378 3379 .l.ll I 3393 3394 3395 3409 3410 3411 3425 3426 3427 3441 3442 3443 3457 3458 3459 3473 3474 3475 3489 3490 3491 3505 3506 3507 3521 3522 3523 3537 3538 3539 3553 3554 3555 3569 3570 3571 3585 3586 3587 3601 3602 3603 3617 3618 3619 3633 3634 3635 3649 3650 3651 3665 3666 3667 3681 3682 3683 3697 3698 3699 3713 3714 3715 3729 3730 3731 3745 3746 3747 3761 3762 3763 3777 3778 3779 3793 3794 3795 3809 3810 3811 3825 3826 3827 3841 3842 3843 3857 3858 3859 3873 3874 3875 3889 3890 3891 3905 3906 3907 1 392, 3922 3923 3937 3938 3939 3953 3954 3955 3969 3970 3971 3985 3986 3987 4001 4002 4003 4017 4018 4019 4033 4034 4035 4049 4050 4051 4065 4066 4067 4081 4082 4083 4 5 6 7 3332 3333 3334 3335 3348 3349 3350 3351 3364 3365 3366 3367 3380 3381 3382 3383 3396 3397 3398 3399 3412 3413 3414 3415 3428 3429 3430 3431 3444 3445 3446 3447 3460 3461 3462 3463 3476 3477 3478 3479 3492 3493 3494 3495 3508 3509 3510 3511 3524 3525 3526 3527 3540 3541 3542 3543 3556 3557 3558 3559 3572 3573 3574 3575 3588 3589 3590 3591 3604 3605 3606 3607 3620 3621 3622 3623 3636 3637 3638 3639 3652 3653 3654 3655 3668 3669 3670 3671 3684 3685 3686 3687 3700 3701 3702 3703 3716 3717 3718 3719 3732 3733 3734 3735 3748 3749 3750 3751 3764 3765 3766 3767 3780 3781 3782 3783 3796 3797 3798 3799 3812 3813 3814 3815 3828 3829 3830 3831 3844 3845 3846 3847 3860 3861 3862 3863 3876 3877 3878 3879 3892 3893 3894 3895 3908 3909 3910 3911 " ~ ~ .l924 3920 3920 3927 3940 3941 3942 3943 3956 3957 3958 3959 3972 3973 3974 3975 3988 3989 3990 3991 4004 4005 4006 4007 4020 4021 4022 4023 4036 4037 4038 4039 4052 4053 4054 4055 4068 4069 4070 4071 4084 4085 4086 4087 r-R 8 9 A B 3336 3337 3338 3339 3352 3353 3354 3355 3368 3369 3370 3371 3384 3385 3386 3387 3400 3401 3402 3403 3416 3417 3418 3419 3432 3433 3434 3435 3448 3449 3450 3451 3464 3465 3466 3467 3480 3481 3482 3483 3496 3497 3498 3499 3512 3513 3514 3515 3528 3529 3530 3531 3544 3545 3546 3547 3560 3561 3562 3563 3576 3577 3578 3579 3592 3593 3594 3595 3608 3609 3610 3611 3624 3625 3626 3627 3640 3641 3642 3643 3656 3657 3658 3659 3672 3673 3674 3675 3688 3689 3690 3691 3704 3705 3706 3707 3720 3721 3722 3723 3736 3737 3738 3739 3752 3753 3754 3755 3768 3769 3770 3771 3784 3785 3786 3787 3800 3801 3802 3803 3816 3817 3818 3819 3832 3833 3834 3835 3848 3849 3850 3851 3864 3865 3866 3867 3880 3881 3882 3883 3896 3897 3898 3899 3912 3913 3914 3915 3928 3929 3930 3931 3944 3945 3946 3947 3960 3961 3962 3963 3976 3977 3978 3979 3992 3993 3994 3995 4008 4009 4010 4011 4024 4025 4026 4027 4040 4041 4042 4043 4056 4057 4058 4059 4072 4073 4074 4075 4088 4089 4090 4091 C D E 3340 3341 3342 3356 3357 3358 3372 3373 3374 3388 3389 3390 3404 3405 3406 3420 3421 3422 3436 3437 3438 3452 3453 3454 3468 3469 3470 3484 3485 3486 3500 3501 3502 3516 3517 3518 3532 3533 3534 3548 3549 3550 3564 3565 3566 3580 3581 3582 3596 3597 3598 3612 3613 3614 3628 3629 3630 3644 3645 3646 3660 3661 3662 3676 3677 3678 3692 3693 3694 3708 3709 3710 3724 3725 3726 3740 3741 3742 3756 3757 3758 3772 3773 3774 3788 3789 3790 3804 3805 3806 3820 3821 3822 3836 3837 3838 3852 3853 3854 3868 3869 3870 3884 3885 3886 3900 3901 3902 3916 3917 3918 3932 3933 3934 3948 3949 3950 3964 3965 3966 3980 3981 3982 3996 3997 3998 4012 4013 4014 4028 4029 4030 F 3343 3359 3::175 , 3391 3407 3423 3439 3455 3471 3487 3503 3519 3535 3551 3567 3583 3599 3615 3631 3647 3663 3679 3695 3711 3727 3743 3759 3775 3791 3807 3823 3839 3855 3871 3887 3903 3919 I 3935 1 3951 3967 1 3983 1 ~aaa ~~;~ 1 4031 4044 4045 4046 4047 4060 4061 4062 4063 4076 4077 4078 4079 4092 4093 4094 4095 TIMER COUNTS CONTENTS COUNTS CONTENTS COUNTS CONTENTS COUNTS CONTENTS COUNTS OF TO OF TO OF TO OF TO COUNTER INTERRUPT COUNTER INTERRUPT COUNTER INTERRUPT COUNTER INTERRUPT FE 254 40 189 02 124 9F 59 FD 253 9A 188 A5 123 3D 58 F8 252 34 187 48 122 7C 57 F7 251 69 186 96 121 F8 56 EE 250 03 185 20 120 Fl 55 DC 249 A7 184 58 119 E2 54 88 248 4F 183 87 118 C5 53 71 247 9E 182 6E 117 8A 52 E3 246 3C 181 DO 116 15 51 C7 245 78 180 8A 115 2A 50 8E 244 FO 179 75 114 55 49 10 243 EO 178 E8 113 AA 48 38 242 Cl 177 06 112 54 47 76 241 82 176 AD 111 A8 46 ED 240 04 175 5A 110 50 45 OA 239 09 174 85 109 AO 44 84 238 12 173 6A 108 41 43 68 237 24 172 05 107 83 42 01 236 48 171 A8 106 06 41 A3 235 90 170 56 105 00 40 47 234 21 169 AC 104 lA 39 8F 233 42 168 58 103 35 38 IF 232 85 167 81 102 68 37 3F 231 OA 166 62 101 07 36 7E 230 14 165 C4 100 AF 35 FC 229 28 164 88 99 5E 34 F9 228 51 163 11 98 80 33 F3 227 A2 162 22 97 78 32 E6 226 45 161 44 96 F6 31 CD 225 88 160 89 95 EC 30 98 224 17 159 13 94 08 29 36 223 2E 158 26 93 80 28 60 222 50 157 4C 92 60 27 08 221 88 156 98 91 CO 26 86 220 77 155 30 90 80 25 6C 219 EF 154 61 89 00 24 09 218 DE 153 C2 88 01 23 82 217 8C 152 84 87 03 22 64 216 79 151 03 86 07 21 C8 215 F2 150 10 85 OF 20 91 214 E4 149 20 84 lE 19 23 213 C9 148 40 83 3D 18 46 212 93 147 81 82 7A 17 80 211 27 146 02 81 F4 16 18 210 4E 145 05 80 E8 15 37 209 9C 144 08 79 DO 14 6F 208 38 143 16 78 Al 13 OF 207 70 142 2C 77 43 12 8E 206 El 141 59 76 87 11 70 205 C3 140 83 75 OE 10 FA 204 86 139 66 74 lC 9 F5 203 OC 138 CC 73 39 8 EA 202 18 137 99 72 72 7 04 201 31 136 32 71 E5 6 A9 200 63 135 65 70 C8 5 52 199 C6 134 CA 69 97 4 A4 198 8C 133 95 68 2F 3 49 197 19 132 28 67 5F 2 92 196 33 313 57 66 8F 1 25 195 67 130 AE 65 7F 0 4A 194 CE 129 5C 64 FE 254 94 193 90 128 89 63 29 192 3A 127 73 62 53 191 74 126 E7 61 A6 190 E9 125 CF 60 C-9 APPENDIX D - INSTRUCTION SUMMARY ACCUMULATOR GROUP INSTRUCTIONS BYTES OF OP OPER- OBJECT STATUS BITS OBJECT INTERRUPT1) DMA CODE AND(S) CODE FUNCTION OVF ZERO CARRY SIGN CYCLES CODE PRIVILEGE SLOTS 2) SR 12 SHIFT RIGHT ONE 0 1/0 0 SR 4 14 SHIFT RIGHT ONE 0 1/0 0 1 SL 1 13 SHIFT LEFT ONE 0 1/0 0 1/0 SL 4 15 SHIFT LEFT FOUR 0 1/0 0 1/0 COM 18 ACC (ACC) G) 0 1/0 0 1/0 H'FF' LNK 19 ACC -(ACC) + CB 1/0 1/0 1/0 1/0 INC 1F ACC -(ACC) + 1 1/0 1/0 1/0 1/0 LIS 7i ACC-H'i' CLR 70 ACC-H'OO' 1 1 1 LI 20 ACC--H'ii' 2.5 2 2 NI 21 ACC --(ACC)AH'ii' 0 1/0 0 1/0 2.5 2 2 01 22 ACC -(ACC) V H'ii' 0 1/0 0 1/0 2.5 2 2 XI 23 ACC -(ACC) G) H'ii' 0 1/0 0 1/0 2.5 2 2 AI 24 ACC-(ACC) + H'ii' 1/0 .1/0 110 1/0 2.5 2 2 (Binary Add) CI 25 H'ii' + (ACC)+1 1/0 1/0 1/0 1/0 2.5 2 2 1) An interrupt request cannot be acknowledged until an instruction without interrupt privilege has completed execution. 2) This number of bytes can be transferred via DMA dUring the instruction's execution. SCRATCHPAD REGISTER INSTRUCTIONS BYTES OF OP OPER- OBJECT STATUS BITS OBJECT INTERRUPT1) DMA OVF ZERO CARRY SIGN CYCLES CODE PRIVILEGE SLOTS 2 ) CODE AND(S) CODE LR y,x A,r* 4r A,KU 00 A,KL 01 A,QU 02 A.QL 03 rA 5r KU,A 04 KLA 05 QUA 06 QLA 07 AS Cr ASD Dr NS Fr XS Er OS 3r * Operand r formats are: FUNCTION GENERAL LOAD REGISTER FORMAT ALLOWABLE OPER- ANDS LISTED BELOW ACC--(r) ACC--tr12) ACC--ir13) ACC--tr14) ACC-ir15) r--{ACC) r12.-...(ACC) r13.-...(ACC) r14--(ACC) r15.-...(ACC) ACC-(ACC)+(rXBinary) 1/0 1/0 1/0 ACC-(ACC)+(r) 1/01/0 1/0 (Decimal) ACC-(ACC)A(r) 0 1/0 0 ACC-(ACC) G) (r) 0 1/0 0 r_(r)+H'FF'(Decrement) 1/01/0 1/0 Direct Addressing othrough 11 (Decimal Form) H'O' through H'B' (hexadecimal form) 0-1 1/0 1 1/0 2 1/0 1 1/0 1 1/0 1.5 Indirect Addressing S or 12 I or 13 o or 14 2 DATA COUNTER INSTRUCTIONS BYTES OF OP OPER- OBJECT STATUS BITS OBJECT INTERRUPT1) DMA CODE ANDIS) CODE FUNCTION OVF ZERO CARRY SIGN CYCLES CODE PRIVILEGE SLOTS 2 ) LR ..... "r- OE r14---(DCU), r15--(DCL) 4 3 \.t,L/\.... LR H,DC 11 r1 O--(DCU) ; r11 --- (DCL) 4 3 LR DC,Q OF DCU--(r14); DCL ---(r15) 4 3 LR DC,H 10 DCU--(r10); DCL ---(r11) 4 3 ADC 8E DC ---(DC) + (ACC) 2.5 1 2 DCI iiii 2A DC---H'iiii' 6 3 5 XDC 2C DC - DC1 2 2 ---> [Memory Interface Circuit Only] INDIRECT SCRATCHPAD ADDRESS REGISTER INSTRUCTIONS BYTES OF OP OPER- OBJECT STATUS BITS OBJECT INTERRUPT1 \ DMA CODe ANDISj CODE FUNCTiON OVF ZERO CARRY SiGN CYCLES CODE PRIVILEGE· , SLOTS 2) LR A,IS OA ACC-(lSAR) LR ISA OB ISAR-(ACC) LlSU a 01100a* ISARu-a LlSL a 01101 a* ISARL_a .. a is 3 bits MEMORY REFERENCE INSTRUCTIONS BYTES OF OP OPER- OBJECT STATUS BITS OBJECT INTERRUPT1) DMA CODE ANDIS) CODE FUNCTION OVF ZERO CARRY SIGN CYCLES CODE PRIVILEGE SLOTS 2) LM 16 ACC ---((DC)) 2.5 2 ST 17 (DC) ---(ACC) 2.5 1 AM 88 ACC~(AC;C)T((DC)) 1/0 1/0 1/0 I/O 2.5 2 (Binary) AMD 89 ACC -..(ACC)+((DC)) I/O I/O 1/0 I/O 2.5 2 (Decimal) NM 8A ACC ___(ACC) 1\ ((DC)) 0 I/O 0 1/0 2.5 2 OM 8B ACC -..(ACC) V ((DC)) 0 1/0 0 liO 2.5 2 XM 8C ACC ---(ACC) E>((DC)) 0 I/O 0 I/O 2.5 2 CM 80 ((DC)) + (ACe) + 1) 1/0 I/O I/O I/O 2.5 2 STATUS REGISTER INSTRUCTIONS BYTES OF OP OPER- OBJECT STATUS BITS OBJECT INTERRUPT1) DMA CODE ANDIS) CODE FUNCTION OVF ZERO CARRY SIGN CYCLES CODE PRIVILEGE SLOTS 2 ) LR W,J 10 W __(r9) 2 Yes' 2 W4 W3 W2 W1 Wo liNT iOVF i ZERO i CARRY ISIGN I (Privileged Instruction)' LR J,W 1E r9___(W) , As a result of a privileged instruction execution, a request for Interrupt service is not acknowledged by the CPU until a subsequent non-privileged instruction is executed. MISCELLANEOUS INSTRUCTIONS r- i BYTES OF OP OPER- OBJECT CODE AND(S) CODE FUNCTION STATUS BITS OBJECT INTERRUPT1) DMA OVF ZERO CARRY SIGN CYCLES CODE PRIVILEGE SLOTS 2 ) NOP 2B NO OPERATION 0-2 PROGRAM COUNTER INSTRUCTIONS BYTES OF OP OPER- OBJECT STATUS BITS OBJECT INTERRUPT1) DMA CODE AND(S) CODE FUNCTION OVF ZERO CARRY SIGN CYCLES CODE PRIVILEGE SLOTS 2 ) LR K,P 08 r12-(PC1U); r13--(PC1L) 4 3 LR P,K 09 PC1U-(r12); PC1L--(r13) 4 3 LR PO,Q OD PCOU-(r14); PCOL--(r15) 4 3 PK OC PCOU-(r12) ; PCOL--(r13) 4 Yes* 3 and PC1-(PCO) Privileged Instruction* PI aaaa** 28 PC,-(PCO); PCO_H'aaaa' 6.5 3 Yes* 5 Privileged Instruction* POP lC PCO--(PC1) 2 Yes* 2 Privileged Instruction* BRANCH INSTRUCTIONS BYTES OF OP OPER- OBJECT CODE ANDIS) CODE FUNCTION STATUS BITS OBJECT INTERRUPT1) DMA OVF ZERO CARRY SIGN CYCLES CODE PRIVILEGE SLOTS 2 ) BR aa 90 PCO-((PCO)+l) + H'aa' 3.5 2 3 aa JMP aaaa 2 29 PCO-H'aaaa' 5.5 3 Yes 1 4 aa aa Privileged Instruction* BT t,aa 4 8t 4 PCO-((PCO)+ H'aa' 3.53 2 3 aa if any test is true or PCO-(PCO)+ 2 if no test is true 3.0 STATUS BIT TESTS 22 2' 20 IZERO ICARRY iSIGN I BP aa 81 PCO-((PCO)+l)+ H'aa' 3.5 2 3 if SIGN=l aa PCO-(PCO)+ 2 if SIGN=O 3.0 BC aa 82 PCO-((PCO)+l)+ H'aa' if CARRY=l 3.5 2 3 aa PCO-(PCO)+ 2 if CARRY=O 3.0 BZ aa 84 PCO-((PCO)+1) H'aa' if ZERO=1 3.5 2 3 aa PCO-(PCO)+ 2 if ZERO=O 3.0 BM aa 91 PCO-((PCO)+1)+ H'aa' if SIGN=O 3.5 2 3 aa PCO-(PCO)+ 2 if SIGN=1 3.0 BNC aa 92 PCO-((PCO)+1)+ H'aa' if CARRY=O 3.5 2 3 aa PCO-(PCO)+ 2 if CARRY=1 3.0 BNZ aa 94 PCO-((PCO)+1)+ H'aa' If ZERO-=O 3.5 aa PCO-(PCO)+ 2 if ZERO=l 3.0 BF t5,aa 9t5 PCO-((PCO)+1)+ H'aa' 3.5 2 2 3 aa if selected status bits are all "0" PCO--(PCO)+2 if any status bit is 1 3.0 TEST CONDITIONS 23 22 2' 2° IOVF I ZERO ICARRYISIGN I BNO aa 98 PCO--((PCO)+1)+ H'aa' if OVF=O 3.52 2 3 aa PCO--(PCO)+ 2 if OVF=l 3.0 BRZ aa 8F PCO.-((PCO)+1)+ H'aa' if ISARi7 2.52 2 2 aa PCO.-(pCO)+ 2 if ISAR=7 2.0 1. As a result of a privileged instruction execution, a request for interrupt service is not acknowledged by the CPU until a subsequent non-privileged instruction is executed. 2. The contents of the accumulator are destroyed. 3. 3,5 cycles if branch is taken. 3.0 cycles if branch is not taken. 4. t is only 3 bits 5. t is four bits 6. 2.5 cycles if branch is taken. 2.0 cycles if branch is not taken. D-3 INTERRUPT CONTROL INSTRUCTIONS BYTES OF OP OPER- OBJECT CODE ANDIS) CODE FUNCTION STATUS BITS OBJECT INTERRUPT,) DMA OVF ZERO CARRY SIGN CYCLES CODE PRIVILEGE SLOTS 2 ) Di EI i A DiSABLE iNTERRUPT lB ENABLE INTERRUPT Pnvlleged InstructIon'" INPUT/OUTPUT INSTRUCTIONS 2 YES* 2 BYTES OF OP OPER- OBJECT CODE ANDIS) CODE STATUS BITS OBJECT INTERRUPT,) DMA OVF ZERO CARRY SIGN CYCLES CODE PRIVILEGE SLOTS 2 ) FUNCTION INS a Aa ACC -(INPUT PORT a) 0 1/0 0 1/0 4** Input Ports 00 to OF only I~I aa 26 ACC --(INPUT PORT aa) 0 1/0 0 I IV 4 2 II~ aa Input Ports04 through FF only OUTS a Ba OUTPUT PORT a-lACe) 4** YES"" Output Ports00to OF only OUT aa 27 OUTPUT PORT aa -(ACe) 4 2 YES* aa Output Ports04 through FF only * As a result of a privileged instruction execution, a request for interrupt service is not acknowledged by the CPU until a subsequent non-privileged instruction is executed. ** 2 cycles when I/O port address is "0" or "1" 3 3 3 3 I I