Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

old skool vc++ 1.0

1 view
Skip to first unread message

rodchar

unread,
Jun 24, 2007, 1:22:00 PM6/24/07
to
hey all,

i'm looking at some old code here and was wondering what the keyword PASCAL
was for?

int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)

thanks,
rodchar

Scott McPhillips [MVP]

unread,
Jun 24, 2007, 1:54:20 PM6/24/07
to

It is (was) a calling convention used with most Windows APIs. As I
recall, it means parameters are pushed onto the stack in the opposite
order than the standard C convention.

Today it is defined as __stdcall

--
Scott McPhillips [MVP VC++]

rodchar

unread,
Jun 24, 2007, 6:43:00 PM6/24/07
to
After doing further research based on your reply I ran into yet another
unfamiliar place. I was trying to read what sounded like a basic article but
I think there may be some implied understanding involved.

Here's the article link:
http://blogs.msdn.com/oldnewthing/archive/2004/01/02/47184.aspx

After just reading the first few sentences I became lost:

The history of calling conventions, part 1
The great thing about calling conventions on the x86 platform is that there
are so many to choose from!
In the 16-bit world, part of the calling convention was fixed by the
instruction set: The BP register defaults to the SS selector, whereas the
other registers default to the DS selector. So the BP register was
necessarily the register used for accessing stack-based parameters.

So is there a place or article I can read that will help me understand this
one? BP register? Huh?

thanks,
rodchar

MrAsm

unread,
Jun 24, 2007, 6:56:11 PM6/24/07
to
On Sun, 24 Jun 2007 15:43:00 -0700, rodchar
<rod...@discussions.microsoft.com> wrote:


>So is there a place or article I can read that will help me understand this

You might read:

http://msdn2.microsoft.com/en-us/library/984x0h58(vs.71).aspx
http://blogs.msdn.com/oldnewthing/archive/2004/01/08/48616.aspx

MrAsm

Scott McPhillips [MVP]

unread,
Jun 24, 2007, 9:36:31 PM6/24/07
to
rodchar wrote:
> After doing further research based on your reply I ran into yet another
> unfamiliar place. I was trying to read what sounded like a basic article but
> I think there may be some implied understanding involved.
>
> Here's the article link:
> http://blogs.msdn.com/oldnewthing/archive/2004/01/02/47184.aspx
>
> After just reading the first few sentences I became lost:
>
> The history of calling conventions, part 1
> The great thing about calling conventions on the x86 platform is that there
> are so many to choose from!
> In the 16-bit world, part of the calling convention was fixed by the
> instruction set: The BP register defaults to the SS selector, whereas the
> other registers default to the DS selector. So the BP register was
> necessarily the register used for accessing stack-based parameters.
>
> So is there a place or article I can read that will help me understand this
> one? BP register? Huh?

If you became lost perhaps you have never learned an assembly language?
Very old skool, but lots of fun. Learn how computers really work!

The BP register (Base Pointer) saves a copy of the stack pointer on
entry into a function. So all parameters passed in the stack are
accessed with BP plus an offset.

Articles on the x86 Intel architecture would be really dusty by now, but
you can go straight to the source - Intel programmer's reference manuals:

http://www.intel.com/design/processor/manuals

Doug Harrison [MVP]

unread,
Jun 24, 2007, 9:59:06 PM6/24/07
to
On Sun, 24 Jun 2007 15:43:00 -0700, rodchar
<rod...@discussions.microsoft.com> wrote:

>After doing further research based on your reply I ran into yet another
>unfamiliar place. I was trying to read what sounded like a basic article but
>I think there may be some implied understanding involved.
>
>Here's the article link:
>http://blogs.msdn.com/oldnewthing/archive/2004/01/02/47184.aspx
>
>After just reading the first few sentences I became lost:
>
>The history of calling conventions, part 1
>The great thing about calling conventions on the x86 platform is that there
>are so many to choose from!
>In the 16-bit world, part of the calling convention was fixed by the
>instruction set: The BP register defaults to the SS selector, whereas the
>other registers default to the DS selector. So the BP register was
>necessarily the register used for accessing stack-based parameters.
>
>So is there a place or article I can read that will help me understand this
>one? BP register? Huh?

The 16-bit stuff I'd just ignore. The 32-bit stuff can be useful,
especially when debugging, and to that end, see:

"Matt's Just Enough Assembly Language to Get By."
http://www.microsoft.com/msj/0298/hood0298.aspx
http://www.microsoft.com/msj/0698/hood0698.aspx

For a lot more detail, there is:

The Art of Assembly Language
http://webster.cs.ucr.edu/


--
Doug Harrison
Visual C++ MVP

rodchar

unread,
Jun 24, 2007, 10:49:06 PM6/24/07
to
question from aritcle:
Since so much of what I'll describe depends on the registers, a quick review
of the commonly used Intel x86 register set is in order. In Figure 1, all
registers are 32 bits except where noted. "Multipurpose" means the register
can hold any arbitrary 32-bit value (for example, literal values, addresses,
and bit flags).

please forgive me for this ahead of time, but what is an Intel x86 register
set in lamen's terms? what is a register?

Scott McPhillips [MVP]

unread,
Jun 25, 2007, 12:34:26 AM6/25/07
to
rodchar wrote:
> question from aritcle:
> Since so much of what I'll describe depends on the registers, a quick review
> of the commonly used Intel x86 register set is in order. In Figure 1, all
> registers are 32 bits except where noted. "Multipurpose" means the register
> can hold any arbitrary 32-bit value (for example, literal values, addresses,
> and bit flags).
>
> please forgive me for this ahead of time, but what is an Intel x86 register
> set in lamen's terms? what is a register?

A register is a storage element, much like memory but located within the
CPU. It is very fast storage because of its intimate connection to the
arithmetic/logic unit and to other registers. Some registers may have
built in capabilities to shift and perform other primitive operations.

Doug Harrison [MVP]

unread,
Jun 25, 2007, 12:35:57 AM6/25/07
to
On Sun, 24 Jun 2007 19:49:06 -0700, rodchar
<rod...@discussions.microsoft.com> wrote:

>question from aritcle:
>Since so much of what I'll describe depends on the registers, a quick review
>of the commonly used Intel x86 register set is in order. In Figure 1, all
>registers are 32 bits except where noted. "Multipurpose" means the register
>can hold any arbitrary 32-bit value (for example, literal values, addresses,
>and bit flags).
>
>please forgive me for this ahead of time, but what is an Intel x86 register
>set in lamen's terms? what is a register?

http://en.wikipedia.org/wiki/Processor_register

rodchar

unread,
Jun 25, 2007, 1:03:00 AM6/25/07
to
how far will i have to go back in order for this to make sense? what's the
best way to relate this stuff to what i think i understand and have some work
experience in c#.net?

Doug Harrison [MVP]

unread,
Jun 25, 2007, 1:38:45 AM6/25/07
to
On Sun, 24 Jun 2007 22:03:00 -0700, rodchar
<rod...@discussions.microsoft.com> wrote:

>how far will i have to go back in order for this to make sense? what's the
>best way to relate this stuff to what i think i understand and have some work
>experience in c#.net?

Your original question was about the PASCAL macro. Calling conventions for
native code have no relevance in .NET, unless perhaps you're trying to
write P/Invoke declarations. If you want to understand it anyway, you need
to have a basic understanding of how functions are called at the assembly
level, including how arguments are passed, which is typically through
registers and/or on the stack, return addresses are stored, stack frames
are specified, etc. Since you've subsequently asked what a "register" is,
I'd say you need to start with Assembly Language 101.

rodchar

unread,
Jun 25, 2007, 8:17:01 AM6/25/07
to
thanks everyone for some direction. regarding my original post as i kept
trying to trace back to gain some understanding i kept reading something that
would lose me, usually the first few sentences of an article. I think I have
enough information to go on for now.
rod.

Joseph M. Newcomer

unread,
Jun 30, 2007, 10:18:02 PM6/30/07
to
PASCAL == _stdcall. The word "PASCAL" is now a noise word representing obsolete code and
should be removed from all files and replaced by an appropriate word like WINAPI or
CALLBACK, both of which are __stdcall.

Here's the deal:

In a __cdecl linkage (the normal default), arguments are pushed on the stack in
right-to-left order, a CALL is executed, and upon return from the CALL, the stack pointer
is incremented by the number of bytes which were pushed (stacks grow from high memory to
low memory, so "push" decrements the stack pointer and "pop" increments it:

int f(int a, int b, int c) { return a+b+c; }

f(1,2,3)
push 3
push 2
push 1
call f
add esp, 0x0C // = 12, three 4-byte ints

f: push ebp
mov ebp, esp

the stack is now

EBP + 16: 3 // c
EBP + 12: 2 // b
EBP + 8 1 // a
EBP + 4 return address
EBP: old EBP

the code is therefore

mov eax, DWORD PTR[EBP+8]
add eax, DWORD PTR[EBP+12]
add eax, DWORD PTR[EBP+16]

finally, we return
mov esp, ebp
pop ebp
ret

In a __stdcall, a platform-specific faster call mechanism is used. In this case, the
nmber of parameters must be fixed at compile time, and the parameters are cleaned up the
callee:

int __stdcall f(int a, int b, int c);

push 3
push 2
push 1
call f
....next line of code

Note there is no code to increment the stack pointer to remove the parameters

f: push ebp
mov ebp, esp
...add instructions as above
mov esp, ebp
pop ebp
ret 0x0C

Here, the ret instruction cleans off the parameters. Note that it takes 1 CPU clock cycle
(350picoseconds on a 2.8GHz machine) to both pop the return address and strip the
parameters.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

0 new messages