2. General Info 2.1
Assembler Library Function Principle
Generating Assembler Code
PowerPC Processor Registers
|This chapter explains how the Assembler Library works and what steps are needed to integrate assembler routines into BASIC programs.|
|2.1 Assembler Library Function Principle
As mentioned in the introduction, the Assembler Library translates the mnemonics into binary machine code. This is done during the BASIC programs runtime (normally, immediately after program start, later if needed). This makes editing assembler routines much easier because cumbersome linking is not necessary. Assembler routines may even be modified or new ones generated during the runtime of the BASIC program.
The tradeoff is that the Assembler Library cannot be used to create standalone applications but that is not really its purpose anyhow. Instead, the purpose of the Assembler Library is to write small and flexible primitives for especially time-consuming operations (mostly loops that have to be run millions of times.)
|2.2 Generating Assembler Code
It is best to define an independent procedure to be called at the start of your program to generate your assembler code (but not before Assembler_Init). This prevents fragmenting heap memory because the assembler code is saved to an unmovable block requested with the command MEMORY.
Each assemble command requires 4 bytes of memory so that it is relatively easy to estimate how much memory to request (if in doubt, overestimate).
To keep a clearer overview over the assembler code it is recommended to define constants for the symbolic referencing of the processor registers right at the start using COMPILER "DEF_CONST," and COMPILER "END_CONST."
|The following procedures are available to direct the Assembler Library regarding where to write the code and when code generation has ended:|
|Directs the Assembler Library to deposit the following code starting at Start_Address. Additionally, use Max_Length to advise the Assembler Library of the max. permissible code length. This ensures that the reserved memory range is not accidentally exceeded.|
|This is the end of the assembler code.|
|We now have all the elements needed to depict a sample procedure for generating assembler code:|
|DEF PROC Generate_Assembler_Code
'General Purpose Register Symbols.
'Floating Point Register Symbols.
'Condition Register Symbols.
My_Code=MEMORY(128):'Allocate memory block for saving code.
Code_Start My_Code,128:'The assembler programm starts at My_Code.
'Lists the actual assembler program.
|The constants defined first can be used symbolically
in the following code. This makes it possible to differentiate between immediate
operators and register designations at once.
Note: You only have to define constants for the registers you are actually using.
Caution: Register designations may be only integer type variables (short or long integers) but not any floating point variables because their conversion to integers with every command would slow down the Assembler Library unnecessarily.
|2.3 PowerPC Processor Registers
The PowerPC processor features 32 general purpose registers (32 bit wide), 32 floating point registers (64 bit wide), and 8 condition registers (4 bit wide); the 8 condition registers are combined to one 32 bit register with some commands. A few of the registers are reserved by the operating system or Omikron Basic for special tasks. The following table provides an overview:
|R0||Use this register any way desired. However, some commands trigger a special behavior where the digit zero is used instead of the register content.|
|R1 = Sp||Points to the static operating system stack. It is best not to use at all.|
|R2 = Toc||Points to the Table of Contents of the Omikron Basic program and thus contains the value SEGPTR - 256. May not be changed.|
|R3 - R12||Use these registers in your assembler program any way desired.|
|R13 - R28||If you want to use these registers, you have to preserve their contents first and then retrieve them before ending your assembler routine.|
|R29||Omikron Basic uses these registers to address the constants. May not be changed.|
|R30||Omikron Basic uses these registers to address the BASIC variables. May not be changed.|
|R31||Points to the dynamic Omikron Basic stack. R31 may not be changed either.|
|Fpr0 - Fpr13||Use these floating point registers any way desired.|
|Fpr14 - Fpr31||Use these floating point registers as well but make sure to restore their contents before exiting your assembler routine.|
|Cr0, Cr1||Use in any way desired.|
|Cr2 - Cr4||Use these condition registers as well but make sure to restore their contents before exiting your assembler routine.|
|Cr5 - Cr7||Use in any way desired.|
|Caution: When you change the link register
in your program, e.g., to call a subroutine, you have to restore it before exiting
the assembler routine.
Note: Use the macro command Store_Register to preserve register contents if you want to use the registers in your assembler routine. The restore command is Restore_Register.
© 2003 Berkhan-Software
www.berkhan.com | Online Orders