Machine Code and Assemblers November 6

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

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

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

Dr. Ramesh K. Karne Department of Computer and Information Sciences, Towson University, Towson, MD /12/2014 Slide 1

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Chapter 11. Addressing Modes

ADDRESSING MODES. Operands specify the data to be used by an instruction

HLA v2.0 Intermediate Code Design Documentation

Machine and Assembly Language Principles

Chapter 2. lw $s1,100($s2) $s1 = Memory[$s2+100] sw $s1,100($s2) Memory[$s2+100] = $s1

Chapter 3: Addressing Modes

Interfacing Compiler and Hardware. Computer Systems Architecture. Processor Types And Instruction Sets. What Instructions Should A Processor Offer?

Module 3 Instruction Set Architecture (ISA)

Systems Architecture I

x86 assembly CS449 Fall 2017

Addressing Modes on the x86

Lecture 3: Instruction Set Architecture

Computer System Architecture

Name: CMSC 313 Fall 2001 Computer Organization & Assembly Language Programming Exam 1. Question Points I. /34 II. /30 III.

Computer Processors. Part 2. Components of a Processor. Execution Unit The ALU. Execution Unit. The Brains of the Box. Processors. Execution Unit (EU)

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

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

The x86 Architecture

Practical Malware Analysis

Hardware and Software Architecture. Chapter 2

CS241 Computer Organization Spring 2015 IA

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 10. Advanced Procedures

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

Assembly Language Each statement in an assembly language program consists of four parts or fields.

W4118: PC Hardware and x86. Junfeng Yang

EEM336 Microprocessors I. The Microprocessor and Its Architecture

Basic Pentium Instructions. October 18

History of the Intel 80x86

CSC201, SECTION 002, Fall 2000: Homework Assignment #3

EECE416 :Microcomputer Fundamentals and Design. X86 Assembly Programming Part 1. Dr. Charles Kim

Credits and Disclaimers

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

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

Assembly Language. Lecture 2 - x86 Processor Architecture. Ahmed Sallam

CPU. IBM PC and compatible Memory Structure

U23 - Binary Exploitation

Assembly Language: Function Calls

We can study computer architectures by starting with the basic building blocks. Adders, decoders, multiplexors, flip-flops, registers,...

Lab 2: Introduction to Assembly Language Programming

Computer Organization & Assembly Language Programming

Machine Language, Assemblers and Linkers"

Lecture 4 CIS 341: COMPILERS

Assembly Language: Function Calls" Goals of this Lecture"

T Reverse Engineering Malware: Static Analysis I

Program Exploitation Intro

Assembly Language. Lecture 2 x86 Processor Architecture

Digital Forensics Lecture 3 - Reverse Engineering

Review Questions. 1 The DRAM problem [5 points] Suggest a solution. 2 Big versus Little Endian Addressing [5 points]

6x86 PROCESSOR Superscalar, Superpipelined, Sixth-generation, x86 Compatible CPU

x86 Assembly Tutorial COS 318: Fall 2017

Assembly Language: Function Calls" Goals of this Lecture"

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

UMBC. 1 (Feb. 9, 2002) seg_base + base + index. Systems Design & Programming 80x86 Assembly II CMPE 310. Base-Plus-Index addressing:

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

Outline. x86 Architecture

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

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

Low Level Programming Lecture 2. International Faculty of Engineerig, Technical University of Łódź

CPEG421/621 Tutorial

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

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

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

x86 assembly CS449 Spring 2016

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

Complex Instruction Set Computer (CISC)

How Software Executes

Addressing Modes. Outline

Instruction Set Architectures

Credits and Disclaimers

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

6/20/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

Memory Models. Registers

Instruction Set Architectures

The Instruction Set. Chapter 5

1 Overview of the AMD64 Architecture

Computer Architecture and Assembly Language. Practical Session 3

Virtual Machines and Dynamic Translation: Implementing ISAs in Software

Procedure-Calling Conventions October 30

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

x86 Programming I CSE 351 Winter

Introduction to IA-32. Jo, Heeseung

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

EEM336 Microprocessors I. Addressing Modes

INTRODUCTION TO IA-32. Jo, Heeseung

Assembly Language Programming: Procedures. EECE416 uc. Charles Kim Howard University. Fall

IA-32 Architecture. CS 4440/7440 Malware Analysis and Defense

Chapter 11. Instruction Sets: Addressing Modes and Formats. Yonsei University

Communicating with People (2.8)

RISC I from Berkeley. 44k Transistors 1Mhz 77mm^2

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

Writing 32-Bit Applications

T Jarkko Turkulainen, F-Secure Corporation

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

THEORY OF COMPILATION

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Transcription:

Machine Code and Assemblers November 6 CSC201 Section 002 Fall, 2000

Definitions Assembly time vs. link time vs. load time vs. run time.c file.asm file.obj file.exe file compiler assembler linker Running program Loader (O.S.) CSC201 Section Copyright 2000, Douglas Reeves 2

The Assembler Converts assembly language program into a machine-executable binary program The assembler does as much processing of the program as possible so program running time will be speeded up CSC201 Section Copyright 2000, Douglas Reeves 3

Tasks of the Assembler Macro expansion (preprocessing) Organize memory, reserve and initialize memory for data Convert instructions and constants into binary Convert symbolic addresses into absolute addresses Mark external references for later resolution CSC201 Section Copyright 2000, Douglas Reeves 4

Memory Organization Separate memory "regions" for the stack, the code, and the data Memory is allocated sequentially as each part of the program is processed The assembler keeps track of how much has been allocated so far Example memory organization Code stack data CSC201 Section Copyright 2000, Douglas Reeves 5

The Symbol Table Symbolic labels help readability, relocatability, and ease of modification A list of the symbolic names encountered so far Data definitions Code labels CSC201 Section Copyright 2000, Douglas Reeves 6

The Symbol Table For code and data, keep track of... Memory address corresponding to that symbolic name, determined during memory allocation For data, keep track of... Data type (byte? word? doubleword? floating-point?) Initial value Size (amount of memory) Table has no use after assembly CSC201 Section Copyright 2000, Douglas Reeves 7

Directives Provide information to the assembler, used to generate correct output "title" -- no processing ".486" -- check for 486-compatible instruction syntax, and generate 486 machine code ".model flat" -- use the flat (not segmented) memory model CSC201 Section Copyright 2000, Douglas Reeves 8

Directives "stdcall" -- generate standard names for symbols, and use a standardized calling convention for procedure calls ".data" -- following are data definitions data definitions ("db", "dw",..) -- allocate the appropriate amount of memory, properly initialized ".code" -- following are executable instructions "end" -- ignore the remainder of this file CSC201 Section Copyright 2000, Douglas Reeves 9

Compiling C to Assembler The user's program will be called by the operating system like a procedure On entry, save and initialize EBP, save registers For this example, assumes local storage has been allocated on the stack *before* the program is called On exit, restore registers and execute "ret" statement CSC201 Section Copyright 2000, Douglas Reeves 10

x86 Machine Code Although assembly languages on machines are similar, machine languages are completely different Pentium machine code "peculiarities" Variable length instruction encodings Many addressing modes Extra byte needed to specify addressing mode in some cases "Repeat" prefixes None of these commonly used today CSC201 Section Copyright 2000, Douglas Reeves 11

Address Resolution As symbols are referenced, replace by the starting address of memory allocated to that symbol What if symbol is defined further down in the program (ex. program label)? Two-pass process One pass to generate machine code and allocate memory second pass to resolve symbolic address references CSC201 Section Copyright 2000, Douglas Reeves 12

Instruction Formats Motivation: understand slightly how CPU "reads" an executable program Goals for instruction format design (conflicting): use minimum space, vs. make decoding as easy as possible x86 encoding: complex, irregular, baroque even All instruction encodings have an "opcode" specifier For the most part, we are looking at 32-bit operand versions only CSC201 Section Copyright 2000, Douglas Reeves 13

Register Specification We re looking at doubleword-length registers only Note: not in order! Binary Symbol 000 001 010 011 100 101 110 111 Register EAX ECX EDX EBX ESP EBP ESI EDI CSC201 Section Copyright 2000, Douglas Reeves 14

Instruction Encoding Instructions = 1 to 9 bytes long First byte Opcode,D,W -or-opcode,v,w -or-opcode,w -or- Opcode,Reg Second byte (if needed) = the Mod,Reg,R/M byte Mod = 2 bits, Reg = 3 bits, R/M = 3 bits) Third byte needed by some addressing modes: SIB byte Scaling = 2 bits, Index register = 3 bits, Base register = 3 bits CSC201 Section Copyright 2000, Douglas Reeves 15

D, V, W D: 0= "reg" field is source, 1= "reg" field is destination V: 0= shift by 1 bit, 1= shift by amount in cl register W: 0= data is byte length, 1= data is word length CSC201 Section Copyright 2000, Douglas Reeves 16

Encoding Instructions Using Appendix C CSC201 Section Copyright 2000, Douglas Reeves 17

Decoding Instructions Using Appendix C CSC201 Section Copyright 2000, Douglas Reeves 18

Conditional Jump Conditional jump instruction encodes displacement from next instruction 32-bit offset for our purposes CSC201 Section Copyright 2000, Douglas Reeves 19