Tuesday, February 1, 2011

C Program to Check How Function Calls Are Made Using Stack

A stack is used by programming languages for implementing function calls. Here's a handy C program to check how function calls are made using stack.



#include < stdio.h >
#include < conio.h >
#include < stdlib.h >
#include < dos.h >

unsigned int far *ptr ;
void ( *p )( void ) ;

void f1( ) ;
void f2( ) ;

void main( )
{
f1( ) ;
f2( ) ;

printf ( "\nback to main..." ) ;
exit ( 1 ) ;
}

void f1( )
{
ptr = ( unsigned int far * ) MK_FP ( _SS, _SP + 2 ) ;
printf ( "\n%d", *ptr ) ;

p = ( void ( * )( ) ) MK_FP ( _CS, *ptr ) ;
( *p )( ) ;
printf ( "\nI am f1( ) function " ) ;
}

void f2( )
{
printf ( "\nI am f2( ) function" ) ;
}

0 comments: