CPS104 Recitation: Assembly Programming

Similar documents
Program Exploitation Intro

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

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

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

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

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

CS241 Computer Organization Spring 2015 IA

Assembly Language: Function Calls

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

SOEN228, Winter Revision 1.2 Date: October 25,

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls" Goals of this Lecture"

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

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

Process Layout and Function Calls

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

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

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

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

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

Assembly Language: IA-32 Instructions

The Hardware/Software Interface CSE351 Spring 2013

CS61 Section Solutions 3

Practical Malware Analysis

Reverse Engineering Low Level Software. CS5375 Software Reverse Engineering Dr. Jaime C. Acosta

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

CSCI 2121 Computer Organization and Assembly Language PRACTICE QUESTION BANK

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

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

CPSC W Term 2 Problem Set #3 - Solution

Second Part of the Course

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Lab 10: Introduction to x86 Assembly

System Programming and Computer Architecture (Fall 2009)

CSE P 501 Compilers. x86 Lite for Compiler Writers Hal Perkins Autumn /25/ Hal Perkins & UW CSE J-1

16.317: Microprocessor Systems Design I Fall 2014

Machine-Level Programming II: Control Flow

Assembly Language Programming - III

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

Compiler Construction D7011E

Machine-Level Programming II: Control and Arithmetic

Computer Architecture and Assembly Language. Practical Session 3

Lab 3. The Art of Assembly Language (II)

Sistemi Operativi. Lez. 16 Elementi del linguaggio Assembler AT&T

Sungkyunkwan University

Machine Level Programming II: Arithmetic &Control

CSE 351: Week 4. Tom Bergan, TA

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

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

CSE351 Spring 2018, Midterm Exam April 27, 2018

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

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

mith College Computer Science CSC231 Assembly Week #11 Fall 2017 Dominique Thiébaut

Towards the Hardware"

Credits to Randy Bryant & Dave O Hallaron

Process Layout, Function Calls, and the Heap

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

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

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

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

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

Instructor: Alvin R. Lebeck

Instruction Set Architecture

x86 assembly CS449 Fall 2017

Instruction Set Architecture

Instruction Set Architecture

Machine-level Programming (3)

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

UW CSE 351, Winter 2013 Midterm Exam

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

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

X86 Stack Calling Function POV

Assembly Language: Part 2

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

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

An Introduction to x86 ASM

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here

Multiprocessor Solution

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

Machine Programming 2: Control flow

ASSEMBLY III: PROCEDURES. Jo, Heeseung

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

Assembly III: Procedures. Jo, Heeseung

W4118: PC Hardware and x86. Junfeng Yang

Instruction Set Architecture

CISC 360 Instruction Set Architecture

Credits and Disclaimers

16.317: Microprocessor Systems Design I Fall 2015

Do not turn the page until 5:10.

Do not turn the page until 11:30.

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

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

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

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

CS429: Computer Organization and Architecture

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

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

1 if (a == b) 2 { 3 /* true stuff here */ 4 } 5 else 6 { 7 /* false stuff here */ 8 } 10 /* done either way (i.e., whether true.

Transcription:

CPS104 Recitation: Assembly Programming Alexandru Duțu 1

Facts OS kernel and embedded software engineers use assembly for some parts of their code some OSes had their entire GUIs written in assembly in the 80s 2

Outline Assembly basics - review From C to assembly Team exercises 3

x86 instructions data transfer instructions all variants of mov push and pop binary arithmetic instructions add, sub, imul, idiv, inc, dec, neg, cmp logical instructions and, or, xor, nor shift-rotate instruction sar, shr, ror, rol control instructions misc jmp, je/jz, jne/jnz, ja/ jnbe, jg/jnle, jge/jnl, call, ret lea - load effective addres nop - no operation 4

Suffixes b - byte (8 bits) s - short (16 bits int or 32 bits float) w - word (16 bits) l - long (32 bits int or 64 bits float) q - quad (64 bits) t - ten bytes (80 bit floating point) 5

Prefixes % for registers $ for constants 6

Stack instructions push - pushes on the stack at %esp position pop - pops from the stack at %esp position call - function call ret - returns %eax as the result of the function enter, leave - set the base stack pointer before and after function execution 7

Function calls arguments are push-ed on the stack eip is push-ed also ebp (base pointer) is updated with the new esp (stack pointer) code of the function is executed ebp is restored to its previous value ret ends the function call (returns %eax) 8

Recommended framework for assembly C file that has main() sets the input calls asm function assembly file (.S) that: contains functions in assembly 9

Example /* main.c */ #include <stdio.h> extern int sum(int x, int y); int main(int argc, const char *argv[]) { int x, y, s, d, e; scanf("%d", &x); scanf("%d", &y); } s = sum(x, y); printf("sum: %d\n", s); return 0; # func.s.text.globl sum sum: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax movl 12(%ebp), %ebx addl %ebx, %eax leave ret # save old base pointer # set new base pointer # get first argument # get second argument # sum y with x # return value in %eax 10

Exercise using the framework presented above, create one assembly function that takes 4 arguments x, y, z, w and computes x*y + z*w 11

/* main.c */ #include <stdio.h> Solution # func.s.text.globl arithmetic extern int arithmetic(int x, int y, int w, int z); int main() { int x, y, z, w, e; scanf("%d", &x); scanf("%d", &y); scanf("%d", &z); scanf("%d", &w); arithmetic: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax movl 12(%ebp), %ebx imull %ebx, %eax movl 16(%ebp), %ecx movl 20(%ebp), %edx # save old base pointer # set new base pointer # get first argument # get second argument # multiply z with w # get third argument # get forth argument } s = arithmetic(x, y, w, z); printf("sum: %d\n", s); return 0; imull %edx, %ecx addl %ecx, %eax leave ret # multiply x and y # final result # return value in %eax 12

Compilation and debug :!gcc -g -m32 -o arithmetic main.c func.s :!./<name_of_programm> -m32 flag is for compiling in 32 bit mode :!gdb arithmetic list main - get line number of main function break <line number of main> - put a breakpoint therer run, next and step - run p $eax ($ebx, $ecx...) - print registers 13

if statement compare and jump instructions compare instructions cmpl jump instructions (branches) jmp, je, jne, jg, jng, jl, etc. if: comparison... jg jge... if_else #then body... jmp if_end if_else: #else body... if_end: 14

e.g.:.text.globl sum exp: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax if: movl $1, %ebx cmpl %ebx, %eax jg if_greater movl %eax, %ebx imull %eax, %eax addl $1, %eax imull $2, %ebx subl %ebx, %eax jmp if_end if_greater: imull %eax, %eax subl $1, %eax if_end: leave ret # save old base pointer # set new base pointer # get first argument Exercise: Write down the C code for the following example. /* solution */ int expr(int x){ if (x > 1) return (x*x - 1); else return (x*x + 1-2*x); } 15

while statement still compare and jump instructions label:... # while body cmp...,... je jne... label 16

Function that computes the linear combination of vectors a and b e.g.:.text.globl linear_comb linear_comb: pushl %ebp # save old base pointer movl %esp, %ebp # set new base pointer movl 8(%ebp), %edx # get dimension leal a, %ebx # pa = &a leal b, %ecx # pb = &b leal (%ebx,%edx,4),%esi movl $0, %eax while: movl (%ebx), %edi imull (%ecx), %edi addl %edi, %eax addl $4, %ebx # pa ++ addl $4, %ecx # pb ++ cmpl %esi, %ebx jl while leave ret.data a:.long 3, 1, 2, 9, 5, 1, 4, 6, 5 b:.long 1, 2, 7, 12, 5, 9, 4, 6, 3 17

Team Exercise write an function in assembly that takes one argument x and computes the following function: f(x) = 25*x - 75, if x >= 3 f(x) = 2*x + 1, if x < 3 18