Chapter 2 of 5
Send a letter to the screen
A register can hold a letter, provided the program and the screen agree on a code for it. The CoCo already knows how to print a character. Give its output routine one byte and watch where the letter appears.
Print one letter
Save letter.asm in your practice folder. In Assembly Development, choose File → Open Source… and open it. Keep the MC6809 target. At the Disk BASIC prompt, choose Run in the assembly window and confirm the load if asked. The CoCo should print A on a line by itself and return to OK. This time the program did something you can see after it has finished.
org $3f00
start lda #'A ; Load this character's ASCII code into A.
jsr [CHROUT] ; Call the routine whose address is at CHROUT.
lda #CR
jsr [CHROUT] ; Leave the next BASIC prompt on its own line.
rts ; Return to the caller in BASIC.
end start
The character literal in the first LDA is written #'A. In this LWASM spelling, a single quote introduces the character that follows it. The assembler looks up its numeric character code. For uppercase A the code is $41, or decimal 65, in a code called ASCII. The instructions still deal in ordinary bytes. The meaning of the byte depends on what receives it.
The first JSR calls a subroutine. Its name stands for “jump to subroutine.” Our subroutine is part of the Color Computer’s built-in ROM, the read-only memory that also supplies BASIC. Its job here is to take the character code in A and send it to the current output, the screen in this exercise. When it finishes, execution resumes on the following line of our program.
The next load puts CR in A. Look near the start of the source for its definition. EQU gives a name to a value, and CR has the value 13. A carriage return is also a character code. The second call asks the ROM to start a new line. That’s how the printed letter gets a line of its own.
Change the message with one keystroke
- In the assembly window choose File → Save a Copy… and save my-letter.asm. Choose File → Open Source… and open that copy. Check the selected filename before you edit.
- In the first LDA, change just the A following the quote to uppercase Z. Leave the A in LDA alone. Choose File → Save Source, then Debug from Entry. At the first instruction click Step once.
- The footer should now show PC $3F02 and A $5A. Which part of the assembled instruction changed when you chose a new letter? Open Build details → Listing. The two bytes in the original were 86 41. The edited instruction uses 86 5A.
- Choose Continue. Look for Z on the CoCo. The same ROM routine just printed a different letter. If the output is still A, check which document is selected and compare its assembled bytes with the two above.
Read the square brackets
CHROUT equ $a002 ; Address of the ROM character-output vector.
CR equ 13 ; Carriage return starts a fresh line.
CHROUT names $A002. At that address the CoCo stores a two-byte address pointing to its character-output routine. An address used like this is called a vector. On JSR [CHROUT], the processor first finds the address stored at CHROUT and then calls the routine there. The brackets tell the processor to take that extra look. The character itself travels in A.
If you Step while JSR is highlighted, the PC follows that call into the ROM. The assembly window can say no verified source line because the instructions at the new address belong to the CoCo’s ROM. Choose Continue to finish. You’ll soon write a subroutine of your own, where both the call and the code it reaches appear in the same source file.
You’ve sent a character out of the CoCo. Next you’ll give a player a chance to send one back.