displaying a decimal number... using 2 byte value...
-
LASERACTIVEGUY
- Posts: 18
- Joined: 29 Dec 2015
displaying a decimal number... using 2 byte value...
If you have a multi-byte value... for instance... say b1= (2) b2= (2) that equals 514 in decimal... how would you code... loading those two bytes to print the decimal representation...?
-
LASERACTIVEGUY
- Posts: 18
- Joined: 29 Dec 2015
Re: displaying a decimal number... using 2 byte value...
This is reference material... question afterwards....
Here is an equivalent routine for converting 16-bit numbers: ; Convert an 16 bit binary value to BCD
;
; This function converts a 16 bit binary value into a 24 bit BCD. It
; works by transferring one bit a time from the source and adding it
; into a BCD value that is being doubled on each iteration. As all the
; arithmetic is being done in BCD the result is a binary to decimal
; conversion. All conversions take 915 clock cycles.
;
; See BINBCD8 for more details of its operation.
;
; Andrew Jacobs, 28-Feb-2004
.ORG $0200
BINBCD16: SED ; Switch to decimal mode
LDA #0 ; Ensure the result is clear
STA BCD+0
STA BCD+1
STA BCD+2
LDX #16 ; The number of source bits
CNVBIT: ASL BIN+0 ; Shift out one bit
ROL BIN+1
LDA BCD+0 ; And add into result
ADC BCD+0
STA BCD+0
LDA BCD+1 ; propagating any carry
ADC BCD+1
STA BCD+1
LDA BCD+2 ; ... thru whole result
ADC BCD+2
STA BCD+2
DEX ; And repeat for next bit
BNE CNVBIT
CLD ; Back to binary
BRK ; All Done.
; A test value to be converted
.ORG $0300
BIN .DW 12345
BCD .DS 3
Here is an equivalent routine for converting 16-bit numbers: ; Convert an 16 bit binary value to BCD
;
; This function converts a 16 bit binary value into a 24 bit BCD. It
; works by transferring one bit a time from the source and adding it
; into a BCD value that is being doubled on each iteration. As all the
; arithmetic is being done in BCD the result is a binary to decimal
; conversion. All conversions take 915 clock cycles.
;
; See BINBCD8 for more details of its operation.
;
; Andrew Jacobs, 28-Feb-2004
.ORG $0200
BINBCD16: SED ; Switch to decimal mode
LDA #0 ; Ensure the result is clear
STA BCD+0
STA BCD+1
STA BCD+2
LDX #16 ; The number of source bits
CNVBIT: ASL BIN+0 ; Shift out one bit
ROL BIN+1
LDA BCD+0 ; And add into result
ADC BCD+0
STA BCD+0
LDA BCD+1 ; propagating any carry
ADC BCD+1
STA BCD+1
LDA BCD+2 ; ... thru whole result
ADC BCD+2
STA BCD+2
DEX ; And repeat for next bit
BNE CNVBIT
CLD ; Back to binary
BRK ; All Done.
; A test value to be converted
.ORG $0300
BIN .DW 12345
BCD .DS 3
-
LASERACTIVEGUY
- Posts: 18
- Joined: 29 Dec 2015
Re: displaying a decimal number... using 2 byte value...
I need HELP... i am a serious idiot... The 6502 LINK above this one has two routines... I coded it exactly as above (2nd routine)... placing in one set of numbers at 25000 and the other at 20000 (25001, 25002 ect, 20000, 20001)... if I input in the two original positions 1 and 1 (signifying a number of 256... my outputs are 100, 8, 98 --- That makes no sense to me... i post code afterward... If those numbers are correct... the result of 1*256+1= 100,898.... am I missing something?
-
LASERACTIVEGUY
- Posts: 18
- Joined: 29 Dec 2015
Re: displaying a decimal number... using 2 byte value...
----------------------------DEC ASSEMBLY
30000- F8 SED
30001- A9 00 LDA #0
30003- 8D 20 4E STA 20000
30006- 8D 21 4E STA 20001
30009- 8D 22 4E STA 20002
30012- 0E A8 61 ASL 25000
30015- 2E A9 61 ROL 25001
30018- AD 20 4E LDA 20000
30021- 6D 20 4E ADC 20000
30024- 8D 20 4E STA 20000
30027- AD 21 4E LDA 20001
30030- 6D 21 4E ADC 20001
30033- 8D 21 4E STA 20001
30036- AD 22 4E LDA 20002
30039- 6D 22 4E ADC 20002
30042- 8D 22 4E STA 20002
30045- CA DEX
30046- D0 DC BNE 30012
30048- D8 CLD
30049- 60 RTS
-------------------------------HEX ASSEMBLY
7530- F8 SED
7531- A9 00 LDA #$00
7533- 8D 20 4E STA $4E20
7536- 8D 21 4E STA $4E21
7539- 8D 22 4E STA $4E22
753C- 0E A8 61 ASL $61A8
753F- 2E A9 61 ROL $61A9
7542- AD 20 4E LDA $4E20
7545- 6D 20 4E ADC $4E20
7548- 8D 20 4E STA $4E20
754B- AD 21 4E LDA $4E21
754E- 6D 21 4E ADC $4E21
7551- 8D 21 4E STA $4E21
7554- AD 22 4E LDA $4E22
7557- 6D 22 4E ADC $4E22
755A- 8D 22 4E STA $4E22
755D- CA DEX
755E- D0 DC BNE $753C
7560- D8 CLD
7561- 60 RTS
10 PRINT CHR$(4);"BLOAD TEST"
11 POKE 25000,0:POKE 25001,1
12 CALL 30000:PRINT PEEK(20000),PEEK(20001),PEEK(20002)
13 REM... LOADING ROUTINE ABOVE...
14 REM PUTTING 0 AND 1 INTO LOCATIONS...
15 REM LOADING, CALLING ROUTINE... PRINTING RESULTS
30000- F8 SED
30001- A9 00 LDA #0
30003- 8D 20 4E STA 20000
30006- 8D 21 4E STA 20001
30009- 8D 22 4E STA 20002
30012- 0E A8 61 ASL 25000
30015- 2E A9 61 ROL 25001
30018- AD 20 4E LDA 20000
30021- 6D 20 4E ADC 20000
30024- 8D 20 4E STA 20000
30027- AD 21 4E LDA 20001
30030- 6D 21 4E ADC 20001
30033- 8D 21 4E STA 20001
30036- AD 22 4E LDA 20002
30039- 6D 22 4E ADC 20002
30042- 8D 22 4E STA 20002
30045- CA DEX
30046- D0 DC BNE 30012
30048- D8 CLD
30049- 60 RTS
-------------------------------HEX ASSEMBLY
7530- F8 SED
7531- A9 00 LDA #$00
7533- 8D 20 4E STA $4E20
7536- 8D 21 4E STA $4E21
7539- 8D 22 4E STA $4E22
753C- 0E A8 61 ASL $61A8
753F- 2E A9 61 ROL $61A9
7542- AD 20 4E LDA $4E20
7545- 6D 20 4E ADC $4E20
7548- 8D 20 4E STA $4E20
754B- AD 21 4E LDA $4E21
754E- 6D 21 4E ADC $4E21
7551- 8D 21 4E STA $4E21
7554- AD 22 4E LDA $4E22
7557- 6D 22 4E ADC $4E22
755A- 8D 22 4E STA $4E22
755D- CA DEX
755E- D0 DC BNE $753C
7560- D8 CLD
7561- 60 RTS
10 PRINT CHR$(4);"BLOAD TEST"
11 POKE 25000,0:POKE 25001,1
12 CALL 30000:PRINT PEEK(20000),PEEK(20001),PEEK(20002)
13 REM... LOADING ROUTINE ABOVE...
14 REM PUTTING 0 AND 1 INTO LOCATIONS...
15 REM LOADING, CALLING ROUTINE... PRINTING RESULTS
- BigDumbDinosaur
- Posts: 9428
- Joined: 28 May 2009
- Location: Midwestern USA (JB Pritzker’s dystopia)
- Contact:
Re: displaying a decimal number... using 2 byte value...
I'm confused. What is it you want to know?
Also, please surround your code with the CODE tags to improve readability. For example:
Also, please surround your code with the CODE tags to improve readability. For example:
Code: Select all
30000- F8 SED
30001- A9 00 LDA #0
30003- 8D 20 4E STA 20000
30006- 8D 21 4E STA 20001
30009- 8D 22 4E STA 20002
30012- 0E A8 61 ASL 25000
30015- 2E A9 61 ROL 25001
30018- AD 20 4E LDA 20000
30021- 6D 20 4E ADC 20000
30024- 8D 20 4E STA 20000
30027- AD 21 4E LDA 20001
30030- 6D 21 4E ADC 20001
30033- 8D 21 4E STA 20001
30036- AD 22 4E LDA 20002
30039- 6D 22 4E ADC 20002
30042- 8D 22 4E STA 20002
30045- CA DEX
30046- D0 DC BNE 30012
30048- D8 CLD
30049- 60 RTS x86? We ain't got no x86. We don't NEED no stinking x86!
-
LASERACTIVEGUY
- Posts: 18
- Joined: 29 Dec 2015
Re: displaying a decimal number... using 2 byte value...
If I have 16 bit number... say (2) (2) that means its 2*256+2 or 512+2 or 514 final result. How do I output that 514 to the screen... (its apple ii, I know the output routines....) how do I get the two bytes to equal 5 1 4 so I can output them... or similar?!?
-
LASERACTIVEGUY
- Posts: 18
- Joined: 29 Dec 2015
Re: displaying a decimal number... using 2 byte value...
I forgot... cant use tables... its actually going into a Compiler... now I really sound off my rocker.. cant program ASM but making a compiler..... don't worry bout me... I had to make an assembler before I could program ASM at all... long story!
- GARTHWILSON
- Forum Moderator
- Posts: 8775
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: displaying a decimal number... using 2 byte value...
Separate out the low nybble with AND #$0F (keeping a copy of the original for the high nybble), and separate out the nigh nybble with LSR LSR LSR LSR. After separating out a nybble for a decimal digit, just add $30 to get the ASCII representation; so 1 is $31, 2 is $32, etc.. (For hex digits, there's a gap between 9 at $39 and A at $41 rather than $3A.)
There's no reason a compiler can't use a table; but you don't need a table here anyway.
There's no reason a compiler can't use a table; but you don't need a table here anyway.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Re: displaying a decimal number... using 2 byte value...
From VTL02SG:
Code: Select all
; - - - - - - - - - - - - - - - - - - - - - - - - - - ;
; Print an unsigned decimal number (0..65535) in var[x]
; entry: var[x] = number to print
; uses: outch:, gthan
; exit: var[x] = 0
;
prnum:
lda #0 ; null delimiter for print
pha
prnum2: ; divide var[x] by 10
lda #0
sta gthan+1 ; clr BCD
lda #16
sta gthan ; {>} = loop counter
prdiv1:
asl 0,x ; var[x] is gradually replaced
rol 1,x ; with the quotient
rol gthan+1 ; BCD result is gradually replaced
lda gthan+1 ; with the remainder
sec
sbc #10 ; partial BCD >= 10 ?
bcc prdiv2
sta gthan+1 ; yes: update the partial result
inc 0,x ; set low bit in partial quotient
prdiv2:
dec gthan
bne prdiv1 ; loop 16 times
lda gthan+1
ora #'0' ; convert BCD result to ASCII
pha ; stack digits in ascending
lda 0,x ; order ('0' for zero)
ora 1,x
bne prnum2 ; } until var[x] is 0
pla
prnum3:
jsr outch ; print digits in descending
pla ; order until delimiter is
bne prnum3 ; encountered
rts6502 sources on GitHub: https://github.com/Klaus2m5
- BigDumbDinosaur
- Posts: 9428
- Joined: 28 May 2009
- Location: Midwestern USA (JB Pritzker’s dystopia)
- Contact:
Re: displaying a decimal number... using 2 byte value...
LASERACTIVEGUY wrote:
I forgot... cant use tables... its actually going into a Compiler... now I really sound off my rocker.. cant program ASM but making a compiler..... don't worry bout me... I had to make an assembler before I could program ASM at all... long story!
x86? We ain't got no x86. We don't NEED no stinking x86!
-
LASERACTIVEGUY
- Posts: 18
- Joined: 29 Dec 2015
Re: displaying a decimal number... using 2 byte value...
I had somebody from the Apple II forum help me with a specific routine that uses partial ROM routines to help... still not concise code but it works (*Only for APPLE II 6502)
Place your 3 binary values (24bit number) into Y,X,A from highest to lowest...
Thank you for your replies.... anybody make anything INCREDIBLY cool lately?
Place your 3 binary values (24bit number) into Y,X,A from highest to lowest...
Code: Select all
30032- AC 39 75 LDY 30009
30035- AE 38 75 LDX 30008
30038- AD 37 75 LDA 30007
30041- 20 4A FF JSR 65354
30044- A9 00 LDA #0
30046- B8 CLV
30047- A2 18 LDX #24
30049- C9 05 CMP #5
30051- 90 03 BCC 30056
30053- E9 85 SBC #133
30055- 38 SEC
30056- 26 69 ROL 105
30058- 26 70 ROL 112
30060- 26 71 ROL 113
30062- 2A ROL
30063- CA DEX
30064- D0 EF BNE 30049
30066- 48 PHA
30067- A9 FD LDA #253
30069- 48 PHA
30070- A9 E1 LDA #225
30072- 48 PHA
30073- 70 E1 BVS 30044
30075- 60 RTS Thank you for your replies.... anybody make anything INCREDIBLY cool lately?
- barrym95838
- Posts: 2056
- Joined: 30 Jun 2013
- Location: Sacramento, CA, USA
Re: displaying a decimal number... using 2 byte value...
John Brooks is a big name in the 8 and 16-bit Apple retro scene, and I'm "humbly proud" to say that he personally complimented my little binary-to-ASCII routine suggestion recently.
https://twitter.com/JBrooksBSI/status/8 ... 8794545153
https://groups.google.com/forum/#!topic ... y27d_TxDHA
Mike B.
P.S. Hey, I found your thread, so I can see that John has already hooked you up.
https://groups.google.com/forum/#!topic ... pYoWYCdXf0
That routine that he offered still has my TenDivMod DNA in there, but he enhanced it with 8 more bits, a V-flag trick, and a nifty chained RTS trick.
P.P.S. Your code above will not work, because you're ROLing the wrong zero-page addresses. A, X and Y are stored in $45, $46 and $47 respectively after the call to $FF4A.
https://twitter.com/JBrooksBSI/status/8 ... 8794545153
https://groups.google.com/forum/#!topic ... y27d_TxDHA
Mike B.
P.S. Hey, I found your thread, so I can see that John has already hooked you up.
https://groups.google.com/forum/#!topic ... pYoWYCdXf0
That routine that he offered still has my TenDivMod DNA in there, but he enhanced it with 8 more bits, a V-flag trick, and a nifty chained RTS trick.
P.P.S. Your code above will not work, because you're ROLing the wrong zero-page addresses. A, X and Y are stored in $45, $46 and $47 respectively after the call to $FF4A.
Re: displaying a decimal number... using 2 byte value...
I was needing to update some decimal printout code. I was initially using a routine that used a tables of powers of ten, but was needing to be able to print up to 32-bit values and didn't like the 40 bytes needed for the tens table. After some searching I found this thread, and the link to John Brooks' "clever V flag" routine.
It was almost exactly what I needed, except I needed two additions:
* I needed to be able to specify where the supplied number was, ideally in 0,X format
* I needed to be able to pad with leading spaces.
The following is my adaptation:
It was almost exactly what I needed, except I needed two additions:
* I needed to be able to specify where the supplied number was, ideally in 0,X format
* I needed to be able to pad with leading spaces.
The following is my adaptation:
Code: Select all
* Print up to 32-bit unsigned decimal number
********************************************
* See forum.6502.org/viewtopic.php?f=2&t=4894
* and groups.google.com/g/comp.sys.apple2/c/_y27d_TxDHA
*
* On entry:
* X=>base of four-byte zero page locations
* Y= number of digits to pad to, 0 for no padding
* Can print fewer than 32 bits by setting higher bytes
* to zero and setting Y appropriately
*
* On exit:
* The four bytes at 0,X to 3,X are set to zero
* X=preserved
* A,Y corrupted
*
* Needs OSPAD = to hold pad count
* OSTEMP = bit counter
* OSWRCH = routine to display a character
*
PRINTDEC sty OSPAD ; Number of padding+digits
ldy #0 ; Digit counter
PRDECDIGIT lda #32 ; 32-bit divide
sta OSTEMP
lda #0 ; Remainder=0
clv ; V=0 means div result = 0
PRDECDIV10 cmp #10/2 ; Calculate OSNUM/10
bcc PRDEC10
sbc #10/2+$80 ; Remove digit & set V=1 to show div result > 0
sec ; Shift 1 into div result
PRDEC10 rol 0,x ; Shift /10 result into OSNUM
rol 1,x
rol 2,x
rol 3,x
rol a ; Shift bits of input into acc (input mod 10)
dec OSTEMP
bne PRDECDIV10 ; Continue 32-bit divide
ora #48
pha ; Push low digit 0-9 to print
iny
bvs PRDECDIGIT ; If V=1, result of /10 was > 0 & do next digit
lda #32
PRDECLP1 cpy OSPAD
bcs PRDECLP2 ; Enough padding pushed
pha ; Push leading space characters
iny
bne PRDECLP1
PRDECLP2 pla ; Pop character left to right
jsr OSWRCH ; Print it
dey
bne PRDECLP2
rts
* Example code:
* LDX #OSNUM ; X=>four bytes in zero page
* LDY #8 ; Y=pad to 8 digits
* JSR PRINTDEC
--
JGH - http://mdfs.net
JGH - http://mdfs.net
- BigDumbDinosaur
- Posts: 9428
- Joined: 28 May 2009
- Location: Midwestern USA (JB Pritzker’s dystopia)
- Contact:
Re: displaying a decimal number... using 2 byte value...
jgharston wrote:
I was needing to update some decimal printout code. I was initially using a routine that used a tables of powers of ten, but was needing to be able to print up to 32-bit values and didn't like the 40 bytes needed for the tens table. After some searching I found this thread, and the link to John Brooks' "clever V flag" routine.
It was almost exactly what I needed, except I needed two additions:
* I needed to be able to specify where the supplied number was, ideally in 0,X format
* I needed to be able to pad with leading spaces.
It was almost exactly what I needed, except I needed two additions:
* I needed to be able to specify where the supplied number was, ideally in 0,X format
* I needed to be able to pad with leading spaces.
The Code page on this site is your friend. Scroll to the bottom and you'll see a link to code that will convert a 32-bit integer to ASCII in any one of four number bases.
x86? We ain't got no x86. We don't NEED no stinking x86!
- barrym95838
- Posts: 2056
- Joined: 30 Jun 2013
- Location: Sacramento, CA, USA
Re: displaying a decimal number... using 2 byte value...
I think he's looking for something much lighter than that, BDD. Otherwise, he wouldn't have been annoyed by 40 bytes of tables. Maybe something along the lines of this? (I realize it still needs to be modified to meet the requested spec, but I don't think I can significantly improve on the subroutine jgharston posted ... well done, sir!)
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!
Mike B. (about me) (learning how to github)
Mike B. (about me) (learning how to github)