why we shouldn t use assembly compilers generate pre8y fast, efficient code tedious, easy to screw up not portable

Size: px
Start display at page:

Download "why we shouldn t use assembly compilers generate pre8y fast, efficient code tedious, easy to screw up not portable"

Transcription

1 chapter 3 part 1 1

2 why we shouldn t use assembly compilers generate pre8y fast, efficient code tedious, easy to screw up not portable 2

3 why you shouldn t use assembly and for that ma8er, why for a lot of things you shouldn t be using C either Corbató's Law: "The number of lines of code a programmer can write in a fixed period of 9me is the same independent of the language used."

4 why we re doing assembly here learn how the machine works possibly faster code. one scenario: write in C profiler tweak the assembly for parts the profiler shows what if no compiler? processor features not easily accessed by higher- level language 4

5 the point we re not trying to prepare you for a job doing assembly programming it s about learning how computers work 5

6 how look at the assembly generated by GCC write some of our own assembly from scratch 6

7 7

8 warning: GCC and Cygwin no difference up unpl now be8er use GCC on Linux with this stuff 8

9 instrucpon set set of of operapons that a processor supports examples load x bytes from this address into register y add what s in register i to what s in register j primipve stuff usually takes lots of these primipve ops to do something really useful 9

10 Other instrucpon sets IA32, Intel64 (x86-64 or x64), IA64 SPARC ARM PowerPC MIPS Alpha JVM we re using: IA32 not easiest, but popular focusing on GCC output on Linux 10

11 some terminology x86 name for the chips IA32 the name of the instrucpon set IA = Intel Architecture note difference between: architecture: what you need to know to program assembly - - instrucpon set, registers microarchitecture: implementa9on e.g., IA32 on non- Intel chips (e.g. AMD) 11

12 GAS assembler used by GCC differences with NASM AT&T syntax be careful when reading the Intel manuals operand order! 12

13 Looking GCC assembly output gcc S another possibly helpful switch: - fverbose- asm 13

14 some tools compiler GCC assembler as aka gas linker ld debugger gdb disassembler - objdump profiler - gprof 14

15 Some History: Why should we care? Important things to take away from the history lesson in the chapter: Moore s law EvoluPon of register names Backward compatability: Goal: run progs compiled for earlier versions of chip But: old baggage support features that new OS, compilers rarely use 15

16 Some History name date transistors notes K 16-bit processor. DOS. 1MB address space. DOS allows 640K K Windows K 32-bit registers. Flat addressing. Can run a Unix OS M Pentium M /MMX M instructions helpful for multimedia processing PentiumPro M conditional move instructions Pentium III M Pentium IV M Core 2 Duo M i M 16

17 In parallel. IA64 chips. name date transistors Itanium M Itanium M Itanium 2 Dual-Core B 17

18 from wikipedia 18

19 famous quote If automobiles had followed the same development cycle as the computer, a Rolls- Royce would today cost $100, get a million miles per gallon, and explode once a year, killing everyone inside. Robert X. Cringely 19

20 From Intel (h8p://download.intel.com/pressroom/kits/corei7/images/nehalem_die_callout.jpg) 20

21 aside: goals then and now big goal then: cram as much processing power on a chip possible big goal now: cram as much processing power on a chip possible BUT don t use so much power some environments: keep the chip small think cell phone 21

22 How bad is your electric bill? Company Servers Electricity Cost ebay 16K MWh $3.7M Akamai 40K MWh $10M Rackspace 50K MWh $12M Microsoft >200K > MWh > 36M Google >500K > MWh >38M USA (2006) 10.9M MWh $4.5B MIT campus MWh $62M from, CuIng the Electric Bill for Internet- Scale Systems, Qureshi et al, CCR

23 8086 Register Example general purpose register 8 general purpose 23

24 386 registers 24

25 64- bit general purpose registers 25

26 26

27 Registers: General purpose PC (EIP IA32, RIP x86-64) CondiPon codes 27

28 32- bit Register File 28

29 general purpose registers register EAX EBX ECX EDX ESI EDI ESP EBP common use accumulator, return pointer to items in data segment loop control I/O pointer src ptr for string ops dst ptr for string ops stack pointer base pointer 29

30 64- bit Register File 30

31 but wait, there s more in x

32 data types in Intel- speak name byte word doubleword quadword double quadword size 1 byte 2 bytes 4 bytes 8 bytes 16 bytes so a word isn t a word? 32

33 simplest funcpon ever 1 int sum() 2 { 3 int x=30; 4 int y=57; 5 int z=39; 6 7 return x+y+z; 8 } 33

34 gcc output of sum funcpon 1.text 2.globl _sum 3 _sum: 4 pushl %ebp # 5 movl %esp, %ebp #, 6 subl $24, %esp #, 7 movl $30, -20(%ebp) #, x 8 movl $57, -16(%ebp) #, y 9 movl $39, -12(%ebp) #, z 10 movl -16(%ebp), %eax # y, y 11 addl -20(%ebp), %eax # x, D addl -12(%ebp), %eax # z, D leave 14 ret 34

35 simplest funcpon ever 1 int sum() 2 { 3 int x=30; 4 int y=57; 5 int z=39; 6 7 return x+y+z; 8 } 1.text 2.globl _sum 3 _sum: 4 pushl %ebp 5 movl %esp, %ebp 6 subl $24, %esp 7 movl $30, -20(%ebp) 8 movl $57, -16(%ebp) 9 movl $39, -12(%ebp) 10 movl -16(%ebp), %eax 11 addl -20(%ebp), %eax 12 addl -12(%ebp), %eax 13 leave 14 ret 35

36 let s try a full program 1 /* file summain.c */ 2 int main(void) 3 { 4 int x=30; 5 int y=57; 6 int z=39; 7 8 return x+y+z; 9 } 36

37 its assembly output 1 /* file summain.s */ 2.text 3.globl _main 4 _main: 5 pushl %ebp 6 movl %esp, %ebp 7 subl $24, %esp 8 movl $30, -20(%ebp) 9 movl $57, -16(%ebp) 10 movl $39, -12(%ebp) 11 movl -16(%ebp), %eax 12 addl -20(%ebp), %eax 13 addl -12(%ebp), %eax 14 leave 15 ret 16.subsections_via_symbols 37

38 same code on SPARC 1.section ".text" 2.align 4 3.global main 4.type main,#function 5.proc 04 6 main: 7!#PROLOGUE# 0 8 save %sp, -128, %sp 9!#PROLOGUE# 1 10 mov 30, %o0 11 st %o0, [%fp-20] 12 mov 57, %o0 13 st %o0, [%fp-24] 14 mov 39, %o0 15 st %o0, [%fp-28] 16 ld [%fp-20], %o0 17 ld [%fp-24], %o1 18 add %o0, %o1, %o0 19 ld [%fp-28], %o1 20 add %o0, %o1, %o0 21 mov %o0, %i0 22 b.ll2 23 nop 24.LL2: 25 ret 26 restore 27.LLfe1: 28.size main,.llfe1-main 29.ident "GCC: (GNU) (release)" 38

39 most of the same on ARM 1 sum: args = 0, pretend = 0, frame = 16 frame_needed = 1, uses_anonymous_args = 0 link register save eliminated. 5 push {r7} 6 sub sp, sp, #20 7 add r7, sp, #0 8 mov r3, #30 9 str r3, [r7, #12] 10 mov r3, #57 11 str r3, [r7, #8] 12 mov r3, #39 13 str r3, [r7, #4] 14 ldr r2, [r7, #12] 15 ldr r3, [r7, #8] 16 adds r2, r2, r3 17 ldr r3, [r7, #4] 18 adds r3, r2, r3 19 mov r0, r3 20 add r7, r7, #20 21 mov sp, r7 22 pop {r7} 23 bx lr 39

40 what to do with it assembling: as - o summain.o summain.s linking: ld - o sum summain.o 40

41 What s going on Assembler - -.s to.o binary encoding of each instrucpon connecpons missing for code in different files Linker references between files stapc linker copies library code into binary dynamic linker linkage when program is run 41

42 42

43 understand what s in each 43

44 opposite direcpon? 44

45 disassembly objdump d filename produces assembly from binary file works for.o file or full executable 45

46 1 /* file AnotherSimpleSum.s */ 2.section.text 3.globl _start 4 _start: 5 /* pushl %ebp */ 6 /* movl %esp, %ebp */ 7 movl $14, %ebx 8 addl $29, %ebx 9 addl $10, %ebx movl $1, %eax 12 int $0x80 1 sum: file format elf32-i386 2 disassembly example.s file disassembled binary 3 4 Disassembly of section.text: <_start>: : bb 0e mov $0xe,%ebx : 83 c3 1d add $0x1d,%ebx c: 83 c3 0a add $0xa,%ebx f: b mov $0x1,%eax : cd 80 int $0x80 46

47 another look at disassembled binary 1 sum: file format elf32-i Disassembly of section.text: <_start>: : bb 0e mov $0xe,%ebx : 83 c3 1d add $0x1d,%ebx c: 83 c3 0a add $0xa,%ebx f: b mov $0x1,%eax : cd 80 int $0x80 let addresses of the instrucpons center opcode + operands some instrucpons longer than others: more frequently used instrucpons shorter opcodes some operapons take more operands 47

48 what can we disassemble? any executable disassembler interprets bytes as assembly src no source code required 48

49 aside: can do this in GDB too disas disassembles current funcpon disas sum disassemble the sum funcpon disas addr disassemble funcpon at addr disas ad 1 ad 2 disas between addr ad 1, ad 2 49

50 very excipng program 1 #include <unistd.h> 2 3 int main(int argc, char **argv) 4 { 5 _exit(17); 6 } 50

51 very excipng program 1 #include <unistd.h> 2 3 int main(int argc, char **argv) 4 { 5 _exit(17); 6 } can see the exit value in the shell by doing: echo $? 51

52 assembly equivalent 1 /* file exitonly.s 2 only slightly modified from Programming from the Ground Up. 3 */ 4.section.data 5.section.text 6.globl _start 7 _start: 8 movl $1, %eax /* this is the linux kernel command */ 9 /* number (system call) for exiting */ 10 /* a program */ 11 movl $0, %ebx /* this is the status number we will */ 12 /* return to the operating system. */ 13 /* Change this around and it will */ 14 /* return different things to */ 15 /* echo $? */ 16 int $0x80 /* this wakes up the kernel to run */ 17 /* the exit command */ 52

53 syscalls how we get services from the OS important idea more next semester 53

54 what happens during a syscall? 1. interrupt pin goes high during current instrucpon 2. control to interrupt handler 3. interrupt handler runs 4. handler returns to next instrucpon 54

55 what are the syscalls in my OS? in Linux, man syscalls seems to work on the Fedora boxes in the lab but not my Ubuntu machine unistd.h lists them: #define NR_restart_syscall 0 #define NR_exit 1 #define NR_fork 2 #define NR_read 3 #define NR_write 4 #define NR_open 5 #define NR_close 6 #define NR_waitpid 7 #define NR_creat 8 #define NR_link 9 #define NR_unlink 10 #define NR_execve 11 #define NR_chdir 12 #define NR_time 13 #define NR_mknod 14 #define NR_chmod

56 Another syscall example. 1 #include <stdio.h> 2 3 void print_question_printf(); 4 5 int main(void) 6 { 7 print_question_printf(); 8 return 0; 9 } void print_question_printf() { 12 char *str = "Who lives in a pineapple under the sea?\n"; printf("%s", str); 15 } 56

57 same thing using write system call 1 #include <unistd.h> 2 #include <string.h> 3 4 void print_question_write(); 5 6 int main(void) 7 { 8 print_question_write(); 9 return 0; 10 } void print_question_write() { 13 char *str = "Who lives in a pineapple under the sea?\n"; write(stdout_fileno, str, 40); 16 } 57

58 Almost the same 1.equ STDOUT, 1 2.equ MSG_LEN, 40 3.equ WRITE_SYSCALL, section.data 6 str: 7.ascii "Who lives in a pineapple under the sea?\n" 8 9.section.text 10.globl _start 11 _start: 12 /* write */ 13 movl $WRITE_SYSCALL, %eax 14 movl $STDOUT, %ebx 15 movl $str, %ecx 16 movl $MSG_LEN, %edx 17 int $0x /* exit */ 20 movl $1, %eax 21 movl $0, %ebx 22 int $0x80 58

59 C Library and system calls What s the relaponship between the funcpons in the C Library and system calls exported by an OS? Tool: strace strace program runs program and prints the system calls executed 59

60 strace on the write version execve("./print_question_write", ["./print_question_write"], [/* 39 vars */]) = 0 brk(0) = 0xcc write(1, "Who lives in a pineapple under t"..., 40Who lives in a pineapple under t ) =

61 strace on the prinx version execve("./print_question_printf", ["./print_question_printf"], [/* 39 vars */]) = brk(0) = 0x1d write(1, "Who lives in a pineapple under t"..., 40Who lives in a pineapple under t ) = 40 61

62 So what does strace tell us? What does strace tell us about the relaponship between the C library funcpon calls and syscalls provided by Linux (or any other OS)? 62

63 binary compapbility Linux runs on my Intel desktop Windows runs on my Intel desktop Why can t I take my Windows binaries and run them on Linux and vice versa? 63

64 for now: old program as template 1 /* file exitonly.s 2 only slightly modified from Programming from the Ground Up. 3 */ 4.section.data 5.section.text 6.globl _start 7 _start: 8 movl $1, %eax /* this is the linux kernel command */ 9 /* number (system call) for exiting */ 10 /* a program */ 11 movl $0, %ebx /* this is the status number we will */ 12 /* return to the operating system. */ 13 /* Change this around and it will */ 14 /* return different things to */ 15 /* echo $? */ 16 int $0x80 /* this wakes up the kernel to run */ 17 /* the exit command */ 64

65 another simple program 1.section.data 2.section.text 3.globl _start 4 _start: 5 movl $20, %ebx /* %ebx=20 */ 6 addl $30, %ebx /* %ebx+=30 */ 7 movl $1, %eax /* exit syscall number */ 8 int $0x80 /* this wakes up the kernel to run */ 9 /* the exit command */ 65

Instruction Set Architectures

Instruction Set Architectures Instruction Set Architectures ISAs Brief history of processors and architectures C, assembly, machine code Assembly basics: registers, operands, move instructions 1 What should the HW/SW interface contain?

More information

Instruction Set Architectures

Instruction Set Architectures Instruction Set Architectures! ISAs! Brief history of processors and architectures! C, assembly, machine code! Assembly basics: registers, operands, move instructions 1 What should the HW/SW interface

More information

System calls and assembler

System calls and assembler System calls and assembler Michal Sojka sojkam1@fel.cvut.cz ČVUT, FEL License: CC-BY-SA 4.0 System calls (repetition from lectures) A way for normal applications to invoke operating system (OS) kernel's

More information

Systems I. Machine-Level Programming I: Introduction

Systems I. Machine-Level Programming I: Introduction Systems I Machine-Level Programming I: Introduction Topics Assembly Programmerʼs Execution Model Accessing Information Registers IA32 Processors Totally Dominate General Purpose CPU Market Evolutionary

More information

x86 assembly CS449 Fall 2017

x86 assembly CS449 Fall 2017 x86 assembly CS449 Fall 2017 x86 is a CISC CISC (Complex Instruction Set Computer) e.g. x86 Hundreds of (complex) instructions Only a handful of registers RISC (Reduced Instruction Set Computer) e.g. MIPS

More information

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION Today: Machine Programming I: Basics History of Intel processors and architectures C, assembly, machine code Assembly Basics:

More information

Program Exploitation Intro

Program Exploitation Intro Program Exploitation Intro x86 Assembly 04//2018 Security 1 Univeristà Ca Foscari, Venezia What is Program Exploitation "Making a program do something unexpected and not planned" The right bugs can be

More information

W4118: PC Hardware and x86. Junfeng Yang

W4118: PC Hardware and x86. Junfeng Yang W4118: PC Hardware and x86 Junfeng Yang A PC How to make it do something useful? 2 Outline PC organization x86 instruction set gcc calling conventions PC emulation 3 PC board 4 PC organization One or more

More information

CNIT 127: Exploit Development. Ch 3: Shellcode. Updated

CNIT 127: Exploit Development. Ch 3: Shellcode. Updated CNIT 127: Exploit Development Ch 3: Shellcode Updated 1-30-17 Topics Protection rings Syscalls Shellcode nasm Assembler ld GNU Linker objdump to see contents of object files strace System Call Tracer Removing

More information

Credits and Disclaimers

Credits and Disclaimers 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

More information

CPEG421/621 Tutorial

CPEG421/621 Tutorial CPEG421/621 Tutorial Compiler data representation system call interface calling convention Assembler object file format object code model Linker program initialization exception handling relocation model

More information

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

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017 CS 31: Intro to Systems ISAs and Assembly Martin Gagné Swarthmore College February 7, 2017 ANNOUNCEMENT All labs will meet in SCI 252 (the robot lab) tomorrow. Overview How to directly interact with hardware

More information

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015 CS165 Computer Security Understanding low-level program execution Oct 1 st, 2015 A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns

More information

Today: Machine Programming I: Basics

Today: Machine Programming I: Basics Today: Machine Programming I: Basics History of Intel processors and architectures C, assembly, machine code Assembly Basics: Registers, operands, move Intro to x86-64 1 Intel x86 Processors Totally dominate

More information

Università Ca Foscari Venezia

Università Ca Foscari Venezia Stack Overflow Security 1 2018-19 Università Ca Foscari Venezia www.dais.unive.it/~focardi secgroup.dais.unive.it Introduction Buffer overflow is due to careless programming in unsafe languages like C

More information

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

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1 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

More information

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

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson X86 Review Process Layout, ISA, etc. CS642: Computer Security Drew Davidson davidson@cs.wisc.edu From Last Time ACL-based permissions (UNIX style) Read, Write, execute can be restricted on users and groups

More information

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

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013 CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013 CONST POINTERS CONST POINTERS 4 ways to declare pointers in combination with const:!! int *ptr! const int *ptr!

More information

Compiler Construction D7011E

Compiler Construction D7011E Compiler Construction D7011E Lecture 8: Introduction to code generation Viktor Leijon Slides largely by Johan Nordlander with material generously provided by Mark P. Jones. 1 What is a Compiler? Compilers

More information

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

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction E I P CPU isters Condition Codes Addresses Data Instructions Memory Object Code Program Data OS Data Topics Assembly Programmer

More information

MACHINE-LEVEL PROGRAMMING I: BASICS

MACHINE-LEVEL PROGRAMMING I: BASICS MACHINE-LEVEL PROGRAMMING I: BASICS CS 429H: SYSTEMS I Instructor: Emmett Witchel Today: Machine Programming I: Basics History of Intel processors and architectures C, assembly, machine code Assembly Basics:

More information

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

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) Function Calls COS 217 Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) 1 Goals of Today s Lecture Finishing introduction to assembly language o EFLAGS register

More information

x86 assembly CS449 Spring 2016

x86 assembly CS449 Spring 2016 x86 assembly CS449 Spring 2016 CISC vs. RISC CISC [Complex instruction set Computing] - larger, more feature-rich instruction set (more operations, addressing modes, etc.). slower clock speeds. fewer general

More information

Practical Malware Analysis

Practical Malware Analysis Practical Malware Analysis Ch 4: A Crash Course in x86 Disassembly Revised 1-16-7 Basic Techniques Basic static analysis Looks at malware from the outside Basic dynamic analysis Only shows you how the

More information

Lecture 2 Assembly Language

Lecture 2 Assembly Language Lecture 2 Assembly Language Computer and Network Security 9th of October 2017 Computer Science and Engineering Department CSE Dep, ACS, UPB Lecture 2, Assembly Language 1/37 Recap: Explorations Tools assembly

More information

Machine-level Representation of Programs. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Machine-level Representation of Programs. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Machine-level Representation of Programs Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Program? 짬뽕라면 준비시간 :10 분, 조리시간 :10 분 재료라면 1개, 스프 1봉지, 오징어

More information

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

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions? administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions? exam on Wednesday today s material not on the exam 1 Assembly Assembly is programming

More information

Assembly Language: Function Calls

Assembly Language: Function Calls Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and returning Passing parameters Storing local variables Handling registers without interference

More information

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

CMSC 313 Lecture 12 [draft] How C functions pass parameters CMSC 313 Lecture 12 [draft] How C functions pass parameters UMBC, CMSC313, Richard Chang Last Time Stack Instructions: PUSH, POP PUSH adds an item to the top of the stack POP removes an

More information

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

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation Compiler Construction SMD163 Lecture 8: Introduction to code generation Viktor Leijon & Peter Jonsson with slides by Johan Nordlander Contains material generously provided by Mark P. Jones What is a Compiler?

More information

CSC 405 Computer Security Shellcode

CSC 405 Computer Security Shellcode CSC 405 Computer Security Shellcode Alexandros Kapravelos akaprav@ncsu.edu Attack plan Attack code Vulnerable code xor ebx, ebx xor eax, eax mov ebx,edi mov eax,edx sub eax,0x388 Vulnerable code xor ebx,

More information

Lab 10: Introduction to x86 Assembly

Lab 10: Introduction to x86 Assembly CS342 Computer Security Handout # 8 Prof. Lyn Turbak Wednesday, Nov. 07, 2012 Wellesley College Revised Nov. 09, 2012 Lab 10: Introduction to x86 Assembly Revisions: Nov. 9 The sos O3.s file on p. 10 was

More information

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls Goals of this Lecture Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and returning" Passing parameters" Storing local variables" Handling registers without interference"

More information

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006 C Compilation Model Comp-206 : Introduction to Software Systems Lecture 9 Alexandre Denault Computer Science McGill University Fall 2006 Midterm Date: Thursday, October 19th, 2006 Time: from 16h00 to 17h30

More information

CS Bootcamp x86-64 Autumn 2015

CS Bootcamp x86-64 Autumn 2015 The x86-64 instruction set architecture (ISA) is used by most laptop and desktop processors. We will be embedding assembly into some of our C++ code to explore programming in assembly language. Depending

More information

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

Machine-Level Programming I: Introduction Jan. 30, 2001 15-213 Machine-Level Programming I: Introduction Jan. 30, 2001 Topics Assembly Programmer s Execution Model Accessing Information Registers Memory Arithmetic operations IA32 Processors Totally Dominate

More information

CPS104 Recitation: Assembly Programming

CPS104 Recitation: Assembly Programming 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

More information

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

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 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 text C program (p1.c p2.c) Compiler (gcc -S) text Asm

More information

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

Carnegie Mellon. 5 th Lecture, Jan. 31, Instructors: Todd C. Mowry & Anthony Rowe Machine Level Programming I: Basics 15 213/18 213: 213: Introduction to Computer Systems 5 th Lecture, Jan. 31, 2012 Instructors: Todd C. Mowry & Anthony Rowe 1 Today: Machine Programming gi: Basics History

More information

Buffer Overflow Attack

Buffer Overflow Attack Buffer Overflow Attack What every applicant for the hacker should know about the foundation of buffer overflow attacks By (Dalgona@wowhacker.org) Email: zinwon@gmail.com 2005 9 5 Abstract Buffer overflow.

More information

Processes (Intro) Yannis Smaragdakis, U. Athens

Processes (Intro) Yannis Smaragdakis, U. Athens Processes (Intro) Yannis Smaragdakis, U. Athens Process: CPU Virtualization Process = Program, instantiated has memory, code, current state What kind of memory do we have? registers + address space Let's

More information

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

Sistemi Operativi. Lez. 16 Elementi del linguaggio Assembler AT&T Sistemi Operativi Lez. 16 Elementi del linguaggio Assembler AT&T Data Sizes Three main data sizes Byte (b): 1 byte Word (w): 2 bytes Long (l): 4 bytes Separate assembly-language instructions E.g., addb,

More information

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls Goals of this Lecture Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and urning" Passing parameters" Storing local variables" Handling registers without interference"

More information

Putting the pieces together

Putting the pieces together IBM developerworks : Linux : Linux articles All of dw Advanced search IBM home Products & services Support & downloads My account Inline assembly for x86 in Linux e-mail it! Contents: GNU assembler syntax

More information

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

CISC 360. Machine-Level Programming I: Introduction Sept. 18, 2008 CISC 360 Machine-Level Programming I: Introduction Sept. 18, 2008 Topics Assembly Programmerʼs Execution Model Accessing Information Registers Memory Arithmetic operations IA32 Processors Totally Dominate

More information

x86 architecture et similia

x86 architecture et similia x86 architecture et similia 1 FREELY INSPIRED FROM CLASS 6.828, MIT A full PC has: PC architecture 2 an x86 CPU with registers, execution unit, and memory management CPU chip pins include address and data

More information

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

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang Project 3 Questions CMSC 313 Lecture 12 How C functions pass parameters UMBC, CMSC313, Richard Chang Last Time Stack Instructions: PUSH, POP PUSH adds an item to the top of the stack POP

More information

Assembly Language Programming Debugging programs

Assembly Language Programming Debugging programs Assembly Language Programming Debugging programs November 18, 2017 Debugging programs During the development and investigation of behavior of system programs various tools are used. Some utilities are

More information

Last Time: Floating Point. Intel x86 Processors. Lecture 4: Machine Basics Computer Architecture and Systems Programming ( )

Last Time: Floating Point. Intel x86 Processors. Lecture 4: Machine Basics Computer Architecture and Systems Programming ( ) Last Time: Floating Point Lecture : Machine Basics Computer Architecture and Systems Programming (252-0061-00) Timothy Roscoe Herbstsemester 2012 Fractional binary numbers IEEE floating point standard:

More information

Machine-Level Programming Introduction

Machine-Level Programming Introduction Machine-Level Programming Introduction Today Assembly programmer s exec model Accessing information Arithmetic operations Next time More of the same Fabián E. Bustamante, Spring 2007 IA32 Processors Totally

More information

CS241 Computer Organization Spring 2015 IA

CS241 Computer Organization Spring 2015 IA CS241 Computer Organization Spring 2015 IA-32 2-10 2015 Outline! Review HW#3 and Quiz#1! More on Assembly (IA32) move instruction (mov) memory address computation arithmetic & logic instructions (add,

More information

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

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and urning Passing parameters Storing local variables Handling registers without interference Returning

More information

Today: Machine Programming I: Basics. Machine Level Programming I: Basics. Intel x86 Processors. Intel x86 Evolution: Milestones

Today: Machine Programming I: Basics. Machine Level Programming I: Basics. Intel x86 Processors. Intel x86 Evolution: Milestones Today: Machine Programming I: Basics Machine Level Programming I: Basics 15 213/1 213: Introduction to Computer Systems 5 th Lecture, Jan 29, 2013 History of Intel processors and architectures C, assembly,

More information

Stack Discipline Jan. 19, 2018

Stack Discipline Jan. 19, 2018 15-410 An Experience Like No Other Discipline Jan. 19, 2018 Dave Eckhardt Brian Railing Slides originally stolen from 15-213 1 15-410, S 18 Synchronization Registration The wait list will probably be done

More information

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 February 9, 2016 CS 31: Intro to Systems ISAs and Assembly Kevin Webb Swarthmore College February 9, 2016 Reading Quiz Overview How to directly interact with hardware Instruction set architecture (ISA) Interface between

More information

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Roadmap. Java: Assembly language: OS: Machine code: Computer system: Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: Computer system: get_mpg: pushq movq... popq ret %rbp %rsp, %rbp

More information

Machine Language, Assemblers and Linkers"

Machine Language, Assemblers and Linkers Machine Language, Assemblers and Linkers 1 Goals for this Lecture Help you to learn about: IA-32 machine language The assembly and linking processes 2 1 Why Learn Machine Language Last stop on the language

More information

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize:

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize: Simple C Program Helloworld.c Programming and Debugging Assembly under Linux slides by Alexandre Denault int main(int argc, char *argv[]) { } printf("hello World"); Programming and Debugging Assembly under

More information

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

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018 CS 31: Intro to Systems ISAs and Assembly Kevin Webb Swarthmore College September 25, 2018 Overview How to directly interact with hardware Instruction set architecture (ISA) Interface between programmer

More information

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

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29 Procedure Calls Young W. Lim 2017-08-21 Mon Young W. Lim Procedure Calls 2017-08-21 Mon 1 / 29 Outline 1 Introduction Based on Stack Background Transferring Control Register Usage Conventions Procedure

More information

Assembly Language Programming - III

Assembly Language Programming - III Assembly Language Programming - III GDB Debugger Please refer to the handout New GDB commands (power.s) info registers (prints all register values) print/d $eax (prints individual register value. Note

More information

Is stack overflow still a problem?

Is stack overflow still a problem? Morris Worm (1998) Code Red (2001) Secure Programming Lecture 4: Memory Corruption II (Stack Overflows) David Aspinall, Informatics @ Edinburgh 31st January 2017 Memory corruption Buffer overflow remains

More information

How Software Executes

How Software Executes How Software Executes CS-576 Systems Security Instructor: Georgios Portokalidis Overview Introduction Anatomy of a program Basic assembly Anatomy of function calls (and returns) Memory Safety Programming

More information

Outline. What Makes a Good ISA? Programmability. Implementability

Outline. What Makes a Good ISA? Programmability. Implementability Outline Instruction Sets in General MIPS Assembly Programming Other Instruction Sets Goals of ISA Design RISC vs. CISC Intel x86 (IA-32) What Makes a Good ISA? Programmability Easy to express programs

More information

Process Layout and Function Calls

Process Layout and Function Calls 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

More information

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

1 /* file cpuid2.s */ 4.asciz The processor Vendor ID is %s \n 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start. 1 /* file cpuid2.s */ 2.section.data 3 output: 4.asciz "The processor Vendor ID is %s \n" 5.section.bss 6.lcomm buffer, 12 7.section.text 8.globl _start 9 _start: 10 movl $0, %eax 11 cpuid 12 movl $buffer,

More information

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

Page # CISC 360. Machine-Level Programming I: Introduction Sept. 18, IA32 Processors. X86 Evolution: Programmerʼs View. Machine-Level Programming I: Introduction Sept. 18, 2008 Topics CISC 360 Assembly Programmerʼs Execution Model Accessing Information Registers Memory Arithmetic operations IA32 Processors Totally Dominate

More information

Giving credit where credit is due

Giving credit where credit is due CSCE 230J Computer Organization Machine-Level Programming I: Introduction Dr. Steve Goddard goddard@cse.unl.edu Giving credit where credit is due Most of slides for this lecture are based on slides created

More information

CS241 Computer Organization Spring Introduction to Assembly

CS241 Computer Organization Spring Introduction to Assembly CS241 Computer Organization Spring 2015 Introduction to Assembly 2-05 2015 Outline! Rounding floats: round-to-even! Introduction to Assembly (IA32) move instruction (mov) memory address computation arithmetic

More information

Credits and Disclaimers

Credits and Disclaimers 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 3 nd Edition by Bryant and O'Hallaron

More information

Machine Level Programming I: Basics. Credits to Randy Bryant & Dave O Hallaron

Machine Level Programming I: Basics. Credits to Randy Bryant & Dave O Hallaron Machine Level Programming I: Basics Lecture 3, Feb. 24, 2011 Alexandre David Credits to Randy Bryant & Dave O Hallaron from Carnegie Mellon 1 Today: Machine Programming I: Basics History of Intel processors

More information

Outline. What Makes a Good ISA? Programmability. Implementability. Programmability Easy to express programs efficiently?

Outline. What Makes a Good ISA? Programmability. Implementability. Programmability Easy to express programs efficiently? Outline Instruction Sets in General MIPS Assembly Programming Other Instruction Sets Goals of ISA Design RISC vs. CISC Intel x86 (IA-32) What Makes a Good ISA? Programmability Easy to express programs

More information

ANITA S SUPER AWESOME RECITATION SLIDES

ANITA S SUPER AWESOME RECITATION SLIDES ANITA S SUPER AWESOME RECITATION SLIDES 15/18-213: Introduction to Computer Systems Stacks and Buflab, 11 Jun 2013 Anita Zhang, Section M WHAT S NEW (OR NOT) Bomblab is due tonight, 11:59 PM EDT Your late

More information

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

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11 X86 Debug Computer Systems Section 3.11 GDB is a Source Level debugger We have learned how to debug at the C level Now, C has been translated to X86 assembler! How does GDB play the shell game? Makes it

More information

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

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved. Implementing Threads Operating Systems In Depth II 1 Copyright 2018 Thomas W Doeppner All rights reserved The Unix Address Space stack dynamic bss data text Operating Systems In Depth II 2 Copyright 2018

More information

CMSC Lecture 03. UMBC, CMSC313, Richard Chang

CMSC Lecture 03. UMBC, CMSC313, Richard Chang CMSC Lecture 03 Moore s Law Evolution of the Pentium Chip IA-32 Basic Execution Environment IA-32 General Purpose Registers Hello World in Linux Assembly Language Addressing Modes UMBC, CMSC313, Richard

More information

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

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control. C Flow Control David Chisnall February 1, 2011 Outline What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope Disclaimer! These slides contain a lot of

More information

CSCE 212H, Spring 2008 Lab Assignment 3: Assembly Language Assigned: Feb. 7, Due: Feb. 14, 11:59PM

CSCE 212H, Spring 2008 Lab Assignment 3: Assembly Language Assigned: Feb. 7, Due: Feb. 14, 11:59PM CSCE 212H, Spring 2008 Lab Assignment 3: Assembly Language Assigned: Feb. 7, Due: Feb. 14, 11:59PM February 7, 2008 1 Overview The purpose of this assignment is to introduce you to the assembly language

More information

Instructor: Alvin R. Lebeck

Instructor: Alvin R. Lebeck X86 Assembly Programming with GNU assembler Lecture 7 Instructor: Alvin R. Lebeck Some Slides based on those from Randy Bryant and Dave O Hallaron Admin Reading: Chapter 3 Note about pointers: You must

More information

Intel assembly language using gcc

Intel assembly language using gcc QOTD Intel assembly language using gcc Assembly language programming is difficult. Make no mistake about that. It is not for wimps and weaklings. - Tanenbaum s 6th, page 519 These notes are a supplement

More information

CSE 351: Week 4. Tom Bergan, TA

CSE 351: Week 4. Tom Bergan, TA CSE 35 Week 4 Tom Bergan, TA Does this code look okay? int binarysearch(int a[], int length, int key) { int low = 0; int high = length - ; while (low

More information

An Experience Like No Other. Stack Discipline Aug. 30, 2006

An Experience Like No Other. Stack Discipline Aug. 30, 2006 15-410 An Experience Like No Other Discipline Aug. 30, 2006 Bruce Maggs Dave Eckhardt Slides originally stolen from 15-213 15-410, F 06 Synchronization Registration If you're here but not registered, please

More information

Low-Level Essentials for Understanding Security Problems Aurélien Francillon

Low-Level Essentials for Understanding Security Problems Aurélien Francillon Low-Level Essentials for Understanding Security Problems Aurélien Francillon francill@eurecom.fr Computer Architecture The modern computer architecture is based on Von Neumann Two main parts: CPU (Central

More information

CS 537 Lecture 2 - Processes

CS 537 Lecture 2 - Processes CS 537 Lecture 2 - Processes Michael Swift 1 Basic Structure Kernel is a big program that starts when you boot your program Has full access to physical hardware. User programs, utilities, services see

More information

Shell Code For Beginners

Shell Code For Beginners Shell Code For Beginners Beenu Arora Site: www.beenuarora.com Email: beenudel1986@gmail.com ################################################################ #.. # # _/ \ _ \ _/ # # / \ \\ \ / // \/ /_\

More information

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

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27 Procedure Calls Young W. Lim 2016-11-05 Sat Young W. Lim Procedure Calls 2016-11-05 Sat 1 / 27 Outline 1 Introduction References Stack Background Transferring Control Register Usage Conventions Procedure

More information

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

The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, 2002 15-213 The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, 2002 Topics Assembly Programmer s Execution Model Accessing Information Registers Memory Arithmetic operations

More information

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86 Lecture 15 Intel Manual, Vol. 1, Chapter 3 Hampden-Sydney College Fri, Mar 6, 2009 Outline 1 2 Overview See the reference IA-32 Intel Software Developer s Manual Volume 1: Basic, Chapter 3. Instructions

More information

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

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 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 CS33 Intro to Computer Systems XI 1 Copyright 2017 Thomas

More information

X86 Stack Calling Function POV

X86 Stack Calling Function POV X86 Stack Calling Function POV Computer Systems Section 3.7 Stack Frame Reg Value ebp xffff FFF0 esp xffff FFE0 eax x0000 000E Memory Address Value xffff FFF8 xffff FFF4 x0000 0004 xffff FFF4 x0000 0003

More information

Computer Labs: Profiling and Optimization

Computer Labs: Profiling and Optimization Computer Labs: Profiling and Optimization 2 o MIEIC Pedro F. Souto (pfs@fe.up.pt) December 15, 2010 Optimization Speed matters, and it depends mostly on the right choice of Data structures Algorithms If

More information

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 03, SPRING 2013

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 03, SPRING 2013 CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 03, SPRING 2013 TOPICS TODAY Moore s Law Evolution of Intel CPUs IA-32 Basic Execution Environment IA-32 General Purpose Registers

More information

+ Machine Level Programming: x86-64 History

+ Machine Level Programming: x86-64 History + Machine Level Programming: x86-64 History + Intel x86 Processors Dominate laptop/desktop/server market Evolutionary design Backwards compatible up until 8086, introduced in 1978 Added more features as

More information

The von Neumann Machine

The von Neumann Machine The von Neumann Machine 1 1945: John von Neumann Wrote a report on the stored program concept, known as the First Draft of a Report on EDVAC also Alan Turing Konrad Zuse Eckert & Mauchly The basic structure

More information

CS429: Computer Organization and Architecture

CS429: Computer Organization and Architecture CS429: Computer Organization and Architecture Warren Hunt, Jr. and Bill Young Department of Computer Sciences University of Texas at Austin Last updated: October 1, 2014 at 08:36 CS429 Slideset 7: 1 Topics

More information

symbolic name data type (perhaps with qualifier) allocated in data area, stack, or heap duration (lifetime or extent)

symbolic name data type (perhaps with qualifier) allocated in data area, stack, or heap duration (lifetime or extent) variables have multiple attributes variable symbolic name data type (perhaps with qualifier) allocated in data area, stack, or heap duration (lifetime or extent) storage class scope (visibility of the

More information

Machine Programming 1: Introduction

Machine Programming 1: Introduction Machine Programming 1: Introduction CS61, Lecture 3 Prof. Stephen Chong September 8, 2011 Announcements (1/2) Assignment 1 due Tuesday Please fill in survey by 5pm today! Assignment 2 will be released

More information

The x86 Architecture

The x86 Architecture The x86 Architecture Lecture 24 Intel Manual, Vol. 1, Chapter 3 Robb T. Koether Hampden-Sydney College Fri, Mar 20, 2015 Robb T. Koether (Hampden-Sydney College) The x86 Architecture Fri, Mar 20, 2015

More information

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls CSC 24: Computing Systems X86 Assembly: Function Calls" 1 Lecture Goals! Challenges of supporting functions" Providing information for the called function" Function arguments and local variables" Allowing

More information

Computer Architecture and Assembly Language. Practical Session 5

Computer Architecture and Assembly Language. Practical Session 5 Computer Architecture and Assembly Language Practical Session 5 Addressing Mode - "memory address calculation mode" An addressing mode specifies how to calculate the effective memory address of an operand.

More information