News:

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

ASK A QUESTION!!!!

Started by EnFeR RoI, January 18, 2010, 07:53:48 AM

Previous topic - Next topic

EnFeR RoI

.MODEL FLAT, STDCALL
.MODEL is an assembler directive that specifies memory model of your program. Under Win32, there's only on model, FLAT model.
STDCALL tells MASM about parameter passing convention. Parameter passing convention specifies the order of  parameter passing, left-to-right or right-to-left, and also who will balance the stack frame after the function call.
Under Win16, there are two types of calling convention, C and PASCAL
C calling convention passes parameters from right to left, that is , the rightmost parameter is pushed first. The caller is responsible for balancing the stack frame after the call. For example, in order to call a function named foo(int first_param, int second_param, int third_param) in C calling convention the asm codes will look like this:

push  [third_param]               ; Push the third parameter
push  [second_param]            ; Followed by the second
push  [first_param]                ; And the first
call    foo
add    sp, 12                                ; The caller balances the stack frame
PASCAL calling convention is the reverse of C calling convention. It passes parameters from left to right and the callee is responsible for the stack balancing after the call.
Win16 adopts PASCAL convention because it produces smaller codes. C convention is useful when you don't know how many parameters will be passed to the function as in the case of wsprintf(). In the case of wsprintf(), the function has no way to determine beforehand how many parameters will be pushed on the stack, so it cannot do the stack balancing.
STDCALL is the hybrid of C and PASCAL convention. It passes parameter from right to left but the callee is responsible for stack balancing after the call.Win32 platform use STDCALL exclusively. Except in one case: wsprintf(). You must use C calling convention with wsprintf().

QuoteCan you explain this line:-
add    sp, 12                                ; The caller balances the stack frame

I currently learning assembly language and new in the world of MASM...
Hope you reply soon.. :wink

Thanks in advance.
EnFeR RoI.

dedndave

first, there is a mistake in that text - lol
for 32-bit code, it should be

        add     esp,12

for 16-bit code, the parameters would be words, and it would be

        add     sp,6

the 3 parameters pushed onto the stack are dwords (4 bytes each)
with the C calling convention, the caller (that usually means you   :bg ) must balance the stack after the call
3 x 4 = 12, so you must adjust the stack pointer by 12 bytes after the call
the advantage is that parameters may be passed back to the caller on the stack by the routine
the C calling convention is typically only used if you are writing ASM routines that will be linked with C programs

otherwise, we normally use the STDCALL convention
the 3 parameters are popped off the stack by the callee (that usually means the routine)
not only does this affect the way routines are called, but the way PROCs are terminated
when you write a PROC, the assembler examines the calling convention to determine whether or not the stack is balanced by RET
in the case of STDCALL, a PROC with 3 dword parameters will end with RET 12,
which balances the stack by discarding the 3 pushed dword parameters

EnFeR RoI

Thanks for the answer and you explain the concept in a very excellent way.
I am just a student and just learning assembly language and need a guidance like you.... 

Quote1)I read a Iczelion tutorial for learning an assembly language, is it reallly a "add esp, 12" instead of add sp, 12??
            2)What is full form of esp and sp(i thinks sp stand for stack pointer)????

Thanks for reply!!!! :bg
EnFeR RoI.


dedndave

yes - it is ESP (extended stack pointer)
in 32-bit code, registers are 32-bits wide and are referenced with the E
if you alter the SP in 32-bit code, it will work most of the time   :wink
the times it isn't ok, it will cause a bug that will be hard to find - lol
that is because the stack pointer is a 32-bit value
if ESP is 80000000h and you add 4 to SP, ESP becomes 80000004h
but, if ESP is 7FFFFFFCh and you add 4 to SP, ESP becomes 7FFF0000h - the stack loses it's place

MichaelW

One small correction:

Quotefor 16-bit code, the parameters would be words

They would not be smaller than a WORD, but they could be larger, for example DWORD, REAL4, REAL8, REAL10, or even a structure.
eschew obfuscation

EnFeR RoI

I finished my first tutorials of Iczelion and thanks for clearing a concept,now go to learning Iczelion second tutorial..........
Hurray for my first tutorial complete. :green
                                    For me, assembly language seems to be easy,hope other tutorials  is also easy for me

Thanks in advance.
EnFeR RoI.

dedndave

i wouldn't expect it to be easy   :bg
if it were easy, every Happy Larry down the block would be an assembly programmer - lol
assembly is not for the faint of heart
but, it can be interesting and fun, even rewarding - just expect to be challenged   :U

EnFeR RoI

Sure it is challenging for me to do the assembly a but the people like you help me a lot and give me a confidence booster not to quit assembly language !! :8)
                           
Thanks in advance.
EnFeR RoI.