Skip to content

First Program

pf4 edited this page Sep 4, 2024 · 1 revision

We will see in different ways how to write and compile a small program that displays "Hi profanOS!" on the screen.

For reasons of simplicity we will note LINUX$ the host shell prompt which allows you to compile profanOS and PROFAN> the Olivine shell of profanOS.

Index

Writing and compiling in Linux

In this first approach we will use linux to write and compile the program and we will only execute it in profan.

Writing the program

All file in directory zapps/ are compiled when the disk image is created. So we will create a file zapps/cmd/hi.c and write the following code with your favorite text editor:

#include <stdio.h>

int main(void) {
    printf("Hi profanOS!\n");
    return 0;
}

Compiling the program

# Just compile the disk image
LINUX$ make disk

# And run profanOS
LINUX$ make run

Running the program

During disk creation, the program was compiled in /bin/cmd we can run it manually or just write its name (See Olivine).

# Full path
PROFAN> . /bin/cmd/hi.elf

# Using the env:PATH
PROFAN> hi

Writing in Linux, Compiling in profanOS

In this second approach we will use linux to write the program and we will compile it in profanOS using tcc.

Writing the program

All file in directory sys_dir/user/ are moved to the profanOS file system when the disk image is created. So we will create a file sys_dir/user/hi.c and write the program in it.

#include <stdio.h>

int main(void) {
    printf("Hi profanOS!\n");
    return 0;
}

Download TinyCC

Heavy profanOS programs are not directly in the main repo, but you can easily retrieve them.

# We can use the gui addons manager and scroll
# down to select tcc and download it.
LINUX$ make gaddons

# -- OR -- cli way
LINUX$ python3 tools/addons.py tcc

Start profanOS

# Build disk and start profanOS vm
LINUX$ make disk run

Compiling the program in profanOS

In profanOS, using tcc we can easily have an executable!

# Go to the user directory
PROFAN> cd /user

# Compile the program
PROFAN> tcc hi.c -o hi.elf

Running the program

Using the . command we can run the program like any other.

# Run the program
PROFAN> . hi.elf

Tcc also has an option that allows you to compile and execute a C program in one go:

# Compile and run the program
PROFAN> tcc -run hi.c

Writing and compiling in profanOS

In this third approach we will write and compile the program directly in profanOS.

Download TinyCC and start the VM

# Select tcc in the gui addons manager
LINUX$ make gaddons

# Build disk and start profanOS vm
LINUX$ make disk run

Writing the program

In profanOS you can use the rim text editor with syntax highlighting to write or edit code. To save the file press Ctrl and to exit press Esc (it's simpler than in vim 🥲)

# Go to the user directory
PROFAN> cd /user

# Start the editor
PROFAN> rim hi.c

Write the program in the editor:

#include <stdio.h>

int main(void) {
    printf("Hi profanOS!\n");
    return 0;
}

Compiling and running the program

Compile the C program like last time

# Compile the program
PROFAN> tcc hi.c -o hi.elf
PROFAN> . hi.elf

# -- OR -- compile and run in one go
PROFAN> tcc -run hi.c

Recover your program

If you want to recover your program in linux, you cat it in the serial console.

# Cat the program and redirect the
# output to the serial port
PROFAN> cat hi.c > /dev/userial

In linux the program will be displayed in the terminal, you can copy it and paste it into a file.

Conclusion

You have seen three different ways to write and compile a program in profanOS. The choice of method depends on your preferences and the size of the program you want to write.

Interpreters for other languages are also available in the addons menu such as lua and sulfur 😊

See also