Page 1 of 1

best way to display decimal number?

Posted: Tue Oct 20, 2009 6:25 am
by adric22
Okay.. so you're coding completely in assembler. Now, you have a number from $00 to $FF that you want to display on the screen for the user, but it needs to be displayed in decimal 000 to 255. What is the easiest/best way to accomplish this?

Posted: Tue Oct 20, 2009 6:31 am
by Mike
The easiest is most probably using three 256 byte tables for hundreds, tenths, and units of the number to be displayed.

For a more compact version take a look at the 6502 source code repository, binary to BCD conversion. Be forewarned though, these examples use the decimal mode of the 6502, and you'll need to include an IRQ routine wedge that makes a CLD before entering other interrupt processing.

The routines result in a BCD number, where 4 bits at a time can be shifted out, and an offset value (for example, $30) added to display a single digit.

There's also the routine to print the line number at $DDCD. It prints the integer in X/A. It however relies on kernal print routines, and will require you to position the cursor beforehand.

Greetings,

Michael

P.S.: Since this is already the 4th thread of a programming question regarding the same project of yours, I'd like to kindly ask you to keep these questions in a single thread.

Posted: Tue Oct 20, 2009 7:49 am
by carlsson
See also the routine at $DDDD which stores an ASCII string on the stack, starting at $00FF. You can find this and many other entries in the sticky thread at top of this section.

Posted: Thu Oct 22, 2009 6:35 pm
by adric22
Well, it was too much work, so I just had it display in hex. I also simplified that when I redefined the character set so I added an extra A-F right after 9.

Posted: Fri Oct 23, 2009 12:28 am
by carlsson
Do you need an exact example how to call the ROM routines? Does your program rely a lot on using the zeropage so you can't call Basic ROM which assumes certain things are not altered?

Posted: Fri Oct 23, 2009 2:15 am
by Diddl
adric22 wrote:Well, it was too much work, so I just had it display in hex.
Use HEXOUT for printing a word (two bytes, 4 digits) and HEX2 to print a byte.



Code: Select all

;------------ PRINT HEX VALUE IN  X/A
HEXOUT
  pha
  lda #"$"
  jsr BSOUT
  pla
  beq HEX0
_relo0200 = . +1
  jsr HEX2
HEX0
  txa
HEX2
  pha
  lsr
  lsr
  lsr
  lsr
  jsr HEX1
  pla
  and #15
HEX1
  clc
  adc #246
  bcc HEX1_2
  adc #6
HEX1_2
  adc #58
  jmp BSOUT

Posted: Fri Oct 23, 2009 11:57 am
by adric22
carlsson wrote:Do you need an exact example how to call the ROM routines? Does your program rely a lot on using the zeropage so you can't call Basic ROM which assumes certain things are not altered?
An example would have been helpful, and I do use a lot of zero-page, but so far I've tried to stick with areas that only affect BASIC, and not the kernel.

Still, I think I'm pretty happy with the way I am doing it now, because I don't think there would really be enough room on the screen for decimal numbers anyway. Here's a screenshot with the "GPS" turned on. You can toggle it on and off with the G key. that way it isn't overlaying your screens the whole time.

Image

Posted: Fri Oct 23, 2009 1:31 pm
by Mike
adric22 wrote:... I don't think there would really be enough room on the screen for decimal numbers anyway.
(At least) three points to note here:

- The VIC chip allows you to change the number of displayed rows, and columns,
- It is possible to dedicate 2 UDG's to a certain number, and produce a 3-digit display with 5 pixel wide digits, or even a 4-digit display with 4 pixel wide digits,
- For an example of a table-driven conversion, you might take a look at the display of TI$ in the border I wrote some time ago. It's a no-brainer to adjust the table to numbers of different size, and base.

Michael

Posted: Fri Oct 23, 2009 3:40 pm
by adric22
Mike wrote:
adric22 wrote:... I don't think there would really be enough room on the screen for decimal numbers anyway.
(At least) three points to note here:

- The VIC chip allows you to change the number of displayed rows, and columns,
- It is possible to dedicate 2 UDG's to a certain number, and produce a 3-digit display with 5 pixel wide digits, or even a 4-digit display with 4 pixel wide digits,
- For an example of a table-driven conversion, you might take a look at the display of TI$ in the border I wrote some time ago. It's a no-brainer to adjust the table to numbers of different size, and base.
I had already thought about doing #2, but it is just too much work. I've never heard of #1. How many columns can you get out of the VIC? I thought 22 was the max.

Posted: Sat Oct 24, 2009 6:04 am
by Mike
adric22 wrote:How many columns can you get out of the VIC?
That depends on whether you're using PAL or NTSC. With PAL, you easily can display 26 columns, and 32 rows. NTSC is more restricted. In most cases you don't get beyond 24 columns, and 27 or 28 rows.

This is because PAL VICs display a bigger border to begin with. The absolute size of the character cells isn't changed for this.

The number of rows, and columns, and horizontal, and vertical positioning are controlled by the VIC registers 36864 .. 36867. The default register values are stored from $EDE4 onwards, and they are different between PAL, and NTSC.

Posted: Sat Oct 24, 2009 5:46 pm
by Richard James
I wrote a Binary to BCD subroutine for the SX28 recently. I posted some notes for how it works on my blog http://richardjames13.wordpress.com/200 ... onversion/