Devoir surveill Vendredi 22 fvrier 14 h15 h

  • Slides: 33
Download presentation
Devoir surveillé Vendredi 22 février 14 h-15 h 20 Amphi Turing Programme n Tous

Devoir surveillé Vendredi 22 février 14 h-15 h 20 Amphi Turing Programme n Tous les sujets traités en TD jusqu’au mercredi 20 inclus Sans document

Adaptation par J. Bétréma Machine-Level Programming : Stack and Procedures Topics n IA 32

Adaptation par J. Bétréma Machine-Level Programming : Stack and Procedures Topics n IA 32 stack discipline n Procedure calls Register saving conventions n Randal E. Bryant & David R. O'Hallaron Carnegie Mellon University http: //csapp. cs. cmu. edu class 07. ppt

Y 86 Program Stack n Region of memory holding program data n Used in

Y 86 Program Stack n Region of memory holding program data n Used in Y 86 (and IA 32) for supporting procedure calls Stack top indicated by %esp (Stack Pointer) Code n Increasing Addresses l Address of top stack element Stack “Top” n %esp Stack “Bottom” – 3– Stack grows toward lower addresses l Top element is at highest address in the stack l When pushing, must first decrement stack pointer l When popping, increment stack pointer Pile et procédures

Stack Operations pushl r. A n Decrement %esp by 4 Store word from r.

Stack Operations pushl r. A n Decrement %esp by 4 Store word from r. A to memory at %esp n Like IA 32 n popl r. A – 4– a 0 r. A 8 b 0 r. A 8 n Read word from memory at %esp n n Save in r. A Increment %esp by 4 n Like IA 32 Pile et procédures

IA 32 Stack Pushing n pushl Src n Fetch operand at Src Decrement %esp

IA 32 Stack Pushing n pushl Src n Fetch operand at Src Decrement %esp by 4 n n Stack Pointer %esp -4 %eax Write operand at address given by %esp Y 86 n n – 5– Src = registre Exemple : pushl %eax empile le contenu du registre %eax Pile et procédures

IA 32 Stack Popping n popl Dest n n Read operand at address given

IA 32 Stack Popping n popl Dest n n Read operand at address given by %esp Increment %esp by 4 n Write to Dest Stack Pointer + 4 %esp Y 86 n n – 6– Dest = registre Exemple : popl %edx dépile dans %edx le mot en sommet de pile Pile et procédures

Stack Operation Examples pushl %eax 0 x 108 123 popl %edx 0 x 104

Stack Operation Examples pushl %eax 0 x 108 123 popl %edx 0 x 104 213 0 x 108 123 0 x 10 c 0 x 110 %eax 213 %edx 555 213 %esp 0 x 108 %esp 0 x 104 0 x 108 – 7– Pile et procédures

Call Chain Example Code Structure f(…) { • • g(); • • } –

Call Chain Example Code Structure f(…) { • • g(); • • } – 8– Vocabulaire : fonction C = procédure = subroutine g(…) { • • • h(); • • • } Call Chain f g Profondeur de l’arbre des appels non bornée h h . . . Pile et procédures

Adresse de retour Procédure appelée Procédure appelante f(…) { • • g(); • •

Adresse de retour Procédure appelée Procédure appelante f(…) { • • g(); • • } jmp g jmp ? ? ? g(…) { • • • h(); • • • } Solution : la procédure appelante sauve l’adresse de retour avant l’appel. – 9– La procédure appelée ignore l’adresse de retour, car elle ignore qui l’a appelée. Où ? sur la pile ! Pile et procédures

Procedure Control Flow n Use stack to support procedure call and return Procedure call:

Procedure Control Flow n Use stack to support procedure call and return Procedure call: Push return address on stack; Jump call label to label Return address value n n Address of instruction beyond call Example from disassembly 804854 e: e 8 3 d 06 00 00 8048553: 8 b 45 0 c call mov 8048 b 90 0 xc(%ebp), %eax l Return address = 0 x 8048553 Procedure return: n – 10 – ret Pop address from stack; Jump to address Pile et procédures

Subroutine Call and Return call Dest n Push address of next instruction onto stack

Subroutine Call and Return call Dest n Push address of next instruction onto stack Start executing instructions at Dest n Like IA 32 n ret 9 0 n Pop value from stack Use as address for next instruction n Like IA 32 n – 11 – 8 0 Pile et procédures

Stack-Based Languages that Support Recursion n Code must be “Reentrant” l Multiple simultaneous instantiations

Stack-Based Languages that Support Recursion n Code must be “Reentrant” l Multiple simultaneous instantiations of single procedure n Need some place to store state of each instantiation l Arguments l Local variables l Return pointer Stack Discipline n State for given procedure needed for limited time l From when called to when return n Callee returns before caller does Stack Allocated in Frames n – 12 – state for single procedure instantiation Pile et procédures

Stack Frames Contents n Local variables n Return information Temporary space n f g

Stack Frames Contents n Local variables n Return information Temporary space n f g h Management n Space allocated when enter procedure l “Set-up” code n Deallocated when return proc l “Finish” code Pointers n n – 13 – Stack pointer %esp indicates stack top Frame pointer %ebp indicates start of current frame Stack “Top” Stack Pointer %esp %ebp Frame Pointer Pile et procédures

Stack Operation f(…) { • • g(); • • } Call Chain f Stack

Stack Operation f(…) { • • g(); • • } Call Chain f Stack Pointer %esp f %ebp Frame Pointer – 14 – • • • Pile et procédures

Stack Operation g(…) { • • • h(); • • • } Call Chain

Stack Operation g(…) { • • • h(); • • • } Call Chain f g Stack Pointer %esp g %ebp Frame Pointer f • • • – 15 – Pile et procédures

Stack Operation h(…) { • • } Call Chain f g h Stack Pointer

Stack Operation h(…) { • • } Call Chain f g h Stack Pointer %esp h %ebp Frame Pointer g f • • • – 16 – Pile et procédures

Stack Operation g(…) { • • • h(); • • • } Call Chain

Stack Operation g(…) { • • • h(); • • • } Call Chain f g h Stack Pointer %esp g %ebp Frame Pointer f • • • – 17 – Pile et procédures

Stack Operation h(…) { • • } Call Chain f h g h Stack

Stack Operation h(…) { • • } Call Chain f h g h Stack Pointer %esp h %ebp Frame Pointer g f • • • – 18 – Pile et procédures

Stack Operation g(…) { • • • h(); • • • } Call Chain

Stack Operation g(…) { • • • h(); • • • } Call Chain f Stack Pointer %esp g h h g %ebp Frame Pointer f • • • – 19 – Pile et procédures

Stack Operation f(…) { • • g(); • • } Call Chain f g

Stack Operation f(…) { • • g(); • • } Call Chain f g h h Stack Pointer %esp f %ebp Frame Pointer – 20 – • • • Pile et procédures

IA 32/Linux Stack Frame Current Stack Frame (“Top” to Bottom) n Stack Pointer (%esp)

IA 32/Linux Stack Frame Current Stack Frame (“Top” to Bottom) n Stack Pointer (%esp) Parameters for function about to call l “Argument build” n Local variables l If can’t keep in registers n n Saved register context Old frame pointer Caller Stack Frame n Return address l Pushed by call instruction n – 21 – Frame Pointer (%ebp) Argument Build Saved Registers + Local Variables Old %ebp Return Addr Arguments Caller Frame Arguments for this call Pile et procédures

Example: swap Calling swap from call_swap int zip 1 = 15213; int zip 2

Example: swap Calling swap from call_swap int zip 1 = 15213; int zip 2 = 91125; void call_swap() {. . . swap(&zip 1, &zip 2); . . . } call_swap: • • • pushl $zip 2 pushl $zip 1 call swap • • • %esp # Global Var Rtn adr &zip 1 void swap(int *xp, int *yp) { int t 0 = *xp; int t 1 = *yp; *xp = t 1; *yp = t 0; } – 22 – Resulting Stack &zip 2 • • • Pile et procédures

swap void swap(int *xp, int *yp) { int t 0 = *xp; int t

swap void swap(int *xp, int *yp) { int t 0 = *xp; int t 1 = *yp; *xp = t 1; *yp = t 0; } swap: pushl %ebp movl %esp, %ebp pushl %ebx movl movl Set Up 12(%ebp), %ecx 8(%ebp), %edx (%ecx), %eax (%edx), %ebx %eax, (%edx) %ebx, (%ecx) movl -4(%ebp), %ebx movl %ebp, %esp popl %ebp ret – 23 – Body Finish Pile et procédures

swap Setup %ebp %esp Old %ebx %esp %ebp Old %ebp %esp Rtn adr &zip

swap Setup %ebp %esp Old %ebx %esp %ebp Old %ebp %esp Rtn adr &zip 1 swap: pushl %ebp movl %esp, %ebp pushl %ebx &zip 2 • • • %ebp – 24 – Pile et procédures

Effect of swap Setup movl 12(%ebp), %ecx # get yp movl 8(%ebp), %edx #

Effect of swap Setup movl 12(%ebp), %ecx # get yp movl 8(%ebp), %edx # get xp. . . Entering Stack %esp Rtn adr &zip 1 &zip 2 • • • Resulting Stack Body Old %ebx %esp Offset (relative to %ebp) 0 Old %ebp 4 Rtn adr 8 xp 12 yp %ebp • • • %ebp – 25 – Pile et procédures

swap Finish %esp Old %ebx %ebp Old %ebp %esp Rtn adr %esp xp Restores

swap Finish %esp Old %ebx %ebp Old %ebp %esp Rtn adr %esp xp Restores %ebx movl -4(%ebp), %ebx movl %ebp, %esp popl %ebp ret yp • • • %ebp – 26 – Observation n Saved & restored register %ebx n Didn’t do so for %eax, %ecx, or %edx Pile et procédures

Register Saving Conventions When procedure f calls g: n f is the caller, g

Register Saving Conventions When procedure f calls g: n f is the caller, g is the callee Can Register be Used for Temporary Storage? f: g: • • • movl $15213, %edx call g addl %edx, %eax • • • ret n – 27 – • • • movl 8(%ebp), %edx addl $91125, %edx • • • ret Contents of register %edx overwritten by g Pile et procédures

Register Saving Conventions When procedure f calls g: n f is the caller, g

Register Saving Conventions When procedure f calls g: n f is the caller, g is the callee Can Register be Used for Temporary Storage? Conventions n “Caller Save” l Caller saves temporary in its frame before calling n “Callee Save” l Callee saves temporary in its frame before using – 28 – Pile et procédures

IA 32/Linux Register Usage Integer Registers n Two have special uses %ebp, %esp n

IA 32/Linux Register Usage Integer Registers n Two have special uses %ebp, %esp n Three managed as callee-save %eax Caller-Save Temporaries %edx %ebx, %esi, %edi l Old values saved on stack prior to using n Callee-Save Temporaries l Do what you please, %esi %edi Three managed as caller-save %eax, %ecx, %edx %ecx Special %ebp %esp but expect any callee to do so, as well n – 29 – Register %eax also stores returned value Pile et procédures

Procédures récursives Le protocole précédent ne limite pas la profondeur de l’arbre des appels.

Procédures récursives Le protocole précédent ne limite pas la profondeur de l’arbre des appels. n Pièces bien placées sur l’échiquier = position solide. n Prêt à parer une attaque inattendue : l Procédure récursive = procédure qui s’appelle-même l Procédure appelante (caller) = procédure appelée (callee) – 30 – Pile et procédures

Informatique théorique Un automate à états finis ne peut pas gérer correctement les appels

Informatique théorique Un automate à états finis ne peut pas gérer correctement les appels de procédures : n Le langage des mots de la forme an bn n’est pas rationnel n Idem pour le langage des parenthèses mots de la forme ()(()())=abaababb a = appel (call) b = retour (return) n Pour parcourir un arbre il faut un automate à pile ! – 31 – Pile et procédures

rfact: pushl %ebp movl %esp, %ebp int rfact (int x) pushl %ebx lit l’argument

rfact: pushl %ebp movl %esp, %ebp int rfact (int x) pushl %ebx lit l’argument { movl 8(%ebp), %ebx int rval; cmpl $1, %ebx if (x <= 1) jle. L 78 return 1; calcule x-1 leal -1(%ebx), %eax rval = rfact (x-1); pushl %eax return rval * x; call rfact } imull %ebx, %eax jmp. L 79 empile l’argument. align 4 avant l’appel récursif. L 78: Registers movl $1, %eax n %eax used without first. L 79: movl -4(%ebp), %ebx saving movl %ebp, %esp n %ebx used, but save at popl %ebp beginning & restore at end ret Recursive Factorial – 32 – Pile et procédures

Summary The Stack Makes Procedures Work n Private storage for each instance of procedure

Summary The Stack Makes Procedures Work n Private storage for each instance of procedure call l Instantiations don’t clobber each other l Addressing of locals + arguments can be relative to stack positions n Can be managed by stack discipline l Procedures return in inverse order of calls IA 32 Procedures Combination of Instructions + Conventions n n Call / Ret instructions Register usage conventions l Caller / Callee save l %ebp and %esp n – 33 – Stack frame organization conventions Pile et procédures