C86 Compiler Features

Explained below are two very useful C86 features that are included in the version of C86 for this class. More detailed information about the features of C86 can be found in the C86 Manual (147 KB). However, many features may not be enabled in our version or may not work properly. Use other C86 features at your own risk.


The -g Compiler Option

This very useful option is used to aid in debugging. It causes the C source code and line numbers to be inserted as comments into the compiler's assembly output. This makes it very easy to see which blocks of assembly code were generated by each line of C code. To use this option, simply add it after the c86 command, as in the following example:

c86 -g myfile.c myfile.s


Support for the asm Keyword

WARNING: You should NOT use inline assembly unless you know exactly what you are doing. Even then it should only be used if you are accessing registers or I/O ports directly or if you need to execute very special instructions. Inline assembly is extremely difficult to update or maintain and is much more prone to mistakes. Do not use inline assembly as a crutch simply because you do not know how to do something in C.

The c86 compiler also allows primitive use of the asm keyword. This keyword allows you to place assembly language statements in the middle of your C source code. However, the asm keyword is primitive in that it will simply insert your assembly code directly into the assembly output without any modification. This leads to some limitations:

  1. Syntax of your assembly is not checked for errors. Therefore, the compiler may not report any errors but the assembler will at a later stage.
  2. It will not work if you access local variables or function arguments by name. Instead you must calculate their position relative to the bp register. Global data can be accessed by name but care must be taken to ensure that they are used appropriately.

The syntax for the asm keyword is simple:

asm("{assembly statement here}");
For example, to enable interrupts at the beginning of an ISR written in C, the following statement could be used:
asm("sti");
This will cause the text "sti" to be inserted as an assembly statement into the compiler's assembly output. The insertion of multiple assembly statements can be done in several ways. For example, suppose that you needed to insert the following assembly statements into your code:
	push ax
	push bx
	sti
The statements must be entered as valid C strings. Therefore, they can be entered in any of the following ways:

    1.   As several asm statements:

	asm("push ax");
	asm("push bx");
	asm("sti");
    2.   As a separated string:
	asm(
	"push ax\n"
	"push bx\n"
	"sti"
	);
    3.   As a single combined string:
	asm("push ax\npush bx\nsti");
The first two options are preferable since they are the most readable. However, the first example generates the most readable assembly code since it will be properly indented.