Search code examples
assemblyx86dostasmreal-mode

Allocation of Stack and Data in a Real Mode MZ .exe under Turbo Assembler 2.0


I have succeeded in creating separate stack, data, and code segments under TASM using the SEGMENT directive, but something still bothers me. When the following code is assembled with Turbo Assembler 2.0, TLINK produces a binary whose size is in the neighborhood of 90KB.

.286

myStack segment para stack 'stack'
db 0FFFFh dup (?)
ends myStack

myData segment para 'data'
msg db 'Memes!$'
db 7FFFh dup (?)
ends myData

myCode segment para 'code'
assume cs:myCode
assume ss:myStack

start:
mov  ax,myData
mov  ds,ax

push offset msg
call write
add  sp,2

mov  ah,4ch
int  21h

write:
push bp
mov  bp,sp
mov  dx,[bp+4]
mov  ah,9h
int  21h
pop  bp
ret

ends myCode
end start

Now it seems to me that the MZ file format should enable an .exe to specify that it requires memory allocation beyond what is actually contained in the binary image (via the min/max paragraphs of memory allocated in addition to the code size entry I'm guessing).

So my question is: how does one coax the assembler/linker into generating an .exe with the appropriate header for allocating memory without directly including placeholder values in the binary image?


Solution

  • Been a while, but I think you can achieve what you are trying to do by placing the code segment first. Then place all the initialized data at the start of the data segment followed by all the uninitialized data in the data segment followed by the stack segment which is all uninitialized. Since all the uninitialized data has been forced to the end, it won't be necessary to allocate any of the space in the file.

    Your code would probably give you the results you want if it looked like:

    .286
    
    myCode segment para 'code'
    assume cs:myCode
    assume ss:myStack
    
    start:
    mov  ax,myData
    mov  ds,ax
    
    push offset msg
    call write
    add  sp,2
    
    mov  ah,4ch
    int  21h
    
    write:
    push bp
    mov  bp,sp
    mov  dx,[bp+4]
    mov  ah,9h
    int  21h
    pop  bp
    ret
    
    ends myCode
    
    myData segment para 'data'
    msg db 'Memes!$'             ; Initialized data first
    db 7FFFh dup (?)             ; Uninitialized data after all initialized data
    ends myData
    
    myStack segment para stack 'stack'
                                 ; Leave blank, this will allow the stack to
                                 ; to use the full 64k segment
    ends myStack
    
    end start