Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

Similar documents
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

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Assembly I: Basic Operations. Jo, Heeseung

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

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Assembly Language: Function Calls

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

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

Assembly Language: Function Calls" Goals of this Lecture"

CS241 Computer Organization Spring 2015 IA

Assembly Language: Function Calls" Goals of this Lecture"

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

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

Machine Programming 1: Introduction

Process Layout and Function Calls

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

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

Sungkyunkwan University

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

CMSC 313 Lecture 12 [draft] How C functions pass parameters

Process Layout, Function Calls, and the Heap

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

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

Machine-Level Programming Introduction

COMP 210 Example Question Exam 2 (Solutions at the bottom)

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

Systems I. Machine-Level Programming I: Introduction

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

Ge-ng at things on the chip you can t easily reach from C

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

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

Instruction Set Architecture

Instruction Set Architecture

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

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

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

X86 Assembly -Data II:1

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

Machine- level Programming

Machine-Level Programming II: Control and Arithmetic

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

The Hardware/Software Interface CSE351 Spring 2015

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

CPS104 Recitation: Assembly Programming

Instruction Set Architecture

CISC 360 Instruction Set Architecture

CPEG421/621 Tutorial

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

Compiler Construction D7011E

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

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

CSE 351: Week 4. Tom Bergan, TA

W4118: PC Hardware and x86. Junfeng Yang

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

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

Second Part of the Course

X86 Stack Calling Function POV

CS241 Computer Organization Spring Addresses & Pointers

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

Machine-level Programming (3)

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

Credits to Randy Bryant & Dave O Hallaron

CS , Fall 2001 Exam 1

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

Machine Language, Assemblers and Linkers"

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

Machine-Level Programming Introduction

Chapter 3 Machine-Level Programming II Control Flow

Credits and Disclaimers

Machine Level Programming I: Basics

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

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

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Program Exploitation Intro

x86 assembly CS449 Fall 2017

Buffer Overflow Attack

Carnegie Mellon. 5 th Lecture, Jan. 31, Instructors: Todd C. Mowry & Anthony Rowe

CS642: Computer Security

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

Instruction Set Architectures

CS241 Computer Organization Spring Loops & Arrays

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

CS213. Machine-Level Programming III: Procedures

CPSC W Term 2 Problem Set #3 - Solution

Machine-level Representation of Programs. 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

CS 3843 Final Exam Fall 2012

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

Instruction Set Architecture

Instruction Set Architectures

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

CSCI 192 Engineering Programming 2. Assembly Language

Transcription:

Homework In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, 361-367 Practice Exam 1 1

In-line Assembly Code The gcc compiler allows you to put assembly instructions in-line between your C statements This is a lot trickier way to integrate assembly code with C than writing C callable functions You need to know a lot more about how the compiler locates variables, uses registers, and manages the stack. Does execute faster than a function call to the equivalent amount of assembly code 2

In-line Assembly Code Example Function with in-line assembly code int foobar(int x, int *y) { int i, *j; asm("pushl %eax"); i = x; j = &i; *y = *j; asm("popl %eax"); return 0; } 3

In-line Assembly Code Resulting.s file at entry point: _foobar: pushl %ebp movl %esp,%ebp subl $8,%esp # space for automatic variables pushl %ebx # will use %ebx for pointers #APP pushl %eax #NO_APP 4

In-Line Assembly Code State of the Stack at maximum depth: %esp %ebp %eax %ebx j i %ebp %eip x y Automatic Variables (negative offsets from %ebp) Argument Variables (positive offsets from %ebp) 5

In-line Assembly Code Body of function logic movl 8(%ebp),%eax # i = x; movl %eax,-4(%ebp) leal -4(%ebp),%ebx movl %ebx,-8(%ebp) movl 12(%ebp),%eax movl -8(%ebp),%edx movl (%edx),%ecx movl %ecx,(%eax) # j = &i; # *y = *j; 6

In-line Assembly Code Resulting.s file at return point: #APP popl %eax #NO_APP xorl %eax,%eax # clear %eax for return 0; jmp L1.align 2,0x90 L1: movl -12(%ebp),%ebx leave # translates to instructions below # movl %ebp, %esp # popl %ebp ret 7

Machine Language Lets look at our disassembly (i386-objdump) of tiny.lnx u18(8)% disas --full tiny.lnx tiny.lnx: file format a.out-i386-linux Contents of section.text: 0000 b8080000 0083c003 a3000200 00cc9090... Contents of section.data: Disassembly of section.text: 00000000 <tiny.opc-100100> movl $0x8,%eax 00000005 <tiny.opc-1000fb> addl $0x3,%eax 00000008 <tiny.opc-1000f8> movl %eax,0x200 0000000d <tiny.opc-1000f3> int3 0000000e <tiny.opc-1000f2> nop 0000000f <tiny.opc-1000f1> Address 0x10 is out of bounds 8

Machine Language Another way to show the same data (used to be tiny.lis file) # comments from the source code 00000000 b8 08 00 00 00 movl $0x8,%eax # comments 00000005 83 c0 03 addl $0x3,%eax # comments 00000008 a3 00 02 00 00 movl %eax,0x200 # comments 0000000d cc int3 # comments 0000000e 90 nop (filler) 0000000f 90 nop (filler) How do we understand the hex code values? We need to understand the machine language coding rules! Built up using various required and optional binary fields Each field has a unique location, size in bits, and meaning for code values 9

Machine Language The i386 byte by byte binary instruction format is shown in the figure with fields: Optional instruction prefix Operation code (op code) Optional Modifier Optional Data Elements Modifiers Instruction Prefixes Opcode ModR/M SIB Displacement Data Elements 0-4 Bytes 1-3 Bytes 0-1 Bytes 0-1 Bytes 0-4 Bytes 0-4 Bytes 10

Machine Language Binary Machine Coding for Some Sample Instructions Opcode ModR/M Data Total movl reg, reg 10001001 11sssddd none 2 movl idata, reg 10111ddd idata 5 addl reg, reg 00000001 11sssddd none 2 addl idata, reg 10000001 11000ddd* idata 6 subl reg, reg 00101001 11sssddd none 2 subl idata, reg 10000001 11101ddd* idata 6 incl reg 01000ddd 1 decl reg 01001ddd 1 11

Machine Language ModR/M 3-Bit Register Codes (for sss or ddd) %eax 000 %esp 100 %ecx 001 %ebp 101 %edx 010 %esi 110 %ebx 011 %edi 111 * Optimization: For some instructions involving %eax, there is a shorter machine code available (hence prefer %eax) 12

Machine Language Examples from tiny.lnx: b8 08 00 00 00 movl $0x8,%eax b8 = 1011 1ddd with ddd = 000 for %eax 08 00 00 00 = immediate data for 0x00000008 83 c0 03 addl $0x3,%eax (See Note) 83 = opcode c0 = 11000ddd with ddd = 000 for %eax 03 = short version of immediate data value Note: Shorter than 81 cx 03 00 00 00 (x!= 0) optimized 13

Machine Language Why should you care about machine language? Generating optimal code for performance!! Example: b8 00 00 00 00 movl $0, %eax # clear %eax Generates five bytes 31 c0 xorl %eax, %eax # clear %eax Generates two bytes Two bytes uses less program memory and is faster to fetch and execute than five bytes!! 14

void exchange(int *x, int *y) /* A C callable assembly language function to exchange two int values via pointers. The function prototype in C is: extern void exchange(int *x, int *y); The function uses the pointer arguments to exchange the values of the two int variables x and y. You can write your assembly code with or (more efficiently) without a stack frame as you wish. */ // Here is a C version of the exchange function for reference: void exchange(int *x, int *y) { int dummy = *x; // three way move *x = *y; *y = dummy; } 15

void exchange(int *x, int *y) _exchange: push %ebp movl %esp, %ebp subl $4, %esp # set up stack frame # allocate dummy automatic variable for 3 way move movl 8(%ebp), %ecx # get argument (pointer to x) movl 12(%ebp), %edx # get argument (pointer to y) movl (%ecx), %eax movl %eax, -4(%ebp) movl (%edx), %eax movl %eax, (%ecx) movl -4(%ebp), %eax movl %eax, (%edx) # three way move movl %ebp, %esp popl %ebp ret # remove auto variable from stack # restore previous stack frame # void return - so %eax is immaterial 16

void exchange(int *x, int *y) /* Without a stack frame using a register variable (%ebx) instead of an automatic variable for three way move: */ _exchange: pushl %ebx # save ebx to use for 3 way move movl 8(%esp), %ecx # get argument (pointer to x) movl 12(%esp), %edx # get argument (pointer to y) movl (%ecx), %ebx movl (%edx), %eax movl %eax, (%ecx) movl %ebx, (%edx) # three way move popl %ebx ret # restore ebx # void return - so %eax is immaterial 17

void exchange(int *x, int *y) /* Without a stack frame and the wizard s version of the three way move: (Saves 4 bytes on the stack and 2 bytes of program memory.) */ _exchange: movl 4(%esp), %ecx # get argument (pointer to x) movl 8(%esp), %edx # get argument (pointer to y) # three way move # x y %eax Data Bus movl (%ecx), %eax # x y x 1R xorl (%edx), %eax # x y x^y 1R xorl %eax, (%ecx) # x^x^y(=y) y x^y 1W xorl %eax, (%edx) # y x^y^y(=x) x^y 1W ret # void return - so %eax is immaterial 18

void exchange(int *x, int *y) Normal 3 way move w/o a stack frame: Contents of section.text: 0000 538b4c24 088b5424 0c8b198b 02890189 S.L$..T$... 0010 1a5bc390 c3 = ret 90 = nop (filler).[.. Wizard s version: Contents of section.text: 0000 8b4c2404 8b542408 8b013302 31013102.L$..T$...3.1.1. 0010 c3909090 c3 = ret 90 = nop (filler)... 19

void exchange(int *x, int *y) Two Students previously gave a thinking out of the box version: (Same as the wizard s version for program memory usage, but uses 8 more bytes on the stack and 4 more bus cycles to memory) _exchange: movl 4(%esp), %ecx # get argument (pointer to x) movl 8(%esp), %edx # get argument (pointer to y) # three way move Data Bus Cycles pushl (%ecx) # 1R + 1W pushl (%edx) # 1R + 1W popl (%ecx) # 1R + 1W popl (%edx) # 1R + 1W ret # void return - so %eax is immaterial 20