{"id":38,"date":"2019-09-20T12:46:00","date_gmt":"2019-09-20T11:46:00","guid":{"rendered":"https:\/\/techblog.dansbo.dk\/?p=38"},"modified":"2024-01-03T13:03:52","modified_gmt":"2024-01-03T12:03:52","slug":"hello-world-on-commander-x16","status":"publish","type":"post","link":"https:\/\/techblog.dansbo.dk\/?p=38","title":{"rendered":"Hello, World on Commander X16"},"content":{"rendered":"\n<p>First things first. I have been following\u00a0<a href=\"https:\/\/www.facebook.com\/groups\/CommanderX16\/\">The 8-bit Guys dream computer<\/a>\u00a0for quite a while now and finally decided that I would like to try my hand on some assembler programming for it.<\/p>\n\n\n\n<p>I grew up with the Commodore 64, but never got beyond BASIC programming on it. Instead I jumped to the PC and went from BASIC to Pascal and later on to C and C++<\/p>\n\n\n\n<p>It turned out it was harder than I thought to get started with assembler programming. First I needed to find a compiler\/assember. I ended up with with the ACME Cross-Assembler which can be found here:\u00a0<a href=\"https:\/\/sourceforge.net\/projects\/acme-crossass\/\">https:\/\/sourceforge.net\/projects\/acme-crossass\/<\/a>\u00a0there are also versions available for Linux\/Ubuntu.<\/p>\n\n\n\n<p>Next I needed to learn a bit of assembly language for the 6502 processor and found a great guide here:\u00a0<a href=\"http:\/\/skilldrick.github.io\/easy6502\/\">http:\/\/skilldrick.github.io\/easy6502\/<\/a><\/p>\n\n\n\n<p>I was ready. I looked at some of the\u00a0<a href=\"https:\/\/github.com\/commanderx16\/x16-demo\/tree\/master\/assembly\">examples for the Commander X16<\/a>, saw how to compile and wrote my first assembler program for the Commander X16, or more precisely for the 6502.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\t<font color=\"orange\">ldx<\/font> <font color=\"blue\">#0<\/font>\t; Load register X with 0\n\t<font color=\"orange\">inx<\/font>\t; Increment register X<\/code><\/pre>\n\n\n\n<p>I compiled the file with\u00a0<strong>acme -f cbm -o test.prg test.asm<\/strong>\u00a0started the emulator with\u00a0<strong>.\/x16emu -prg test.prg<\/strong>\u00a0and then\u2026 Nothing.<\/p>\n\n\n\n<p>I expected there to be some sort of code or something in the emulator, but when I did a\u00a0<strong>LIST<\/strong>, nothing. I knew that\u00a0<strong>RUN<\/strong>\u00a0would not do anything. Obviously I was missing something. I posted a comment on the\u00a0<a href=\"https:\/\/www.facebook.com\/groups\/CommanderX16\/\">Commander X16 Prototype Facebook<\/a>\u00a0page asking how to get startet and was told that there are plenty of beginners guides.<\/p>\n\n\n\n<p>After spending quite a lot of time searching through the documentation, the examples and the internet in general, I remembered that I was also a member of the joint forum of\u00a0<a href=\"https:\/\/murray2.com\/\">The 8-bit guy and The Geek Pub<\/a>. I thought that maybe there was a bit more information to find there and by chance I saw a\u00a0<a href=\"https:\/\/murray2.com\/threads\/hello-from-france.317\/\">post<\/a>\u00a0from another new programmer who seemed to have come a bit further than me, so I asked him for help and he provided a\u00a0<a href=\"https:\/\/murray2.com\/threads\/hello-from-france.317\/#post-1409\">great first example<\/a>.<\/p>\n\n\n\n<p>I compiled his example, loaded it into the emulator and\u2026 Still nothing. When I did a\u00a0<strong>LIST<\/strong>, it showed nothing and\u00a0<strong>RUN<\/strong>\u00a0did not do anything. I startet writing a long reply to him, but the page updated and I saw his addition of how to actually run the program.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SYS $1000<\/code><\/pre>\n\n\n\n<p>Suddenly all these memories started rolling in and things startet clicking into place. I remembered loading games on the C64, doing a\u00a0<strong>LIST<\/strong>\u00a0and seeing just a single line. I remembered that I was baffled as how an entire game could be contained in a single short line. A bit of looking around on the internet gave me the answer. BASIC programs can be startet with the\u00a0<strong>RUN<\/strong>\u00a0command, but assembler\/binary programs needs to be started with the\u00a0<strong>SYS<\/strong>\u00a0command, telling it where in memory the program is located. Still, this program did not show anything when doing\u00a0<strong>LIST<\/strong>.<\/p>\n\n\n\n<p>Again, my new friend\u00a0<a href=\"https:\/\/murray2.com\/members\/vincentf.548\/\">VincentF<\/a>\u00a0was able to help out and provided a\u00a0<a href=\"https:\/\/bigcode.wordpress.com\/2016\/10\/22\/basic-loader-for-commodore-64-assembly-programs\/\">link to a post<\/a>\u00a0explaining how the single BASIC line is created for a binary program.<\/p>\n\n\n\n<p>The post is about the Commodore 64 and it made me remember that the official documentation for the Commander X16 states that it supports the C64 Kernal API. VincentF\u2019s example had already showed me how to call an API function so I started searching for some documentation of the C64 Kernal API and found some\u00a0<a href=\"http:\/\/sta.c64.org\/cbm64krnfunc.html\">here<\/a>.<\/p>\n\n\n\n<p>UPDATE: I have made my own list of C64 KERNAL functions. Find it on my new\u00a0<a href=\"https:\/\/cx16.dk\/c64-kernal-routines\/\" target=\"_blank\" rel=\"noreferrer noopener\">cx16<\/a>\u00a0domain.<\/p>\n\n\n\n<p>Finally I was ready to write my own \u201cHello, World!\u201d-Example, and here it is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>*=<font color=\"blue\">$0801<\/font>\t\t\t; Assembled code should start at $0801\n\t\t\t; (where BASIC programs start)\n\t\t\t; The real program starts at $0810 = 2064\n!byte <font color=\"blue\">$0C<\/font>,<font color=\"blue\">$08<\/font>\t\t; $080C - pointer to next line of BASIC code\n!byte <font color=\"blue\">$0A<\/font>,<font color=\"blue\">$00<\/font>\t\t; 2-byte line number ($000A = 10)\n!byte <font color=\"blue\">$9E<\/font>\t\t; SYS BASIC token\n!byte <font color=\"blue\">$20<\/font>\t\t; &#91;space]\n!byte <font color=\"blue\">$32<\/font>,<font color=\"blue\">$30<\/font>,<font color=\"blue\">$36<\/font>,<font color=\"blue\">$34<\/font>\t; $32=\"2\",$30=\"0\",$36=\"6\",$34=\"4\"\n\t\t\t; (ASCII encoded nums for dec starting addr)\n!byte <font color=\"blue\">$00<\/font>\t\t; End of Line\n!byte <font color=\"blue\">$00<\/font>,<font color=\"blue\">$00<\/font>\t\t; This is address $080C containing\n\t\t\t; 2-byte pointer to next line of BASIC code\n\t\t\t; ($0000 = end of program)\n*=<font color=\"blue\">$0810<\/font>\t\t\t; Here starts the real program\n\nCHROUT=<font color=\"blue\">$FFD2<\/font>\t\t; CHROUT outputs a character (C64 Kernal API)\nCHRIN=<font color=\"blue\">$FFCF<\/font>\t\t; CHRIN read from default input\n\n\t<font color=\"orange\">ldx<\/font>\t<font color=\"blue\">#0<\/font>\t; X register is used to index the string\nloop:\n\t<font color=\"orange\">lda<\/font>\t.string,x ; Load character from string into A reg\n\t<font color=\"orange\">beq<\/font>\tend\t; If the character was 0, jump to end label\n\t<font color=\"orange\">jsr<\/font>\tCHROUT\t; Output character stored in A register\n\t<font color=\"orange\">inx<\/font>\t\t; Increment X register\n\t<font color=\"orange\">jmp<\/font>\tloop\t; Jump back to loop label to print next char\nend:\n\t<font color=\"orange\">jsr<\/font>\tCHRIN\t; Read input until Enter\/Return is pressed\n\t<font color=\"orange\">rts<\/font>\t\t; Return to caller\n\n.string !pet\t<font color=\"red\">\"hello, world!!\"<\/font>,<font color=\"blue\">13<\/font>,<font color=\"blue\">0<\/font><\/code><\/pre>\n\n\n\n<p>UPDATE: Save the program as\u00a0<strong>hellow.asm<\/strong><br>Compile it with\u00a0<strong>acme -f cbm -o HELLOW.PRG hellow.asm<\/strong><br>Start the emulator with:\u00a0<strong>x16emu -prg HELLOW.PRG<\/strong><br>In the emulator, type:\u00a0<strong>LIST<\/strong><br>Type:\u00a0<strong>RUN<\/strong><\/p>\n\n\n\n<p>I hope that this will be able to help other newcomers understand how to write assembly programs for the 6502\/Commodore 64\/Commander X16. If anything at all is unclear or there are any questions, please do not hesitate to write a comment or contact me directly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First things first. I have been following\u00a0The 8-bit Guys dream computer\u00a0for quite a while now and finally decided that I would like to try my hand on some assembler programming&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-38","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=\/wp\/v2\/posts\/38","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=38"}],"version-history":[{"count":1,"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=\/wp\/v2\/posts\/38\/revisions"}],"predecessor-version":[{"id":39,"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=\/wp\/v2\/posts\/38\/revisions\/39"}],"wp:attachment":[{"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}