"Hello, World!" with cc65

Chat about anything CX16 related that doesn't fit elsewhere
SlithyMatt
Posts: 913
Joined: Tue Apr 28, 2020 2:45 am

"Hello, World!" with cc65

Post by SlithyMatt »



2 hours ago, kliepatsch said:




. To me, the 2061 still seems like a magic number. Where does it come from?



 



2061 = $080D

It's simply the start of the machine language program. Before that is just tokenized BASIC and the CPU would choke trying to run that directly, instead of with the BASIC interpreter.

geek504
Posts: 95
Joined: Wed Aug 26, 2020 4:52 pm

"Hello, World!" with cc65

Post by geek504 »



45 minutes ago, SlithyMatt said:




2061 = $080D



It's simply the start of the machine language program. Before that is just tokenized BASIC and the CPU would choke trying to run that directly, instead of with the BASIC interpreter.



My hello.asm does a byte by byte commentary on these magic numbers before your assembly code:


Quote




 



;

; cl65 -o hello2.prg -t cx16 -C cx16-asm.cfg hello2.asm

;

; Also, you should count on CHROUT modifying X, so you should flank that jsr with phx and plx.

; Run with x16emu.exe -prg hello2.prg -run -scale 2



    .org $0801                  ; Assembled code should start at $0801

                                ; (where BASIC programs start)

                                ; The real program starts at $0810 = 2064



                                                 ; 10 SYS 2064

    .byte $0C, $08                    ; $080C - pointer to next line of BASIC code

    .byte $0A, $00                    ; 2-byte line number ($000A = 10)

    .byte $9E                             ; SYS BASIC token

    .byte $20                             ; [space]

    .byte $32, $30, $36, $34   ; $32="2",$30="0",$36="6",$34="4"

    .byte $00                             ; End of Line

    .byte $00, $00                     ; This is address $080C containing

                                                  ; 2-byte pointer to next line of BASIC code

                                                  ; ($0000 = end of program)

    .byte $00, $00                     ; Padding so code starts at $0810



    CHROUT = $FFD2



    ldx #0

again:

    lda hello, x

    cmp #0

    beq done

    stx SaveX

    jsr CHROUT

    ldx SaveX

    inx

    jmp again

done:

    rts

SaveX: .byte 0

hello:

    .byte   "hello world! ", $00



 



Enjoy!

SlithyMatt
Posts: 913
Joined: Tue Apr 28, 2020 2:45 am

"Hello, World!" with cc65

Post by SlithyMatt »


You can lose that CMP #0 instruction. It's unnecessary. The LDA before already sets the Z bit the exact same way.

geek504
Posts: 95
Joined: Wed Aug 26, 2020 4:52 pm

"Hello, World!" with cc65

Post by geek504 »



On 10/10/2020 at 11:18 PM, SlithyMatt said:




You can lose that CMP #0 instruction. It's unnecessary. The LDA before already sets the Z bit the exact same way.



Point noted, just as the stx/ldx SaveX could be replaced by the most efficient phx/plx to save a few bytes more. Sometimes a more verbose code explains better than an efficient code ? Efficient code sometimes look like magic!

MontyHall
Posts: 8
Joined: Tue Oct 13, 2020 11:04 pm

"Hello, World!" with cc65

Post by MontyHall »


I can't get hello world to work.  I'm running ubuntu 20 and compiled cc65.

When I make I get the following:


(base) rick@rick:~/workspace/x16-hello-cc65$ make

make -C asm

make[1]: Entering directory '/home/rick/workspace/x16-hello-cc65/asm'

cl65 -t cx16 -o HELLOASM.PRG -l hello.list hello.asm

ld65: Error: Cannot find config file 'cx16.cfg'

make[1]: *** [Makefile:4: all] Error 1

make[1]: Leaving directory '/home/rick/workspace/x16-hello-cc65/asm'

make: *** [Makefile:2: all] Error 2


I have cc65/bin in my path. I suspect I need to hack the make file to point to configs.

MontyHall
Posts: 8
Joined: Tue Oct 13, 2020 11:04 pm

"Hello, World!" with cc65

Post by MontyHall »


What's the performance penalty of using cc65 C over pure assembly?  Have to admit would like to rapid prototype in C w/o the assembly albatross.  Would you say cc65 C is 2x slower?  It certainly can't be worse than basic 2.0.

User avatar
desertfish
Posts: 1096
Joined: Tue Aug 25, 2020 8:27 pm
Location: Netherlands

"Hello, World!" with cc65

Post by desertfish »


It should be way way faster than basic.  Some micro benchmarks comparing several languages (including raw assembly)  is here https://github.com/KarolS/millfork-benchmarks/tree/master/6502

 

MontyHall
Posts: 8
Joined: Tue Oct 13, 2020 11:04 pm

"Hello, World!" with cc65

Post by MontyHall »


That's impressive.  Reasonable hit in speed using cc65 C.

geek504
Posts: 95
Joined: Wed Aug 26, 2020 4:52 pm

"Hello, World!" with cc65

Post by geek504 »



1 hour ago, MontyHall said:




What's the performance penalty of using cc65 C over pure assembly?  Have to admit would like to rapid prototype in C w/o the assembly albatross.  Would you say cc65 C is 2x slower?  It certainly can't be worse than basic 2.0.



Just don't do a printf() version of "Hello, World!" That'll generate a binary of at least 2,000 bytes! Use CHROUT or STROUT instead with assembly or C using a specific X16 library.

geek504
Posts: 95
Joined: Wed Aug 26, 2020 4:52 pm

"Hello, World!" with cc65

Post by geek504 »



1 hour ago, MontyHall said:




I have cc65/bin in my path.



You need other directories defined as well... here's my Windows batch file I run before compiling with cc65:


Quote




set CC65_HOME=c:\cc65

set CA65_INC=c:\cc65\asminc

set CC65_INC=c:\cc65\include

set LD65_LIB=c:\cc65\lib

set LD65_CFG=c:\cc65\cfg

set LD65_OBJ=c:\cc65\obj

set PATH=%PATH%;c:\cc65\bin



 

Post Reply