best way to display decimal number?

Basic and Machine Language

Moderator: Moderators

Post Reply
adric22
Vic 20 Hobbyist
Posts: 143
Joined: Fri Mar 11, 2005 6:54 pm

best way to display decimal number?

Post 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?
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post 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.
carlsson
Class of '6502
Posts: 5516
Joined: Wed Mar 10, 2004 1:41 am

Post 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.
Anders Carlsson

Image Image Image Image Image
adric22
Vic 20 Hobbyist
Posts: 143
Joined: Fri Mar 11, 2005 6:54 pm

Post 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.
carlsson
Class of '6502
Posts: 5516
Joined: Wed Mar 10, 2004 1:41 am

Post 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?
Anders Carlsson

Image Image Image Image Image
Diddl
Vic 20 Afficionado
Posts: 425
Joined: Wed Jun 10, 2009 3:18 am

Post 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
adric22
Vic 20 Hobbyist
Posts: 143
Joined: Fri Mar 11, 2005 6:54 pm

Post 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
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post 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
adric22
Vic 20 Hobbyist
Posts: 143
Joined: Fri Mar 11, 2005 6:54 pm

Post 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.
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post 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.
Richard James
Vic 20 Devotee
Posts: 269
Joined: Mon Feb 04, 2008 6:06 am

Post 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/
Change is inevitable except from a vending machine.
Post Reply