💾 Archived View for mirrors.apple2.org.za › archive › www.textfiles.com › apple › alinto.txt captured on 2023-11-04 at 16:07:02.
View Raw
More Information
⬅️ Previous capture (2023-03-20)
-=-=-=-=-=-=-
INTRODUCTION TO ASSEMBLY LANGUAGE PROGRAMMING
CHARLES R. SMITH
REPRINTED FROM: THE MAPLE ORCHARD
BOX 696, STATION B
WILLOWDALE, ONTARIO
WHAT IS ASSEMBLER AND MACHINE LANGUAGE ?
MOST OF US HAVE SOME FAMILIARITY WITH BASIC, WITH ITS ENGLISH LIKE
COMMANDS SUCH AS 'PRINT', 'INPUT' AND 'FOR .. NEXT'. THERE ARE OTHER
HIGH-LEVEL LANGUAGES, LIKE FORTRAN AND PASCAL, WHICH SIMILARLY ARE
FORMED OF ENGLISH LIKE COMMANDS. THE APPLE'S CENTRAL PROCESSING UNIT
(CPU) CANNOT UNDERSTAND SUCH ENGLISH COMMANDS, BUT INSTEAD THE CPU
MUST 'INTERPRET' THE COMMANDS OR 'TRANSLATE' THEM INTO A FORM WHICH IT
CAN UNDERSTAND. WHEN RUNNING A PROGRAM IN BASIC, THE APPLE TAKES EACH
LINE OF THE PROGRAM AND INTERPRETS IT INTO A MACHINE LANGUAGE FORM
WHICH FOR THE CPU. ONCE INTERPRETED, THE CPU THEN EXECUTES THE LINE.
APPLESOFT IS THEREFORE CALLED A 'BASIC INTERPRETER' OR IS AN
'INTERPRETED BASIC'.
THE OPPOSITE OF THE HIGH-LEVEL, STRUCTURED LANGUAGES ARE ASSEMBLER AND
MACHINE LANGUAGE. THE TERMS 'ASSEMBLY LANGUAGE' AND 'MACHINE LANGUAGE'
ARE OFTEN USED INTERCHANGABLY, ALTHOUGH THERE IS A DIFFERENCE.
PROGRAMMING IN MACHINE LANGUAGE IS ACCOMPLISHED WHEN HEXADECIMAL OR
BINARY DATA IS LOADED INTO THE COMPUTER BYTE-BY-BYTE (EG. ENTERING THE
MONITOR BY CALL -151 AND INPUTTING '300: A9 0A ...'). WHEN COMPUTERS
WERE IN THEIR INFANCY, THIS WAS THE ONLY OPTION AVAILABLE. THIS FORM
OF PROGRAMMING CAN ONLY BE INTELLIGIBLE TO PEOPLE WHO THINK LIKE A
COMPUTER. TO WE LESSER MORTALS, WRITING A PROGRAM IN MACHINE LANGUAGE
IS THE ULTIMATE EXERCISE IN FRUSTRATION. (AN ASIDE: WHEN STEVE
WOZNIAK AND STEVE JOBS FIRST BUILT AN APPLE I COMPUTER, THEY TOOK IT
TO PARTIES AND ENTERED HUNDREDS OF HEX CODES BY HAND JUST TO LOAD IN A
PROGRAM TO DEMONSTRATE THIS FIRST APPLE'S ABILITY. ALLEGEDLY, DOS WAS
FIRST WRITTEN THIS WAY!)
ENTER ASSEMBLERS. AN ASSEMBLER IS SIMPLY A TOOL WHICH ALLOWS A PERSON
TO WRITE A PROGRAM USING SOMEWHAT MORE INTELLIGIBLE COMMANDS, AND THEN
THE ASSEMBLER PROGRAM CONVERTS ("ASSEMBLES") THE ENGLISH-LIKE COMMANDS
INTO THE CORRESPONDING MACHINE-LANGUAGE CODE.
WHILE THERE IS A DEFINITE AND SIGNIFICANT DIFFERENCE BETWEEN
'ASSEMBLY' LANGUAGE AND 'MACHINE' LANGUAGE, THE TWO TERMS ARE OFTEN
USED INTERCHANGABLY. THROUGHOUT THIS ARTICLE, SUCH INTERCHANGABILITY
SHALL BE USED.
WHY PROGRAM IN MACHINE LANGUAGE ?
IF ASSEMBLY LANGUAGE IS TOUGHER THAN BASIC, WHY BOTHER BEING A GLUTTON
FOR PUNISHMENT? IS THERE SOMETHING TO BE SAID FOR SUCH AN ARCANE
LANGUAGE? CONSIDER THESE THREE REASONS: SPEED, FLEXIBILITY AND MEMORY
MANAGEMENT.
THE MOST PROMINENT BENEFIT OF A MACHINE LANGUAGE PROGRAM CAN BE SEEN
ANYTIME YOU PLAY A COMMERCIALLY-PRODUCED, ARCADE STYLE GAME. SUCH
GAMES ARE INVARIABLY WRITTEN ON AN ASSEMBLER (THE PEOPLE AT
SOUTHWESTERN DATA SYSTEMS TELL ME THAT THE GAMES FROM SIRIUS ARE
WRITTEN ON MERLIN). THE EXECUTION SPEED OF A MACHINE LANGUAGE PROGRAM
IS MUCH FASTER THAN A CORRESPONDING BASIC PROGRAM ARCADE QUALITY GAMES
SIMPLY CANNOT BE WRITTEN IN BASIC. EVEN BASIC WHICH HAS BEEN COMPILED
INTO MACHINE LANGUAGE (BY APPLESOFT COMPILERS SUCH AS TASC OR
SPEEDSTAR) IS STILL SLOWER AND MORE MEMORY-HUNGRY THAN AN EQUIVALENT
PROGRAM WRITTEN IN ASSEMBLER. THE ADVANTAGE OF SPEED CAN BE USED IN
OTHER SITUATIONS WHICH WILL SIGNIFICANTLY ENHANCE THE APPLE'S
CAPABILITY. SORTING, MATRIX OPERATIONS AND DISK UTILITIES ARE BUT A
FEW REASONS TO USE THE SPEED OF MACHINE LANGUAGE.
THE SECOND REASON TO PROGRAM IN MACHINE LANGUAGE IS THE INCREASED
FLEXIBILITY WHICH IT OFFERS. THE COMMANDS IN APPLESOFT CAN BE ENHANCED
BY THE USE OF SHORT MACHINE LANGUAGE ROUTINES HIDDEN AWAY IN MEMORY.
PRINT FORMATTING FUNCTIONS, SOUND GENERATION, SORTING, STRING HANDLING
AND SWAPPING, DECIMAL TO HEX CONVERSIONS, GARBAGE COLLECTION AND
MEMORY CLEARANCE, AND ARRAY OPERATIONS ARE BUT SOME OF THE MORE
COMMONLY USED SUBROUTINES. WHILE SUCH MACHINE LANGUAGE ENHANCEMENTS
ARE AVAILABLE COMMERCIALLY, THEY CAN ALSO BE HAD AT A VERY LOW COST,
AS THEY ARE LISTED IN SUCH PUBLICATIONS AS NIBBLE AND CALL-APPLE.
THE THIRD ADVANTAGE RELATES TO MEMORY MANAGEMENT. MACHINE LANGUAGE
PROGRAMS ARE MORE EFFICIENT IN THEIR COMSUMPTION OF MEMORY THAN ARE
THEIR BASIC COUNTERPARTS. BECAUSE OF THIS, IT IS POSSIBLE TO HAVE
SEVERAL MACHINE LANGUAGE ROUTINES RESIDING IN MEMORY CUNCURRENTLY,
THEREBY ENHANCING APPLESOFT WHILE AT THE SAME TIME AVOIDING A
SIGNIFICANT REDUCTION IN THE MEMORY AVAILABLE TO THE USER.
WHAT IS NEEDED TO GET STARTED
TO BEGIN PROGRAMMING IN ASSEMBLER/ MACHINE LANGUAGE, IT IS NOT
NECESSARY TO PURCHASE ANY SOFTWARE TOOLS. A GOOD BOOK, HOWEVER, CAN BE
INVALUABLE. ONCE YOUR INTEREST IS SPARKED, YOU MAY DEVELOP A SUDDEN
URGE TO PURCHASE AN ASSEMBLER. OTHER MACHINE LANGUAGE UTILITIES ARE
HELPFUL.
THERE ARE THREE DIFFERENT LEVELS OF SOPHISTICATION AT WHICH YOU MAY
ENTER MACHINE LANGUAGE PROGRAMMING:
1) MACHINE LANGUAGE USING THE MONITOR
2) ASSEMBLY LANGUAGE USING THE MINI-ASSEMBLER
3) ASSEMBLY LANGUAGE USING AN ASSEMBLER.
LET'S LOOK AT EACH OF THESE IN ORDER.
MACHINE LANGUAGE USING THE MONITOR
ALTHOUGH NOT THE MOST FLEXIBLE APPROACH TO MACHINE LANGUAGE, THIS IS
THE CHEAPEST ROUTE TO GO, AND IT FORMS A GOOD STARTING PLACE. THE WAY
TO BEGIN IS TO EXIT APPLESOFT AND GET INTO THE MONITOR BY MEANS OF THE
COMMAND:
]CALL -151
WHICH YIELDS THE '*' PROMPT.
FROM HERE WE CAN ENTER A PROGRAM BY GIVING THE STARTING ADDRESS AND
THE HEXADECIMAL DATA. TRY THE FOLLOWING:
- 300:AD 30 C0 88 D0 FD 4C 00 30 <RET>
WHERE YOU ENTERED THE NUMBER 300, A COLON, THEN 9 SETS OF HEXADECIMAL
DATA. BE SURE THAT YOU DO NOT LEAVE A SPACE BETWEEN THE COLON AND THE
FIRST NUMBER. AT THE END OF THE LINE, ENTER A <CARRIAGE RETURN>. UP TO
85 HEXADECIMAL NUMBERS CAN BE ENTERED AT ONCE THIS WAY.
WHAT THIS PROGRAM DOES IS TOGGLE THE SPEAKER BY ADDRESSING ITS MEMORY
LOCATIO (AD 30 C0), COUNTING DOWNWARDS FROM 255 TO 0 (88 D0 FD) AND
THEN JUMPING BACK TO THE START (4C 00 30). IN OTHER WORDS, THIS SHORT
ROUTINE KEEPS A CONSTANT TONE GOING ON THE SPEAKER UNTIL YOU HIT
<RESET> TO GET OUT OF IT. TO START THIS CPROGRAM ONCE YOU HAVE TYPED
IN THE ABOVE LINE, SIMPLY GO TO LOCATION $300 AND DO WHAT'S THERE BY
THE COMMAND:
*300G <RET>
YOU HAVE JUST ENTERED YOUR FIRST MACHINE LANGUAGE PROGRAM (THIS
PROGRAM EXAMPLE IS COURTESY OF SC MACRO ASSEMBLER).
NOW GO ON TO GREATER THINGS. GRAB AN ASSEMBLY/MACHINE LANGUAGE LISTING
AND ENTER IT INTO YOUR COMPUTER. IF YOU ARE ENTERING AN ASSEMBLY
LISTING, YOU WILL SEE THE HEX CODE IN THE LEFT HAND COLUMNS. FOR
EXAMPLE, THE ABOVE PROGRAM LOOKS LIKE:
0300 - AD 30 C0 1 TONE LDA $C030 0303 - 88 2 LOOP DEY 0304 - D0 FD 3
BNE LOOP 0306 - 4C 00 03 4 JMP TONE
REMEMBER TO SAVE YOUR WORK ON DISK. THIS IS DONE WITH THE BSAVE
COMMAND. IT IS NECESSARY TO SPECIFY THE STARTING ADDRESS AND LENGTH
PARAMETERS USING THE 'A$___' AND 'L$___' COMMANDS. IF A PROGRAM WENT
FROM 0300 TO 0320, IT WOULD BE (HEX) 20 OR (DECIMAL) 32 BYTES LONG.
THEREFORE IT WOULD BE SAVED BY THE COMMAND:
- BSAVE MYPROGRAM,A$300,L$20
WHERE A$ IS THE STARTING ADDRESS IN HEXADECIMAL AND L$ IS THE LENGTH
IN HEX. IT COULD ALSO BE SAVED BY THE COMMAND:
WHERE A IS THE STARTING ADDRESS IN DECIMAL (768=300 HEX) AND L IS THE
LENGTH IN BYTES.
TO CHECK A PROGRAM LISTING, ENTER THE START AND END ADDRESS, SEPERATED
BY A PERIOD. FOR THE SHORT TONE PROGRAM ABOVE, ENTER:
*300.308 <RET>
IF YOU WANT TO SEE THE DISASSEMBLED CODE, ENTER THE STARTING ADDRESS
AND "L" TO LIST 20 COMMANDS. TO SEE THIS PROGRAM, ENTER:
*300L <RET>
(TO MAKE THE ENTRY AND CORRECTION OF MACHINE LANGUAGE PROGRAMS, ONE OF
THE DISKS FROM THE INTERNATIONAL APPLE CORE (DISK 25, MACHINE LANGUAGE
UTILITIES) CONTAINS A UTILITY TO FACILITATE THIS PROCESS. IT IS CALLED
BEU (FOR BINARY ENTRY UTILITY) AND WILL LOCATE ITSELF EITHER ON A 16K
RAM CARD OR IN THE APPLE'S 48K RAM.)
PROGRAMMING WITH THE MINI-ASSEMBLER
IN THE ORIGINAL APPLE II MACHINE WAS A MONITOR FUNCTION ENTITLED THE
"MINI-ASSEMBLER". ITS JOB WAS TO FACILITATE ENTRY OF MACHINE CODE INTO
THE COMPUTER. WHEN APPLESOFT WAS DEVELOPED FOR THE APPLE II PLUS, THIS
FUNCTION (ALONG WITH STEP AND TRACE IN THE MONITOR ROM) WERE DELETED.
THAT IS THE BAD NEWS. NOW FOR THE GOOD NEWS. IF YOU HAVE A 16K RAM
CARD, LOADING INTEGER BASIC UPON BOOTING UP THE DOS 3.3 SYSTEM MASTER
DISK WILL LOAD THE MINI-ASSEMBLER INTO YOUR MACHINE. THIS CAN BE
ACCESSED BY ENTERING INTEGER WITH THE COMMAND:
]INT
WHICH YIELDS THE '>' PROMPT. THEN ENTER:
>CALL -151
TO GET THE MONITOR '*' PROMPT. THEN GO TO THE MINI-ASSEMBLER WITH THE
COMMAND:
WHICH YIELDS THE EXCLAMATION (!) PROMPT.
THE MINI-ASSEMBLER IS STILL AVAILABLE TO THE OWNER OF A 48K APPLE ][
PLUS, AS IT HAS BEEN MODIFIED AND INCLUDED ON THE IAC DISK #25.
INSTRUCTIONS FOR ITS USE ARE GIVEN IN AN ACCOMPANYING FILE, WHILE MORE
COMPLETE INSTRUCTIONS ARE GIVEN IN THE APPLE ][ REFERENCE MANUAL.
APPLE //E OWNERS CAN ACCESS THE MINI-ASSEMBLER THROUGH INTEGER BASIC.
3) ASSEMBLER
ONCE YOU'VE GOTTEN YOUR FEET WET BY GAINING ACCESS TO THE MONITOR AND
ENTERING A HEX LISTING, YOU WILL REALIZE THE FACT THAT THERE HAS TO BE
A BETTER WAY. OBTAINING AN ASSEMBLER IS THAT WAY. THERE ARE AT LEAST
TEN COMMERCIALLY AVAILABLE EDITOR/ASSEMBLERS, AND THE PERFECT ONE IS
PROBABLY STILL TO BE WRITTEN. BUYING AN ASSEMBLER IS LIKE OBTAINING
ANY OTHER SOFTWARE, AS THE AMOUNT OF MONEY TO BE SPENT AND THE
CONFIGURATION OF YOUR MACHINE WILL BE CONTROLLING FACTORS. SEVERAL
ASSEMBLERS ARE REVIEWED IN THIS ISSUE. A PUBLIC DOMAIN ASSEMBLER
CALLED MASM IS INCLUDED IN THE IAC'S SPECIAL MACHINE LANGUAGE UTILITY
DISK.
OTHER AIDS
OTHER TYPES OF AIDS ARE ALSO WORTHY OF CONSIDERATION. A DISASSEMBLER
IS A HANDY TOOL WHICH WILL PERMIT YOU TO TAKE MACHINE LANGUAGE
(HEXADECIMAL) CODE AND RE-CONVERT IT INTO AN ASSEMBLER SOURCE LISTING.
FROM THERE, YOU CAN EDIT, ALTER AND RE-ASSEMBLE THE CODE TO MAKE IT
FUNCTION IN A DIFFERENT MANNER OR AT A NEW MEMORY LOCATION. A
DISASSEMBLER IS AVAILABLE IN THE APPLE'S MONITOR AND IS ABLE TO
DISASSEMBLE TO THE SCREEN, USING THE
COMMAND, WHERE XXXX IS THE MEMORY LOCATION AT WHICH TO BEGIN
DISASSEMBLY. A PUBLIC DOMAIN DISASSEMBLER IS INCLUDED ON THE MACHINE
LANGUAGE DISK, AND SOME VERY GOOD ASSEMBLERS ARE INCLUDED WITH SOME
ASSEMBLER AS PART OF THE PACKAGE (EG. MERLIN AND LISA). OTHERS ARE
AVAILABLE AS A STAND-ALONE PRODUCT (SUCH AS DISASM FROM RAK-WARE).
DEBUGGERS ARE FOR THOSE WHO ARE VERY SERIOUS - THEY PERMIT STEP AND
TRACE FUNCTIONS AND A HOST OF OTHER ADVANCED FEATURES. TWO EXAMPLES
OF COMMERCIAL PRODUCTS ARE BUG BYTER AND MUNCH-A-BUG. BEU ON THE
MACHINE LANGUAGE DISK ALSO CONTAINS DEBUG ROUTINES.
THERE IS EVEN A NEW PRODUCT WHICH WILL TURN YOUR MACHINE INTO A
'VISIBLE' APPLE AS IT SHOWS YOU ON THE HI-RES SCREEN HOW YOUR APPLE
WORKS, DISPLAYING THE INSTRUCTIONS, REGISTERS, FLAGS AND MEMORY
LOCATIONS AS IT EXECUTES A PROGRAM ONE STEP AT A TIME.
AND WHERE WOULD WE BE WITHOUT BOOKS? A VARIETY ARE AVAILABLE, EACH ONE
FOR A SLIGHTLY DIFFERENT READER. THERE ARE SOME GENERAL REFERENCES ON
PROGRAMMING THE 6502 MICROPROCESSORS, SEVERAL GOOD BOOKS SPECIFICALLY
WRITTEN FOR APPLE 6502 MACHINE LANGUAGE (REVIEWED IN THIS ISSUE), AND
BOOKS WHICH SERVE AS A ROAD-MAP OR ATLAS TO THE APPLE'S INTERNALS
(ALSO REVIEWED HERE). MUCH IS WRITTEN IN THE MONTHLY MAGAZINES, AND
THERE IS EVEN A MONTHLY PUBLICATION ON MACHINE LANGUAGE PROGAMMING.
BUT THE BEST STARTING PLACE IS THE APPLE ][ REFERENCE MANUAL, WHICH
LISTS THE OPCODES AVAILABLE ON THE 6502 AND SOME IMPORTANT INTERNAL
ROUTINES.
WHEN ALL IS SAID AND DONE, A PERSON WHO KNOWS A FEW BASIC COMMANDS CAN
EASILY FIND THEIR WAY AROUND MACHINE LANGUAGE. THERE IS A CERTAIN JOY
IN ENTERING, DEBUGGING AND RUNNING A PROGRAM, BE IT IN BASIC,
ASSEMBLER OR WHATEVER. AND ISN'T THAT JOY A BIG REASON FOR OWNING AN
APPLE?