Question about function call in a microprocessor?

Guest
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.
 
harisrini@gmail.com wrote in 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.
Well, to give Microchip's PIC micro-controller as an example. It only has
one working register (W). I have not used any other chips, but I know
Intel chips in your home PC have many more.

I'd use the W register for the return value. But Id need to tell my
assembler in advance that I wanted to use a,b and c. Because I have not
OS that can dynamically allocate memory for me.

So for int k = foo (int a, int b, int c);

I would globally define a, b, c
Set some values in a,b,c
Call the foo function

Foo:
Movlw a,w
Addwf b,w
Addwf c,w
Return

And end up with the result in the W register.

The stack is used by the processor to remember program jumps, because the
location of Foo in program memory is way off in some distant place. But I
never worry about this.

The only time the Stack is of concern for me is in implementing recursive
functions, because the stack is only so deep and could overflow.

It's good that you know C but you will quickly answer any questions you
have about the low level stuff, if you looked at a datasheet of any
micro-controller or processor.

Here is a simple one
http://ww1.microchip.com/downloads/en/DeviceDoc/35007b.pdf


DaveC

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
harisrini@gmail.com wrote:

...in a assembly code...
[...]

I propose something like this,

main {
[snip]

Decide whether you want to write assembly language or C.

If you are programming in C, your compiler does all of this for you.

If you are programming in assembly, you will know how to do this
as part of knowing how to program in assembly language.
 
Spehro Pefhany wrote:
Guy Macon wrote:

harisrini@gmail.com wrote:

...in a assembly code...

[...]

I propose something like this,

main {

[snip]

Decide whether you want to write assembly language or C.

If you are programming in C, your compiler does all of this for you.

If you are programming in assembly, you will know how to do this
as part of knowing how to program in assembly language.

If you're programming a small micro in C, you'd better understand
both.
Good point. I was only thinking of the PC case.
 
On Fri, 11 Feb 2005 01:07:22 -0800, harisrini wrote:

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.
Thre is no one answer to this. Different processors and compilers do it
differently. Note that many processors don't have a "stack", per se.

--
Keith
 

Welcome to EDABoard.com

Sponsor

Back
Top