Introduction -Browse- PowerPC Command Set

Contents | Start

2. General Info
2.1
2.2
2.3
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:



Code_Start Start_Address[,Max_Length]
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.
Code_End
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
 COMPILER "DEF_CONST"
  'General Purpose Register Symbols.
  R0=0:R1=1:R2=2:R3=3:R4=4:R5=5:R6=6:R7=7:R8=8:R9=9:R10=10:R11=11
  R12=12:R13=13:R14=14:R15=15:R16=16:R17=17:R18=18:R19=19:R20=20
  R21=21:R22=22:R23=23:R24=24:R25=25:R26=26:R27=27:R28=28:R29=29
  R30=30:R31=31
  'Floating Point Register Symbols.
  Fpr0=0:Fpr1=1:Fpr2=2:Fpr3=3:Fpr4=4:Fpr5=5:Fpr6=6:Fpr7=7:Fpr8=8
  Fpr9=9:Fpr10=10:Fpr11=11:Fpr12=12:Fpr13=13:Fpr14=14:Fpr15=15
  Fpr16=16:Fpr17=17:Fpr18=18:Fpr19=19:Fpr20=20:Fpr21=21:Fpr22=22
  Fpr23=23:Fpr24=24:Fpr25=25:Fpr26=26:Fpr27=27:Fpr28=28:Fpr29=29
  Fpr30=30:Fpr31=31
  'Condition Register Symbols.
  Cr0=0:Cr1=1:Cr2=2:Cr3=3:Cr4=4:Cr5=5:Cr6=6:Cr7=7
 COMPILER "END_CONST"
 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.
  '..........
 Code_End
END_PROC

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.

Introduction -Browse- PowerPC Command Set

Contents | Start



© 2003 Berkhan-Software
www.berkhan.com | Online Orders