B e f o r e e x e c u t i n g a f u n c t i o n , a l l o f t h e p a r a m e t e r s f o r t h e f u n c t i o n i s p u s h e d o n t o t h e s t a c k i n t h e r e v e r s e o r d e r
t h a t t h e y a r e d o c u m e n t e d . T h e " c a l l " i n s t r u c t i o n d o e s t w o t h i n g s . F i r s t , i t p u s h e e d t h e a d d r e s s o f t h e n e x t i n s t r u c t i o n ,
w h i c h i s t h e r e t u r n a d d r e s s , o n t o t h e s t a c k . S e c o n d , t h e ' % e i p ' i s m o d i f i e d b y i t t o p o i n t t o t h e s t a r t o f t h e f u n c t i o n .
S o , a t t h e t i m e t h e f u n c t i o n s t a r t s , t h e s t a c k l o o k s l i k e t h i s :
P a r a m e t e r # N
. . .
P a r a m e t e r 2
P a r a m e t e r 1
R e t u r n A d d r e s s < - - - ( % e s p )
N o w t h e f u n c t i o n c a n w o r k . T h e f i r s t t h i n g i s d o e s i s s a v e t h e c u r r e n t b a s e p o i n t r e g i s t e r , ' % e b p ' , b y d o i n g " p u s h l % e b p " .
T h e b a s e p o i n t c a n b e u s e d t o a c c e s s f u n c t i o n p a r a m e t e r s a n d l o c a l v a r i a b l e s . N e x t i t d o i n g " m o v l % e s p , % e b p " i n o r d e r t o
c o p y t h e s t a c k p o i n t e r t o " % e b p " . T h i s a l l o w s y o u t o b e a b l e t o a c c e s s t h e f u n c t i o n p a r a m e t e r s a s f i x e d i n d e x e s f r o m t h e b a s e
p o i n t e r . " % e b p " w i l l a l w a y s b e w h e r e t h e s t a c k p o i n t e r w a s a t t h e b e g i n n i n g o f t h e f u n c t i o n , s o i t i s m o r e o r l e s s a c o n s t a n t
r e f e r e n c e t o t h e s t a c k f r a m e ( t h e s t a c k f r a m c o n s i s t s f o a l l o f t h e s t a c k v a r i a b l e s u s e d w i t h i n a f u n c t i o n , i n c l u d i n g p a r a m e t e r s ,
l o c a l v a r i a b l e s , a n d t h e r e t u r n a d d r e s s ) .
A t t h i s p o i n t , t h e s t a c k l o o k s l i k e t h i s :
P a r a m e t e r # N < - - - N * 4 + 4 ( % e b p )
. . .
P a r a m e t e r 2 < - - - 1 2 ( % e b p )
P a r a m e t e r 1 < - - - 8 ( % e b p )
R e t u r n A d d r e s s < - - - 4 ( % e b p )
O l d % e b p < - - - ( % e s p ) a n d ( % e b p )
N e x t , t h e f u n c t i o n r e s e r v e r s s p a c e o n t h e s t a c k f o r a n y l o c a l v a r i a b l e s i t n e e d s . T h i s i s d o n e b y s i m p l y m o v i n g t h e s t a c k p o i n t
o u t o f t h e w a y .
P a r a m e t e r # N < - - - N * 4 + 4 ( % e b p )
. . .
P a r a m e t e r 2 < - - - 1 2 ( % e b p )
P a r a m e t e r 1 < - - - 8 ( % e b p )
R e t u r n A d d r e s s < - - - 4 ( % e b p )
O l d % e b p < - - - ( % e s p ) a n d ( % e b p )
L o c a l V a r i a b l e 1 < - - - - 4 ( % e b p )
L o c a l V a r i a b l e 2 < - - - - 8 ( % e b p )
S o w e c a n a c c e s s a l l o f t h e d a t a w e n e e d f o r t h i s f u n c t i o n b y u s i n g b a s e p o i n t e r a d d r e s s i n g u s i n g d i f f e r e n t o f f s e t s f r o m " % e b p " .
% e b p w a s m a d e s p e c i f i c a l l y f o r t h i s p u r p o s e , w h i c h i s w h y i t i s c a l l e d t h e b a s e p o i n t e r . Y o u c a n u s e o t h e r r e g i s t e r s i n b a s e
p o i n t e r a d d r e s s i n g m o d e , b u t t h e x 8 6 a r c h i t e c t u r e m a k e s u s i n g t h e % e b p r e g i s t e r a l o t f a s t e r .
W h e n a f u n c t i o n i s d o n e e x e c u t i n g , i t d o e s t h r e e t h i n g s :
1 . I t s t o r e s i t ' s r e t u r n v a l u e i n " % e a x " .
2 . I t r e s e t s t h e s t a c k t o w h a t i t w a s w h e n i t w a s c a l l e d ( i t g e t s r i d o f t h e c u r r e n t s t a c k f r a m e a n d p u t s t h e s t a c k f r a m e o f
t h e c a l l i n g c o d e b a c k i n t o e f f e c t ) .
3 . I t r e t u r n s c o n t r o l b a c k t o w h e r e v e r i t w a s c a l l e d f r o m . T h i s i s d o n e u s i n g t h e ' r e t ' i n s t r u c t i o n , w h i c h p o p s w h a t e v e r v a l u e
i s a t t h e t o p o f t h e s t a c k , a n d s e t s t h e i n s t r u c t i o n p o i n t e r , " % e i p " , t o t h a t v a l u e .
S o , b e f o r e a f u n c t i o n r e t u r n s c o n t r o l t o t h e c o d e t h a t c a l l e d i t , i t m u s t r e s t o r e t h e p r e v i o u s s t a c k f r a m e . N o t e a l s o t h a t
w i t h o u t d o i n g t h i s , ' r e t ' w o u l d n ' t w o r k , b e c a u s e i n o u r c u r r e n t s t a c k f r a m e , t h e r e t u r n a d d r e s s i s n o t a t t h e t o p o f t h e s t a c k .
T h e r e f o r e , b e f o r e w e r e t u r n , w e h a v e t o r e s e t t h e s t a c k p o i n t e r " % e s p " a n d b a s e p o i n t e r " % e b p " t o w h a t t h e y w e r e w h e n t h e f u n c t i o n
b e g a n .
m o v l % e b p , % e s p
p o p l % e b p
r e t
A t t h i s p o i n t , y o u s h o u l d c o n s i d e r a l l l o c a l v a r i a b l e s t o b e d i s p o s e d o f .