User Tools

Site Tools


wswan:guide:c_assembly

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
wswan:guide:c_assembly [2024/02/17 10:55] – created asiewswan:guide:c_assembly [2025/12/31 12:59] (current) asie
Line 1: Line 1:
 ====== Writing assembly ====== ====== Writing assembly ======
  
-For writing entire functions in ASMas well as more complex projects, it can be preferable to write external assembly files.+In the Wonderful toolchain, assembly files use the ''.s'' extension. They are compiled using the GNU assembler and the C preprocessor.
  
-In the Wonderful toolchain, these files use the ''.s'' extension and are compiled using the GNU assembler and the C preprocessor.+===== Creating an assembly file =====
  
 It is recommended to start with a preamble similar to the following: It is recommended to start with a preamble similar to the following:
Line 17: Line 17:
  
   - Include the Wonderful toolchain's basic definitions. This provides some useful assembly defines and macros.   - Include the Wonderful toolchain's basic definitions. This provides some useful assembly defines and macros.
-  - Include the libws library's definitions. This library supports being included in an assembler, and provides hardware-related defines. You may wish to skip this line if targetting the WonderWitch.+  - Include the libws library's definitions. This header file supports being included in an assembler context, and provides hardware-related defines - such as I/O port names and masks.
   - Tell the assembler to emit 16-bit code; this is useful as the GNU assembler supports all kinds of x86 code, including 32-bit and 64-bit.   - Tell the assembler to emit 16-bit code; this is useful as the GNU assembler supports all kinds of x86 code, including 32-bit and 64-bit.
-  - Tell the assembler to emit 80186 code; this unlocks some additional 80186-exclusive opcodes supported on the NEC V30MZ +  - Tell the assembler to emit 80186 code; this unlocks some additional 80186-exclusive opcodes supported on the NEC V30MZ. 
-  - Tell the assembler to emit Intel-syntax assembly; this is entirely optional, but the examples for external assembly files in this guide all make use of the Intel syntax, as opposed to the AT&T syntax used in inline assembly.+  - Tell the assembler to emit Intel-syntax assembly; this is entirely optional, but the examples for external assembly files in this guide all make use of the Intel syntax, as opposed to the default AT&T syntax used in inline assembly.
  
 +Note that to export a symbol (make it visible outside of the specific file, or translation unit), you need to use ''.global'', like so:
 +
 +<code C>
 +        .global strlen
 +strlen:
 +</code>
 +
 +===== Useful macros =====
 +
 +Including ''wonderful.h'' provides some useful macros from ''ia16.h''.
 +
 +==== Portable calling convention ====
 +
 +The ''small'' and ''medium'' targets use different calling conventions (near vs. far functions by default). The following macros abstract these differences away:
 +
 +  * **IA16_CALL** - use this macro in place of the ''call'' instruction to call C-callable functions.
 +  * **IA16_CALL_LOCAL** - use this macro in place of the ''call'' instruction when calling another function //in the same section//.
 +  * **IA16_JMP** - use this macro in place of the ''jmp'' instruction to jump to C-callable functions.
 +  * **IA16_RET** - use this macro in place of the ''ret'' instruction to return from a C-callable function.
 +  * **IA16_CALL_STACK_OFFSET(x)** - for portability between code models, you can use this macro to get a stack offset with the function return address accounted for. For example, in a //near// code model, ''IA16_CALL_STACK_OFFSET(10)'' returns ''12'', but in a //far// code model, it returns ''14''.
wswan/guide/c_assembly.1708167324.txt.gz · Last modified: by asie