Instructor: Alvin R. Lebeck

Similar documents
Machine-Level Programming II: Control and Arithmetic

Process Layout and Function Calls

Credits to Randy Bryant & Dave O Hallaron

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

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

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

Process Layout, Function Calls, and the Heap

Sungkyunkwan University

Machine- Level Programming III: Switch Statements and IA32 Procedures

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

Assembly Language: Function Calls

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

Machine Level Programming II: Arithmetic &Control

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

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

Credits and Disclaimers

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

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

CS213. Machine-Level Programming III: Procedures

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

Machine-level Programming (3)

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

Machine- Level Programming II: Arithme6c & Control

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

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

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

Compiler Construction D7011E

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

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Machine- Level Programming II: Arithme c & Control

Machine- Level Programming II: Arithme6c & Control

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

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

Sungkyunkwan University

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address

Assembly I: Basic Operations. Jo, Heeseung

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

System Programming and Computer Architecture (Fall 2009)

CS61 Section Solutions 3

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

The Hardware/Software Interface CSE351 Spring 2013

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

Instruction Set Architecture

Instruction Set Architecture

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

Second Part of the Course

CPS104 Recitation: Assembly Programming

Giving credit where credit is due

Instruction Set Architecture

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

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

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

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

CS241 Computer Organization Spring Addresses & Pointers

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Machine Programming 1: Introduction

CISC 360 Instruction Set Architecture

Instruction Set Architecture

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

X86 Assembly -Procedure II:1

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

CS241 Computer Organization Spring 2015 IA

CIT Week13 Lecture

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

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

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

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

Machine Programming 3: Procedures

x86 assembly CS449 Fall 2017

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

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

Chapter 4! Processor Architecture!

Assembly Language: IA-32 Instructions

cmovxx ra, rb 2 fn ra rb irmovl V, rb rb V rmmovl ra, D(rB) 4 0 ra rb D mrmovl D(rB), ra 5 0 ra rb D OPl ra, rb 6 fn ra rb jxx Dest 7 fn Dest

Machine-Level Programming III: Procedures

Machine- Level Programming III: Switch Statements and IA32 Procedures

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

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

CS429: Computer Organization and Architecture

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

Handout #2: Machine-Level Programs

Giving credit where credit is due

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

CS61, Fall 2012 Midterm Review Section

Machine-Level Programming II: Control Flow

CPSC W Term 2 Problem Set #3 - Solution

CS 3843 Final Exam Fall 2012

Program Exploitation Intro

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

Systems I. Machine-Level Programming V: Procedures

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

X86 Stack Calling Function POV

Intel assembly language using gcc

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

Machine Program: Procedure. Zhaoguo Wang

Computer Science 104:! Y86 & Single Cycle Processor Design!

Intel Instruction Set (gas)

Transcription:

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 explicitly inipalize/set to NULL Assembly Programming (x86) Quick InstrucPon Review Assembly Language Simple one funcpon program High level constructs (control) Interfacing to a C program Procedure Calling ConvenPons 1

Some ArithmePc and Logical OperaPons Two Operand InstrucPons: Format ComputaPon addl Src,Dest Dest = Dest + Src subl Src,Dest Dest = Dest - Src imull Src,Dest Dest = Dest * Src sall Src,Dest Dest = Dest << Src Also called shll sarl Src,Dest Dest = Dest >> Src ArithmePc shrl Src,Dest Dest = Dest >> Src Logical xorl Src,Dest Dest = Dest ^ Src andl Src,Dest Dest = Dest & Src orl Src,Dest Dest = Dest Src Watch out for argument order! No dispncpon between signed and unsigned int (why?) 2 Some ArithmePc OperaPons One Operand InstrucPons incl Dest Dest = Dest + 1 decl Dest Dest = Dest - 1 negl Dest Dest = - Dest notl Dest Dest = ~Dest See book for more instrucpons Note: suffix l is for 32- bit values, b for byte, w for 16- bit 3

Address ComputaPon InstrucPon leal Src,Dest Src is address mode expression Set Dest to address denoted by expression Uses CompuPng addresses without a memory reference E.g., translapon of p = &x[i]; CompuPng arithmepc expressions of the form x + k*y k = 1, 2, 4, or 8 Example int mul12(int x) { return x*12; } Converted to ASM by compiler: leal (%eax,%eax,2), %eax ;t <- x+x*2 sall $2, %eax ;return t<<2 4 CondiPon Codes (Implicit Senng) Single bit registers CF Carry Flag (for unsigned) SF Sign Flag (for signed) ZF Zero Flag OF Overflow Flag (for signed) Implicitly set (think of it as side effect) by arithmepc operapons Not set by lea instrucpon Explicitly set by compare and test instrucpons Allow for condiponal change of PC via jump instrucpons 5

Procedure Control Flow Use stack to support procedure call and return Procedure call: call label Push return address on stack Jump to label Return address: Address of the next instrucpon right ater call Example from disassembly 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax Return address = 0x8048553 Procedure return: ret Pop address from stack Jump to address 6 X86 w/ Gnu Assembly Language One instrucpon per line. Numbers are base- 10 integers or Hex w/ leading 0x. IdenPfiers: alphanumeric, _,. string starpng in a lexer or _ Labels: idenpfiers starpng at the beginning of a line followed by : Comments: everything following # Pll end- of- line. DirecPves: convey informapon to the assembler InstrucPon format: Space and, separated fields. [Label:] <op> src, dest [# comment] [Label:].DirecPve [arg1], [arg2],... 7

Assembly Language (cont.) DirecPves: tell the assembler what to do... Format. <string> [arg1], [arg2]... Examples.data [address] # start a data segment. [opponal begining address].text [address] # start a code segment..globl # declare a label externally visible.ascii <string> # store a string in memory..asciiz <string> # store a null terminated string in memory.long w1, w2,..., wn # store n 32- bit values in memory..align n # align segment on 2 n byte boundary. 8 A simple funcpon Add two numbers together x and y.text # declare text segment.globl sum # declare funcpon name for external call sum: # label for funcpon movl x, %edx # load M[x] into %edx movl y, %eax # load M[y] into %eax addl %edx, %eax # %eax = %eax + %edx movl %eax, x # store %eax into M[x] ret # return to calling funcpon.data # declare data segment x:.long 10 # inipalize x to 10 y:.long 2 # inipalize y to 2 9

Typical Code Segments- - IF if (x!= y) x = x + y; y = 2; General Rule is to invert condipon if (x == y) goto skip x = x + y skip: y = 2; skip:! Assume %ecx contains x and %edx contains y cmpl %ecx, %edx je skip addl %edx, %ecx movl $2, %edx! 10 Typical Code Segments IF- else if (x!= y) x = x + y; else x = x y; Invert condipon check and use goto if (x == y) goto L1 x = x + y; goto done L1: x = x - y; done:! L1: Assume %ecx contains x and %edx contains y cmpl %ecx, %edx # compute condipon je L1 # checking!(condipon) addl %edx, %ecx jmp done subl %edx, %ecx done:!! # x = x + y # x = x - y 11

The C code int sum( ){ int i; int sum = 0; for(i=0; i <= 100; i++) sum = sum + i*i ; return(sum); // put sum into %eax } Let s write the assembly :) 12 Sum array Task: sum together the integers stored in memory.text.globl sum sum: # Fill in what goes here.data num_array:.long 35, 16, 42, 19, 55, 91, 24, 61, 53 14

Calling an Assembly FuncPon from C Main in normal C file Declare funcpon using extern E.g., extern int foo(); Foo is our assembly funcpon in a.s file 16 Review: Procedure Call and Return int equal(int a1, int a2) { } int tsame; tsame = 0; if (a1 == a2) tsame = 1; return(tsame); main() { } int x,y,same; x = 43; y = 2; same = equal(x,y); // other computapon 0x10000 0x10004 0x10008 movl $43, %ecx movl $2, %edx call 0x30408 0x30408 movl $0, %eax 0x3040c cmpl %ecx, %edx! 0x30410 jne 0x30418! 0x30414 movl $1, %eax 0x30418 addl %edx, %ecx 0x3041c ret PC M[%esp] 0x10000?? 0x10004?? 0x10008?? 0x30408 0x1000c 0x3040c 0x1000c 0x30410 0x1000c 0x30414 0x1000c 0x30418 0x1000c 0x3041c 0x1000c 0x1000c?? 17

Procedure Call GAP ISA Level call and return instrucpons C Level Local Name Scope change tsame to same Recursion Arguments/parameters and Return Value (funcpons) Assembly Level Must bridge gap between HLL and ISA SupporPng Local Names Passing Arguments/Parameters (arbitrary number? What data structure? 18 Procedure Call (Stack) Frame Procedures use a frame in the stack to: Hold values passed to procedures as arguments. Save registers that a procedure may modify, but which the procedure s caller does not want changed. To provide space for local variables. (variables with local scope) To evaluate complex expressions. 19

IA32/Linux Stack Frame Current Stack Frame ( Top to Boxom) Argument build: Parameters for funcpon about to call Local variables If can t keep in registers Saved register context Old frame pointer Caller Stack Frame Return address Pushed by call instrucpon Arguments for this call Frame pointer %ebp Stack pointer %esp Caller Frame Arguments Return Addr Old %ebp Saved Registers + Local Variables Argument Build 20 Register Saving ConvenPons When procedure yoo calls who: yoo is the caller who is the callee Can Register be used for temporary storage? yoo: movl $15213, %edx call who addl %edx, %eax who: movl 8(%ebp), %edx addl $18243, %edx ret Contents ret of register %edx overwrixen by who This could be trouble something should be done! Need some coordinapon 21

Register Saving ConvenPons When procedure yoo calls who: yoo is the caller who is the callee Can Register be used for temporary storage? ConvenPons Caller Save Caller saves temporary values in its frame before the call Callee Save Callee saves temporary values in its frame before using 22 IA32/Linux+Windows Register Usage %eax, %edx, %ecx Caller saves prior to call if values are used later %eax also used to return integer or pointer value %ebx, %esi, %edi Callee saves if wants to use them %esp, %ebp special form of callee save Restored to original values upon exit from procedure Caller- Save Temporaries Callee- Save Temporaries Special %eax %edx %ecx %ebx %esi %edi %esp %ebp 23

IA32/GCC Procedure Calling ConvenPons Calling Procedure Step- 1: Save caller- saved registers Save registers %eax, %ecx, %edx if they contain live values at the call site. Step- 2: Setup the arguments: Push arguments onto the stack in reverse order Step- 3: Execute a call instrucpon. 24 IA32/GCC Calling ConvenPons (cont.) Called RouPne Step- 1: Update the frame pointer pushl %ebp movl %esp, %ebp Step- 2: Allocate space for frame Subtract the frame size from the stack pointer subl $<frame- size>, %esp Space is for local variables and saved registers May oten allocate more space than necessary. Step- 3: Save callee- saved registers in the frame. Registers %ebx, %esi, %edi are saved if they are used. 25

IA32/GCC Calling ConvenPons (cont.) On return from a call Step- 1: Put return value in register %eax. (if value is returned) Step- 2: Restore callee- saved registers. Restore %ebx, %esi, %edi if needed Step- 3: Pop the stack leave Equivalent to movl %ebp, %esp popl %ebp Step- 4: Return ret # %eip = M[%esp]; %esp = %esp - 4 26 C FuncPon call with one parameter #include <stdio.h> #include <stdlib.h> // declare the funcpon as externally defined // computes sum of elements 0 to i of an array defined in sum_array extern int sum_array(int i); int main(void) { int result; result = sum_array(7); prin ("Array sum = %d\n",result); return EXIT_SUCCESS; } 27

Sample FuncPon.text!!!# declare the text segment!.globl sum_array!!# declare the function label (note the _ in this label)!!!!!# the C program calls sum(int)! sum_array:!!pushl %ebp!!# save old frame pointer!!movl %esp, %ebp!!# set new stack pointer!!movl 8(%ebp), %eax!# read arg1 from stack, put into %eax!!leal num_array, %edx!# load address of num_array into %edx (p = &num_array)!!leal (%edx,%eax,4), %ecx!# load address of num_array+arg into %ecx!!movl $0, %eax!!# move 0 to running sum (%eax)! loop:!!!# label for loop structure!!addl (%edx), %eax!!# add value *p to running sum (%eax)!!addl $4, %edx!!# increment pointer in memory (p++)!!cmpl %ecx, %edx!!# compare pointer to termination (p < (num_array+arg1))!!jl loop!!!# jump to loop if (p < (num_array+arg1))!!leave!!!# prepare stack for return (movl %ebp, %esp; popl %ebp)!!ret!!!# return to calling routine (result is in %eax)!!.data!!!# declare data segment and array with 9 32-bit integers! num_array:.long 35, 16, 42, 19, 55, 91, 24, 61, 53! 28 Simple Assembly Examples Return sum of two arguments Return pointer to a string (string is declared in.s file) 29

x86 Assembly Programming Assembly Language Text file (with.s for eclipse) One instrucpon per line Labels, direcpves, etc. High- level Constructs If If- else Loops Memory (array) accesses Calling assembly from C Calling ConvenPons Examples in docs secpon of course web site Next Pme recursion & pointers! 30