Okay, let's write an "optimal" startup code for an AmigaOS program in assembly language. "Optimal" here means:
- Correctness: Follows AmigaOS conventions and executable format loader expectations.
- Efficiency: Minimal overhead, fast execution.
- Self-Contained: Properly initializes and exits.
- Standard: Uses common practices.
The AmigaOS loader handles reading the executable file, setting up the initial stack (a7), and providing the Exec library base pointer (SysBase) in register a6. It also typically zeros the BSS section. The program's entry point (usually labeled _start or start) is then called.
The primary goal of the startup code is to:
- Preserve essential registers provided by the loader (
a6 - SysBase).
- Optionally save other registers or arguments (
d0 - command line args).
- Perform any minimal initialization needed (though often the OS provides enough).
- Provide a clean exit mechanism by calling
Exit() from dos.library.
Here's a common and efficient startup sequence using standard 68k assembly syntax (compatible with assemblers like Vasm, Devpac, ASM-One, etc.).
Assumptions:
- The assembler/linker will correctly generate the Amiga executable header (
h0) and program header (ph0). This code focuses on the runtime part executed after the loader jumps to the entry point.
- Standard include files (
exec/exec.i, dos/dos.i) are available for LVO (Library Vector Offset) definitions.
*---------------------------------------------------------------------------
* Optimal AmigaOS Startup Code
*
* Purpose: Minimal program entry point.
* Saves essential registers, opens dos.library, and exits cleanly.
* Assembler: Assumes Vasm or similar (syntax might need slight tweaks for others)
* Target: AmigaOS 68k
*---------------------------------------------------------------------------
* Include standard AmigaOS definitions
INCLUDE "exec/exec_lib.i" ; For Exec library offsets (LVOs)
INCLUDE "dos/dos_lib.i" ; For DOS library offsets (LVOs)
* Define constants
DOS_LIBRARY_NAME equ "DOS/library" ; Name of the DOS library
DOS_MIN_VERSION equ 37 ; Minimum DOS version required (e.g., V36+)
; Use 0 for max compatibility if needed
* === Executable File Header (h0) and Program Header (ph0) ===
* NOTE: These are typically generated *by the assembler/linker*, not written
* directly in the runtime code section. The assembler directive
* `FILE AUTO` (vasm) or similar implicitly handles this.
* We start the code section below.
; Make sure we are in the CODE section
SECTION code,CODE
_start:
*---------------------------------------------------------------------------
* Entry Point (_start)
*---------------------------------------------------------------------------
* AmigaOS Loader provides:
* A6 = Executable Library Base (SysBase)
* D0 = Pointer to AmigaDOS Process structure (CH0, command line info)
* A7 = Stack Pointer
* Optimal Startup Actions:
* 1. Preserve SysBase (A6) as it's needed for all OS calls.
* 2. Save the command line arguments pointer (D0) if needed later.
* 3. Open dos.library to access OS functions like Exit().
* 4. Call dos.library's Exit() function for a clean termination.
; --- Register Preservation ---
move.l a6, a5 ; Save SysBase (Exec library base) in A5 (common practice)
move.l d0, a4 ; Save CH0 (command line arguments pointer) in A4
; --- Open dos.library ---
; We need dos.library to call Exit().
; OpenLibrary takes: D0=Version, A1=Name String Pointer
; It returns Library Base in A1 (or A0 if A1 was zero initially).
; OpenLibrary is called via ExecBase (which is in A6).
move.l a5, a6 ; Ensure SysBase is in A6 (restored from A5)
lea dosName(pc), a1 ; Load address of library name string into A1
moveq #DOS_MIN_VERSION, d0 ; Load required DOS version into D0
jsr _LVOOpenLibrary(a6) ; Call OpenLibrary(DOS_LIBRARY_NAME, DOS_MIN_VERSION)
; --- Check if dos.library opened successfully ---
tst.l a1 ; Check if the returned library pointer is NULL
beq.s .failExit ; If NULL (zero), something went wrong, jump to failure exit
; Store the DOS library base pointer for later use
move.l a1, d2 ; Save DOS Base pointer in D2 (A1 will be needed for Exit call)
; --- Perform Actual Program Work ---
; >>> Your program's code logic would go here <<<
; For this example, we proceed directly to exit.
; --- Clean Program Exit ---
move.l d2, a6 ; Load DOS library base into A6
moveq #0, d0 ; Set exit code to 0 (success)
jsr _LVOExit(a6) ; Call Exit(0)
.failExit:
; --- Handle Failure (e.g., couldn't open dos.library) ---
; If dos.library failed to open, we cannot call Exit() from it.
; We must use Exec's methods or simply halt. Returning an error code
; via Exit() requires the DOS library.
; Option 1: Use Exec's Exit which might not be standard/available.
; Option 2: Halt execution (simple, but not clean).
; Option 3: Try to report error via console if possible (more complex).
; Let's try returning to the loader with an error code if possible.
; The loader might expect ExecBase (A6).
move.l a5, a6 ; Restore SysBase (Exec library base) into A6
moveq #ERROR_DOS_OPEN, d0 ; Use a non-zero exit code (e.g., 20)
; Ideally, we'd call a function to cleanly terminate without DOS base,
; but Exit() is the standard POSIX-like way. If we can't open DOS,
; we are somewhat stuck for a perfectly clean exit.
; A simple halt is often used in such minimal examples:
move.l #$4E4F, d0 ; Opcode for 'MOVE.L #value, D0' -> "NO" ($4E4F)
trap #1 ; Halt execution using TRAP #1 (simulates AmigaDOS Halt)
; Note: This halts, doesn't cleanly exit process.
*---------------------------------------------------------------------------
* Data Section
*---------------------------------------------------------------------------
SECTION data,DATA
dosName:
dc.b DOS_LIBRARY_NAME,0 ; DOS library name string (null-terminated)
even ; Ensure word alignment
dosBase:
ds.l 1 ; Placeholder for DOS library base pointer (if needed elsewhere)
*---------------------------------------------------------------------------
* BSS Section (Uninitialized Data)
*---------------------------------------------------------------------------
SECTION bss,BSS
ds.l 1 ; Example uninitialized variable space
*---------------------------------------------------------------------------
* End of Code
*---------------------------------------------------------------------------
END _start ; Define the entry point label for the linker/assembler
Explanation and Optimality Considerations:
SECTION directives: Used to organize code, data, and BSS. Assemblers like Vasm require these.
_start label: The conventional entry point.
- Register Preservation (
move.l a6, a5, move.l d0, a4): a6 holds SysBase (Exec library base), which is essential for all OS calls. Saving it immediately in a5 is a standard practice, ensuring it's available even if other calls modify a6. Saving d0 (command line arguments) in a4 is also common if they might be needed.
- Opening
dos.library: Necessary to call Exit(). OpenLibrary is called via SysBase (a6). Using lea for the string and moveq for the version number is efficient.
- Error Checking (
tst.l a1, beq.s .failExit): Crucial for robustness. If OpenLibrary fails (returns NULL), the program must handle it. Branching using .s (short form) is slightly more optimal than a long branch.
- Storing DOS Base (
move.l a1, d2): Stores the returned library base pointer. d2 is chosen arbitrarily, assuming it won't conflict with OS call requirements.
- Calling
Exit(): This is the standard AmigaOS way to terminate a process. It takes the return code in d0. Using the saved dosBase pointer (loaded into a6) and the function offset _LVOExit is the correct way to call it.
.failExit Handling: This demonstrates a minimal failure path. If dos.library cannot be opened, we can't use Exit(). Halting via TRAP #1 with a "NO" message is a common way to indicate a fatal error during startup. A more complex program might attempt to write to the console or use other low-level methods.
END _start: Tells the assembler/linker where the program execution begins.
- No
RTS: After Exit(), the program terminates; an RTS (Return from Subroutine) is not needed and would be incorrect.
This code provides a solid, efficient, and conventional foundation for any AmigaOS assembly program.