PRINTDCI quick-and-easy ASM printing routine


This short routine makes it easy to print strings from assembly code, or even from the mini-assembler.  (Especially in the Enhanced monitor, which lets you enter ASCII characters using a ` prefix.)



Assemblers like EDASM have a directive called DCI that encodes an ASCII string in such a way that the end of the string can be distinguished by checking its high bit.  The DCI statement converts your string into ASCII bytes so that most characters have the high bit cleared, but the last character has its high bit set.

The PRINTDCI routine pops the return address off the stack, parses-and-prints ASCII characters starting at the return address until it finds the last character (denoted by its high bit), and then pushes an updated return address onto the stack and returns to the next opcode.

PRINTDCI source code ASM

COUT1     EQU   $FDF0


* This routine displays the ASCII text encoded into

* the memory immediately following the caller's

* address.  The last byte of the string is identified by the high bit. 

* Routine will RTS to the next opcode immediately after the string. 


PRINTDCI  PLA                 ;Pull sender's low-order address from stack.

          STA   $10           ;Store it at $10.

          PLA                 ;Pull high-order from stack.

          STA   $11           ;Store at $11.

          BNE   PRINTDCI1     ;Go enter the printing loop.


PRINTDCI0 ORA   #$80          ;Add high bit for compatibility

          JSR   COUT1         ;print it

PRINTDCI1 LDA   $10           ;Get address at ($10-$11) and increment with carry.

          CLC                  ;Clear previous carry. 

          ADC   #1            ;Advance by 1, set carry bit if needed  

          STA   $10

          LDA   $11

          ADC   #0            ;Adds only the carry bit, if applicable 

          STA   $11

          LDY   #0

          LDA   ($10),Y       ;Get next character.

          BPL   PRINTDCI0     ;If no high bit, go print character. 

          LDA   $11           ;Load high byte of last character address 

          PHA                  ;push it onto the stack 

          LDA   $10           ;Load low-byte of last character address 

          PHA                  ;push onto stack (6502 will increment it) 

          LDA   ($10),Y       ;Get last character again

          JMP   COUT1         ;Tail call to COUT1, new return address on stack 

How to use PRINTDCI it in ASM:

In the simplest case, when you want to print a text string, just insert JSR PRINTDCI into your assembler source code, then on the next line use the assembler's DCI directive to enter your string.  On the line after that, just continue your assembler program.

Here's an example, from KR9600 TESTER showing two branches that print short messages.



You can also use ASC and DB to print more elaborate strings, like titles and column headings.  If you want more precise control over the end-of-string, use the MSB OFF and MSB ON directive to control the high bit yourself.  Here's how KR9600 TESTER begins by printing its title, line breaks, and column headings via a single call to JSR PRINTDCI followed by contiguous control-characters and string directives.


The source code for PRINTDCI is listed at the top of this blogpost.  And the source for PRINTDCI and KR9600 TESTER are both included as text files on the Prince of Persia Pre-boot disk with KR9600 TESTER attached to the KR9600 Tester blogpost.