2

Apple II was the first computer I used and programmed on, but only in BASIC, and back in 1979. I learned machine code on my ZX Spectrum which was tape only.

On Apple II how do you load/run a machine code program from the disk from the BASIC prompt?

  • Do you just type the program file's name?
  • Do you first load it and then run it?
  • Does the system know which files are machine code? From a file extension, file header, or metadata in the filesystem/catalog/directory?
  • Does it require a BASIC launch program that knows what address to load it in ram and the start executing machine code with a command that takes that address as an argument?
  • Some other way?
  • Is it different between Apple DOS vs ProDOS?
  • It it different between Integer BASIC and Applesoft BASIC?

This is surprisingly resisting my Googling efforts. I thought it would be trivial to find.

(I'm only interested in the "original" Apple II models, not the IIGS, which is surely different.)

2

3 Answers 3

8

In Apple DOS simply

BRUN nameoffile

if it is an executable binary (type B in CATALOG) it will load and call the machine language at the address that was used to save the binary. If it is just a blob of binary (not code) it will of course crash with BRK call and land in the monitor. Following options are possible Dn to choose the disk, Ss slot number for the disk controller, Vn to select the volume, Aa to load at a specific address (which is commonly not a good idea for programs, as 6502 code is rarely position independant).

It is also possible to just load the binary with the BLOAD command and then call the binary with CALL if you know where the binary was loaded.

EDIT: On normally loaded DOS 3.3 the last BLOAD/BRUN address is in $AA72.$AA73 and the lengths in $AA60.$AA61.

In BASIC

?PEEK(43616)+PEEK(43617)*256
?PEEK(43634)+PEEK(43635)*256
6
  • If the B is stored in the CATALOG (filesystem in my wording), where is "the address that was used to save the binary" stored? Also in the CATALOG or in a file header? Is the load address and the start address always the same? Commented Aug 8 at 9:47
  • 1
    The four first bytes of the content of a binary file contains the address and the length of the binary blob. The values are set during the BSAVE call with the Aaaaa, Lllll parameter. The catalog contains only the length of the files in number of sectors. This applies only to B type (i.e. type 4) files. Commented Aug 8 at 10:00
  • I found this page on the File Descriptive Entry which contains 'file type and flags' at offset $02, which can have value $40 for a "b type file" - is that the same B? Or would it be the value $04 which means "BINARY file"? Commented Aug 8 at 10:06
  • 1
    $04 is for Binary files. The $08, $10, $20 and $40 were defined but never used. According to Beneath Apple DOS book. Commented Aug 8 at 10:15
  • 1
    @PatrickSchlüter: "10 - RELOCATABLE object module file" is documented on page 4-6 in Beneath Apple DOS (fifth printing). This "R" filetype was occasionally used, e.g. on the Applesoft/DOS Toolkit disks. I vaguely recall seeing the (08) "S" type used also, but very rarely. Commented Aug 8 at 11:28
4

Technically, you don't.

You're running programs (and generally doing everything that requires disk access) from DOS or ProDOS - Without a DOS booted, there is no way to access the disk drive on an Apple II - it just looks as not connected. You will thus not find any disk-related commands in the BASIC manuals - you need to consult the DOS3.3 and ProDOS manuals.

After booting to a DOS (via autoboot or the proper PR# command), BASIC seems to know a number of additional commands (which are, in fact, DOS commands). There are some subtile differences in these new commands (you can, for example, not squeeze multiple DOS commands into a single line, and not all of them can be executed from a program - which is a bit special, anyhow, requiring escaped PRINT instructions). Explaining all of the commands for both DOSs here would reproduce what you can read in a DOS or ProDOS manual and doesn't make much sense.

File information is stored differently between DOS and ProDOS. DOS puts a simple 4-byte header in front of the file containing load address and load length. BRUN uses this information to load the file to the proper place. ProDOS has a much more complicated file header (that you might want to look up in a ProDOS manual), but essentially, it stores load address and length as well for BRUN. BRUN starts the file from its load address.

10
  • By "in front of" do you mean as the first bytes of the file or on the disk just before the data which constitutes the file? If I were to load the file into a program that doesn't understand the format would I see these bytes or not? In Patrick's answer he says it's in the CATALOG. Commented Aug 8 at 9:53
  • Yes. What else could I mean? You would see these four bytes.
    – tofro
    Commented Aug 8 at 10:05
  • 1
    Patrick says The information that the file is binary is in the catalog - which is correct
    – tofro
    Commented Aug 8 at 10:09
  • 2
    Yes, the information that it is a binary file is indeed in the CATALOG. The load address and the length are not in the CATALOG though, but in the content of the file. Commented Aug 8 at 10:19
  • 1
    If one is using a floppy emulator and a machine code program of 4096 bytes or fewer is linked to start loading at $0800 and start execution at $0801, is preceded by a byte 1-15 indicating the number of 256-byte pages to load, and the file is padded out to 143,360 bytes, mounting that file and doing PR#6 would execute the program therein directly without having to load any DOS first.
    – supercat
    Commented Aug 8 at 21:38
3

Partial ProDOS answer.

Thanks to the information in the other answers which are more about Apple DOS, I was able to find (some of?) the information for ProDOS which differs.

ProDOS file header:

UPDATE: It turns out that this metadata is stored in the directory structure of the files, not as a header in the first bytes of the files' data: Apple II Binary File Format

         +----------------------------+
 1 byte  | storage_type | name_length | $00
         |----------------------------|
         |                            | $01
         /                            /
15 bytes /         file_name          /
         |                            | $0F
         |----------------------------|
 1 byte  |         file_type          | $10
         |----------------------------|
         |                            | $11
 2 bytes |        key_pointer         | $12
         |----------------------------|
         |                            | $13
 2 bytes |        blocks_used         | $14
         |----------------------------|
         |                            | $15
 3 bytes |            EOF             |
         |                            | $17
         |----------------------------|
         |                            | $18
         |          creation          |
 4 bytes |        date & time         |
         |                            | $1B
         |----------------------------|
 1 byte  |          version           | $1C
         |----------------------------|
 1 byte  |        min_version         | $1D
         |----------------------------|
 1 byte  |           access           | $1E
         |----------------------------|
         |                            | $1F
 2 bytes |          aux_type          | $20
         |----------------------------|
         |                            | $21
         |                            |
 4 bytes |          last mod          |
         |                            | $24
         |----------------------------|
         |                            | $25
 2 bytes |       header_pointer       | $26
         +----------------------------+

At offset $1f:

aux_type (2 bytes): A general-purpose field in which a system program can store additional information about the internal format of a file. For example, the ProDOS BASIC system program uses this field to record the load address of a BASIC program or binary file, or the record length of a text file. I'd assume a binary file that is machine code just works with BRUN and BLOAD by loading at this address and jumping to the same address to execute, and will probably crash for binary files which are not machine code.

There is also a longer list of file types than with Apple DOS. I removed the ones from the chart which only pertain to the Apple III.

File Type      Preferred Use
$00            Typeless file
$01            Bad block file
$04            ASCII text file
$06            General binary file
$08            Graphics screen file
$0F            Directory file
$19            AppleWorks Data Base file
$1A            AppleWorks Word Processor file
$1B            AppleWorks Spreadsheet file
$1C-$EE        Reserved
$EF            Pascal area
$F0            ProDOS CI added command file
$F1-$F8        ProDOS user defined files 1-8
$F9            ProDOS reserved
$FA            Integer BASIC program file
$FB            Integer BASIC variable file
$FC            Applesoft program file
$FD            Applesoft variables file
$FE            Relocatable code file (EDASM)
$FF            ProDOS system file

I'm not sure if this applies only/both to $06 General binary file and $FC Applesoft program file. The types are probably somewhat like a suggestion and might behave the same.

But then there's $FE Relocatable code file (EDASM), which will surely have a more complicated file format for the reloc info. They require the special tools RBOOT and RLOAD.

3
  • 1
    The Relocatable code file is what later operating systems will call an object file - They are not intended for direct execution, but should be processed by a linker.
    – tofro
    Commented Aug 8 at 12:58
  • 1
    In the CiderPress II project, You can find a brief description of DOS and ProDOS filesystem structures next to the code that implements them.
    – fadden
    Commented Aug 8 at 14:31
  • 1
    @hippietrail: you may also want to review the DOS section of this doc. Some 'B' files had an embedded length that was significantly shorter than the file content, so that BRUN would just execute a tiny loader within a larger file. Also, ciderpress2.com/doc-index.html may have additional relevant info if you're working with old file formats.
    – fadden
    Commented Aug 8 at 14:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .