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

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

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

Giving credit where credit is due

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

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

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

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

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

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Systems I. Machine-Level Programming I: Introduction

Machine-Level Programming Introduction

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

Assembly I: Basic Operations. Jo, Heeseung

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-Level Programming Introduction

Machine- level Programming

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

The Hardware/Software Interface CSE351 Spring 2015

X86 Assembly -Data II:1

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

CS429: Computer Organization and Architecture

Machine Programming 1: Introduction

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

Instruction Set Architectures

Machine Level Programming I: Basics

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

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

Computer Systems Architecture I. CSE 560M Lecture 3 Prof. Patrick Crowley

Instruction Set Architectures

Sungkyunkwan University

Today: Machine Programming I: Basics

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

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

MACHINE-LEVEL PROGRAMMING I: BASICS

Machine-Level Programming II: Control and Arithmetic

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

CS241 Computer Organization Spring 2015 IA

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

CS241 Computer Organization Spring Addresses & Pointers

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

Credits to Randy Bryant & Dave O Hallaron

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

Process Layout and Function Calls

Assembly I: Basic Operations. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

+ Machine Level Programming: x86-64 History

Process Layout, Function Calls, and the Heap

Machine-Level Programming I: Introduction

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

Machine-Level Programming I Introduction

Machine Level Programming II: Arithmetic &Control

Machine Programming I: Basics

Chapter 3 Machine-Level Programming I: Basics. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Machine-Level Programming I Introduction

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

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

Machine- Level Programming: Basics

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

Compila(on, Disassembly, and Profiling

Machine Level Programming: Basics

Credits and Disclaimers

Machine-Level Programming I: Basics

Machine-level Programming (3)

Machine Level Programming: Basics

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

Machine- Level Programming I: Basics

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

CS213. Machine-Level Programming III: Procedures

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

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

Machine- Level Programming II: Arithme c & Control

Instructor: Alvin R. Lebeck

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

Machine Programming 3: Procedures

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

Machine- Level Programming II: Arithme6c & Control

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

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

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

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

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

CS , Fall 2001 Exam 1

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

CPS104 Recitation: Assembly Programming

CS429: Computer Organization and Architecture

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

The Hardware/Software Interface CSE351 Spring 2015

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

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

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

Sungkyunkwan University

Machine-Level Programming III: Procedures

Machine Language, Assemblers and Linkers"

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

Second Part of the Course

Machine-Level Programming I: Basics

Credits and Disclaimers

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

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

Assembly Language: Function Calls

Transcription:

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 s Execution Model Accessing Information isters Memory Arithmetic operations Programmer-Visible State EIP Program Counter Address of next instruction ister File Heavily used program data Condition Codes Store status information about most recent arithmetic operation Used for conditional branching Memory Stack Byte addressable array Code, user data, (some) OS data Includes stack used to support procedures F4A 2 Datorarkitektur 2008 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 text binary C program (p1.c p2.c) Compiler (gcc -S) Asm program (p1.s p2.s) Assembler (gcc or as) Object program (p1.o p2.o) Linker (gcc or ld) Static libraries (.a) Compiling Into Assembly C Code int sum(int x, int y) int t = x+y; return t; Obtain with command gcc -O -S code.c Produces file code.s Generated Assembly _sum: pushl %ebp movl %esp,%ebp movl 12(%ebp),%eax addl 8(%ebp),%eax movl %ebp,%esp popl %ebp ret binary Executable program (p) F4A 3 Datorarkitektur 2008 F4A 4 Datorarkitektur 2008

Assembly Characteristics Minimal Data Types Integer data of 1, 2, or 4 bytes Data values Addresses (untyped pointers) Floating point data of 4, 8, or 10 bytes No aggregate types such as arrays or structures Just contiguously allocated bytes in memory Primitive Operations Perform arithmetic function on register or memory data Transfer data between memory and register Load data from memory into register Store register data into memory Transfer control Unconditional jumps to/from procedures Code for sum 0x401040 <sum>: 0x55 0xe5 0x8b 0x0c 0x03 0x08 0xec 0x5d 0xc3 Object Code Total of 13 bytes Each instruction 1, 2, or 3 bytes Starts at address 0x401040 Assembler Translates.s into.o Binary encoding of each instruction Nearly-complete image of executable code Missing linkages between code in different files Linker Resolves references between files Combines with static run-time libraries E.g., code for malloc, printf Some libraries are dynamically linked Linking occurs when program begins execution F4A 5 Conditional branches Datorarkitektur 2008 F4A 6 Datorarkitektur 2008 Machine Instruction Example int t = x+y; addl 8(%ebp),%eax Similar to expression x += y 0x401046: 03 45 08 C Code Add two signed integers Assembly Add 2 4-byte integers Long words in GCC parlance Same instruction whether signed or unsigned Operands: x: ister %eax y: Memory M[%ebp+8] t: ister %eax» Return function value in %eax Object Code 3-byte instruction Stored at address 0x401046 F4A 7 Datorarkitektur 2008 Disassembling Object Code Disassembled 00401040 <_sum>: 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 8b 45 0c mov 0xc(%ebp),%eax 6: 03 45 08 add 0x8(%ebp),%eax 9: 89 ec mov %ebp,%esp b: 5d pop %ebp c: c3 ret Disassembler objdump -d p Useful tool for examining object code Analyzes bit pattern of series of instructions Produces approximate rendition of assembly code Can be run on either a.out (complete executable) or.o file F4A 8 Datorarkitektur 2008

Alternate Disassembly Object 0x401040: 0x55 0xe5 0x8b 0x0c 0x03 0x08 0xec 0x5d 0xc3 Disassembled 0x401040 <sum>: push %ebp 0x401041 <sum+1>: mov %esp,%ebp 0x401043 <sum+3>: mov 0xc(%ebp),%eax 0x401046 <sum+6>: add 0x8(%ebp),%eax 0x401049 <sum+9>: mov %ebp,%esp 0x40104b <sum+11>: pop %ebp 0x40104c <sum+12>: ret Within gdb Debugger gdb p disassemble sum Disassemble procedure x/13b sum Examine the 13 bytes starting at sum F4A 9 Datorarkitektur 2008 Moving Data movl Source,Dest: Move 4-byte ( long ) word Moving Data Lots of these in typical code Operand Types Immediate: Constant integer data Like C constant, but prefixed with $ E.g., $0x400, $-533 Encoded with 1, 2, or 4 bytes ister: One of 8 integer registers But %esp and %ebp reserved for special use Others have special uses for particular instructions Memory: 4 consecutive bytes of memory Various address modes %eax %ebx %esi %edi %esp %ebp F4A 10 Datorarkitektur 2008 movl Operand Combinations Simple Addressing Modes movl Source Imm Mem Destination C Analog Mem Mem movl $0x4,%eax temp = 0x4; movl $-147,(%eax) *p = -147; movl %eax, temp2 = temp1; movl %eax,() *p = temp; movl (%eax), temp = *p; Cannot do memory-memory transfers with single instruction Normal (R) Mem[[R]] ister R specifies memory address movl (),%eax Displacement D(R) Mem[[R]+D] ister R specifies start of memory region Constant displacement D specifies offset movl 8(%ebp), F4A 11 Datorarkitektur 2008 F4A 12 Datorarkitektur 2008

Using Simple Addressing Modes void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp), movl 8(%ebp), movl (),%eax movl (),%ebx movl %eax,() movl %ebx,() movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Body F4A 13 Datorarkitektur 2008 Set Up Finish Understanding Swap void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; ister %eax %ebx Variable yp xp t1 t0 movl 12(%ebp), # ecx = yp movl 8(%ebp), # edx = xp movl (),%eax movl (),%ebx movl %eax,() movl %ebx,() Offset # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx Stack F4A 14 Datorarkitektur 2008 12 8 4 0-4 yp xp Rtrn adr Old %ebp Old %ebx %ebp Understanding Swap %eax %ebx %esi %edi %esp 0x100 %ebp 0x104 Offset 12 8 4 %ebp 0-4 Address 123 0x124 456 0x120 0x11c 0x118 0x114 0x120 0x110 0x124 0x10c Rtrn adr 0x108 Old %ebp 0x104 Old %ebx 0x100 movl 12(%ebp), # ecx = yp movl 8(%ebp), # edx = xp movl (),%eax # eax = *yp (t1) movl (),%ebx # ebx = *xp (t0) movl %eax,() # *xp = eax movl %ebx,() # *yp = ebx F4A 15 Datorarkitektur 2008 yp xp Indexed Addressing Modes Most General Form D: D(Rb,Ri,S) Mem[[Rb]+S*[Ri]+ D] Constant displacement 1, 2, or 4 bytes Rb: Base register: Any of 8 integer registers Ri: Index register: Any, except for %esp Unlikely you d use %ebp, either S: Scale: 1, 2, 4, or 8 Special Cases (Rb,Ri) D(Rb,Ri) (Rb,Ri,S) Mem[[Rb]+[Ri]] Mem[[Rb]+[Ri]+D] Mem[[Rb]+S*[Ri]] F4A 16 Datorarkitektur 2008

Address Computation Examples Address Computation Instruction 0xf000 0x100 leal Src,Dest Src is address mode expression Set Dest to address denoted by expression Expression 0x8() (,) (,,4) Computation 0xf000 + 0x8 0xf000 + 0x100 0xf000 + 4*0x100 Address 0xf008 0xf100 0xf400 Uses Computing address without doing memory reference E.g., translation of p = &x[i]; Computing arithmetic expressions of the form x + k*y + d k = 1, 2, 4, or 8. 0x80(,,2) 2*0xf000 + 0x80 0x1e080 F4A 17 Datorarkitektur 2008 F4A 18 Datorarkitektur 2008 Some Arithmetic Operations Format Two Operand Instructions addl Src,Dest subl Src,Dest imull Src,Dest Computation Dest = Dest + Src Dest = Dest - Src Dest = Dest * Src sall Src,Dest Dest = Dest << Src Also called shll sarl Src,Dest Dest = Dest >> Src Arithmetic shrl Src,Dest Dest = Dest >> Src Logical xorl Src,Dest andl Src,Dest orl Src,Dest Dest = Dest ^ Src Dest = Dest & Src Dest = Dest Src Some Arithmetic Operations Format One Operand Instructions Computation incl Dest Dest = Dest + 1 decl Dest Dest = Dest - 1 negl Dest notl Dest Dest = - Dest Dest = ~ Dest F4A 19 Datorarkitektur 2008 F4A 20 Datorarkitektur 2008

Using leal for Arithmetic Expressions int arith (int x, int y, int z) int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; arith: pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax movl 12(%ebp), leal (,%eax), leal (,,2), sall $4, addl 16(%ebp), leal 4(,%eax),%eax imull,%eax movl %ebp,%esp popl %ebp ret Set Up Body Finish F4A 21 Datorarkitektur 2008 Understanding arith movl 8(%ebp),%eax # eax = x movl 12(%ebp), # edx = y leal (,%eax), # ecx = x+y (t1) leal (,,2), # edx = 3*y sall $4, # edx = 48*y (t4) addl 16(%ebp), # ecx = z+t1 (t2) leal 4(,%eax),%eax # eax = 4+t4+x (t5) imull,%eax # eax = t5*t2 (rval) Offset 16 12 8 4 0 z y x Rtn adr Old %ebp Stack %ebp int arith (int x, int y, int z) int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; F4A 22 Datorarkitektur 2008 Summary: Abstract Machines Machine Models Data Control C mem proc Assembly mem regs alu Stack Cond. processor Codes 1) char 1) loops 2) int, float 2) conditionals 3) double 3) switch 4) struct, array 4) Proc. call 5) pointer 5) Proc. return 1) byte 3) branch/jump 2) 2-byte word 4) call 3) 4-byte long word 5) ret 4) contiguous byte allocation 5) address of initial byte F4A 23 Datorarkitektur 2008 Whose Assembler? Intel/Microsoft Format lea eax,[ecx+ecx*2] sub esp,8 cmp dword ptr [ebp-8],0 mov eax,dword ptr [eax*4+100h] Intel/Microsoft Differs from GAS Operands listed in opposite order mov Dest, Src movl Src, Dest Constants not preceded by $, Denote hex with h at end 100h $0x100 Operand size indicated by operands rather than operator suffix sub subl Addressing format shows effective address computation [eax*4+100h] $0x100(,%eax,4) GAS/Gnu Format leal (,,2),%eax subl $8,%esp cmpl $0,-8(%ebp) movl $0x100(,%eax,4),%eax F4A 24 Datorarkitektur 2008