Process Layout and Function Calls

Similar documents
Process Layout, Function Calls, and the Heap

Sungkyunkwan University

Machine-Level Programming II: Arithmetic & Control. Complete Memory Addressing Modes

Machine-Level Programming II: Control and Arithmetic

Credits to Randy Bryant & Dave O Hallaron

Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012

CS241 Computer Organization Spring 2015 IA

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Assembly I: Basic Operations. Jo, Heeseung

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

CS241 Computer Organization Spring Addresses & Pointers

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Machine Level Programming II: Arithmetic &Control

Questions about last homework? (Would more feedback be useful?) New reading assignment up: due next Monday

Second Part of the Course

The Hardware/Software Interface CSE351 Spring 2013

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

Assembly Language: IA-32 Instructions

Machine-Level Programming I: Introduction Jan. 30, 2001

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

Credits and Disclaimers

System Programming and Computer Architecture (Fall 2009)

Machine- Level Programming II: Arithme c & Control

Instruction Set Architecture

Instruction Set Architecture

Software. Hardware. x86 basics. ISA View. a brief history of x86 10/6/15. Program, Application. Programming Language. Compiler/Interpreter

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

Machine- Level Programming II: Arithme6c & Control

Instructor: Alvin R. Lebeck

Instruction Set Architecture

CAS CS Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

Chapter 3 Machine-Level Programming II Control Flow

Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT

Machine Programming 1: Introduction

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

CS61 Section Solutions 3

Machine- Level Programming II: Arithme6c & Control

How Software Executes

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

CISC 360 Instruction Set Architecture

Instruction Set Architecture

Compiler Construction D7011E

Giving credit where credit is due

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Machine-Level Programming II: Control Flow

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

Intel Instruction Set (gas)

Assembly Language: Part 2

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

Credits and Disclaimers

Machine Programming 2: Control flow

CSC 8400: Computer Systems. Machine-Level Representation of Programs

The Hardware/Software Interface CSE351 Spring 2015

Handout #2: Machine-Level Programs

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

Chapter 4 Processor Architecture: Y86 (Sections 4.1 & 4.3) with material from Dr. Bin Ren, College of William & Mary

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

The Hardware/Software Interface CSE351 Spring 2015

x86 assembly CS449 Fall 2017

CISC 360. Machine-Level Programming I: Introduction Sept. 18, 2008

How Software Executes

Assembly Language II: Addressing Modes & Control Flow

Assembly Language: Function Calls" Goals of this Lecture"

Chapter 4! Processor Architecture!

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Program Exploitation Intro

Condition Codes The course that gives CMU its Zip! Machine-Level Programming II Control Flow Sept. 13, 2001 Topics

Machine-Level Programming Introduction

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

Machine-Level Programming Introduction

Page # CISC 360. Machine-Level Programming I: Introduction Sept. 18, IA32 Processors. X86 Evolution: Programmerʼs View.

Credits and Disclaimers

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

Condition Codes. Lecture 4B Machine-Level Programming II: Control Flow. Setting Condition Codes (cont.) Setting Condition Codes (cont.

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

Assembly Language: Function Calls

The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, 2002

X86 Assembly -Data II:1

Sungkyunkwan University

Assembly Language: Function Calls" Goals of this Lecture"

Machine Language CS 3330 Samira Khan

CF Carry Flag SF Sign Flag ZF Zero Flag OF Overflow Flag. ! CF set if carry out from most significant bit. "Used to detect unsigned overflow

Giving credit where credit is due

CPS104 Recitation: Assembly Programming

You may work with a partner on this quiz; both of you must submit your answers.

Control flow. Condition codes Conditional and unconditional jumps Loops Switch statements

IA32 Processors The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, X86 Evolution: Programmer s View

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

15-213/ Final Exam Notes Sheet Spring 2013!

Machine Programming 3: Procedures

! Starting in 1978 with ! Added more features as time goes on. ! Still support old features, although obsolete

CS429: Computer Organization and Architecture

Intro to GNU Assembly Language on Intel Processors

Transcription:

Process Layout and Function Calls CS 6 Spring 07 / 8

Process Layout in Memory Stack grows towards decreasing addresses. is initialized at run-time. Heap grow towards increasing addresses. is initialized at run-time. BSS section size fixed at compile-time. is initialized at run-time. was grouped into Data in CS6C. Data section is initialized at compile-time. Text section holds the program instructions (read-only). Stack dynamic growth Heap BSS Data Text 0xc0000000 high address low address 0x08048000 Process Layout / 8

Process Layout in Memory Stack grows towards decreasing addresses. is initialized at run-time. Heap grow towards increasing addresses. is initialized at run-time. BSS section size fixed at compile-time. is initialized at run-time. was grouped into Data in CS6C. Data section is initialized at compile-time. Text section holds the program instructions (read-only). Stack dynamic growth Heap BSS Data Text 0xc0000000 high address low address 0x08048000 uninitialized variables initialized variables Process Layout / 8

IA- Caveats Key Differences Between AT&T Syntax and Intel Syntax Parameter Order Parameter Size AT&T Intel src before dst dst before src movl $4, %eax mov eax, 5 Mnemonics suffixed with Derived from name of register a letter indicating size of that is used (e.g. rax, operands: q for qword, l for eax, ax, al imply q, l, w, b, long (dword), w for word, rectively) and b for byte addl $4, % add, 4 Sigils Immediate values prefixed with a $, registers prefixed with a % Assembler automatically detects type of symbols; i.e., whether they are registers, constants or something else [] Adapted from: https://en.wikipedia.org/wiki/x86_assembly_language#syntax IA- / 8

Function Calls void foo(int a, int b, int c) int bar[]; char qux[]; bar[0] = A ; qux[0] = 0x4; int main(void) int i = ; foo(,, ); urn 0; Function Calls 4 / 8

int main(void) int i = ; foo(,, ); urn 0; main: subl $4,% movl $,-4(%ebp) pushl $ pushl $ pushl $ call foo addl $,% xorl %eax,%eax ebp

int main(void) int i = ; foo(,, ); urn 0; main: subl $4,% movl $,-4(%ebp) pushl $ pushl $ pushl $ call foo addl $,% xorl %eax,%eax ebp

int main(void) int i = ; foo(,, ); urn 0; main: subl $4,% movl $,-4(%ebp) pushl $ pushl $ pushl $ call foo addl $,% xorl %eax,%eax + ebp

int main(void) int i = ; foo(,, ); urn 0; main: subl $4,% movl $,-4(%ebp) pushl $ pushl $ pushl $ call foo addl $,% xorl %eax,%eax ebp

int main(void) int i = ; foo(,, ); urn 0; main: subl $4,% movl $,-4(%ebp) pushl $ pushl $ pushl $ call foo addl $,% xorl %eax,%eax ebp

int main(void) int i = ; foo(,, ); urn 0; main: subl $4,% movl $,-4(%ebp) pushl $ pushl $ pushl $ call foo addl $,% xorl %eax,%eax ebp

int main(void) int i = ; foo(,, ); urn 0; main: subl $4,% movl $,-4(%ebp) pushl $ pushl $ pushl $ call foo addl $,% xorl %eax,%eax ebp rip

void foo(int a, int b, int c) int bar[]; char qux[]; bar[0] = A ; qux[0] = 0x4; foo: subl $,% movl $65,-8(%ebp) movb $66,-(%ebp) rip ebp

void foo(int a, int b, int c) int bar[]; char qux[]; bar[0] = A ; qux[0] = 0x4; foo: subl $,% movl $65,-8(%ebp) movb $66,-(%ebp) rip ebp

void foo(int a, int b, int c) int bar[]; char qux[]; bar[0] = A ; qux[0] = 0x4; foo: subl $,% movl $65,-8(%ebp) movb $66,-(%ebp) rip (m) + ebp

void foo(int a, int b, int c) int bar[]; char qux[]; bar[0] = A ; qux[0] = 0x4; foo: subl $,% movl $65,-8(%ebp) movb $66,-(%ebp) rip (m) ebp

void foo(int a, int b, int c) int bar[]; char qux[]; bar[0] = A ; qux[0] = 0x4; foo: subl $,% movl $65,-8(%ebp) movb $66,-(%ebp) rip 00 00 00 4 (m) ebp

void foo(int a, int b, int c) int bar[]; char qux[]; bar[0] = A ; qux[0] = 0x4; foo: subl $,% movl $65,-8(%ebp) movb $66,-(%ebp) rip 00 00 00 4 4 (m) ebp

void foo(int a, int b, int c) int bar[]; char qux[]; bar[0] = A ; qux[0] = 0x4; foo: subl $,% movl $65,-8(%ebp) movb $66,-(%ebp) rip 00 00 00 4 4 (m) + ebp : movl %ebp,% popl %ebp

void foo(int a, int b, int c) int bar[]; char qux[]; bar[0] = A ; qux[0] = 0x4; foo: subl $,% movl $65,-8(%ebp) movb $66,-(%ebp) rip 00 00 00 4 4 ebp : movl %ebp,% popl %ebp

void foo(int a, int b, int c) int bar[]; char qux[]; bar[0] = A ; qux[0] = 0x4; foo: subl $,% movl $65,-8(%ebp) movb $66,-(%ebp) rip 00 00 00 4 4 ebp : popl %eip

int main(void) int i = ; foo(,, ); urn 0; main: subl $4,% movl $,-4(%ebp) pushl $ pushl $ pushl $ call foo addl $,% xorl %eax,%eax ebp rip 00 00 00 4 4

int main(void) int i = ; foo(,, ); urn 0; main: subl $4,% movl $,-4(%ebp) pushl $ pushl $ pushl $ call foo addl $,% xorl %eax,%eax rip 00 00 00 4 4 ebp

int main(void) int i = ; foo(,, ); urn 0; main: subl $4,% movl $,-4(%ebp) pushl $ pushl $ pushl $ call foo addl $,% xorl %eax,%eax rip 00 00 00 4 4 ebp

int main(void) int i = ; foo(,, ); urn 0; main: subl $4,% movl $,-4(%ebp) pushl $ pushl $ pushl $ call foo addl $,% xorl %eax,%eax + ebp rip 00 00 00 4 4

int main(void) int i = ; foo(,, ); urn 0; main: subl $4,% movl $,-4(%ebp) pushl $ pushl $ pushl $ call foo addl $,% xorl %eax,%eax ebp rip 00 00 00 4 4

int main(void) int i = ; foo(,, ); urn 0; main: subl $4,% movl $,-4(%ebp) pushl $ pushl $ pushl $ call foo addl $,% xorl %eax,%eax ebp (rip) rip 00 00 00 4 4

MIPS IA- [Reference] RISC vs CISC IA- has many more instructions IA- instructions are variable length IA- instructions can have implicit arguments and side effects Limited Number of Registers MIPS has 8 general purpose registers ($s0-$s7, $t0-$t9) IA- has 6 (%eax, %edx, %ecx, %ebx, %esi, %edi) This means lots of stack operations! Operand Directions MIPS: mov dst src IA-: mov src dst Memory operations Very common to see push/pop/mov in IA- We ll see more of this later The list goes on! Reference 6 / 8

MIPS IA- [Reference] Registers Use MIPS IA Notes Program Counter PC %eip Can not be referenced directly Stack Pointer $sp % Frame Pointer $fp %ebp Return Address $ra - RA kept on stack in IA- Return Value ( bit) $v0 %eax %eax not used solely for RV Argument Registers $a0-$a - Passed on stack in IA- Zero $0 - Use immediate value on IA- Register Terminology SFP saved frame pointer: saved %ebp on the stack OFP old frame pointer: old %ebp from the previous stack frame RIP urn instruction pointer: urn address on the stack Reference 7 / 8

IA- [Reference] IA Instructions Addressing Modes movl Src,Dest Dest = Src addl Src,Dest Dest = Dest + Src subl Src,Dest Dest = Dest - Src imull Src,Dest Dest = Dest * Src sall Src,Dest Dest = Dest << Src sarl Src,Dest Dest = Dest >> Src shrl Src,Dest Dest = Dest >> Src xorl Src,Dest Dest = Dest ^ Src andl Src,Dest Dest = Dest & Src orl Src,Dest Dest = Dest Src incl Dest Dest = Dest + decl Dest Dest = Dest - negl Dest Dest = - Dest notl Dest Dest = ~ Dest leal Src,Dest Dest = address of Src cmpl Src,Src Sets CCs Src Src testl Src,Src Sets CCs Src & Src jmp label jump je label jump equal jne label jump not equal js label jump negative jns label jump non-negative jg label jump greater (signed) jge label jump greater or equal (signed) jl label jump less (signed) jle label jump less or equal (signed) ja label jump above (unsigned) jb label jump below (unsigned) Immediate $val Val Normal (R) Mem[Reg[R]] Register R specifies memory address movl (%ecx),%eax Displacement D(R) Mem[Reg[R]+D] Register R specifies start of memory region Constant displacement D specifies offset movl 8(%ebp),%edx Indexed D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D] D: Rb: Ri: Constant displacement,, or 4 bytes Base register: Any of 8 integer registers Index register: S: Scale:,, 4, or 8 Condition Codes CF Carry Flag ZF Zero Flag SF Sign Flag OF Overflow Flag %eax %edx %ecx %ebx %esi %edi % %ebp Reference 8 / 8