News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

.model ?

Started by cman, September 27, 2007, 06:44:28 PM

Previous topic - Next topic

cman

How is the .model directive used with medium, large, huge memory models? I know that this sets different limits for code and data segment sizes and and defaults to far function calls for proceedures , but how does the assembler use multible code segments in a single application? Thanks for any details on how this all works ( the book I'm reading on this isn't too clear on this  :'( ).

BogdanOntanu

Your book is old.

For Windows all you need is

. model flat,stdcall


Ignore all far pointer, segments, huge, large, small and stuff like this for modern win32 programming
You can return to segmentation and this kind of stuff when you are more advanced and when you will need it (if ever)
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

MichaelW

cman,

This example is a starting point for answering your questions, modify as necessary. Assemble with:

ml /Fl /Sa /Sc /c filename.asm

And link with:

link16 /MAP filename.obj;

And you should get a listing and a map file that will show you what the assembler and linker produced. In the listing file, the "----" and "R" are for relocatable addresses that the linker must resolve (this is part of the mechanism that allows a 16-bit program to be loaded and run at any segment address where there is room for it).


.model large
.stack
.data

    nearvar dw 0

.fardata

    farvar1 dw 0
    farvar2 dw 0

.code

proc1 proc

    ; No segment overrided needed because mov, like
    ; most instructions that access data, uses DS by
    ; default, and the .startup code loaded DS with
    ; the segment address of DGROUP, which includes
    ; the near data segment.

    mov ax, nearvar

    ; ML will encode this as a far return (opcode CBh)
    ; because it's in a far procedure.

    ret

proc1 endp

proc2 proc

    ; ML will encode this as a near call (opcode E8h),
    ; because the destination is in the same segment,
    ; so the address can be resolved directly, but
    ; will precede it with a push cs (opcode 0Eh) to
    ; effectively emulate a far call, so the far
    ; return (in the called procedure) will work
    ; correctly.

    call proc1

    ; ML will encode this as a far return (opcode CBh)
    ; because it's in a far procedure.

    ret

proc2 endp


    call proc1

.code startup
.startup

    mov ax, seg farvar1
    mov es, ax
    assume es: seg farvar1

    ; ML will add ES: segment override prefix (26h) for
    ; both of these accesses. Without the assume the
    ; prefix would need to be coded.

    mov ax, farvar1
    mov ax, farvar2

    ; ML will encoded this as a far call (opcode 9Ah)
    ; because proc1 is in a different code segment.

    call proc1

.exit
end


eschew obfuscation

cman

Wow , thanks for all the information guys! :U This is very helpful. I guess the book I'm working out of is old ( the copyright is 1998 but the book only seems to cover old DOS type assembler ). Does anyone know of a book that covers modern Win32 assembler? Thanks again!

boogara

Quote from: cman on September 28, 2007, 06:49:19 PM
Wow , thanks for all the information guys! :U This is very helpful. I guess the book I'm working out of is old ( the copyright is 1998 but the book only seems to cover old DOS type assembler ). Does anyone know of a book that covers modern Win32 assembler? Thanks again!
Iczelion is probably most famous for modern MASM assembler tutorials.

cman

Thanks for the information! I also have a question on the .stack directive ( I know this is outdated 16-bit assembly information, but I'm curious about this  :bg). How does one know ahead of time how much stack space will be needed ( without sitting and counting ) to be reserved with this directive. What happens if the amount of stack space requested if too small? Thanks for any information...  :U

u

Stack trouble is what I've experienced in half of my projects for mobile devices. Your app simply crashes unexpectedly, or lives-on to do damage to the data it was processing, and then crash. Fortunately in PCs it's hard to make such problems, just do mind the stack-guard page, that lets your stack grow. (skipping access to that page by specifying more than 4k or 8k function-local variables and not "touching" those pages in the correct order causes a GPF)
Please use a smaller graphic in your signature.

Rainstorm

cman wrote..
Quote). Does anyone know of a book that covers modern Win32 assembler?..

http://website.masm32.com/files/td_win32asm_all.zip

Those are pretty good I think.
they don't use the invoke statements for os calls..but they are well commented &
I found them to be quiet helpful.

~