Credits and Disclaimers

Similar documents
Credits and Disclaimers

Credits and Disclaimers

CS61 Section Solutions 3

System Programming and Computer Architecture (Fall 2009)

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

The Hardware/Software Interface CSE351 Spring 2013

Assembly Language II: Addressing Modes & Control Flow

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

Machine Programming 2: Control flow

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

Process Layout and Function Calls

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

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 II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Credits and Disclaimers

Assembly II: Control Flow

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

Machine-Level Programming II: Control and Arithmetic

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

Machine-Level Programming II: Control Flow

Machine Level Programming II: Arithmetic &Control

Assembly Language: Part 2

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

Credits and Disclaimers

Credits to Randy Bryant & Dave O Hallaron

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Second Part of the Course

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

Control. Young W. Lim Mon. Young W. Lim Control Mon 1 / 16

CS241 Computer Organization Spring Addresses & Pointers

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

Introduction to 8086 Assembly

Homework 0: Given: k-bit exponent, n-bit fraction Find: Exponent E, Significand M, Fraction f, Value V, Bit representation

Assembly Language: IA-32 Instructions

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

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

Chapter 3 Machine-Level Programming II Control Flow

Process Layout, Function Calls, and the Heap

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2016 Lecture 12

Sungkyunkwan University

Machine Level Programming: Control

Giving credit where credit is due

Credits and Disclaimers

CSCI 2021: x86-64 Control Flow

Intel Instruction Set (gas)

Machine- Level Programming II: Arithme6c & Control

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Part 2

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

CS 2505 Computer Organization I

Control flow (1) Condition codes Conditional and unconditional jumps Loops Conditional moves Switch statements

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

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

Credits and Disclaimers

Controlling Program Flow

Machine-Level Programming II: Control

Machine-Level Programming II: Control

CS241 Computer Organization Spring Loops & Arrays

CPS104 Recitation: Assembly Programming

CISC 360. Machine-Level Programming II: Control Flow Sep 23, 2008

Page # CISC 360. Machine-Level Programming II: Control Flow Sep 23, Condition Codes. Setting Condition Codes (cont.)

Instruction Set Architecture

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

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

mith College Computer Science CSC231 Assembly Week #9 Spring 2017 Dominique Thiébaut

x86-64 Programming II

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

CS 33. Machine Programming (2) CS33 Intro to Computer Systems XII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Page 1. Condition Codes CISC 360. Machine-Level Programming II: Control Flow Sep 17, Setting Condition Codes (cont.)

Computer Systems C S Cynthia Lee

An Introduction to x86 ASM

Towards the Hardware"

Introduction to Computer Systems. Exam 1. February 22, Model Solution fp

Program Exploitation Intro

CISC 360. Machine-Level Programming II: Control Flow Sep 17, class06

Assembly Programming III

Basic Assembly Instructions

Handout #2: Machine-Level Programs

Machine- Level Programming II: Arithme c & Control

UW CSE 351, Winter 2013 Midterm Exam

CSE351 Spring 2018, Midterm Exam April 27, 2018

BAHAR DÖNEMİ MİKROİŞLEMCİLER LAB4 FÖYÜ

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

Machine- Level Programming II: Arithme6c & Control

Computer Architecture..Second Year (Sem.2).Lecture(4) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans.

L09: Assembly Programming III. Assembly Programming III. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson

CS367. Program Control

CS429: Computer Organization and Architecture

Introduction to Computer Systems. Exam 1. February 22, This is an open-book exam. Notes are permitted, but not computers.

IFE: Course in Low Level Programing. Lecture 6

Machine- level Programming II: Control Flow

Review addressing modes

x86 Programming II CSE 351 Winter

Instruction Set Architecture

Instruction Set Architecture

How Software Executes

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

Transcription:

Credits and Disclaimers 1 The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 2 nd Edition by Bryant and O'Hallaron /GAS Syntax on WikiBooks (http://en.wikibooks.org/wiki/x86_assembly/gas_syntax) Using Assembly Language in Linux by Phillip?? (http://asm.sourceforge.net/articles/linasm.html) The C code was compiled to assembly with gcc version 4.5.2 on Ubuntu Linux. Unless noted otherwise, the assembly code was generated using the following command line: gcc S m32 O0 file.c AT&T assembly syntax is used, rather than Intel syntax, since that is what the gcc tools use.

Comparing Operands 2 The compare instruction facilitates the comparison of operands: cmpl rightop, leftop The instruction performs a subtraction of its operands, discarding the result. The instruction sets flags in the machine status word register (EFLAGS) that record the results of the comparison: CF carry flag; indicates overflow for unsigned operations OF overflow flag; indicates operation caused 2's complement overflow SF sign flag; indicates operation resulted in a negative value ZF zero flag; indicates operation resulted in zero For our purposes, we will most commonly check these codes by using the various jump instructions.

Conditional Jump Instructions 3 The conditional jump instructions check the relevant EFLAGS flags and jump to the instruction that corresponds to the label if the flag is set: # make jump if last result was: je label # zero jne label # nonzero js label # negative jns label # nonnegative jg label # positive (signed >) jge label # nonnegative (signed >=) jl label # negative (signed <) jle label # nonpositive (signed <=) ja label # above (unsigned >) jae label # above or equal (unsigned >=) jb label # below (unsigned <) jbe label # below or equal (unsigned <=).

C to Assembly: if 4 gcc -S -m32 O0 if.c movl $5, -4(%ebp) # y = 5 cmpl $0, 8(%ebp) # compare x to 0 int y = 5; if ( x >= 0 ) { y++; } js.l2 # goto.l2 if negative addl $1, -4(%ebp) # y++

C to Assembly: if movl $5, -4(%ebp) # y = 5 cmpl $0, 8(%ebp) # compare x to 0 5 js.l2 # goto.l2 if negative addl $1, -4(%ebp) # y++ int y = 5; if ( x < 0 ) goto L2; L2: y++;

C to Assembly: if else int y = 5; 6.L3: if ( x >= 0 ) y++; else y--; movl $5, -4(%ebp) # y = 5 cmpl $0, 8(%ebp) # compare x to 0 js.l2 # goto.l2 if negative addl $1, -4(%ebp) # y++ jmp.l3 # goto.l3 after y++ subl $1, -4(%ebp) # y-- gcc -S -m32 O0 ifelse.c

C to Assembly: if else 7.L3: movl $5, -4(%ebp) # y = 5 cmpl $0, 8(%ebp) # compare x to 0 js.l2 # goto.l2 if negative addl $1, -4(%ebp) # y++ jmp.l3 # goto.l3 after y++ subl $1, -4(%ebp) # y-- int y = 5; if ( x < 0 ) goto L2; y++; goto L3; L2: y--; L3: gcc -S -m32 O0 ifelse.c

C to Assembly: do while int y = 0; 8 movl $0, -4(%ebp) # y = 0 addl $1, -4(%ebp) # y++ do { y++; x--; } while ( x > 0); subl $1, 8(%ebp) # x-- cmpl $0, 8(%ebp) # compare x to 0 jg.l2 # goto.l2 if positive gcc -S -m32 O0 dowhile.c

C to Assembly: do while 9 movl $0, -4(%ebp) # y = 0 addl $1, -4(%ebp) # y++ subl $1, 8(%ebp) # x-- cmpl $0, 8(%ebp) # compare x to 0 jg.l2 # goto.l2 if positive gcc -S -m32 O0 dowhile.c L2: int y = 0; y++; x--; if ( x > 0) goto L2;

C to Assembly: while int y = 0; 10.L3: movl $0, -4(%ebp) # y = 0 while ( x > 0 ) { y++; jmp.l2 # goto compare x to 0 x--; # entry test } addl $1, -4(%ebp) # y++ subl $1, 8(%ebp) # x-- cmpl $0, 8(%ebp) # compare x to 0 jg.l3 # goto loop entry if positive gcc -S -m32 O0 while.c

C to Assembly: while 11.L3: movl $0, -4(%ebp) # y = 0 jmp.l2 # goto compare x to 0 # entry test addl $1, -4(%ebp) # y++ subl $1, 8(%ebp) # x-- cmpl $0, 8(%ebp) # compare x to 0 jg.l3 # goto loop entry if positive Note that the compiler translated the C while loop to a logically-equivalent do-while loop. gcc -S -m32 O0 while.c int y = 0; goto L2; L3: y++; x--; L2: if (x > 0) goto L3;

Reverse Engineering: Assembly to C 12 Let's consider a short assembly function: f:.l3: pushl %ebp movl %esp, %ebp subl $16, %esp movl $1, -4(%ebp) movl $2, -8(%ebp) jmp.l2 movl -4(%ebp), %eax imull -8(%ebp), %eax movl %eax, -4(%ebp) addl $1, -8(%ebp) movl -8(%ebp), %eax cmpl 8(%ebp), %eax jle.l3 movl -4(%ebp), %eax leave ret This is stack setup code; the compiler creates this; it is not represented in C. We're going to reconstruct an equivalent function in C. The first step will be to identify the things that do not translate to C This is cleanup and return code; it corresponds to a return statement in C.

Reverse Engineering: Assembly to C The next step will be to identify variables 13 f:.l3: movl $1, -4(%ebp) movl $2, -8(%ebp) jmp.l2 movl -4(%ebp), %eax imull -8(%ebp), %eax movl %eax, -4(%ebp) addl $1, -8(%ebp) movl -8(%ebp), %eax cmpl 8(%ebp), %eax jle.l3 movl -4(%ebp), %eax We're going to reconstruct an equivalent function in C. The next step will be to identify variables

Reverse Engineering: Assembly to C Variables will be indicated by memory accesses. 14 Filtering out repeat accesses yields these assembly statements: f: movl movl cmpl $1, -4(%ebp) $2, -8(%ebp) 8(%ebp), %eax There's an access to a variable on the stack at ebp-4; this must be a local (auto) variable. Let's call it Local1 There's another access to a variable on the stack at ebp-8; this must also be a local (auto) variable. Let's call it Local2. There's an access to a variable on the stack at ebp+8; this must be a parameter passed to the function. Let's call it Param1.

Reverse Engineering: Assembly to C 15 Now we'll assume the variables are all C ints, and considering that the first two accesses are initialization statements, so far we can say the function in question looks like: f(int Param1) { } int Local1 = 1; int Local2 = 2; And another clue is the statement that stores the value of the variable we're calling Local1 into the register eax right before the function returns. That indicates what's returned and the return type: int f(int Param1) { } int Local1 = 1; int Local2 = 2; return Local1;

Reverse Engineering: Assembly to C 16 Now, there are two jump statements, a comparison statement, and two labels, all of which indicate the presence of a loop f:.l3: jmp.l2 movl -8(%ebp), %eax cmpl 8(%ebp), %eax jle.l3 The first jump is unconditional that looks like a C goto. So, this skips the loop body the first time through The comparison is using the parameter we're calling Param1 (first argument) and we see that the register eax is holding the value of the variable we're calling Local2 (second argument). Moreover, the conditional jump statement that follows the comparison causes a jump back to the label at the top of the loop, if Local2 <= Param1.

Reverse Engineering: Assembly to C What we've just discovered is that there is a while loop: 17 f:.l3: jmp.l2 movl -8(%ebp), %eax cmpl 8(%ebp), %eax jle.l3 int f(int Param1) { } int Local1 = 1; int Local2 = 2; while (Local2 <= Param1) { } return Local1; The final step is to construct the body of the loop, and make sure we haven't missed anything else

Reverse Engineering: Assembly to C Here's what's left, including the loop boundaries for clarity: 18 f:.l3: jmp.l2 movl -4(%ebp), %eax imull -8(%ebp), %eax movl %eax, -4(%ebp) addl $1, -8(%ebp) movl -8(%ebp), %eax cmpl 8(%ebp), %eax jle.l3 eax = Local1 eax = Local1 * Local2 Local1 = eax = Local1 * Local2 Local2 = Local2 + 1 And that will do it

Reverse Engineering: Assembly to C Here's our function: int f(int Param1) { int Local1 = 1; int Local2 = 2; while (Local2 <= Param1) { Local1 = Local1 * Local2; Local2++; } 19 } return Local1; So, what is it computing really?