Assembly - Cornell University

Assembly - Cornell University

Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 2.8 and 2.12 Goals for Today Review: Calling Conventions call a routine (i.e. transfer control to procedure) pass arguments return to the caller fixed length, variable length, recursively Putting results in a place where caller can find them Manage register Today

More on Calling Conventions globals vs local accessible data callee vs callrer saved registers Calling Convention examples and debugging 2 Goals for Today Review: Calling Conventions call a routine (i.e. transfer control to procedure) pass arguments return to the caller fixed length, variable length, recursively Putting results in a place where caller can find them Manage register Today

More on Calling Conventions globals vs local accessible data callee vs callrer saved registers Calling Convention examples and debugging Warning: There is no one true MIPS calling convention. lecture != book != gcc != spim != web 3 Recap: Conventions so far first four arg words passed in $a0, $a1, $a2, $a3 remaining arg words passed in parents stack frame return value (if any) in $v0, $v1 stack frame at $sp contains $ra (clobbered on JAL to sub-functions) $fp contains $fp contains local vars (possibly clobbered by sub-functions) contains extra arguments to sub-functions (i.e. argument spilling) contains space for first 4 arguments to sub-functions callee save regs are preserved

caller save regs are not $sp saved ra saved fp saved regs ($s0 ... $s7) locals outgoing args 4 r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 r14 r15 MIPS Register Conventions

$zero zero $at assembler temp $v0 function return values $v1 $a0 $a1 function arguments $a2 $a3 $t0 $t1 $t2 $t3 temps (caller save) $t4 $t5 $t6 $t7 r16 r17 r18 r19 r20 r21 r22 $s0

$s1 $s2 $s3 $s4 $s5 $s6 saved (callee save) r23 $s7 r24 r25 r26 r27 r28 r29 r30 r31 $t8 $t9 $k0 $k1 $gp $sp $fp $ra more temps (caller save) reserved for kernel

global data pointer stack pointer frame pointer return address 5 Globals and Locals Global variables in data segment Exist for all time, accessible to all routines Dynamic variables in heap segment Exist between malloc() and free() Local variables in stack frame Exist solely for the duration of the stack frame Dangling pointers into freed heap mem are bad Dangling pointers into old stack frames are bad C lets you create these, Java does not int *foo() { int a; return &a; } 6 Caller-saved vs. Callee-saved Caller-save: If necessary ($t0 .. $t9) save before calling anything; restore after it returns Callee-save: Always ($s0 .. $s7) save before modifying; restore before returning Caller-save registers are responsibility of the caller Caller-save register values saved only if used after call/return The callee function can use caller-saved registers

Callee-save register are the responsibility of the callee Values must be saved by callee before they can be used Caller can assume that these registers will be restored 7 Caller-saved vs. Callee-saved Caller-save: If necessary ($t0 .. $t9) save before calling anything; restore after it returns Callee-save: Always ($s0 .. $s7) save before modifying; restore before returning MIPS ($t0-$t0), x86 (eax, ecx, and edx) are caller-save a function can freely modify these registers but must assume that their contents have been destroyed if it in turns calls a function. MIPS $s0 - $s7), x86 (ebx, esi, edi, ebp, esp) are callee-save A function may call another function and know that the callee-save registers have not been modified However, if it modifies these registers itself, it must restore them to their original values before returning. 8 Caller-saved vs. Callee-saved Caller-save: If necessary ($t0 .. $t9) save before calling anything; restore after it returns Callee-save: Always ($s0 .. $s7) save before modifying; restore before returning

A caller-save register must be saved and restored around any call to a subroutine. In contrast, for a callee-save register, a caller need do no extra work at a call site (the callee saves and restores the register if it is used). 9 Caller-saved vs. Callee-saved Caller-save: If necessary ($t0 .. $t9) save before calling anything; restore after it returns Callee-save: Always ($s0 .. $s7) save before modifying; restore before returning CALLER SAVED: MIPS calls these temporary registers, $t0-t9 the calling routine saves the registers that it does not want a called procedure to overwrite register values are NOT preserved across procedure calls CALLEE SAVED: MIPS calls these saved registers, $s0-s8 register values are preserved across procedure calls the called procedure saves register values in its AR, uses the registers for local variables, restores register values before it returns. 10 Caller-saved vs. Callee-saved Caller-save: If necessary ($t0 .. $t9) save before calling anything; restore after it returns Callee-save: Always ($s0 .. $s7) save before modifying; restore before returning

Registers $t0-$t9 are caller-saved registers that are used to hold temporary quantities that need not be preserved across calls Registers $s0-s8 are callee-saved registers that hold long-lived values that should be preserved across calls 11 Calling Convention Example int test(int a, int b) { int tmp = (a&b)+(a|b); int s = sum(tmp,1,2,3,4,5); int u = sum(s,tmp,b,a,b,a); return u + a + b; } 12 Calling Convention Example int test(int a, int b) { int tmp = (a&b)+(a|b); int s = sum(tmp,1,2,3,4,5); int u = sum(s,tmp,b,a,b,a); return u + a + b; } How many bytes do we need to allocate for the stack frame? a) 24

b) 32 c) 40 d) 44 e) 48 test: Prolog MOVE $s0, $a0 MOVE $s1, $a1 AND $t0, $a0, $a1 OR $t1, $a0, $a1 ADD $t0, $t0, $t1 MOVE $a0, $t0 LI $a1, 1 LI $a2, 2 LI $a3, 3 LI $t1, 4 SW $t1 16($sp) LI $t1, 5 SW $t1, 20($sp) SW $t0, 24($sp) JAL sum NOP LW $t0, 24($sp) MOVE $a0, $v0 # s MOVE $a1, $t0 # tmp MOVE $a2, $s1 # b MOVE $a3, $s0 # a SW $s1, 16($sp) SW $s0, 20($sp) JAL sum

NOP # add u (v0) and a (s0) ADD $v0, $v0, $s0 ADD $v0, $v0, $s1 # $v0 = u + a + b Epilog 13 Calling Convention Example int test(int a, int b) { int tmp = (a&b)+(a|b); int s = sum(tmp,1,2,3,4,5); int u = sum(s,tmp,b,a,b,a); return u + a + b; } $fp saved ra saved fp saved regs ($s0 and $s1) locals ($t0) outgoing args space for a0 - a3 $sp and 4th and 6th arg test:

Prolog MOVE $s0, $a0 MOVE $s1, $a1 AND $t0, $a0, $a1 OR $t1, $a0, $a1 ADD $t0, $t0, $t1 MOVE $a0, $t0 LI $a1, 1 LI $a2, 2 LI $a3, 3 LI $t1, 4 SW $t1 16($sp) LI $t1, 5 SW $t1, 20($sp) SW $t0, 24($sp) JAL sum NOP LW $t0, 24($sp) MOVE $a0, $v0 # s MOVE $a1, $t0 # tmp MOVE $a2, $s1 # b MOVE $a3, $s0 # a SW $s1, 16($sp) SW $s0, 20($sp) JAL sum NOP # add u (v0) and a (s0) ADD $v0, $v0, $s0 ADD $v0, $v0, $s1 # $v0 = u + a + b Epilog

14 Calling Convention Example int test(int a, int b) { int tmp = (a&b)+(a|b); int s = sum(tmp,1,2,3,4,5); int u = sum(s,tmp,b,a,b,a); return u + a + b; } $fp 40 36 32 28 24 20 16 12 8 4 0 $sp saved ra saved fp saved reg $s1 saved reg $s0 local $t0 outgoing 6th arg outgoing 5th arg

space for $a3 space for $a2 space for $a1 space for $a0 test: Prolog MOVE $s0, $a0 MOVE $s1, $a1 AND $t0, $a0, $a1 OR $t1, $a0, $a1 ADD $t0, $t0, $t1 MOVE $a0, $t0 LI $a1, 1 LI $a2, 2 LI $a3, 3 LI $t1, 4 SW $t1 16($sp) LI $t1, 5 SW $t1, 20($sp) SW $t0, 24($sp) JAL sum NOP LW $t0, 24($sp) MOVE $a0, $v0 # s MOVE $a1, $t0 # tmp MOVE $a2, $s1 # b MOVE $a3, $s0 # a SW $s1, 16($sp) SW $s0, 20($sp) JAL sum

NOP # add u (v0) and a (s0) ADD $v0, $v0, $s0 ADD $v0, $v0, $s1 # $v0 = u + a + b Epilog 15 Calling Convention Example: Prolog, Epilog test: # allocate frame # save $ra # save old $fp # callee save ... # callee save ... # set new frame pointer ... ... # restore # restore # restore old $fp # restore $ra # dealloc frame 16 Calling Convention Example: Prolog, Epilog test: ADDIU $sp, $sp, -44

SW $ra, 40($sp) SW $fp, 36($sp) SW $s1, 32($sp) SW $s0, 28($sp) ADDIU $fp, $sp, 40 Body LW $s0, 28($sp) LW $s1, 32($sp) LW $fp, 36($sp) LW $ra, 40($sp) ADDIU $sp, $sp, 44 JR $ra # allocate frame # save $ra # save old $fp # callee save ... # callee save ... # set new frame pointer ... ... # restore # restore # restore old $fp # restore $ra # dealloc frame 17 Calling Convention Example int test(int a, int b) { int tmp = (a&b)+(a|b);

int s = sum(tmp,1,2,3,4,5); int u = sum(s,tmp,b,a,b,a); return u + a + b; } How can we optimize the code? test: Prolog MOVE $s0, $a0 MOVE $s1, $a1 AND $t0, $a0, $a1 OR $t1, $a0, $a1 ADD $t0, $t0, $t1 MOVE $a0, $t0 LI $a1, 1 LI $a2, 2 LI $a3, 3 LI $t1, 4 SW $t1 16($sp) LI $t1, 5 SW $t1, 20($sp) SW $t0, 24($sp) JAL sum NOP LW $t0, 24($sp) MOVE $a0, $v0 # s MOVE $a1, $t0 # tmp MOVE $a2, $s1 # b MOVE $a3, $s0 # a

SW $s1, 16($sp) SW $s0, 20($sp) JAL sum NOP # add u (v0) and a (s0) ADD $v0, $v0, $s0 ADD $v0, $v0, $s1 # $v0 = u + a + b Epilog 18 Calling Convention Example: Prolog, Epilog test: ADDIU $sp, $sp, -44 SW $ra, 40($sp) SW $fp, 36($sp) SW $s1, 32($sp) SW $s0, 28($sp) ADDIU $fp, $sp, 40 Body LW $s0, 28($sp) LW $s1, 32($sp) LW $fp, 36($sp) LW $ra, 40($sp) ADDIU $sp, $sp, 44 JR $ra # allocate frame # save $ra

# save old $fp # callee save ... # callee save ... # set new frame pointer ... ... # restore # restore # restore old $fp # restore $ra # dealloc frame 19 Minimum stack size for a standard function? $fp saved ra saved fp saved regs ($s0 ... $s7) locals $sp outgoing args 20 Leaf Functions Leaf function does not invoke any other functions int f(int x, int y) { return (x+y); }

Optimizations? $fp No saved regs (or locals) No outgoing args Dont push $ra No frame at all? $sp saved ra saved fp saved regs ($s0 ... $s7) locals outgoing args 21 Anatomy of an executing program 0xfffffffc top system reserved 0x80000000 0x7ffffffc stack dynamic data (heap) 0x10000000 static data

0x00400000 0x00000000 code (text) system reserved bottom 22 Debugging init(): 0x400000 printf(s, ): 0x4002B4 vnorm(a,b): 0x40107C main(a,b): 0x4010A0 pi: 0x10000000 str1:is running? 0x10000004 What func Who called it? Has it called anything? Will it? Args? Stack depth? Call trace? CPU: $pc=0x004003C0 $sp=0x7FFFFFAC

$ra=0x00401090 0x00000000 0x0040010c 0x7FFFFFF4 0x00000000 0x00000000 0x00000000 0x00000000 0x004010c4 0x7FFFFFDC 0x00000000 0x00000000 0x00000015 0x7FFFFFB0 0x10000004 0x0040109023 Administrivia Upcoming agenda Schedule PA2 Design Doc Mtg for this Sunday or Monday HW3 due next Tuesday, March 13th PA2 Work-in-Progress circuit due before spring break Spring break: Saturday, March 17th to Sunday, March 25th HW4 due after spring break, before Prelim2

Prelim2 Thursday, March 29th, right after spring break PA2 due Monday, April 2nd, after Prelim2 24 Recap How to write and Debug a MIPS program using calling convention first four arg words passed in $a0, $a1, $a2, $a3 remaining arg words passed in parents stack frame return value (if any) in $v0, $v1 stack frame at $sp saved ra $fp contains $ra (clobbered on JAL to sub-functions) saved fp contains $fp saved regs contains local vars (possibly ($s0 ... $s7) clobbered by sub-functions) contains extra arguments to sub-functions (i.e. argument spilling) contains space for first 4 arguments to sub-functions callee save regs are preserved caller save regs are not

Global data accessed via $gp locals $sp outgoing args 25

Recently Viewed Presentations

  • Color Theory - PC\|MAC

    Color Theory - PC\|MAC

    Color Theory Ms. Prinkey What is color? Color is what the eye sees when light is reflected off an object. Hue Hue is a color's name. Specifically: Hue is the name of a distinct color of the spectrum—red, green, yellow,...
  • ME/AE 339 Computational Fluid Dynamics K. M. Isaac

    ME/AE 339 Computational Fluid Dynamics K. M. Isaac

    ME/AE 339 Computational Fluid Dynamics K. M. Isaac Topic3_PDE Topic 3 Discretization of PDE Dicretization of Partial Differential Equations (CLW: 7.2, 7.3) We will follow a procedure similar to the one used in the previous class We consider the unsteady...
  • Objectives & Agenda - psiweb.org

    Objectives & Agenda - psiweb.org

    Objectives & Agenda. Objective: Use Case Study to propose suggestions for how statisticians can be better equipped to contribute to the cross-functional teams developing new medicines
  • United Postal Stationery Society Introduction to U.S. Postal

    United Postal Stationery Society Introduction to U.S. Postal

    Die types are defined in the United Postal Stationery envelope catalogs, along with a chart for easy identification. Surface Printed Dies. Lithography. Lithography uses chemical processes to create an image. For instance, the positive part of an image is a...
  • Wascana Monitoring Avian Productivity and Survivorship ...

    Wascana Monitoring Avian Productivity and Survivorship ...

    Black head and gray back. Young robins have black dots on their chest. Gray Catbird. All gray body. Dark gray cap. Burgundy feathers under its tail. Often hear catbirds giving their 'meow' call - hence the name catbird. Red-winged Blackbird.
  • Higher Close Reading - Imagery - methodinyourmadness

    Higher Close Reading - Imagery - methodinyourmadness

    Higher Close Reading - Imagery Ms Nitsche Starter Look at the Charlie Brooker article in your Newspaper booklets. 1. Read lines 9-10: "…a lightshow for cattle" and comment on what this conveys about what the writer believes the cinema is...
  • Diapositive 1 - frop.fr

    Diapositive 1 - frop.fr

    I/ Les sens de la vie! II/Quels nutriments pour se construire III/ Le microbiote IV/ Les premiers pas vers l'autonomie immunitaire V/ Structure - fonction- environnement
  • Diapositive 1

    Diapositive 1

    History of the observational study. Several international workshops in francophone Africa on programme management of MDR-TB organised by The Union: