CS241 Computer Organization Spring 2015 IA

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

CS241 Computer Organization Spring Introduction to Assembly

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Assembly I: Basic Operations. Jo, Heeseung

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

Credits and Disclaimers

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

Process Layout and Function Calls

CS241 Computer Organization Spring Addresses & Pointers

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

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

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

Credits and Disclaimers

Sungkyunkwan University

Machine Programming 1: Introduction

Machine Level Programming I: Basics

x86 Programming I CSE 351 Winter

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

Process Layout, Function Calls, and the Heap

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

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

X86 Assembly -Data II:1

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

Today: Machine Programming I: Basics

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

Machine-Level Programming II: Control and Arithmetic

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

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

Credits to Randy Bryant & Dave O Hallaron

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

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

x86 Assembly Tutorial COS 318: Fall 2017

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

x86 64 Programming I CSE 351 Autumn 2018 Instructor: Justin Hsia

The x86 Architecture

The Hardware/Software Interface CSE351 Spring 2015

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

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

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

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

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

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

Binghamton University. CS-220 Spring x86 Assembler. Computer Systems: Sections

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

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

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

Giving credit where credit is due

ASSEMBLY III: PROCEDURES. Jo, Heeseung

CS213. Machine-Level Programming III: Procedures

Assembly III: Procedures. Jo, Heeseung

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

Machine-Level Programming Introduction

Instruction Set Architectures

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

The von Neumann Machine

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

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

Instruction Set Architectures

Machine-level Programming (3)

Assembly Language: Function Calls

Reverse Engineering II: Basics. Gergely Erdélyi Senior Antivirus Researcher

Instruction Set Architecture

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

Instruction Set Architecture

Reverse Engineering II: The Basics

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

Assembly Language: Function Calls" Goals of this Lecture"

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

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

Machine-Level Programming Introduction

Introduction to IA-32. Jo, Heeseung

CS 3843 Final Exam Fall 2012

INTRODUCTION TO IA-32. Jo, Heeseung

Assembly Language: Function Calls" Goals of this Lecture"

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

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

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

MACHINE-LEVEL PROGRAMMING I: BASICS

Systems I. Machine-Level Programming I: Introduction

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

Machine-Level Programming III: Procedures

x86 assembly CS449 Fall 2017

UMBC. A register, an immediate or a memory address holding the values on. Stores a symbolic name for the memory location that it represents.

Assembly Language: IA-32 Instructions

CPS104 Recitation: Assembly Programming

MODE (mod) FIELD CODES. mod MEMORY MODE: 8-BIT DISPLACEMENT MEMORY MODE: 16- OR 32- BIT DISPLACEMENT REGISTER MODE

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

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

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

This is a medical robot, guided by a skilled surgeon and designed to get to places doctors are unable to reach without opening a pacent up.

Machine Programming 2: Control flow

The Hardware/Software Interface CSE351 Spring 2013

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

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

Complex Instruction Set Computer (CISC)

Intro to GNU Assembly Language on Intel Processors

The von Neumann Machine

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

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

CISC 360 Instruction Set Architecture

Transcription:

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, imul, xor, shr, ) stack frame Read: CS:APP2 Chapter 3, sections 3.1 3.5 Quiz on 2s-complement & float today Lab#1 Datalab due Feb. 24, teams encouraged Exam#1 Thursday, Feb. 19, 8:00 pm

Carnegie Mellon Assembly Characteristics: 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 Conditional branches

Move instruction: mov IA32 instructions:! movl moves an int (4 bytes) transfer reg reg, reg mem, mem reg Memory addressing:! D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D] D displacement 1, 2, or 4 bytes Rb Base register: Any of 8 integer registers Ri Index register: Any, except for %esp (or %ebp) S: Scale: 1, 2, 4, or 8

Data formats movl moves an int, long or pointer movb moves a byte, movw moves a word, etc. C Data type Assembly suffix Size (bytes) char Byte b 1 short Word w 2 int Double Word l 4 long int Double Word l 4 char * Double Word l 4 float Single Precision s 4 double Double Precision l 8 cf. Figure 3.1, p. 167

Carnegie Mellon Integer Registers (IA32) Origin (mostly obsolete) %eax %ax %ah %al accumulate general purpose %ecx %edx %ebx %esi %cx %dx %bx %si %ch %dh %bh %cl %dl %bl counter data base source index %edi %di destination index %esp %sp stack pointer %ebp %bp base pointer 16-bit virtual registers (backwards compatibility)

Carnegie Mellon Moving Data: IA32 Moving Data movx Source, Dest x in {b, w, l} movl Source, Dest: Move 4-byte long word movw Source, Dest: Move 2-byte word movb Source, Dest: Move 1-byte byte %eax %ecx %edx %ebx %esi %edi %esp %ebp Lots of these in typical code

Carnegie Mellon Moving Data: IA32 Moving Data movl Source, Dest: Operand Types Immediate: Constant integer data Example: $0x400, $-533 Like C constant, but prefixed with $ Encoded with 1, 2, or 4 bytes Register: One of 8 integer registers Example: %eax, %edx But %esp and %ebp reserved for special use Others have special uses for particular instructions Memory: 4 consecutive bytes of memory at address given by register Simplest example: (%eax) Various other address modes %eax %ecx %edx %ebx %esi %edi %esp %ebp

Carnegie Mellon movl Operand Combinations Source Dest Src,Dest C Analog Imm Reg Mem movl $0x4,%eax temp = 0x4; movl $-147,(%eax) *p = -147; movl Reg Reg Mem movl %eax,%edx movl %eax,(%edx) temp2 = temp1; *p = temp; Mem Reg movl (%eax),%edx temp = *p; Cannot do memory-memory transfer with a single instruction

Carnegie Mellon Simple Memory Addressing Modes Normal (R) Mem[Reg[R]] Register R specifies memory address movl (%ecx),%eax Mem[Reg[R]+D] Register R specifies start of memory region Constant displacement D specifies offset movl 8(%ebp),%edx Displacement D(R)

Carnegie Mellon Address Computation Examples %edx %ecx 0xf000 0x100 Expression Address Computation Address 0x8(%edx) 0xf000 + 0x8 0xf008 (%edx,%ecx) 0xf000 + 0x100 0xf100 (%edx,%ecx,4) 0xf000 + 4*0x100 0xf400 0x80(,%edx,2) 2*0xf000 + 0x80 0x1e080

Arithmetic & logic operations Instruction add sub imul xor or and sal (or shl) sar shr Description adds subtraction integer multiply exclusive or or and left shift arithmetic right shift logical right shift cf. Figure 3.7, p. 178

Memory layout of a process /* add 1 to x */ int main() { int x = 17; FP (%ebp) SP (%esp) x = x + 1; return 0; } PC

IA32/Linux Stack Frame! Current Stack Frame ( Top to Bottom) Argument build: Parameters for function 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 instruction Arguments for this call Frame pointer %ebp Stack pointer %esp Caller Frame Arguments Return Addr Old %ebp Saved Registers + Local Variables Argument Build

IA32 Stack! Region of memory managed with stack discipline! Grows toward lower addresses! Register %esp contains lowest stack address = address of top element Stack Pointer: %esp Stack Bottom Increasing Addresses Stack Grows Down Stack Top

Stack Frames! Contents Local variables Return information Temporary space! Management Space allocated when enter procedure Set-up code Frame Pointer: %ebp Stack Pointer: %esp Previous Frame Frame for proc Stack Top Deallocated when return Finish code

/* add1.c */ int main() { } int x = 17; x = x + 1; return 0; compile with gcc c S -m32 add1.c.file.text.globl main.type main: leal andl pushl pushl movl pushl subl movl addl movl addl popl popl leal ret.size.ident "add1.c" main, @function 4(%esp), %ecx $-16, %esp -4(%ecx) %ebp %esp, %ebp %ecx $16, %esp $17, -8(%ebp) $1, -8(%ebp) $0, %eax $16, %esp %ecx %ebp -4(%ecx), %esp main,.-main "GCC: (GNU) 4.1.2

/* add1.c */ int main() { } int x = 17; x = x + 1; return 0; address of x is Frame Pointer - 8.file "add1.c".text.globl main.type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $16, %esp movl $17, -8(%ebp) # x = 17 addl $1, -8(%ebp) # x++ movl $0, %eax # return 0 addl $16, %esp popl %ecx popl %ebp leal -4(%ecx), %esp ret.size main,.-main.ident "GCC: (GNU) 4.1.2

variable address x FP 12 y FP - 8 int main() { int x = 17; int y = -2; x = x + y; return 0; }.file "tmain.c".text.globl main.type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $16, %esp movl $17, -12(%ebp) # x = 17 movl $-2, -8(%ebp) # y = -2 movl -8(%ebp), %eax addl %eax, -12(%ebp) movl $0, %eax addl $16, %esp popl %ecx popl %ebp leal -4(%ecx), %esp ret

variable address x FP 12 y FP - 8 int main() { int x = 17; int y = -2; x = x + y; return 0; }.file "tmain.c".text.globl main.type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $16, %esp movl $17, -12(%ebp) # x = 17 movl $-2, -8(%ebp) # y = -2 movl -8(%ebp), %eax # eax = y addl %eax, -12(%ebp) # x = eax + x movl $0, %eax addl $16, %esp popl %ecx popl %ebp leal -4(%ecx), %esp ret

variable address x FP 12 y FP - 8 int main() { int x = 17; int y = -2; x = x + y; return 0; }.file addxy.c".text.globl main.type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $16, %esp movl $17, -12(%ebp) # x = 17 movl $-2, -8(%ebp) # y = -2 movl -8(%ebp), %eax # eax = y addl %eax, -12(%ebp) # x = eax + x movl $0, %eax addl $16, %esp popl %ecx popl %ebp leal -4(%ecx), %esp ret

variable address x FP 12 y FP - 8 int main() { int x = 17; int y = -2; x = 8*x + y; return 0; }.file "multby8.c".text.globl main.type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $16, %esp movl $17, -12(%ebp) # x = 17 movl $-2, -8(%ebp) # y = -2 movl -12(%ebp), %eax sall $3, %eax addl -8(%ebp), %eax movl %eax, -12(%ebp) movl $0, %eax addl $16, %esp

variable address x FP 12 y FP - 8 int main() { int x = 17; int y = -2; x = 8*x + y; return 0; }.file "multby8.c".text.globl main.type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $16, %esp movl $17, -12(%ebp) # x = 17 movl $-2, -8(%ebp) # y = -2 movl -12(%ebp), %eax # eax = x sall $3, %eax addl -8(%ebp), %eax movl %eax, -12(%ebp) movl $0, %eax addl $16, %esp

variable address x FP 12 y FP - 8 int main() { int x = 17; int y = -2; x = 8*x + y; return 0; }.file "multby8.c".text.globl main.type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $16, %esp movl $17, -12(%ebp) # x = 17 movl $-2, -8(%ebp) # y = -2 movl -12(%ebp), %eax # eax = x sall $3, %eax # multiply by 8 is <<3 addl -8(%ebp), %eax movl %eax, -12(%ebp) movl $0, %eax addl $16, %esp

variable address x FP 12 y FP - 8 int main() { int x = 17; int y = -2; x = 8*x + y; return 0; }.file "multby8.c".text.globl main.type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $16, %esp movl $17, -12(%ebp) # x = 17 movl $-2, -8(%ebp) # y = -2 movl -12(%ebp), %eax # eax = x sall $3, %eax # multiply by 8 is <<3 addl -8(%ebp), %eax # eax += y movl %eax, -12(%ebp) # x = eax movl $0, %eax # return 0 addl $16, %esp