Skip to main content

C programming

The BD Software C compiler is included on drive A: or drive B: if you have the CP/M filesystem loaded on the Micro SD card. The following text is an exert from the BDS C Wikipedia article.

BDS C (or the BD Software C Compiler) is a compiler for a sizeable subset of the C programming language, that ran on and generated code for the Intel 8080 and Zilog Z80 processors. It was the first C compiler for CP/M. It was written by Leor Zolman and first released in 1979 when he was 20 years old. "BDS" stands for "Brain Damage Software."

BDS C User's Guide

Refer to the BDS C User's Guide for more about the language and its implementation.

Compile C applications

The CP/M disk image includes a simple HW.C (Hello, world) application. BDS C language has support for Intel 8080 CPU input and output port instructions. The HW.C application displays the system tick count, UTC, and local date and time, and then sleeps for 1 second. For more information about Intel 8080 IO port mappings, refer to Intel 8080 input and output ports.

Follow these steps to list, compile, link, and run the HW.C file:

  1. List the hw.c file

    type hw.c
    /* Copyright (c) Microsoft Corporation. All rights reserved.
    Licensed under the MIT License. */

    /* C application to demonstrate use of Intel 8080 IO Ports */

    main()
    {
    unsigned c, l;
    char buffer[50];

    printf("\nHello from the Altair 8800 emulator\n\n");

    for (c = 0; c < 65535; c++)
    {
    printf("Count:%u\n", c);
    printf("System tick count: %s\n", get_port_data(41, buffer, 50));
    printf("UTC date and time: %s\n", get_port_data(42, buffer, 50));
    printf("Local date and time: %s\n\n", get_port_data(43, buffer, 50));

    sleep(1); /* Sleep for 1 second */
    }
    }

    /* Sleep for n seconds */
    sleep(seconds)
    char seconds;
    {
    outp(30, seconds); /* Enable sleep for N seconds */
    while (inp(30)); /* Wait for sleep to expire */
    }

    /* Get data from Intel 8080 IO port */
    char *get_port_data(port_num, buffer, buffer_len)
    int port_num;
    char *buffer;
    int buffer_len;
    {
    char ch;
    int index;

    index = 0;

    while ((ch = inp(port_num)) && index < buffer_len) {
    buffer[index++] = ch;
    }
    buffer[index] = 0x00;

    return buffer;
    }
  2. Compile the hw.c file:

    cc hw
  3. Link the hw application:

    clink hw
  4. Run the hw application:

    hw
  5. Stop the hw application by selecting Ctrl+C.

Editing hw.c with Word-Master

In the following steps use the Word-Master text editor to edit a file. It's highly recommended to edit files with Visual Studio Code and then copy the file to the Altair filesystem using the CP/M gf command.

  1. Edit the hw.c file with Word-Master:

    wm hw.c
  2. Switch the web terminal to character input mode by selecting Ctrl+L.

  3. Make your changes to the hw.c file.

  4. Save your updates to the hw.c file:

    1. Select the Esc key.
    2. Select E to exit. Your file changes are saved to disk.
  5. Switch the web terminal to line input mode by selecting Ctrl+L.

  6. Then compile, link, and run the updated hw.c application.