Tuesday 25 April 2023

Building .exe files larger than 64kb

Ever since I started programming for DOS, I've had to face the limitation of .com files, which have a maximum file size of 64kb. Today, I figured out exactly why this is, and a way that I can get larger files compiled! The way .com files work, is that all the code and data is dumped into a single data segment at offset 100h. Each segment in DOS has a size of 64kb, hence the limit for .com files. Working within limitations can be fun, but there are times where I've wanted to add more than I could. That changed today, where I had quite the revelation. I knew before that if you use a linker, you can utilize multiple segments, but I had no idea how to use them. I've finally figured it out, and it's shockingly simple:

segment code

..start:

mov ax,stack
mov ss,ax
mov sp,stack_top

mov ax,yems
mov ds,ax
mov dx,hello
mov ah,9
int 21h
mov ax,moar
mov ds,ax
mov dx,hi2
mov ah,9
int 21h
mov ah,4ch
int 21h

segment yems
hello: db "hello world$"

segment dayter
hi:
incbin "E:\dos\low1.raw"
incbin "E:\dos\low2.raw"
incbin "E:\dos\low3.raw"
incbin "E:\dos\low4.raw"
incbin "E:\dos\low5.raw"
incbin "E:\dos\low6.raw"
incbin "E:\dos\low7.raw"
incbin "E:\dos\low8.raw"
incbin "E:\dos\low1b.raw"
incbin "E:\dos\low3b.raw"
incbin "E:\dos\low5b.raw"
incbin "E:\dos\low7b.raw"
incbin "E:\dos\low8b.raw"
incbin "E:\dos\low1c.raw"
segment moar
hi2:
        db "does this still work?$"
incbin "E:\dos\extreme.raw"
segment stack stack
resb 256
stack_top:

All you have to do is change register ds to contain the address of the appropriate segment, and you're all good! The code above is a mess, but it's what I used for testing. All files included in segment dayter add up to 63kb. The file in segment moar is 16.9 (nice) kb. The whole .exe is about 80kb!

Because I like to do all the development in my Windows environment, I needed to use a 32-bit linker. For this, I highly recommend Alink! Of course, I'm using NASM for assembling the code. Here are the commands, for future reference:

nasm -f obj -o file.obj file.asm
alink file.obj

No comments:

Post a Comment

Amiga module player for DOS - the first draft!

After one week, I've finally pulled off what I previously thought impossible - an entire module player, running in real mode DOS! I'...