Here's an optimal startup code for AmigaOS in 68000 assembly language:
; Optimal AmigaOS Startup Code
; Assembles with DevPac/VASM
INCDIR "include:"
INCLUDE "exec/exec_lib.i"
INCLUDE "dos/dos_lib.i"
INCLUDE "workbench/startup.i"
SECTION code,CODE
; Entry point
_start:
move.l 4.w,a6 ; Get ExecBase
move.l a6,_SysBase ; Save it
; Check if started from Workbench or CLI
sub.l a1,a1 ; Clear a1
jsr -294(a6) ; FindTask(NULL) - get current task
move.l d0,a0
move.l pr_CLI(a0),d0 ; Check if CLI process
bne.s .from_cli
; Started from Workbench - get startup message
lea pr_MsgPort(a0),a0
move.l _SysBase,a6
jsr -384(a6) ; WaitPort
lea pr_MsgPort(a0),a0
jsr -372(a6) ; GetMsg
move.l d0,_WBStartup ; Save startup message
bra.s .open_libs
.from_cli:
clr.l _WBStartup ; Clear WB startup flag
.open_libs:
; Open DOS library
lea dosname(pc),a1
moveq #36,d0 ; Minimum version
move.l _SysBase,a6
jsr -552(a6) ; OpenLibrary
move.l d0,_DOSBase
beq .exit_fail ; Exit if can't open DOS
; Set up return address for main program
pea .cleanup(pc)
; Jump to main program
bsr _main
.cleanup:
; Close DOS library
move.l _DOSBase,d0
beq.s .no_dos
move.l d0,a1
move.l _SysBase,a6
jsr -414(a6) ; CloseLibrary
.no_dos:
; If started from Workbench, reply to startup message
move.l _WBStartup,d0
beq.s .exit_clean
move.l d0,a1
move.l _SysBase,a6
jsr -378(a6) ; ReplyMsg
.exit_clean:
moveq #0,d0 ; Success return code
bra.s .exit
.exit_fail:
moveq #20,d0 ; RETURN_FAIL
.exit:
; Final exit - return to system
move.l d0,-(sp)
move.l _DOSBase,d0
beq.s .no_dos_exit
move.l d0,a1
move.l _SysBase,a6
jsr -414(a6) ; CloseLibrary
.no_dos_exit:
move.l (sp)+,d0
rts
; Your main program starts here
_main:
; Your program code goes here
; Example: Print "Hello AmigaOS!"
move.l _DOSBase,a6
jsr -60(a6) ; Output() - get stdout
move.l d0,d1 ; File handle
move.l #hello_msg,d2 ; Message pointer
move.l #hello_len,d3 ; Message length
jsr -48(a6) ; Write()
rts
; Data section
SECTION data,DATA
dosname: dc.b "dos.library",0
hello_msg: dc.b "Hello AmigaOS!",10,0
hello_len equ *-hello_msg-1
; BSS section for variables
SECTION bss,BSS
_SysBase: ds.l 1 ; Exec library base
_DOSBase: ds.l 1 ; DOS library base
_WBStartup: ds.l 1 ; Workbench startup message
END
Key features of this startup code:
Optimal Design Elements:
- Detects whether launched from CLI or Workbench automatically
- Opens only essential libraries (DOS.library with version checking)
- Proper cleanup sequence that closes libraries and replies to Workbench messages
- Minimal memory footprint
- Standard AmigaOS return codes
Structure:
- Uses proper section organization (CODE, DATA, BSS)
- Saves system library bases for later use
- Sets up stack-based cleanup for guaranteed resource deallocation
- Handles both CLI and Workbench startup scenarios
Usage:
Replace the _main routine with your actual program logic. The startup code will handle all system initialization and cleanup automatically. The example shows a simple "Hello AmigaOS!" output to demonstrate DOS library usage.
This code is compatible with standard Amiga assemblers like DevPac, AsmOne, or VASM, and follows AmigaOS programming conventions for maximum compatibility across different Amiga systems.