Re: Question about function call in a microprocessor?
From: Roger Lascelles (invalidl_at_invalid.invalid)
Date: 02/12/05
- Next message: pooua_at_aol.com: "Re: Speakers for High Frequency Sound"
- Previous message: no_one: "Re: Speakers for High Frequency Sound"
- In reply to: harisrini_at_gmail.com: "Question about function call in a microprocessor?"
- Next in thread: Andrew Holme: "Re: Question about function call in a microprocessor?"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 12 Feb 2005 12:00:25 +1100
<harisrini@gmail.com> wrote in message
news:1108112842.485983.209510@o13g2000cwo.googlegroups.com...
> Hello all the experts here,
> I would like to know how a typical porcessor executes a function call
> or subroutine call in a assembly code.
>
> I know it uses stack for doing it and I do understand the mechanism
> about the stack here. I would want to go one step further and ask you
> how it exaclty saves the internal variable, returned variable and call
> in functions and all those things. If you see I am asking something
> like a compiler design, where in it maps the high-level language into
> architecture specific assembly code. I would really appreciate if you
> could do with an example.
>
> I propose something like this,
>
> main {
>
> int k = foo (int a, int b, int c);
> foo uses lets says 5 local varaibles.
>
> where in foo calls another function
> int l = foo1 (int d, int e);
> foo1 uses 2 local variables.
>
> Please shed some thought on this.
>
> Thanks in advance
> Hariharan K Srinivasan.
>
This is the sort of question you can best answer by investigation. Google
will
help you more than the busy people on newsgroups can. You need to get a C
compiler and look at the output listing. I recommend Pelles C from :
http://www.smorgasbordet.com/pellesc/
If you target the 80x86 family, you get a beautiful clear assembly you can
work through. Unlike many small and RISC processors, the 80x86 family
compilers tend to give consistent output for high level languages no matter
how many levels deep are your function calls.
Pelles C gives you a nice editor to write and compile from. To see the
output
listing run the PODUMP utility with the /DISASM switch on the .OBJ files .
Here is what I get :
15: void foo1( int d, int e )
16: {
_foo1:
[00000000] 55 push ebp
[00000001] 89E5 mov ebp,esp
@1:
[00000003] 5D pop ebp
[00000004] C3 ret
19: void foo( int a, int b, int c )
20: {
_foo:
[00000010] 55 push ebp
[00000011] 89E5 mov ebp,esp
[00000013] 83EC10 sub esp,+10
[00000016] 53 push ebx
21: int v1, v2, v3, v4, v5;
22: v1 = 0;
[00000017] 31DB xor ebx,ebx
23:
24: foo1( v1, a );
[00000019] 8B4508 mov eax,dword ptr [ebp+8]
[0000001C] 50 push eax
[0000001D] 53 push ebx
[0000001E] E800000000 call _foo1
[00000023] 83C408 add esp,+8
@2:
[00000026] 5B pop ebx
[00000027] 89EC mov esp,ebp
[00000029] 5D pop ebp
[0000002A] C3 ret
The function call to foo1() pushes on the stack the parameters from in eax
and ebx.
The ebp is an indexing register used to access the stack variables belonging
to
a function.
sub esp,+10 makes room for local variables while add esp+8 releases that
stack
space at the end of the function.
If you start with simple function calls, you can work out what is going on.
Roger
- Next message: pooua_at_aol.com: "Re: Speakers for High Frequency Sound"
- Previous message: no_one: "Re: Speakers for High Frequency Sound"
- In reply to: harisrini_at_gmail.com: "Question about function call in a microprocessor?"
- Next in thread: Andrew Holme: "Re: Question about function call in a microprocessor?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|