Jump to content
  • 0
JohnGill

displaying the value of a byte on screen in decimal

Question

Hi all,

Remarkably, I've managed to write an assembly routine that displays the value of a byte, in decimal, at a specified location on screen, and it works! (Got all my sec's and clc's the right way round and everything! I'm feeling very chuffed with myself and my over-inflated ego deems a hearty round of applause is in order 😉 )   I didn't google any examples, I wanted to have a bash at writing it myself from scratch as a learning exercise as much as anything. I know this is re-inventing the wheel and all that, but I wanted to write it specifically for the CX16 to be efficient as possible. 

It uses four bytes of zp memory (2 bytes for required x,y tile/cell position on screen, 1 byte for required colour attribute, and then the actual number to be written in decimal). It compiles down to only 74 bytes (excluding setting the vera stride, which is assumed to be 1 before calling the routine).

Downside of this method is that it always displays leading zeroes. (eg, if the number is 26, it displays 026 etc)

I presume this is a routine that all proper programmers have written hundreds of times in their sleep, is this the best way of doing it? It only has to display 0-255.

 

; vera stride needs to be set to 1 previously
; write the number to be displayed into display_byte
; pass the required x,y cursor position to the routine in decimal_x and decimal_y
; set colour to the attribute byte required for the number colour
; this example uses zp $20 - $23 but could be any free zp values

display_byte = $20
decimal_x = $21
decimal_y = $22
colour = $23

        lda decimal_y
        sta $9f21                       ; set the vera y-cursor
        lda decimal_x
        asl a
        sta $9f20                      ; set the vera x_cursor (has to be x *2 - two bytes per cell to allow for attrib. bytes)
        
        ldx #0                            ; use x to count up the number of hundreds, tens and units
        lda display_byte
        sec


hundreds_loop                      ; hundreds first
        sbc #100
        bcc do_tens
        inx
        sta display_byte
        jmp hundreds_loop


do_tens
        jsr display_digit            ; display the hundreds digit (also reset x)
tens_loop                              ; count up the tens
        sbc #10
        bcc do_units
        inx
        sta display_byte
        jmp tens_loop


do_units
        jsr display_digit              ; display the tens digit (+ reset x)
units_loop                               ; count up the units
        sbc #1
        bcc finish
        inx
        sta display_byte
        jmp units_loop


finish
        jsr display_digit               ; display the units digit

        rts

display_digit
        txa                                      ; put the decimal column digit from x into acc
        clc
        adc #$30                           ; $30 is the screen code for the number 0
        sta $9f23                           ; write out the number onto the screen
        lda colour
        sta $9f23                           ; write out the attrib byte onto the screen
        ldx #0                                 ; reset x
        lda display_byte
        sec                                      ; get ready for next column subtractions

        rts

  • Like 1

Share this post


Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Now that you've gone through this exercise, you should try doing it with Binary-Coded-Decimal (BCD). It is natively supported by the 6502. You simply set the decimal bit (SED) and then ADC and SBC work in a different way to make this process much faster.

I have several routines here for going back and forth between binary values and decimal strings: https://github.com/SlithyMatt/x16-makewave/blob/master/makewave.asm

  • Thanks 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Please review our Terms of Use