I started with masm32 and i have some questions

I read this

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

I also read that in Pascal Convention the called func responsible for stack and in C the caller is responsible..

Can anybody explain how this works??
Coz I dont see any need in balancing stack

first we push 3 params then calling the func
now i guess sp goes 2 bytes up and than the functions pops the three params to use em..
Then sp goes 8 bytes down to the return address and returns no>????
this is how i imagine api funcs work

mov ax,2
mov bx,4
push ax
push bx 
call func

ret
        
        
func proc       
    add sp,2 
    pop cx
    pop dx
    sub sp,6 ;balancing stack .... so why it says caller should balance??
             ret
    func endp

so why balance stack if its all goes nice??
anybody please explain how win32 func call work..

There's one very large difference between pascal and C.
C contains functions with the ability to accept variable amounts of arguments. Such as... printf.
Pascal doesn't have(?)/can't handle variables argument implementations very well.

And your call to lets say printf would look like..

push eax   ;lets say contains the number 15
    push format  ;lets say contains beg address of '%d is the number', 10, 0
    call _printf
    add esp, 8

So that printf call would look like this in C

int i = 15;
    printf("%d is the number\n", i);

There's more to the C calling convention as well such as the preservation of certain registers.

ok but why the caller balance the stack??

doesnt the function balances it after it returns??

just call _printf


why does the caller balances the stack and not the function itself??

ok but why the caller should balance the stack after the function execution doesnt the function balances the stack when it pops the arguments??

ok but why the caller should balance the stack after the function execution doesnt the function balances the stack when it pops the arguments??

No because they don't pop them off the stack. Not to mention the stack pointer as well as the stack has to be kept in the same condition as it was passed to it.
The C calling convention is the way it is because that's how it was designed for not only the language but also for unix.
It's easier to implement variable argument length functions such as printf if the caller handles cleaning up the stack. It's also more stable because in the C calling convention a large majority of the registers are considered 'sacred' including ebp and esp so if I call a function then I can expect ebp and esp to be in the same condition as they were before I called that function.

So you don't want to do it then huh? You want the calling function to do it? That's fine then write your own functions/macros in assembly and don't worry about it.
You want to utilize that wealth of usefulness known as the C standard library? Alright then you gotta play by it's rules. (Also note you're playing by the OSs rules as well)

Like writing drivers for linux, yeah some parts of the kernels system calls I don't like for writing drivers but what's my other option? Writing my own kernel....

Thank you man you explained me a lot of new things I never knew before.

But can you specify what happens inside the asm when I call a kernel function.

Because you said that kernel funcs dont pop anything from the stack so how it works??

can you tell me what happens in theory inside the func and give me some asm code to realize it better??

and what did you ment when you said the stack and sp should stay the same doesnt the stack should have the return addres??

Please answer my questions Its very important for me btw I very appreciate your help!!

But can you specify what happens inside the asm when I call a kernel function.

A kernel function from what kernel? Which O/S? You can't directly call functions used inside the kernel of an O/S unless your code resides within the kernel itself or within a module of the kernel.
Most o/s's however have a system call interface to help drivers and user programs accomplish what they need to do such as I/O.
Whatever goes on inside that system call when it's called is completely dependent on it's code.

Because you said that kernel funcs dont pop anything from the stack so how it works??

I never said anything of the sort in relation to Operating systems.
If you're referring to me saying that in the C calling convention the C function called doesn't pop anything off the stack then what I was saying is.. the function doesn't pop something off the stack with the intention of fixing the stack for the caller. If the function wants to pop something off the stack then it just sets up the stack pointers so that it can then the stack pointers are changed back to how they were before the function was called before the function returns to the caller.
Although in more times then not it's easier to just referencing stuff on the stack like an array of memory like [ebp - 4] or the likes.
I'm not going to write you asm code because quite frankly I don't think you understand a lick of assembly.

and what did you ment when you said the stack and sp should stay the same doesnt the stack should have the return addres??

Most C functions from the stdlib return their return value in eax but yes you can return it in the stack if you wanted to but that doesn't have anything to do with esp being the same as it was when the callee returns as it was when the callee was called.

I recommend you go buy Jeff duntemanns book asm programming the second edition and read if you're that interested in assembly.

Thanks man anyway i found the explanation here
http://www.delphi3000.com/articles/article_3771.asp?SK=

and why did you say that i dont understand lick of asm
the fact that you are a pro coder with expirience and you learned all this stuff for years and I am a 15 years old guy that just started learning asm at school doesnt means you have to be rude ....

plus i know all the asm basics:
8086 instructions stack interputs procedures macros memory calculating.

in my school we just started learning 8086 and i learned it in 2 months from reading in internet and asking questions from nice people like you.

i dont know shit about win32 asm thats why I ask questions from you...

by the way I read all the asm codes here and understood em very well if you could just show me some little piece of code like that i wouldnt have to google it
http://www.delphi3000.com/articles/article_3771.asp?SK=

i understood that it works by moving to bp the stack pointer and than use bp to move values from stack
i mean bp role plays sp for the time win32 function is called and than bp pops back to restore its original value than it returns and adds to sp few bytes.. that explain whys you said stack and bp stay the same after func..
see i know some asm :)

anyway thank you very much for your help you really know alot about programming!!!!
srsly u rock dude just plesae no need to be rude :) i'm just kid lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.