x86 Assembly Tutorial COS 318: Fall 2017

Similar documents
Project 1: Bootloader. COS 318 Fall 2015

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

CS241 Computer Organization Spring 2015 IA

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

W4118: PC Hardware and x86. Junfeng Yang

The x86 Architecture

Introduction to IA-32. Jo, Heeseung

INTRODUCTION TO IA-32. Jo, Heeseung

Complex Instruction Set Computer (CISC)

The Microprocessor and its Architecture

Hardware and Software Architecture. Chapter 2

Addressing Modes on the x86

x86 architecture et similia

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

Code segment Stack segment

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

Basic Execution Environment

Assembler Programming. Lecture 2

Basic Information. CS318 Project #1. OS Bootup Process. Reboot. Bootup Mechanism

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

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

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

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

Assembly Language: Function Calls

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Module 3 Instruction Set Architecture (ISA)

Assembly Language: Function Calls" Goals of this Lecture"

IA32 Intel 32-bit Architecture

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

UMBC. contain new IP while 4th and 5th bytes contain CS. CALL BX and CALL [BX] versions also exist. contain displacement added to IP.

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

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

Assembly Language: Function Calls" Goals of this Lecture"

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

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

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

Reverse Engineering II: The Basics

COS 318: Operating Systems. Overview. Prof. Margaret Martonosi Computer Science Department Princeton University

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

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

Program Exploitation Intro

EEM336 Microprocessors I. Data Movement Instructions

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 September 25, 2018

Credits and Disclaimers

Chapter 11. Addressing Modes

Assembly Language Lab # 9

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

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

System calls and assembler

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

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

8086 INTERNAL ARCHITECTURE

CMSC Lecture 03. UMBC, CMSC313, Richard Chang

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

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

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

Practical Malware Analysis

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

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

x86 assembly CS449 Fall 2017

A4 Sample Solution Ch3

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

iapx Systems Electronic Computers M

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

T Reverse Engineering Malware: Static Analysis I

CS401 Assembly Language Solved MCQS From Midterm Papers

An Introduction to x86 ASM

SRI VENKATESWARA COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF ECE EC6504 MICROPROCESSOR AND MICROCONTROLLER (REGULATION 2013)

9/25/ Software & Hardware Architecture

Systems I. Machine-Level Programming V: Procedures

CS642: Computer Security

Assembly Language: IA-32 Instructions

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

The Instruction Set. Chapter 5

if 2 16bit operands multiplied the result will be

Lab 2: Introduction to Assembly Language Programming

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

Intel x86 Memory. Architecture. The x86 isn't all that complex it just doesn't make a lot of sense. Program Segments. x86 Data and Address Ranges

Reverse Engineering II: The Basics

Come and join us at WebLyceum

Assembly Language Programming Introduction

EC-333 Microprocessor and Interfacing Techniques

Intel 8086 MICROPROCESSOR ARCHITECTURE

Lecture (02) The Microprocessor and Its Architecture By: Dr. Ahmed ElShafee

Process Layout and Function Calls

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

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

CS 16: Assembly Language Programming for the IBM PC and Compatibles

Credits and Disclaimers

Registers. Ray Seyfarth. September 8, Bit Intel Assembly Language c 2011 Ray Seyfarth

Experiment 8 8 Subroutine Handling Instructions and Macros

CC411: Introduction To Microprocessors

The von Neumann Machine

MICROPROCESSOR MICROPROCESSOR ARCHITECTURE. Prof. P. C. Patil UOP S.E.COMP (SEM-II)

CSCI 192 Engineering Programming 2. Assembly Language

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

Microcomputer Architecture..Second Year (Sem.2).Lecture(2) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

MICROPROCESSOR PROGRAMMING AND SYSTEM DESIGN

Instruction Set Architectures

Transcription:

x86 Assembly Tutorial COS 318: Fall 2017

Project 1 Schedule Design Review: Monday 9/25 Sign up for 10-min slot from 3:00pm to 7:00pm Complete set up and answer posted questions (Official) Precept: Monday 9/25, 7:30pm Due: Sunday, 10/01, 11:55pm

Overview Assembly Language Overview Registers, Flags, Memory Addressing, Instructions, Stack / Calling Convention BIOS Quick kernel debugging tutorial

Registers General Purpose Register: 8,16,32 bits 31 15 7 0 AH AL AX = AH AL BH BL BX = BH BL CH CL CX = CH CL DH DL DX = DH DL BP SI DI SP EAX EBX ECX EDX EBP ESI EDI ESP Segment Registers (16bits) CS DS SS ES FS GS Instruction Pointer: EIP (32bits) Flags(32bits): EFLAGS

Flags Function of flags Control the behavior of CPU Save the status of last instruction Details: https://en.wikipedia.org/wiki/flags_register

Flags Important flags: CF: carry flag ZF: zero flag SF: sign flag IF: interrupt (sti, cli) DF: direction (std, cld)

AT&T syntax Prefix register names with % (e.g. %ax) Instruction format: instr src,dest movw %ax,%bx Prefix constants (immediate values) with $ movw $1,%ax Suffix instructions with size of data b for byte,w for word(16bits), l for long(32 bits)

Memory Addressing (Real Mode) 1MB memory Valid address range: 0x00000 ~ 0xFFFFF See full 1MB with 20-bit addresses 16-bit segments and 16-bit offsets

Memory Addressing (Real Mode) Format (AT&T syntax): segment:displacement(base,index) Offset = Base + Index + Displacement Address = (Segment * 16) + offset Displacement: Constant Base: %bx, %bp Index: %si, %di Segment: %cs, %ds, %ss, %es, %fs, %gs

Memory Addressing (Real Mode) segment:displacement(base,index) Components are optional Default segment: %bp: %ss; %bx, %si, %di : %ds; You can override: %es:(%bx) Examples: (%si) = %ds: (%si) memory address: %ds * 16 + %si +4(%bp) = %ss + 4(%bp) memory address: %ss * 16 + 4 + %bp 100 = %ds:100

Instructions: Arithmetic & Logic add/sub{l,w,b} source,dest inc/dec/neg{l,w,b} dest cmp{l,w,b} source,dest and/or/xor{l,w,b} source,dest... Restrictions No more than one memory operand

Instructions: Data Transfer mov{l,w,b} source, dest xchg{l,w,b} dest movsb/movsw %es:(%di) %ds:(%si) Often used with %cx to move a number of bytes movw $0x10,%cx rep movsw Segment registers can only appear with registers

Grows from high to low Stack Layout Lowest address = top of stack %esp points to top of the stack Used to reference temporary variables %ebp points to bottom of stack frame... (function arg. n ) function arg. 1 return address old %ebp local var. 1 (local var. n ) (callee-save regs) callee-save reg 1 %ebp + 8 %ebp + 4 %ebp %ebp - 4 Used for local vars + function args. (temp var. n ) temp var. 1 %esp + 4 %esp

When calling a function: Calling Convention 1. Push caller-save regs onto stack 2. Push function args onto stack 3. Push return address + branch In subroutine: 1. Push old %ebp + set %ebp = %esp 2. Allocate space for local variables... (function arg. n ) function arg. 1 return address old %ebp local var. 1 (local var. n ) (callee-save regs) callee-save reg 1 (temp var. n ) %ebp + 8 %ebp + 4 %ebp %ebp - 4 %esp + 4 3. Push callee-save regs if necessary temp var. 1 %esp

Instructions: Stack Access pushl source %esp %esp - 4 %ss:(%esp) source popl dest dest %ss:(%esp) %esp %esp + 4... (function arg. n ) function arg. 1 return address old %ebp local var. 1 (local var. n ) (callee-save regs) callee-save reg 1 (temp var. n ) temp var. 1 %ebp + 8 %ebp + 4 %ebp %ebp - 4 %esp + 4 %esp

Instructions: Control Flow jmp label %eip label ljmp NEW_CS, offset %cs NEW_CS %eip offset call label push %eip %eip label ret pop %eip

Instructions: Conditional Jump Relies on %eflags bits Most arithmetic operations change %eflags j* label Jump to label if * flag is 1 jn* label Jump to label if * flag is 0

Assembler Directives Commands that speak directly to the assembler Are not instructions Examples:.globl - defines a list of symbols as global.equ - defines a constant (like #define).bytes,.word,.asciz - reserve space in memory

Assembler Segments Organize memory by data properties.text - holds executable instructions.bss - holds zero-initialized data (e.g. static int i;).data - holds initialized data (e.g. char c = a ;).rodata - holds read-only data Stack / Heap - Set up by linker / loader / programmer

BIOS Services Use BIOS services through int instruction Must store parameters in specified registers Triggers a software interrupt int INT_NUM int $0x10 - video services int $0x13 - disk services int $0x16 - keyboard services

Kernel testing / debugging We provide some test cases with each project Run make TEST=1 where appropriate For debugging: use qemu-gdb Run make qemu-gdb In another terminal, run gdb from same directory

Useful GDB Commands r - show register values sreg - show segment registers s - step into instruction n - next instruction c - continue u <start> <stop> - disassembles C code into assembly b - set a breakpoint d <n> - delete a breakpoint bpd / bpe <n> - disable / enable a breakpoint x/nx addr - display hex dump of N words, starting at addr x/ni addr - display N instructions, starting at addr

Design Review Be ready to answer the following questions: At what point does the processor start executing 32-bit code? What exactly causes the switch from 16 to 32-bit mode? What is the last instruction of the boot loader executed, and what is the first instruction of the kernel it loads? Where is the first instruction of the kernel? How does the boot loader decide how many sectors it must read in order to fetch the entire kernel from disk? Where does it get this information?