B1101 Call of Duty ENGR xD52 Eric VanWyk Fall 2013.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

The University of Adelaide, School of Computer Science
Recursion in MIPS Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Leaf and Non-Leaf Procedures A leaf procedure is one that.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Procedure Calls Prof. Sirer CS 316 Cornell University.
Computer Architecture CSCE 350
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
B1010 Advanced Math Stuff ENGR xD52 Eric VanWyk Fall 2012.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
CS 536 Spring Code generation I Lecture 20.
Accessing parameters from the stack and calling functions.
20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15.
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
Intro to Computer Architecture
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
Microprocessors Frame Pointers and the use of the –fomit-frame-pointer switch Feb 25th, 2002.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
B1100 Call Me Maybe ENGR xD52 Eric VanWyk Fall 2012.
Linked Lists in MIPS Let’s see how singly linked lists are implemented in MIPS on MP2, we have a special type of doubly linked list Each node consists.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Procedures 2a MIPS code examples Making Use of a Stack.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University See P&H 2.8 and 2.12.
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 2.8 and 2.12.
Subroutines, parameters and the stack Bryan Duggan.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 22: Unconventional.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Compiler Construction Code Generation Activation Records
1 Assembly Language: Function Calls Jennifer Rexford.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Programs – Calling Conventions
Computer Architecture & Operations I
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
© Craig Zilles (adapted from slides by Howard Huang)
CSCI206 - Computer Organization & Programming
143A: Principles of Operating Systems Lecture 4: Calling conventions
Procedures (Functions)
ENGR xD52 Eric VanWyk Fall 2014
CSCI206 - Computer Organization & Programming
Discussion Section – 11/3/2012
Prof. Hakim Weatherspoon
CSCI206 - Computer Organization & Programming
MIPS Procedure Calls CSE 378 – Section 3.
The University of Adelaide, School of Computer Science
Topic 3-a Calling Convention 1/10/2019.
10/4: Lecture Topics Overflow and underflow Logical operations
Multi-modules programming
Procedures and Calling Conventions
Systems Architecture I
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
CSC 497/583 Advanced Topics in Computer Security
© Craig Zilles (adapted from slides by Howard Huang)
Presentation transcript:

b1101 Call of Duty ENGR xD52 Eric VanWyk Fall 2013

Acknowledgements Joseph Hershberger (National Instruments) Patterson & Hennessy: Book & Lecture Notes Nemanja Trifunovic Giovani Gracioli Microsoft MSDN

Today Calling Conventions

My Testing Strategy List the types of problems I face List the assumptions I make List the cases to catch these problems Compress list of cases using assumptions

The Life of a Called Function The caller stores parameters “somewhere” Control (Execution) is passed to the callee The callee does some stuff The callee stores the results “somewhere” Control is passed back to the caller

Calling Conventions A Calling Convention standardizes where those “somewheres” are. – Registers? Data Memory? – Return values? – How do we return control back to the caller? It is just another type of contract – The great thing about standards is that there are so many to choose from.

Calling Convention We have decisions to make for registers: – Are they used as part of the call? – Are they preserved across the call? – Are they reserved for other uses? … and about passing arguments around – In registers? – On the stack? – In generic data memory?

MIPs Specifics Uses $vN to store results – V for Valuetta Uses $aN to store first 4 arguments – A is for Argument, that’s good enough for me – Extra are pushed to the stack

MIPs Specifics $sN are Saved Temporaries – The callee is responsible for saving these if used – The caller can assume they are unchanged $tN are Volatile Temporaries – The callee can do whatever it wants with these – The caller can’t rely on these across a call Advantages/Disadvantages to these?

Frame Pointer $sp can move around during a procedure – This makes frame contents shift around Relative to the stack pointer – Makes debugging hard A Frame Pointer stays put during a procedure – Makes debugging easier! – Makes compiling easier too, but I don’t care

Frame Pointer Isn’t strictly necessary – But it makes debugging so much easier Not all implementations use it – GNU MIPS C Compiler does – MIPS MIPS C compiler does not

A MIPS calling convention’s frame From WinCE 5.0 Arguments at top of previous frame $ra Saved nonvolatiles Locals

Factorial Function int Fact(int n){ if(n>1) return n* Fact(n-1) else return 1

Factorial Function int Fact(int n){ if(n<=1) goto end: return n* Fact(n-1) end: return 1

Factorial Function $v0 Fact(int n){ if(n<=1) goto end: $v0 =n* Fact(n-1) jr $ra end: $v0 = 1 jr $ra

Factorial Function $v0 Fact ($a0) ble $a0, 1, end: Fact(n-1) $v0 =n* Fact(n-1) jr $ra end: $v0 = 1 jr $ra We have most of what we need: – Goto flow control for if – jr $ra for return – Registers assigned Now we need to call Fact – What do we save? – What order? Lets focus on the call site

Factorial Function Call Site To Call Fact: – Push registers I need to save $ra $a0 – Setup Arguments N-1: $a0 = $a0-1 – Jump and Link Fact: – Restore registers

Factorial Function Call Site sub $sp, $sp, 8 sw $ra, 4($sp) sw $a0, 0($sp) sub $a0, $a0, 1 jal fact lw $ra, 4($sp) lw $a0, 0($sp) add $sp, $sp, 8 To Call Fact: – Push $ra, $a0 – Setup $a0 – Jump and Link Fact: – Restore $ra, $a0

Factorial Function Call Site sub $sp, $sp, 8 sw $ra, 4($sp) sw $a0, 0($sp) sub $a0, $a0, 1 jal fact lw $ra, 4($sp) lw $a0, 0($sp) add $sp, $sp, 8 To Call Fact: – Push $ra, $a0 – Setup $a0 – Jump and Link Fact: – Restore $ra, $a0

Factorial Function Call Site sub $sp, $sp, 8 sw $ra, 4($sp) sw $a0, 0($sp) sub $a0, $a0, 1 jal fact lw $ra, 4($sp) lw $a0, 0($sp) add $sp, $sp, 8 To Call Fact: – Push $ra, $a0 – Setup $a0 – Jump and Link Fact: – Restore $ra, $a0

Factorial Function Call Site sub $sp, $sp, 8 sw $ra, 4($sp) sw $a0, 0($sp) sub $a0, $a0, 1 jal fact lw $ra, 4($sp) lw $a0, 0($sp) add $sp, $sp, 8 To Call Fact: – Push $ra, $a0 – Setup $a0 – Jump and Link Fact: – Restore $ra, $a0

Factorial Function fact: ;if(N<=1) return 1 ble $a0, 1, end: ;Push $ra, $a0 sub $sp, $sp, 8 sw $ra, 4($sp) sw $a0, 0($sp) ;Argument N-1 sub $a0, $a0, 1 jal fact ;Pop $ra, $a0 lw $ra, 4($sp) lw $a0, 0($sp) add $sp, $sp, 8 ;Return N*Fact(N-1) mul $v0, $v0, $a0 jr $ra end: ;Return 1 $v0 = 1 jr $ra

Calling Function li $a0, 4 jal factorial move $s0, $v0 li $a0, 2 jal factorial move $s1, $v0 li $a0, 7 jal factorial move $s2, $v0 li $v0, 10 syscall Calls Factorial several times Stores results in $sN li is a pseudoinstruction – What does it assemble to?? The final two lines call a special simulator function to end execution – 10 means exit – Look up other syscalls in help

Key Gotchas jal calls a subroutine jr $ra returns from it Sandwich jal with push and pop pair – Caller responsible for stack (CDECL) There are other options, but be consistent!

Practice You have 40 minutes. Do any of the following: Get recursive factorial working and step trace it Pay attention to $sp! Pretend mul&mult don’t exist – Write a leaf that does their job with add&shift in a loop. – Drawing plan on whiteboard might help Write IQ Multiply: IQmult(a, b, Q) – Multiply two IQN numbers – IQ24 means I8Q24 – Hint: MULT $t0, $t1 stores the results in $HI$LO – Retrieve using mfhi and mflo

Vocab Blowing the Stack / Stack Overflow / Stack Smashing – Running out of stack space – Writes over other data! (Heap) – Possible security vulnerability Unwind the Stack – Popping “frames” off the stack – Usually in the context of exception handling Walking the Stack – Looking at the Stack and figuring out where you are – Usually in the context of Debugging – Need to be able to “see” where the frames are

Vocab Misaligning or Unbalancing the stack – Pushing or popping a different number of times – Catastrophic Error! Stack Dump – Produced when program crashes – Helps you understand where you were when stuff went wrong

Stack Dump Example Error Message: Value cannot be null. Parameter name: value Exception: ArgumentNullException Suggestion: Contact National Instruments to report this error Stack Trace: at System.BitConverter.ToInt32(Byte[] value, Int32 startIndex) at NationalInstruments.LabVIEW.VI.VirtualMachine.Target.ExecutionHighlighting.ProcessUpdate(Byte[] buffer) at NationalInstruments.X3.Model.DeviceModel.OnExHighlightReadCompletedEventHandler(Object sender, ReadCompletedEventArgs e) at NationalInstruments.X3.Model.DeviceModel.<>c__DisplayClass37. b__36(Object theSender, RequestCompletedEventArgs theE)

Hack the Stack How can you use this to execute arbitrary code?

Hack the Stack How can you use this to execute arbitrary code? void bad(){ char * badcode = { bad instructions }; *(&a-1)= a; }

x86 Calling Conventions Full of “gotchas” – These are usually why calling other binaries fail __cdecl (See-Deckle) – Supports all C functionality __stdcall (Standard Call) (ha) – Win32 API uses this __fastcall – Uses registers more aggressively to go faster Thiscall – Used in C++ by member functions All described in the following slides (which I will skip) The following slides based from Nemanja Trifunovic’s article “Calling Conventions Demystified”

See Deckle Expand arguments to 32 bits All arguments are on stack, right to left Decorated with an underscore Caller does stack cleanup – This allows for a variable number of arguments … printf(const char * format, …); – Generates bigger executables Each call site performs cleanup Nemanja Trifunovic

See Deckle ; Calling Add(7,19) push 19 push 7 call _add add esp, 8; clean up stack ;result is in eax ; body ; // function prolog push ebp mov ebp,esp sub esp,0C0h push ebx push esi push edi lea edi,[ebp-0C0h] mov ecx,30h mov eax,0CCCCCCCCh rep stos dword ptr [edi] ; // return a + b; mov eax,dword ptr [a] add eax,dword ptr [b] ; // function epilog pop edi pop esi pop ebx mov esp,ebp pop ebp ret int __cdecl add(int a, int b);

“Standard” Call Expand Arguments to 32 bits All arguments are on stack, right to left Decorated with: – Prepended underscore – and number of bytes stack needs Callee does stack cleanup – Smaller Executables – Fixed number of arguments

Standard Call ; Calling Add(7,19) push 19 push 7 call add esp, 8; clean up stack ;result is in eax ; body (nearly identical) ; // function prolog push ebp mov ebp,esp sub esp,0C0h push ebx push esi push edi lea edi,[ebp-0C0h] mov ecx,30h mov eax,0CCCCCCCCh rep stos dword ptr [edi] ; // return a + b; mov eax,dword ptr [a] add eax,dword ptr [b] ; // function epilog pop edi pop esi pop ebx mov esp,ebp pop ebp ret int __stdcall add(int a, int b);

Fast Call Attempts to go fast by using registers for the first two arguments Function decorated by: – – + number of bytes required by arguments

Fast Call ; Calling Add(7,19) mov edx, 19 mov ecx, 7 call ;result is in eax ; // function prolog push ebp mov ebp,esp sub esp,0D8h push ebx push esi push edi push ecx lea edi,[ebp-0D8h] mov ecx,36h mov eax,0CCCCCCCCh rep stos dword ptr [edi] pop ecx mov dword ptr [ebp-14h],edx mov dword ptr [ebp-8],ecx ; // return a + b; mov eax,dword ptr [a] add eax,dword ptr [b] ;// function epilog pop edi pop esi pop ebx mov esp,ebp pop ebp ret int __fastcall add(int a, int b);

Why do I care? You rarely need to know this level of detail Helpful for understanding interop Helpful for debugging Understand the limitations and COSTS of functions – Inlining is nice

Back To Reality Final Projects! – Create one idea for a Final Project (Individually) – Put it on a piece of whiteboard – Sell it to the Class

Back To Reality Final Exam Questions – 1 st and 2 nd are due November 4 th – ( ノ ಥ 益 ಥ )ノ ┻━┻ HW3 is Posted – Due Monday

Back To Reality Final Exam Questions – 1 st and 2 nd are due November 4 th – ( ノ ಥ 益 ಥ )ノ ┻━┻ HW3 is Posted – Due Monday – You can start now. – ┬ ─ ┬ ノ ( ゜ - ゜ノ )