Introduction to programming in C with Z88dk: Compiling and testing a "Hello World"

Pincha aquí para verlo en español

At the time of writing this tutorial, the latest stable version is v1.9 z88dk, which was released in 2009, although we can download a snapshot with all the latest, because they are automatically generated every day. We got to install the official website v1.9 stable or latest snapshot. The windows installation is automatic and if you want to install on any other platform you can read the documentation. Once installed we can basically invoke the compiler from any directory on your pc.

With any text or code editor (with notepad in Windows) generate a file called z88dk01.c with the following contents:

#include <stdio.h>

main()
{
  printf("Hello world");
  fgetc_cons();
}

To compile from the command line (or better make a batch file) in the same directory where is the file z88dk01.c, run:

zcc +cpc -lndos -o z88dk01.bin z88dk01.c

If all goes well, we have generated a new file z88dk01.bin, which can run on the Amstrad CPC. For testing, we will load the generated binary in an emulator, for this we must create a file dsk (disk image) with the binary that we compiled in order to load / run in the emulator. For this we use for example ManageDSK or CPCDiskXP.

To generate a dsk with CPCDiskXP, we execute the program, then we press in "Dsk Editor", then we press in "New" select the default format "CPC Data" and then we press Ok. Now click "Add Files" to add our binary, look for the directory where we compiled and select the file z88dk01.bin, now CPCDiskXP will ask if you want to add the Amsdos header, press Yes and let the values displayed by default and click Ok. Now we press the button "Save" to store our Dsk, I have called it "z88dk01.dsk" in the same directory. With the latest version of CPCDiskXP, we can generate a dsk from the command line (or bat) as follows:

CPCDiskXP -File z88dk01.bin -AddAmsdosHeader 6000 -AddToNewDsk z88dk01.dsk

We load the dsk our favorite emulator (I usually use CPCE and Winape) and execute it using run"z88dk01  and you should see the following:

 

z88dk also allows us to generate the binary directly with Amsdos header to make our life easier, simply by using "-create-app", will generate a file z88dk01.cpc, which already has the header. On the other hand, if you want to specify a memory address for the program different from the default (0x6000), you can do, using the directive "-zorg=" followed by a decimal value. For example :

zcc +cpc -create-app -lndos -zorg=384 -o z88dk01.bin z88dk01.c

This will generate a file z88dk01.cpc with Amsdos header and memory address to load 384 (0x180). Create a Dsk with it and load it in the emulator using run"z88dk01.cpc

You can download a zip file with z88dk01.c, z88dk01.bat and z88dk01.dsk here: Introduction_to_programming_in_Z88dk_Compiling_and_testing_a_Hello_World.zip

 

 

www.CPCMania.com 2012