Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
 Welcome to our latest new user Abynx ! (Registered 2024-06-09) You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > Fastest time printing binary in BASIC or Assembly
2024-01-11 08:49
Mr SQL

Registered: Feb 2023
Posts: 121
Fastest time printing binary in BASIC or Assembly

Interesting Video on 8-bit show and tell:
https://www.youtube.com/watch?v=P8t6otqoz_E

What's the fastest time you can print binary to the screen in BASIC or Assembly?

I got it down to 26 seconds in BASIC without a pre-calc routine.
2024-01-11 11:42
spider-j

Registered: Oct 2004
Posts: 450
To be fair I also used the KERNAL output routine for printing the chars.

Small solution with loops / 78 Bytes PRG:
https://trans.jansalleine.com/c64/num2binary.prg
                    !cpu 6510
; ==============================================================================
save_num    = 0x02
CHAROUT     = 0xF1CA
; ==============================================================================
                    *= 0x0801
                    ; basic TI$ timer wrapper program:
                    ; --------------------------------
                    ; 0 TI$="000000":SYS2092
                    ; 1 PRINT"TIME:"TI/60
                    ; --------------------------------
                    ; RESULT: 8.91666667
                    !byte 0x18, 0x08, 0x00, 0x00
                    !byte 0x54, 0x49, 0x24, 0xB2
                    !byte 0x22, 0x30, 0x30, 0x30
                    !byte 0x30, 0x30, 0x30, 0x22
                    !byte 0x3A, 0x9E, 0x32, 0x30
                    !byte 0x39, 0x32, 0x00, 0x2A
                    !byte 0x08, 0x01, 0x00, 0x99
                    !byte 0x22, 0x54, 0x49, 0x4D
                    !byte 0x45, 0x3A, 0x22, 0x54
                    !byte 0x49, 0xAD, 0x36, 0x30
                    !byte 0x00, 0x00, 0x00
; ==============================================================================
                    *= 0x082C
                    ldy #0
--                  sty save_num
                    ldx #0
-                   clc
                    rol save_num
                    bcc +
                    lda #'1'
                    !byte 0x2C
+                   lda #'0'
                    jsr CHAROUT
                    inx
                    cpx #8
                    bne -
                    lda #0x0D
                    jsr CHAROUT
                    iny
                    bne --
                    rts


Slightly faster solution with unrolled loops / 28974 Bytes PRG:
https://trans.jansalleine.com/c64/num2binary_unrolled.prg
                    !cpu 6510
; ==============================================================================
save_num    = 0x02
CHAROUT     = 0xF1CA
; ==============================================================================
                    *= 0x0801
                    ; basic TI$ timer wrapper program:
                    ; --------------------------------
                    ; 0 TI$="000000":SYS2092
                    ; 1 PRINT"TIME:"TI/60
                    ; --------------------------------
                    ; RESULT: 8.9
                    !byte 0x18, 0x08, 0x00, 0x00
                    !byte 0x54, 0x49, 0x24, 0xB2
                    !byte 0x22, 0x30, 0x30, 0x30
                    !byte 0x30, 0x30, 0x30, 0x22
                    !byte 0x3A, 0x9E, 0x32, 0x30
                    !byte 0x39, 0x32, 0x00, 0x2A
                    !byte 0x08, 0x01, 0x00, 0x99
                    !byte 0x22, 0x54, 0x49, 0x4D
                    !byte 0x45, 0x3A, 0x22, 0x54
                    !byte 0x49, 0xAD, 0x36, 0x30
                    !byte 0x00, 0x00, 0x00
; ==============================================================================
                    *= 0x082C
                    !for i, 0, 255 {
                         lda #i
                         sta save_num
                         !for j, 0, 7 {
                              clc
                              rol save_num
                              bcc +
                              lda #'1'
                              !byte 0x2C
+                             lda #'0'
                              jsr CHAROUT
                         }
                         lda #0x0D
                         jsr CHAROUT
                    }
                    rts


EDIT: corrected ror -> rol.
2024-01-11 12:00
spider-j

Registered: Oct 2004
Posts: 450
Ah, no need to clear carry. Doesn't change anything in the loop version though. But unrolled version gets down to 8.88333334:
                    !cpu 6510
; ==============================================================================
save_num    = 0x02
CHAROUT     = 0xF1CA
; ==============================================================================
                    *= 0x0801
                    ; basic TI$ timer wrapper program:
                    ; --------------------------------
                    ; 0 TI$="000000":SYS2092
                    ; 1 PRINT"TIME:"TI/60
                    ; --------------------------------
                    ; RESULT: 8.88333334
                    !byte 0x18, 0x08, 0x00, 0x00
                    !byte 0x54, 0x49, 0x24, 0xB2
                    !byte 0x22, 0x30, 0x30, 0x30
                    !byte 0x30, 0x30, 0x30, 0x22
                    !byte 0x3A, 0x9E, 0x32, 0x30
                    !byte 0x39, 0x32, 0x00, 0x2A
                    !byte 0x08, 0x01, 0x00, 0x99
                    !byte 0x22, 0x54, 0x49, 0x4D
                    !byte 0x45, 0x3A, 0x22, 0x54
                    !byte 0x49, 0xAD, 0x36, 0x30
                    !byte 0x00, 0x00, 0x00
; ==============================================================================
                    *= 0x082C
                    !for i, 0, 255 {
                         lda #i
                         sta save_num
                         !for j, 0, 7 {
                              asl save_num
                              bcc +
                              lda #'1'
                              !byte 0x2C
+                             lda #'0'
                              jsr CHAROUT
                         }
                         lda #0x0D
                         jsr CHAROUT
                    }
                    rts
2024-01-11 12:13
spider-j

Registered: Oct 2004
Posts: 450
Hm. Okay, measuring time with TI$ seems very unreliable. At least when using VICE autostart and / or warp. Someone should test it on real machine. But even there I'm not sure if TI$ is consistent (?) ...
2024-01-11 13:43
Jetboy

Registered: Jul 2006
Posts: 232
Probably not good enough to time machine language code.(TI$)
2024-01-11 16:16
Martin Piper

Registered: Nov 2007
Posts: 647
If you're using Vice use the monitor command "stopwatch" (or "sw") to reset and display the stopwatch. Can be used at the start of the run command until it returns by adding a couple of break points. Also remember to SEI early to avoid the regular IRQ.
2024-01-11 18:33
Frantic

Registered: Mar 2003
Posts: 1633
This probably does not count, but something like this seems obvious if speed is really the priority. This one assumes that you have a custom charset that has a "0" char at position 0 and a "1" char at position 1 in the charset, and it will always print the result at a specific fixed location on the screen.

;a register contains the byte to display as binary
ldx #1
sax $0407
lsr a
sax $0406
lsr a
sax $0405
lsr a
sax $0404
lsr a
sax $0403
lsr a
sax $0402
lsr a
sax $0401
lsr a
sax $0400

=8*2+8*4=48 cycles, so you could do it thousands of times in a single second.

SAX is one of the illegal opcodes, in case someone happens to be unfamiliar with that:
http://unusedino.de/ec64/technical/aay/c64/bsax.htm
2024-01-11 19:02
TWW

Registered: Jul 2009
Posts: 541
I Watched the video, and it seemed that the criteria was that it was supposed to be possible to invoke it from normal BASIC and that printing the result to the screen should update cursor etc like the print command does. Not to familiar with BASIC, but I figgure 'unrolling' some of the loops he tested should help. Also the assembler code snippet relied heavily on BASIC to fetch parameters and print. A specialized routine would be much faster but hell, what a hazzle :D
2024-01-11 20:48
spider-j

Registered: Oct 2004
Posts: 450
Quoting Martin Piper
If you're using Vice use the monitor command "stopwatch" (or "sw") to reset and display the stopwatch. Can be used at the start of the run command until it returns by adding a couple of break points. Also remember to SEI early to avoid the regular IRQ.

Well in the mentioned video the TI$ basic snippet is used to track the time. So I stuck to it, because otherwise it wouldn't be comparable at all.
2024-01-11 21:03
ws

Registered: Apr 2012
Posts: 235
Am i missing something here or is the most time actually wasted by kernal scrolling the output? Why not poke the results to the screen to a fixed location? Or even use chr$(19) aka home?
2024-01-11 21:14
chatGPZ

Registered: Dec 2001
Posts: 11154
Thats probably correct - so just a faster scroll routine would speed this up signficantly :)
 
... 29 posts hidden. Click here to view all posts....
 
Previous - 1 | 2 | 3 | 4 - Next
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
Earthshaker/Silicon ..
Ghost/Quantum
WVL/Xenon
t0m3000/HF^BOOM!^IBX
Krill/Plush
Guests online: 92
Top Demos
1 Next Level  (9.7)
2 Mojo  (9.7)
3 13:37  (9.7)
4 Coma Light 13  (9.7)
5 Edge of Disgrace  (9.7)
6 Aliens in Wonderland  (9.6)
7 Comaland 100%  (9.6)
8 Uncensored  (9.6)
9 No Bounds  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 Layers  (9.6)
2 Cubic Dream  (9.6)
3 Party Elk 2  (9.6)
4 Copper Booze  (9.6)
5 TRSAC, Gabber & Pebe..  (9.5)
6 Rainbow Connection  (9.5)
7 It's More Fun to Com..  (9.5)
8 Dawnfall V1.1  (9.5)
9 Daah, Those Acid Pil..  (9.5)
10 Birth of a Flower  (9.5)
Top Groups
1 Nostalgia  (9.4)
2 Oxyron  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 SHAPE  (9.3)
Top NTSC-Fixers
1 Pudwerx  (10)
2 Booze  (9.7)
3 Stormbringer  (9.7)
4 Fungus  (9.6)
5 Grim Reaper  (9.3)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.188 sec.