A little about .Model Flat,Stdcall
Win32 Asm Basics
By Lord Lucifer
Tue January 26, 1999
 

Introduction:



At the top of nearly every win32asm source file you will find the line:
.model flat,Stdcall
This tells the assembler what memory model and calling convention to use.  This article will describe the meanings of these statements...
 

Flat:



Windows uses a flat memory model, which basically is a 32-bit protected mode architecture, with segmentation effectively disabled. The reason that windows uses the flat memory model is for portability; The segmented architecture of the intel processor is not found on other processors.

For more information on the Flat Memory Model see the Intel Documentation at:
ftp://download.intel.com/design/intarch/papers/esc_ia_p.pdf
 

Stdcall:



The Windows API uses the stdcall calling convention exclusively (excepty for one function - wsprintf). The proprties of the stdcall convention are that the function parameters are pushed onto the stack in reverse order and the function is responsible for adjusting the stack. The stdcall option in the .model directive tells the assembler that every procedure uses the stcall convention. It basically changes the code inserted by the assembler for the PROC and ENDP directives:

;Written code
Procedure PROC p1:DWORD, p2:DWORD
    xor eax,eax
    ret
Procedure ENDP

;Assembled code
Procedure PROC p1:DWORD, p2:DWORD
    ENTERD 00000h,0    ; inserted by assembler
    xor eax,eax
    LEAVED             ; inserted by assembler
    RET 00008h         ; changed by assembler
Procedure ENDP

As can be seen, using stdcall adds some overhead to the program (7 bytes in this example). However, it allows the ability to pass function parameters on the stack easily. One advantage of using assembly language, though, is the ability to pass parameters using the registers, which both increases speed and reduces code size.

Generally, for type checking, readability, and programming ease it is worthwile to pass parameters via the stack. But if maximum speed and minimum code size are required, parameters should be passed through the registers.
 

For a plaintext copy: modelfs.txt



Copyright (C) 1999
Lord Lucifer lord-lucifer@usa.net