Assembly Language: Function Calls" Jennifer Rexford!

Save this PDF as:

Size: px
Start display at page:

Transcription

1 Assembly Language: Function Calls" Jennifer Rexford! 1

2 Goals of this Lecture" Function call problems:! Calling and returning! Passing parameters! Storing local variables! Handling registers without interference! Returning values! IA-32 solutions to those problems! Pertinent instructions and conventions! 2

3 Recall from Last Lecture" Examples of Operands! Immediate Operand! movl \$5,! CPU uses 5 as source operand! movl \$i,! CPU uses address denoted by i as source operand! Register Operand! movl %eax,! CPU uses contents of EAX register as source operand! 3

4 Recall from Last Lecture (cont.)" Memory Operand: Direct Addressing! movl i,! CPU fetches source operand from memory at address i! Memory Operand: Indirect Addressing! movl (%eax),! CPU considers contents of EAX to be an address! Fetches source operand from memory at that address! Memory Operand: Base+Displacement Addressing! movl 8(%eax),! CPU computes address as 8 + [contents of EAX]! Fetches source operand from memory at that address! 4

5 Recall from Last Lecture (cont.)" Memory Operand: Indexed Addressing! movl 8(%eax, %ecx),! Computes address as 8 + [contents of EAX] + [contents of ECX]! Fetches source operand from memory at that address! Memory Operand: Scaled Indexed Addressing! movl 8(%eax, %ecx, 4),! Computes address as 8 + [contents of EAX] + ([contents of ECX] * 4)! Fetches source operand from memory at that address! Same for destination operand, except! Destination operand cannot be immediate! 5

6 Function Call Problems" 1. Calling and returning! How does caller function jump to callee function?! How does callee function jump back to the right place in caller function?! 2. Passing parameters! How does caller function pass parameters to callee function?! 3. Storing local variables! Where does callee function store its local variables?! 4. Handling registers! How do caller and callee functions use same registers without interference?! 5. Returning a value! How does callee function send return value back to caller function?! 6

7 Problem 1: Calling and Returning" How does caller function jump to callee function?! I.e., Jump to the address of the calleeʼs first instruction! How does the callee function jump back to the right place in caller function?! I.e., Jump to the instruction immediately following the most-recently-executed call instruction! 7

8 Attempted Solution: Use Jmp Instruction" Attempted solution: caller and callee use jmp instruction! P: # Function P jmp R # Call R Rtn_point1: R: # Function R jmp Rtn_point1 # Return 8

9 Attempted Solution: Use Jmp Instruction" Problem: callee may be called by multiple callers! P: # Function P jmp R # Call R Rtn_point1: R: # Function R jmp??? # Return Q: # Function Q jmp R # Call R Rtn_point2: 9

10 Attempted Solution: Use Register" Attempted solution 2: Store return address in register! P: # Function P movl \$Rtn_point1, %eax jmp R # Call R Rtn_point1: R: # Function R jmp *%eax # Return Q: # Function Q movl \$Rtn_point2, %eax jmp R # Call R Rtn_point2: Special form of jmp instruction; we will not use 10

11 Attempted Solution: Use Register" Problem: Cannot handle nested function calls! P: # Function P movl \$Rtn_point1, %eax jmp Q # Call Q Rtn_point1: R: # Function R jmp *%eax # Return Q: # Function Q movl \$Rtn_point2, %eax jmp R # Call R Rtn_point2: jmp %eax # Return Problem if P calls Q, and Q calls R Return address for P to Q call is lost 11

12 IA-32 Solution: Use the Stack" May need to store many return addresses! The number of nested functions is not known in advance! A return address must be saved for as long as the function invocation continues, and discarded thereafter! Addresses used in reverse order! E.g., function P calls Q, which then calls R! Then R returns to Q which then returns to P! Last-in-first-out data structure (stack)! Caller pushes return address on the stack! and callee pops return address off the stack! IA 32 solution: Use the stack via call and ret! EIP for Q EIP for P 12

13 IA-32 Call and Ret Instructions" Ret instruction knows the return address! P: # Function P R: # Function R call R call Q 1! 2! ret Q: # Function Q call R ret 13

14 IA-32 Call and Ret Instructions" Ret instruction knows the return address! P: # Function P call R call Q 3! 4! 5! R: # Function R ret Q: # Function Q 6! call R ret 14

15 Implementation of Call" ESP (stack pointer register) points to top of stack! 0 Instruction" pushl src popl dest Effective Operations" subl \$4, %esp movl src, (%esp) movl (%esp), dest addl \$4, %esp ESP 15

16 Implementation of Call" EIP (instruction pointer register) points to next instruction to be executed! Instruction" pushl src popl dest call addr Effective Operations" subl \$4, %esp movl src, (%esp) movl (%esp), dest addl \$4, %esp pushl %eip jmp addr Call instruction pushes return address (old EIP) onto stack ESP before call 0 Note: can t really access EIP directly, but this is implicitly what call is doing 16

17 Implementation of Call" 0 Instruction" Effective Operations" pushl src subl \$4, %esp movl src, (%esp) popl dest movl (%esp), dest addl \$4, %esp call addr pushl %eip jmp addr ESP after call Old EIP 17

18 Implementation of Ret" 0 Instruction" pushl src popl dest call addr ret Effective Operations" subl \$4, %esp movl src, (%esp) movl (%esp), dest addl \$4, %esp pushl %eip jmp addr pop %eip Ret instruction pops stack, thus placing return address (old EIP) into EIP Note: can t really access EIP directly, but this is implicitly what ret is doing. ESP before ret Old EIP 18

19 Implementation of Ret" 0 Instruction" pushl src popl dest call addr Effective Operations" subl \$4, %esp movl src, (%esp) movl (%esp), dest addl \$4, %esp pushl %eip jmp addr ret pop %eip ESP after ret 19

20 Problem 2: Passing Parameters" Problem: How does caller function pass parameters to callee function?! int add3(int a, int b, int c) { int d; d = a + b + c; return d; } int f(void) { return add3(3, 4, 5); } 20

21 Attempted Solution: Use Registers" Attempted solution: Pass parameters in registers! f: movl \$3, %eax movl \$4, %ebx movl \$5, %ecx call add3 add3: # Use EAX, EBX, ECX ret 21

22 Attempted Solution: Use Registers" Problem: Cannot handle nested function calls! f: movl \$3, %eax movl \$4, %ebx movl \$5, %ecx call add3 add3: movl \$6, %eax call g # Use EAX, EBX, ECX # But EAX is corrupted! ret Also: How to pass parameters that are longer than 4 bytes?! 22

23 IA-32 Solution: Use the Stack" Caller pushes parameters before executing the call instruction! 0 ESP before pushing params 23

24 IA-32 Parameter Passing" Caller pushes parameters in the reverse order! Push N th param first! Push 1 st param last! So first param is at top of the stack at the time of the Call! 0 ESP before call Param 1 Param Param N 24

25 IA-32 Parameter Passing" Callee addresses params relative to ESP: Param 1 as 4(%esp)! 0 ESP after call Old EIP Param 1 Param Param N 25

26 IA-32 Parameter Passing" After returning to the caller! 0 ESP after return Param 1 Param Param N 26

27 IA-32 Parameter Passing" the caller pops the parameters from the stack! 0 ESP after popping params 27

28 IA-32 Parameter Passing" For example:! f: # Push parameters pushl \$5 pushl \$4 pushl \$3 call add3 # Pop parameters addl \$12, %esp add3: movl 4(%esp), wherever movl 8(%esp), wherever movl 12(%esp), wherever ret 28

29 Base Pointer Register: EBP" Problem:! As callee executes, ESP may change! E.g., preparing to call another function! Error-prone for callee to reference params as offsets relative to ESP! Solution:! Use EBP as fixed reference point to access params! ESP after call 0 Old EIP Param 1 Param Param N EBP 29

30 Using EBP" Need to save old value of EBP! Before overwriting EBP register! 0 Callee executes prolog! pushl %ebp movl %esp, %ebp ESP Old EBP Old EIP Param 1 Param Param N EBP 30

31 Base Pointer Register: EBP" Callee executes prolog! pushl %ebp 0 movl %esp, %ebp Regardless of ESP, callee can reference param 1 as 8(%ebp), param 2 as 12(%ebp), etc.! ESP, EBP Old EBP Old EIP Param 1 Param Param N 31

32 Base Pointer Register: EBP" Before returning, callee must restore ESP and EBP to their old values! ESP 0 Callee executes epilog! movl %ebp, %esp popl %ebp ret EBP Old EBP Old EIP Param 1 Param Param N 32

33 Base Pointer Register: EBP" Callee executes epilog! movl %ebp, %esp 0 popl %ebp ret ESP, EBP Old EBP Old EIP Param 1 Param Param N 33

34 Base Pointer Register: EBP" Callee executes epilog! movl %ebp, %esp 0 popl %ebp ret ESP Old EIP Param 1 Param Param N EBP 34

35 Base Pointer Register: EBP" Callee executes epilog! movl %ebp, %esp 0 popl %ebp ret ESP Param 1 Param Param N EBP 35

36 Problem 3: Storing Local Variables" Where does callee function store its local variables?! int add3(int a, int b, int c) { int d; d = a + b + c; return d; } int foo(void) { return add3(3, 4, 5); } 36

37 IA-32 Solution: Use the Stack" Local variables:! Short-lived, so donʼt need a permanent location in memory! Size known in advance, so donʼt need to allocate on the heap! So, the function just uses the top of the stack! Store local variables on the top of the stack! The local variables disappear after the function returns! int add3(int a, int b, int c) { int d; d = a + b + c; return d; } int foo(void) { return add3(3, 4, 5); } 37

38 IA-32 Local Variables" Local variables of the callee are allocated on the stack! 0 Allocation done by moving the stack pointer! Example: allocate memory for two integers! subl \$4, %esp! subl \$4, %esp! (or equivalently, subl \$8, %esp)! Reference local variables as negative offsets relative to EBP! -4(%ebp)! -8(%ebp)! ESP EBP Var 2 Var 1 Old EBP Old EIP Param 1 Param Param N 38

39 IA-32 Local Variables" For example:! add3: # Allocate space for d subl \$4, %esp # Access d movl whatever, -4(%ebp) ret 39

40 Problem 4: Handling Registers" Problem: How do caller and callee functions use same registers without interference?! Registers are a finite resource!! In principle: Each function should have its own set of registers! In reality: All functions must use the same small set of registers! Callee may use a register that the caller also is using! When callee returns control to caller, old register contents may be lost! Caller function cannot continue where it left off! 40

41 IA-32 Solution: Define a Convention" IA-32 solution: save the registers on the stack! Someone must save old register contents! Someone must later restore the register contents! Define a convention for who saves and restores which registers! 41

42 IA-32 Register Handling" Caller-save registers! EAX, EDX, ECX ESP If necessary! Caller saves on stack before call! Caller restores from stack after call! Callee-save registers! EBX, ESI, EDI If necessary! EBP Callee saves on stack after prolog! Callee restores from stack before epilog! Caller can assume that values in EBX, ESI, EDI will not be changed by callee! 0 Var 2 Var 1 Saved EBX, ESI,EDI Old EBP Old EIP Param 1 Param Param N Saved EAX, EDX,ECX 42

43 Problem 5: Return Values" Problem: How does callee function send return value back to caller function?! In principle:! Store return value in stack frame of caller! Or, for efficiency:! Known small size => store return value in register! Other => store return value in stack! int add3(int a, int b, int c) { int d; d = a + b + c; return d; } int foo(void) { return add3(3, 4, 5); } 43

44 IA-32 Return Values" IA-32 Convention:! Integral type or pointer:! Store return value in EAX! char, short, int, long, pointer! Floating-point type:! Store return value in floatingpoint register! (Beyond scope of course)! Structure:! Store return value on stack! (Beyond scope of course)! int add3(int a, int b, int c) { int d; d = a + b + c; return d; } int foo(void) { return add3(3, 4, 5); } 44

45 Stack Frames" Summary of IA-32 function handling:! Stack has one stack frame per active function invocation! ESP points to top (low memory) of current stack frame! EBP points to bottom (high memory) of current stack frame! Stack frame contains:! Return address (Old EIP)! Old EBP! Saved register values! Local variables! Parameters to be passed to callee function! 45

46 A Simple Example" int add3(int a, int b, int c) { int d; d = a + b + c; return d; } /* In some calling function */ x = add3(3, 4, 5); 46

47 Trace of a Simple Example 1" x = add3(3, 4, 5); Low memory ESP EBP High memory 47

48 Trace of a Simple Example 2" x = add3(3, 4, 5); Low memory # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx ESP Old EDX Old ECX Old EAX EBP High memory 48

49 Trace of a Simple Example 3" x = add3(3, 4, 5); Low memory # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl \$5 pushl \$4 pushl \$3 ESP Old EDX Old ECX Old EAX EBP High memory 49

50 Trace of a Simple Example 4" x = add3(3, 4, 5); Low memory # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl \$5 pushl \$4 pushl \$3 # Call add3 call add3 ESP Old EIP Old EDX Old ECX Old EAX EBP High memory 50

51 Trace of a Simple Example 5" int add3(int a, int b, int c) { int d; d = a + b + c; return d; } Low memory # Save old EBP pushl %ebp Prolog ESP Old EBP Old EIP Old EDX Old ECX Old EAX EBP High memory 51

52 Trace of a Simple Example 6" int add3(int a, int b, int c) { int d; d = a + b + c; return d; } Low memory # Save old EBP pushl %ebp # Change EBP movl %esp, %ebp Prolog ESP EBP Old EBP Old EIP Old EDX Old ECX Old EAX High memory 52

53 Trace of a Simple Example 7" int add3(int a, int b, int c) { int d; d = a + b + c; return d; } # Save old EBP pushl %ebp # Change EBP movl %esp, %ebp # Save caller-save registers if necessary pushl %ebx pushl %esi pushl %edi Unnecessary here; add3 will not change the values in these registers Low memory ESP EBP Old EDI Old ESI Old EBX Old EBP Old EIP Old EDX Old ECX Old EAX High memory 53

54 Trace of a Simple Example 8" int add3(int a, int b, int c) { int d; d = a + b + c; return d; } # Save old EBP pushl %ebp # Change EBP movl %esp, %ebp # Save caller-save registers if necessary pushl %ebx pushl %esi pushl %edi # Allocate space for local variable subl \$4, %esp Low memory ESP EBP Old EDI Old ESI Old EBX Old EBP Old EIP Old EDX Old ECX Old EAX High memory 54

55 Trace of a Simple Example 9" int add3(int a, int b, int c) { int d; d = a + b + c; return d; } # Save old EBP pushl %ebp # Change EBP movl %esp, %ebp # Save caller-save registers if necessary pushl %ebx pushl %esi pushl %edi # Allocate space for local variable subl \$4, %esp # Perform the addition movl 8(%ebp), %eax addl 12(%ebp), %eax addl 16(%ebp), %eax movl %eax, -16(%ebp) Low memory ESP EBP Access params as positive offsets relative to EBP Access local vars as negative offsets relative to EBP High memory 12 Old EDI Old ESI Old EBX Old EBP Old EIP Old EDX Old ECX Old EAX 55

56 Trace of a Simple Example 10" int add3(int a, int b, int c) { int d; d = a + b + c; return d; } # Copy the return value to EAX movl -16(%ebp), %eax # Restore callee-save registers if necessary movl -12(%ebp), %edi movl -8(%ebp), %esi movl -4(%ebp), %ebx Low memory ESP EBP 12 Old EDI Old ESI Old EBX Old EBP Old EIP Old EDX Old ECX Old EAX High memory 56

57 Trace of a Simple Example 11" int add3(int a, int b, int c) { int d; d = a + b + c; return d; } # Copy the return value to EAX movl -16(%ebp), %eax # Restore callee-save registers if necessary movl -12(%ebp), %edi movl -8(%ebp), %esi movl -4(%ebp), %ebx # Restore ESP movl %ebp, %esp Epilog Low memory ESP EBP 12 Old EDI Old ESI Old EBX Old EBP Old EIP Old EDX Old ECX Old EAX High memory 57

58 Trace of a Simple Example 12" int add3(int a, int b, int c) { int d; d = a + b + c; return d; } # Copy the return value to EAX movl -16(%ebp), %eax # Restore callee-save registers if necessary movl -12(%ebp), %edi movl -8(%ebp), %esi movl -4(%ebp), %ebx # Restore ESP movl %ebp, %esp # Restore EBP Epilog popl %ebp Low memory ESP 12 Old EDI Old ESI Old EBX Old EBP Old EIP Old EDX Old ECX Old EAX EBP High memory 58

59 Trace of a Simple Example 13" int add3(int a, int b, int c) { int d; d = a + b + c; return d; } # Copy the return value to EAX movl -16(%ebp), %eax # Restore callee-save registers if necessary movl -12(%ebp), %edi movl -8(%ebp), %esi movl -4(%ebp), %ebx # Restore ESP movl %ebp, %esp # Restore EBP popl %ebp # Return to calling function ret Low memory ESP 12 Old EDI Old ESI Old EBX Old EBP Old EIP Old EDX Old ECX Old EAX EBP High memory 59

60 Trace of a Simple Example 14" x = add3(3, 4, 5); # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl \$5 pushl \$4 pushl \$3 # Call add3 call add3 # Pop parameters addl \$12, %esp Low memory ESP 12 Old EDI Old ESI Old EBX Old EBP Old EIP Old EDX Old ECX Old EAX EBP High memory 60

61 Trace of a Simple Example 15" x = add3(3, 4, 5); # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl \$5 pushl \$4 pushl \$3 # Call add3 call add3 # Pop parameters addl %12, %esp # Save return value movl %eax, wherever Low memory ESP 12 Old EDI Old ESI Old EBX Old EBP Old EIP Old EDX Old ECX Old EAX EBP High memory 61

62 Trace of a Simple Example 16" x = add3(3, 4, 5); # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl \$5 pushl \$4 pushl \$3 # Call add3 call add3 # Pop parameters addl %12, %esp # Save return value movl %eax, wherever # Restore caller-save registers if necessary popl %edx popl %ecx popl %eax Low memory ESP EBP High memory 12 Old EDI Old ESI Old EBX Old EBP Old EIP Old EDX Old ECX Old EAX 62

63 Trace of a Simple Example 17" x = add3(3, 4, 5); Low memory # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl \$5 pushl \$4 pushl \$3 # Call add3 call add3 # Pop parameters addl %12, %esp # Save return value movl %eax, wherever # Restore caller-save registers if necessary popl %edx popl %ecx popl %eax # Proceed! ESP EBP High memory 63

64 Summary" Calling and returning! Call instruction: push EIP onto stack and jump! Ret instruction: pop stack to EIP! Passing parameters! Caller pushes onto stack! Callee accesses as positive offsets from EBP! Caller pops from stack! 64

65 Summary (cont.)" Storing local variables! Callee pushes on stack! Callee accesses as negative offsets from EBP! Callee pops from stack! Handling registers! Caller saves and restores EAX, ECX, EDX if necessary! Callee saves and restores EBX, ESI, EDI if necessary! Returning values! Callee returns data of integral types and pointers in EAX! 65

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 20: Stack Frames 7 March 08 CS 412/413 Spring 2008 Introduction to Compilers 1 Where We Are Source code if (b == 0) a = b; Low-level IR code

Instruction Set Architecture

CS:APP Chapter 4 Computer Architecture Instruction Set Architecture Randal E. Bryant adapted by Jason Fritts http://csapp.cs.cmu.edu CS:APP2e Hardware Architecture - using Y86 ISA For learning aspects

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com

CSCI-UA.0201-003 Computer Systems Organization Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com Some slides adapted (and slightly modified)

CS61: Systems Programing and Machine Organization

CS61: Systems Programing and Machine Organization Fall 2009 Section Notes for Week 2 (September 14 th - 18 th ) Topics to be covered: I. Binary Basics II. Signed Numbers III. Architecture Overview IV.

l Status flags or condition codes are set when the in a zero l Example 1 l mov 0xFFFFFFFF %eax l add 1 %eax

Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else While, for loop Assembly programmer view PC SP BR ALU IR GPR Status Flags ALU: Arithmetic Logic

Y86 Instruction Set. Operations. Moves. Branches. %eax %ecx %edx %ebx %esi %edi %esp %ebp R E G I S T E R S. jne. jge. jmp 7 0. cmovne 2 4.

Y86 Instruction Set Byte 0 1 2 3 4 5 halt 0 0 nop 1 0 rrmovl ra, rb 2 0 ra rb irmovl V, rb 3 0 8F rb V rmmovl ra, D(rB) 4 0 ra rb D mrmovl D(rB), ra 5 0 ra rb D R E G I S T E R S %eax %ecx %edx %ebx %esi

Return-oriented programming without returns

Faculty of Computer Science Institute for System Architecture, Operating Systems Group Return-oriented programming without urns S. Checkoway, L. Davi, A. Dmitrienko, A. Sadeghi, H. Shacham, M. Winandy

Machine-Level Programming II: Arithmetic & Control

Mellon Machine-Level Programming II: Arithmetic & Control 15-213 / 18-213: Introduction to Computer Systems 6 th Lecture, Jan 29, 2015 Instructors: Seth Copen Goldstein, Franz Franchetti, Greg Kesden 1

Machine Programming II: Instruc8ons

Machine Programming II: Instrucons Move instrucons, registers, and operands Complete addressing mode, address computaon (leal) Arithmec operaons (including some x6 6 instrucons) Condion codes Control,

CS:APP Chapter 4 Computer Architecture Instruction Set Architecture. CS:APP2e

CS:APP Chapter 4 Computer Architecture Instruction Set Architecture CS:APP2e Instruction Set Architecture Assembly Language View Processor state Registers, memory, Instructions addl, pushl, ret, How instructions

Context switch in Linux. Gabriel Kliot, Technion 1 Context switch in Linux OS course

Context switch in Linux Gabriel Kliot, Technion 1 Context switch in Linux OS course Memory layout general picture Stack Stack Stack Process X user memory Process Y user memory Process Z user memory Stack

Lecture 4: Machine Code And Assembly Language

CSCI-GA.1144-001 PAC II Lecture 4: Machine Code And Assembly Language Some slides adapted (and slightly modified) from: Clark Barrett Jinyang Li Randy Bryant Dave O Hallaron Mohamed Zahran (aka Z) mzahran@cs.nyu.edu

Assembly Language: Overview! Jennifer Rexford!

Assembly Language: Overview! Jennifer Rexford! 1 Goals of this Lecture! Help you learn:! The basics of computer architecture! The relationship between C and assembly language! IA-32 assembly language,

Lecture 27 C and Assembly

Ananda Gunawardena Lecture 27 C and Assembly This is a quick introduction to working with x86 assembly. Some of the instructions and register names must be check for latest commands and register names.

A Tiny Guide to Programming in 32-bit x86 Assembly Language

CS308, Spring 1999 A Tiny Guide to Programming in 32-bit x86 Assembly Language by Adam Ferrari, ferrari@virginia.edu (with changes by Alan Batson, batson@virginia.edu and Mike Lack, mnl3j@virginia.edu)

l C-Programming l A real computer language l Data Representation l Everything goes down to bits and bytes l Machine representation Language

198:211 Computer Architecture Topics: Processor Design Where are we now? C-Programming A real computer language Data Representation Everything goes down to bits and bytes Machine representation Language

Lecture 14 Project, Assembler and Exam Source code Compiler phases and program representations Frontend Lexical analysis (scanning) Backend Immediate code generation Today Project Emma Söderberg Revised

Computer Architectures

Computer Architectures Parameters Passing to Subroutines and Operating System Implemented Virtual Instructions (System Calls) Czech Technical University in Prague, Faculty of Electrical Engineering Ver.1.10

Stack Overflows Mitchell Adair Outline Why? What? There once was a VM Virtual Memory Registers Stack stack1, stack2, stack3 Resources Why? Real problem Real money Real recognition Still prevalent Very

Intel 8086 architecture

Intel 8086 architecture Today we ll take a look at Intel s 8086, which is one of the oldest and yet most prevalent processor architectures around. We ll make many comparisons between the MIPS and 8086

CHAPTER 6 TASK MANAGEMENT This chapter describes the IA-32 architecture s task management facilities. These facilities are only available when the processor is running in protected mode. 6.1. TASK MANAGEMENT

x64 Cheat Sheet Fall 2015

CS 33 Intro Computer Systems Doeppner x64 Cheat Sheet Fall 2015 1 x64 Registers x64 assembly code uses sixteen 64-bit registers. Additionally, the lower bytes of some of these registers may be accessed

Buffer Overflows. Security 2011

Buffer Overflows Security 2011 Memory Organiza;on Topics Kernel organizes memory in pages Typically 4k bytes Processes operate in a Virtual Memory Space Mapped to real 4k pages Could live in RAM or be

Under The Hood: The System Call

2 Under The Hood: The System Call In this note, we ll peak under the hood of one simple and neat OS called xv6 [CK+08]. The xv6 kernel is a port of an old UNIX version 6 from PDP-11 (the machine it was

Computer Architecture and Assembly Language. Practical Session 4

Computer Architecture and Assembly Language Practical Session 4 Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _, \$, #, @,

Off-by-One exploitation tutorial

Off-by-One exploitation tutorial By Saif El-Sherei www.elsherei.com Introduction: I decided to get a bit more into Linux exploitation, so I thought it would be nice if I document this as a good friend

Chapter 4 Processor Architecture

Chapter 4 Processor Architecture Modern microprocessors are among the most complex systems ever created by humans. A single silicon chip, roughly the size of a fingernail, can contain a complete high-performance

Lecture 4. Announcements:

Announcements: Lecture 4 HW1 posted on Saturday morning. We gave a little over a week. We are going to discuss relevant material over the next two lectures. Lab1 posted. But if you start doing it, you

64-Bit NASM Notes. Invoking 64-Bit NASM

64-Bit NASM Notes The transition from 32- to 64-bit architectures is no joke, as anyone who has wrestled with 32/64 bit incompatibilities will attest We note here some key differences between 32- and 64-bit

Intel IA-32 Assembly

Intel IA-32 Assembly Simon Kågström Department of Systems and Software Engineering Blekinge Institute of Technology Ronneby, Sweden http://www.ipd.bth.se/ska/unix programming.html Simon Kågström (BTH,

X86-64 Architecture Guide

X86-64 Architecture Guide For the code-generation project, we shall expose you to a simplified version of the x86-64 platform. Example Consider the following Decaf program: class Program { int foo(int

Abysssec Research. 1) Advisory information. 2) Vulnerable version

Abysssec Research 1) Advisory information Title Version Discovery Vendor Impact Contact Twitter CVE : Apple QuickTime FlashPix NumberOfTiles Remote Code Execution Vulnerability : QuickTime player 7.6.5

Machine Programming I: Basics

Machine Programming I: Basics History of Intel processors and architectures Intel processors (Wikipedia) Intel microarchitectures C, assembly, machine code Assembly basics: registers, operands, move instrucaons

CSC 574 Computer and Network Security. Reverse Engineering

CSC 574 Computer and Network Security Reverse Engineering Alexandros Kapravelos kapravelos@ncsu.edu (Derived from slides by Chris Kruegel) Introduction Reverse engineering process of analyzing a system

Machine-Code Generation for Functions

Machine-Code Generation for Functions Cosmin Oancea cosmin.oancea@diku.dk University of Copenhagen December 2012 Structure of a Compiler Programme text Lexical analysis Binary machine code Symbol sequence

More MIPS: Recursion. Computer Science 104 Lecture 9

More MIPS: Recursion Computer Science 104 Lecture 9 Admin Homework Homework 1: graded. 50% As, 27% Bs Homework 2: Due Wed Midterm 1 This Wed 1 page of notes 2 Last time What did we do last time? 3 Last

Software Vulnerabilities

Software Vulnerabilities -- stack overflow Code based security Code based security discusses typical vulnerabilities made by programmers that can be exploited by miscreants Implementing safe software in

Chapter 2: MIPS Instruction Set (cont d)

Chapter 2: MIPS Instruction Set (cont d) 1 Review Instruction Meaning add \$s1,\$s2,\$s3 \$s1 = \$s2 + \$s3 sub \$s1,\$s2,\$s3 \$s1 = \$s2 \$s3 addi \$s1,\$s2,4 \$s1 = \$s2 + 4 ori \$s1,\$s2,4 \$s2 = \$s2 4 lw \$s1,100(\$s2)

Return-to-libc Attack Lab

Laboratory for Computer Security Education 1 Return-to-libc Attack Lab Copyright c 2006-2010 Wenliang Du, Syracuse University. The development of this document is funded by the National Science Foundation

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings: 9.1-9.

Code Generation I Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings: 9.1-9.7 Stack Machines A simple evaluation model No variables

Chapter 3 Machine-Level Representation of Programs

Chapter 3 Machine-Level Representation of Programs Computers execute machine code, sequences of bytes encoding the low-level operations that manipulate data, manage memory, read and write data on storage

Stack Allocation. Run-Time Data Structures. Static Structures

Run-Time Data Structures Stack Allocation Static Structures For static structures, a fixed address is used throughout execution. This is the oldest and simplest memory organization. In current compilers,

Return-oriented Programming

Systems and Internet Infrastructure Security Network and Security Research Center Department of Computer Science and Engineering Pennsylvania State University, University Park PA Return-oriented Programming

Architecture and Programming of x86 Processors

Brno University of Technology Architecture and Programming of x86 Processors Microprocessor Techniques and Embedded Systems Lecture 12 Dr. Tomas Fryza December 2012 Contents A little bit of one-core Intel

Memory Basics and Assembly Programs by Nicole Tobias

Memory Basics and Assembly Programs by Nicole Tobias Adapted from text: LINUX Assembly Programming by Neveln Appendix C from Computer Organization and Design by Patterson and Hennessey Basics of Memory

1 The Java Virtual Machine

1 The Java Virtual Machine About the Spec Format This document describes the Java virtual machine and the instruction set. In this introduction, each component of the machine is briefly described. This

Intel microprocessor history. Intel x86 Architecture. Early Intel microprocessors. The IBM-AT

Intel x86 Architecture Intel microprocessor history Computer Organization and Assembly Languages g Yung-Yu Chuang with slides by Kip Irvine Early Intel microprocessors Intel 8080 (1972) 64K addressable

Systems Design & Programming Data Movement Instructions. Intel Assembly

Intel Assembly Data Movement Instruction: mov (covered already) push, pop lea (mov and offset) lds, les, lfs, lgs, lss movs, lods, stos ins, outs xchg, xlat lahf, sahf (not covered) in, out movsx, movzx

Computer Organization and Architecture

Computer Organization and Architecture Chapter 11 Instruction Sets: Addressing Modes and Formats Instruction Set Design One goal of instruction set design is to minimize instruction length Another goal

W4118 Operating Systems. Junfeng Yang

W4118 Operating Systems Junfeng Yang Outline Linux overview Interrupt in Linux System call in Linux What is Linux A modern, open-source OS, based on UNIX standards 1991, 0.1 MLOC, single developer Linus

Lecture Outline. Stack machines The MIPS assembly language. Code Generation (I)

Lecture Outline Code Generation (I) Stack machines The MIPS assembl language Adapted from Lectures b Profs. Ale Aiken and George Necula (UCB) A simple source language Stack- machine implementation of the

Processing Unit. Backing Store

SYSTEM UNIT Basic Computer Structure Input Unit Central Processing Unit Main Memory Output Unit Backing Store The Central Processing Unit (CPU) is the unit in the computer which operates the whole computer

Computer Organization and Assembly Language

Computer Organization and Assembly Language Lecture 8 - Strings and Arrays Introduction We already know that assembly code will execute significantly faster than code written in a higher-level language

System Call Tracing using ptrace. #include <sys/ptrace.h> long int ptrace(enum ptrace_request request, pid_t pid, void * addr, void * data)

System Call Tracing using ptrace Introduction! Synopsis 1 #include long int ptrace(enum ptrace_request request, pid_t pid, void * addr, void * data)! Description 2 The ptrace system call

5. Calling conventions for different C++ compilers and operating systems

5. Calling conventions for different C++ compilers and operating systems By Agner Fog. Technical University of Denmark. Copyright 2004-2014. Last updated 2014-08-07. Contents 1 Introduction... 3 2 The

Hotpatching and the Rise of Third-Party Patches

Hotpatching and the Rise of Third-Party Patches Alexander Sotirov asotirov@determina.com BlackHat USA 2006 Overview In the next one hour, we will cover: Third-party security patches _ recent developments

Attacking x86 Windows Binaries by Jump Oriented Programming

Attacking x86 Windows Binaries by Jump Oriented Programming L. Erdődi * * Faculty of John von Neumann, Óbuda University, Budapest, Hungary erdodi.laszlo@nik.uni-obuda.hu Abstract Jump oriented programming

TODAY, FEW PROGRAMMERS USE ASSEMBLY LANGUAGE. Higher-level languages such

9 Inline Assembly Code TODAY, FEW PROGRAMMERS USE ASSEMBLY LANGUAGE. Higher-level languages such as C and C++ run on nearly all architectures and yield higher productivity when writing and maintaining

Heap-based Buffer Overflow Vulnerability in Adobe Flash Player

Analysis of Zero-Day Exploit_Issue 03 Heap-based Buffer Overflow Vulnerability in Adobe Flash Player CVE-2014-0556 20 December 2014 Table of Content Overview... 3 1. CVE-2014-0556 Vulnerability... 3 2.

Introduction. Application Security. Reasons For Reverse Engineering. This lecture. Java Byte Code

Introduction Application Security Tom Chothia Computer Security, Lecture 16 Compiled code is really just data which can be edit and inspected. By examining low level code protections can be removed and

Assembly Language Programming: Subroutines

Assembly Language Programming: Subroutines by Alex Milenkovich, milenkovic@computer.org Objectives: Introduce subroutines, subroutine nesting, processor stack, and passing the parameters to subroutines.

Giving credit where credit is due

CSCE 230J Computer Organization Processor Architecture VI: Wrap-Up Dr. Steve Goddard goddard@cse.unl.edu http://cse.unl.edu/~goddard/courses/csce230j Giving credit where credit is due ost of slides for

Lecture 3: MIPS Instruction Set

Lecture 3: MIPS Instruction Set Today s topic: More MIPS instructions Procedure call/return Reminder: Assignment 1 is on the class web-page (due 9/7) 1 Memory Operands Values must be fetched from memory

Simple C Programs 1 Goals for this Lecture Help you learn about: Simple C programs Program structure Defining symbolic constants Detecting and reporting failure Functionality of the gcc command Preprocessor,

8. Procedures. X86 Assembly Language Programming for the PC 71

8 Procedures X86 Assembly Language Programming for the PC 71 Stack Operation A stack is a region of memory used for temporary storage of information Memory space should be allocated for stack by the programmer

Computer Systems Architecture

Computer Systems Architecture http://cs.nott.ac.uk/ txa/g51csa/ Thorsten Altenkirch and Liyang Hu School of Computer Science University of Nottingham Lecture 10: MIPS Procedure Calling Convention and Recursion

Syscall Proxying Pivoting Systems

Syscall Proxying Pivoting Systems Filipe Balestra filipe@balestra.com.br Rodrigo Rubira Branco rodrigo@kernelhacking.com rodrigo@risesecurity.org Agenda: What is Syscall Proxying? - Why Syscall Proxy is

CS:APP Chapter 4 Computer Architecture. Wrap-Up. William J. Taffe Plymouth State University. using the slides of

CS:APP Chapter 4 Computer Architecture Wrap-Up William J. Taffe Plymouth State University using the slides of Randal E. Bryant Carnegie Mellon University Overview Wrap-Up of PIPE Design Performance analysis

W4118: virtual machines

W4118: virtual machines Instructor: Junfeng Yang References: Modern Operating Systems (3 rd edition), Operating Systems Concepts (8 th edition), previous W4118, and OS at MIT, Stanford, and UWisc Virtual

MIPS Calling Convention

MIPS Calling Convention This document summarizes the calling conventions that we expect you to use in ECE 314 for homework problems and assembly language programming projects. These are the rules for how

Optimizing YUV-RGB Color Space Conversion Using Intel s SIMD Technology

Optimizing YUV-RGB Color Space Conversion Using Intel s SIMD Technology Étienne Dupuis August 2003 Abstract Multimedia players must deal with MPEG video streams. Rendering these highly compressed streams

The Beast is Resting in Your Memory On Return-Oriented Programming Attacks and Mitigation Techniques To appear at USENIX Security & BlackHat USA, 2014

Intelligent Things, Vehicles and Factories: Intel Workshop on Cyberphysical and Mobile Security 2014, Darmstadt, June 11 The Beast is Resting in Your Memory On Return-Oriented Programming Attacks and Mitigation

Two s Compliment Negative integers are represented using two s compliment representation.

Chapter 1 Data representation Binary numbers Base 2 numbers in which each binary digit is either a 0 or a 1 Ie. 01000001b = 65d Integer storage sizes Unsigned Integers Signed Integers BYTE (1 byte) 0 to

Chapter 7D The Java Virtual Machine

This sub chapter discusses another architecture, that of the JVM (Java Virtual Machine). In general, a VM (Virtual Machine) is a hypothetical machine (implemented in either hardware or software) that directly

Compilers I - Chapter 4: Generating Better Code

Compilers I - Chapter 4: Generating Better Code Lecturers: Paul Kelly (phjk@doc.ic.ac.uk) Office: room 304, William Penney Building Naranker Dulay (nd@doc.ic.ac.uk) Materials: Office: room 562 Textbook

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

Lecture Handout Computer Architecture Lecture No. 2 Reading Material Vincent P. Heuring&Harry F. Jordan Chapter 2,Chapter3 Computer Systems Design and Architecture 2.1, 2.2, 3.2 Summary 1) A taxonomy of

Software Fingerprinting for Automated Malicious Code Analysis

Software Fingerprinting for Automated Malicious Code Analysis Philippe Charland Mission Critical Cyber Security Section October 25, 2012 Terms of Release: This document is approved for release to Defence

Programming from the Ground Up

Programming from the Ground Up Jonathan Bartlett Edited by Dominick Bruno, Jr. Programming from the Ground Up by Jonathan Bartlett Edited by Dominick Bruno, Jr. Copyright 2003 by Jonathan Bartlett Permission

Reverse Engineering Analysis of Vulnerabilities. Luis Miras

Reverse Engineering Analysis of Vulnerabilities Luis Miras luis@ringzero.net Agenda [*] Types of reversing [*] Assembly refresher [*] Some vulnerabilities in assembly [*] Tools and plugins [*] Advisory

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms 1 Activation Records activation declaration location Recall that an activation

Operating Systems. Week 2 Recitation: The system call. Paul Krzyzanowski. Rutgers University. Spring 2015

Operating Systems Week 2 Recitation: The system call Paul Krzyzanowski Rutgers University Spring 2015 February 14, 2015 2014-2015 Paul Krzyzanowski 1 System calls System calls are an operating system s

Programming from the Ground Up

Programming from the Ground Up Jonathan Bartlett Edited by Dominick Bruno, Jr. Programming from the Ground Up by Jonathan Bartlett Edited by Dominick Bruno, Jr. Copyright 2003 by Jonathan Bartlett Permission

CS:APP Chapter 3: Machine-Level Representations of Programs

CS:APP Chapter 3: Machine-Level Representations of Programs Randal E. Bryant David R. O Hallaron January 30, 2002 When programming in a high-level language, such as C, we are shielded from the detailed,

CSC 2405: Computer Systems II

CSC 2405: Computer Systems II Spring 2013 (TR 8:30-9:45 in G86) Mirela Damian http://www.csc.villanova.edu/~mdamian/csc2405/ Introductions Mirela Damian Room 167A in the Mendel Science Building mirela.damian@villanova.edu

Efficient Fine-Grained Binary Instrumentation with Applications to Taint-Tracking

Efficient Fine-Grained Binary Instrumentation with Applications to Taint-Tracking Prateek Saxena, R. Sekar and Varun Puranik Department of Computer Science Stony Brook University, Stony Brook, NY, USA.

Using Block Prefetch for Optimized Memory Performance Advanced Micro Devices Mike Wall Member of Technical Staff Developer Performance Team

Using Block Prefetch for Optimized Memory Performance Advanced Micro Devices Mike Wall Member of Technical Staff Developer Performance Team Introduction In recent years, clock speeds of X86 processors

Computer Organization and Components

Computer Organization and Components IS5, fall 25 Lecture : Pipelined Processors ssociate Professor, KTH Royal Institute of Technology ssistant Research ngineer, University of California, Berkeley Slides

Systems Design & Programming Arithmetic, Logic and Control Instructions. Intel Assembly

Intel Assembly Arithmetic Operations: Addition Subtraction Multiplication Division Comparison Negation Increment Decrement Logic Operations: AND OR XOR NOT shift rotate compare (test) 1 Arithmetic Operations

http://www.ecsl.cs.sunysb.edu/palladium.html Computer Science Department State University of New York Stony Brook, NY 11794-4400

Integrating segmentation and paging protection for safe, efficient and transparent software extensions Tzi-cker Chiueh Ganesh Venkitachalam Prashant Pradhan Computer Science Department State University

Reduced Instruction Set Computer vs Complex Instruction Set Computers

RISC vs CISC Reduced Instruction Set Computer vs Complex Instruction Set Computers for a given benchmark the performance of a particular computer: P = 1 I C 1 S where P = time to execute I = number of

Administration. Instruction scheduling. Modern processors. Examples. Simplified architecture model. CS 412 Introduction to Compilers

CS 4 Introduction to Compilers ndrew Myers Cornell University dministration Prelim tomorrow evening No class Wednesday P due in days Optional reading: Muchnick 7 Lecture : Instruction scheduling pr 0 Modern

Multiplication and Division Instructions

Multiplication and Division Instructions MUL Instruction IMUL Instruction DIV Instruction Signed Integer Division Implementing Arithmetic Expressions Irvine, Kip R. Assembly Language for Intel-Based Computers,

Cryptography and Network Security Prof. D. Mukhopadhyay Department of Computer Science and Engineering

Cryptography and Network Security Prof. D. Mukhopadhyay Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Module No. # 01 Lecture No. # 39 System Security Welcome

Introduction to Machine- and Assembly-Language Programming. Prepared for Tynan (and other crazy people)

Introduction to Machine- and Assembly-Language Programming Prepared for Tynan (and other crazy people) by Prof. Dr. Brad Richards University of Applied Sciences, NW Switzerland Institute for Business Information

G-Free: Defeating Return-Oriented Programming through Gadget-less Binaries

G-Free: Defeating Return-Oriented Programming through Gadget-less Binaries Kaan Onarlioglu Bilkent University, Ankara onarliog@cs.bilkent.edu.tr Davide Balzarotti Eurecom, Sophia Antipolis balzarotti@eurecom.fr

Unpacked BCD Arithmetic. BCD (ASCII) Arithmetic. Where and Why is BCD used? From the SQL Server Manual. Packed BCD, ASCII, Unpacked BCD

BCD (ASCII) Arithmetic The Intel Instruction set can handle both packed (two digits per byte) and unpacked BCD (one decimal digit per byte) We will first look at unpacked BCD Unpacked BCD can be either

EBP (Base Pointer) BP ESI (Source Index) SI EDI (Destination Index) DI ESP (Stack Pointer) SP

Intel x86 Assembly Language Programming CIS 450 { Computer Organization and Architecture Copyright c2001 Tim Bower The Intel x86 line of CPUs use the accumulator machine model. Registers Note that each