Jump to content

How do I load and run another executable in asm?

Recommended Posts

Good topic here discussing XIO 40:




DOS 2.x is a little different, IIRC, since it doesn't present binary load via CIO. My personal preference is a bespoke loader, since it's then guaranteed to work (and it doesn't take much code).

Link to comment
Share on other sites

When I used real floppies, my main 2.5 Dos had an Autorun which optionally loaded my copy of AsmEd.


It did so by recursively putting the keycodes into CH - $2FC, so the program loads as if the user typed the commands needed to do so (including DOS if Basic is present).


You could do something like that - use a VBlank routine that puts each keystroke in then waits for CH = $FF which means that it has been processed and the next one can be inserted.

On the XL OS you can also disable the keyclick if desired. And if you want go the extra step of setting colour registers so it's hidden from view.


Alternatively like mentioned, do your own loader. The binary load algorithm is fairly simple. Pseudo code follows:

Open file.  Set Run address points to "RTS".
Loop until EOF. 
  Get start address.  If start address = $FFFF then get start address again.
  Get end address.  Set current = start address.  Set Init address points to "RTS".
  Loop until current>end address. 
     Get byte, store at current.  Increment current.
     End loop.
  JSR indirect through INIT.
  End loop.
JSR indirect through RUN.
JMP through DOSVEC ($0A)  - this covers the case if the program returns control to the loader.
Edited by Rybags
Link to comment
Share on other sites

Here's actual code. It's the loader for the segmented FDISK (loaded from the SDX CAR: device).

; CAR: loader for FDISK

	icl 'sdx_equ.inc'
	icl 'macros.inc'

	org $80
BStart	.ds 2
BEnd	.ds 2
BLen	.ds 2

	org $8000

	.local Start
	jsr LoadBin
	bmi Error
	inc FileNum
	lda FileNum
	cmp #'3'
	bcc Loop
	jmp $2000 ; comes here if no run address
	ldx #0
	mwa #ErrorMsg icbadr,x
	mwa #40 icblen,x
	mva #9 iccom,x
	jmp ciov

	.local OpenFile
	ldx #16
	mva #12 iccom,x
	jsr ciov
	mva #<File icbadr,x
	mva #>File icbadr+1,x
	mwa #64 icblen,x
	mva #3 iccom,x
	mva #4+32+64 icaux1,x ; use SDX path and scan mode
	mva #2 icaux2,x ; look for hidden files
	jmp ciov
	.local LoadBin
	jsr OpenFile
	bmi Error
	jsr ReadBlock
	bpl Loop
	mva #12 iccom,x
	jsr ciov
	cpy #136 ; EOF
	bne @+
	ldy #1

	.local ReadBlock
	jsr GetPair
	bmi Error
	lda HeadBuf
	and HeadBuf+1
	cmp #$ff
	bne NoSignature
	jsr GetPair
	bmi Error
	mwa HeadBuf BStart
	jsr GetPair
	mwa HeadBuf BEnd
	sbw BEnd BStart BLen
	inw BLen
	mwa BStart icbadr,x
	mwa BLen icblen,x
	mva #7 iccom,x
	jsr ciov
	bmi Error
	cpw BStart $02E2
	bne NotInitBlock
	jmp ($2e2)
	lda #1

	.local GetPair
	ldx #16
	mwa #HeadBuf icbadr,x
	mwa #2 icblen,x
	mva #7 iccom,x
	jmp ciov

	.byte 'Error loading '
	.byte 'D:FDISK'
	.byte '0.OVL',155,0
	.ds 2
	run Start

The code to handle the three overlay files is superfluous to the loading of a single binary, so there's really not much to it. Just be careful to assemble the loader somewhere out of the way (e.g. in the stack or somewhere you're sure won't be overwritten by the loaded binary).

Link to comment
Share on other sites

In SpartaDOS X (mads format):


      blk reloc main

start ldx #$01
lp    lda fnamep,x
      sta FILE_P,x
      bpl lp

      lda #$00
      sta FLAG
      jsr U_LOAD

fnamep .word fname
fname .byte 'FOOBAR.COM',$9b

This will load and run a binary named FOOBAR.COM from the current drive/directory. The binary can be of any type recognized by SDX.


When the loaded program returns (with RTS), this one will also terminate.



  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...