This is a cache of https://vccplus.brianholman.com/manuals/your-first-6809-programs/choice/. It is a snapshot of the page as it appeared on 2026-11-21T00:01:28.492+0000.
Ask the CoCo to choose · Your First 6809 Programs · VCC Plus
← Manual LibraryVCC Plus 3.7.1 · Reading on the web

Try the exercises in VCC Plus. Example links download files here; open them in the app to work beside the emulator.

The complete book · PDF editionBook 7: Your First 6809 Programs

19 pages · VCC Plus 3.7.1 · Keep a copy to read or print.

Download PDF
Contents

Chapter 3 of 5

Ask the CoCo to choose

A question mark waits on the CoCo screen. Tap X and it keeps waiting. Tap 2 and it answers M for Mars. There are only two accepted keys, but the keyboard can supply many more. Where does the program send all the others?

Try the two routes

Save choice.asm and open it with File → Open Source… in Assembly Development. From the Disk BASIC prompt choose Run. Click the CoCo display. When ? appears, press and release X. Give the program a moment, then press 2. A single keypress is enough. Look for ?M and OK. Run again and press 1. This time look for ?E.

The CoCo display shows ?M and returns to BASIC after the Mars key was accepted.

If nothing happens when you press 2, look for the blinking insertion point in the Mac editor. Choose Input → Focus CoCo Display to send keystrokes to the guest instead. The program polls the CoCo keyboard itself. It has no need for a Return key to finish a line of typing.

Read the question from the keyboard

start       lda     #'?         ; Show that the program is waiting for a key.
            jsr     [CHROUT]
choose      jsr     [POLCAT]    ; Check the keyboard without waiting for Enter.
            cmpa    #'1
            beq     earth       ; A matching comparison sets the Z flag.
            cmpa    #'2
            bne     choose      ; No key or an unexpected key: try again.

The first two instructions print the question mark using the output vector from the previous chapter. POLCAT is another ROM vector, this time for checking the keyboard. The call at the label choose returns a character code in A, or zero when there is no key to report.

CMPA compares the value already in A with the value in its operand. Imagine subtracting the two to see whether the answer would be zero. The comparison leaves A alone and records the result in the condition-code register, abbreviated CC. One bit in CC is the zero flag, Z. Equal values give a difference of zero and set that flag. The following branch can use it to choose the next instruction.

Start at choose and take the Mars route by hand. When the player presses 2, A holds $32, the ASCII code for the key marked 2. The first comparison asks if it matches the code for the 1 key, $31. BEQ means “branch if equal,” so that branch passes by. The second comparison finds a match. BNE means “branch if unequal.” This time it passes by too. The next instruction in memory happens to be the one at mars. A branch can make a choice by letting execution carry on in order.

Now try the same walk for X. It differs from both accepted character codes. Which instruction sends the processor back to choose? Zero from an idle keyboard follows that same route. The question mark gets printed only once because the loop goes back to the keyboard call, below the print.

Catch the choice before it prints

mars        lda     #'M
            bra     printit     ; Skip the Earth path.
earth       lda     #'E
printit     jsr     [CHROUT]    ; Both paths meet here with one letter in A.
            lda     #CR
            jsr     [CHROUT]
            rts
  1. Click anywhere on the line that starts mars and choose Set Stop. Do the same on the line beginning earth. The Stops menu should now list two locations. Both stops are on instructions that will execute only after the program accepts a key.
  2. Choose Run. At the question mark press 1. The workspace stops before the Earth load at PC $3F16. The incoming character is still in A, so expect $31. Step once. The load has replaced the player’s key with $45, the code for uppercase E. Continue to let the guest print it.
  3. Run again and press 2. At the Mars stop PC is $3F12 and A is $32. Predict what the screen will show. Continue to check. The BRA printit below the Mars load always branches. It carries the M past the Earth load so both routes can use one output call.
  4. Choose Stops → Clear All Stops in the assembly workspace. A fresh run can now play straight through. To look up one of the decisions later, open Develop → Assembly Quick Reference… and search for CMPA, CC.Z, BEQ or BNE.

Compare this pair of decisions with the BASIC planet menu or the C program’s key loop. All three ask the same question. In assembly you can point separately to the instruction that compares two values and the instruction that acts on its answer.

Original VCC Plus manuals © 2026 Brian K. Holman. Third-party references retain their authors’ copyrights and licenses. Artwork notices. Edition 3.7.1; source and export identity. Printable edition: PDF source and export identity.

A closer look

Open original