• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

Asm com loader

Hey, I am looking for a sample of a com loader writen in asm. I want to use only bios ints to do the file I/O. Can anyone help me? Or direct me to a place on the web?

0
laeuchli
Asked:
laeuchli
  • 7
  • 7
1 Solution
 
laeuchliAuthor Commented:
Adjusted points to 20
0
 
nils pipenbrinckCommented:
20 points are a ehm.. not that much points... anyways.. what do you want to do? load a file from a self written asm com program using bios calls?

nils.
0
 
laeuchliAuthor Commented:
I want to load a com file from a asm program I wrote and start it running. I gave 20 points cause thats all I have. I will add to it, as I get more.
0
Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

 
nils pipenbrinckCommented:
ok.. you want to load a .com file.. that's the way I do it:

Generate a code that loads the com file to ds:0100h. then copy this code to a place which won't be overridden. This place could be ds+4096. That's far enough since a com cannot be larger than 64k.

jump to this code (you have to do a far jump!).

now you load the new .com file (this overrides your old .com file image in memory). set the sp register to where it normally is when you enter a .com program (i just checked it's f2ceh on my system).

now all you have to do is to make sure es equals ds and do a far jump to the start of your loaded com image (which will be at ds:0100h).

it's not that hard ...

ah.. last thing.. make sure the d-flag is cleared before you jump. some programs assume it's state cleared.

Nils



That's it.



0
 
laeuchliAuthor Commented:
I know the theory, I need some code. My code keeps failing.
0
 
nils pipenbrinckCommented:
here is some code.. I used the nasm assembler, but the code is very generic.. just a little com program.

please note, that there is no direct far jump on x86. therefore I used the retf instruction to "fake" a far jump.. the only thing I did is to push the segment and offset onto the stack and did the jump with a retf.


          ORG 100h

SECTION .text

start:
          ; copy loadercode to a save place.. (64k ahead of program)
          mov   si, loadercode
          xor   di, di
          mov   cx, loaderend-loadercode
          mov   ax, ds
          add   ax, 4112        ; that's 010100h bytes after program start.
          push  ax              ; push destination segment
          push  di              ; push destination offst
          mov   es, ax
          cld
          rep   movsb           ; copy the code..
          retf                  ; retf to fake a farjump

filename: db "test.com",0

loadercode:
          ; open file:
          mov   ah, 3dh         ; subfunction: openfile
          mov   al, 0           ; open for read access
          mov   dx, filename
          int   21h

          ; read from file (64k-1 max)
          mov   bx, ax          ; filehandle
          mov   dx, 0100h       ; start offset
          mov   cx, 0ffffh      ; # of bytes
          mov   ah, 3fh
          int   21h

          ; close file
          mov   ah, 3eh
          int   21h

          ; cleanup registers (some com depend on the state
          ; of the registers at startup..
          push  ds
          pop   es
          xor   ax, ax
          xor   bx, bx
          xor   cx, cx
          xor   dx, dx
          xor   si, si
          xor   di, di

          push  ds              ; push start segment onto stack
          push  word 0100h      ; same with offset
          retf                  ; fake a far jump with retf
          nop
loaderend:
0
 
nils pipenbrinckCommented:
ehm.. no error checking will be done if the file test.com doesn't exist..

I think the program will most likely crash or go into a infinite loop.. I don't know.

Nils
0
 
nils pipenbrinckCommented:
some last thing:

there are also programs who depends on the memory contents of sp to 0ffffh. I trash this memory... so if you want to support these rare programs you have to restore this memory contents before you retf to the loaded code..

0
 
laeuchliAuthor Commented:
Adjusted points to 25
0
 
laeuchliAuthor Commented:
does it make a difference to the program if I change the code to use the int 10h?
0
 
nils pipenbrinckCommented:
Hm.. I guess you can't load a file using int10.. if you can tell me how :)

anyways.. it doesn't make any difference.. you can place any code before my code you want to.. just make sure, that there is nothing on the stack when you execute my "move to save area code"...

nils

0
 
nils pipenbrinckCommented:
oh well..

and ds and es should be equal to cs.. otherwise it'll suck..

nils
0
 
laeuchliAuthor Commented:
so whats the finaly code with all the corrections? I am getting confused. I know how to do file i/o, but you have to use int 13, not int 10, my mistake.
0
 
laeuchliAuthor Commented:
Adjusted points to 50
0

Featured Post

IT Degree with Certifications Included

Aspire to become a network administrator, network security analyst, or computer and information systems manager? Make the most of your experience as an IT professional by earning your B.S. in Network Operations and Security.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now