Assembly Language Programming

Similar documents
Chapter 3: Assembly Language Fundamentals. Cristina G. Rivera

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 3: Assembly Language Fundamentals

Assembly Language for Intel-Based Computers, 5 th Edition. Kip Irvine. Chapter 3: Assembly Language Fundamentals

Chapter Overview. Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 3: Assembly Language Fundamentals.

Assembly Language for Intel-Based Computers, 5 th Edition

Assembly Language for Intel-Based Computers, 5 th Edition. Chapter 3: Assembly Language Fundamentals

Assembly Fundamentals

Assembly Language Fundamentals

Assembly Language. Lecture 3 Assembly Fundamentals

Introduction to Assembly Language

Assembly Language Fundamentals

Chapter 3 (Part a) Assembly Language Fundamentals

Introduction to Assembly Language

Lab 3: Defining Data and Symbolic Constants

Assembly Language for Intel-Based Computers, 4 th Edition

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

Assembly Language LAB

Faculty of Engineering Computer Engineering Department Islamic University of Gaza Assembly Language Lab # 2 Assembly Language Fundamentals

Chapter 3: Addressing Modes

Computer Organization and Assembly Language. Lab Session 3

Lab 2: Introduction to Assembly Language Programming

Lab 4: Basic Instructions and Addressing Modes

Module 3 Instruction Set Architecture (ISA)

Marking Scheme. Examination Paper Department of CE. Module: Microprocessors (630313)

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

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

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

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

Practical Malware Analysis

The x86 Architecture

CSE 505 Lecture 8: Introduction to Assembly Language

Basic Assembly SYSC-3006

We will first study the basic instructions for doing multiplications and divisions

ORG ; TWO. Assembly Language Programming

EEM336 Microprocessors I. Addressing Modes

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

Microprocessor and Assembly Language Week-5. System Programming, BCS 6th, IBMS (2017)

CPU. IBM PC and compatible Memory Structure

Defining and Using Simple Data Types

ECOM Computer Organization and Assembly Language. Computer Engineering Department CHAPTER 7. Integer Arithmetic

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

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

Lab 5: Input/Output using a Library of Procedures

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 5: Procedures. Chapter Overview. The Book's Link Library

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

Machine and Assembly Language Principles

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

Microprocessors & Assembly Language Lab 1 (Introduction to 8086 Programming)

Constants and Expressions. Lecture 7: Assembly Language Programs. Constants and Expressions (cont.) Statements. Names. Assembly Directives

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

Lab 6: Conditional Processing

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

Chapter 7 Integer Arithmetic

Assembly Language Fundamentals. Chapter 3

Experiment 3 3 Basic Input Output

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

Language of x86 processor family

Introduction to IA-32. Jo, Heeseung

Marking Scheme. Examination Paper Department of CE. Module: Microprocessors (630313)

Marking Scheme. Examination Paper. Module: Microprocessors (630313)

INTRODUCTION TO IA-32. Jo, Heeseung

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 8. Conditional Processing

Assembly Language for Intel-Based Computers, 4 th Edition

Complex Instruction Set Computer (CISC)

Integer Arithmetic Part2

22 Assembly Language for Intel-Based Computers, 4th Edition. 3. Each edge is a transition from one state to another, caused by some input.

Ethical Hacking. Assembly Language Tutorial

EEM336 Microprocessors I. Data Movement Instructions

Integer Arithmetic. Shift and rotate. Overview. Shift and Rotate Instructions

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

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

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

Faculty of Engineering Student Number:

MICROPROCESSOR TECHNOLOGY

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

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 6: Conditional Processing

COMPUTER ENGINEERING DEPARTMENT

Assembly Language LAB

Process Layout and Function Calls

Reverse Engineering II: The Basics

EC 333 Microprocessor and Interfacing Techniques (3+1)

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

Assignment no:4 on chapter no :3 : Instruction set of 8086

CS499. Intel Architecture

Computer Organization and Assembly Language. Lab Session 4

Instructions moving data

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

Microprocessor. By Mrs. R.P.Chaudhari Mrs.P.S.Patil

Assembly Language. Lecture 5 Procedures

3.1 DATA MOVEMENT INSTRUCTIONS 45

Integer Arithmetic. Computer Organization and Assembly Languages Yung-Yu Chuang 2005/11/17. with slides by Kip Irvine

Assembly Language. Lecture 5 Procedures

Assembly Language Lab # 4 Data Transfer & Arithmetic (1)

mith College Computer Science CSC231 - Assembly Week #4 Dominique Thiébaut

Conditional Processing

SOEN228, Winter Revision 1.2 Date: October 25,

Lecture 9. INC and DEC. INC/DEC Examples ADD. Arithmetic Operations Overflow Multiply and Divide

Arithmetic and Logic Instructions And Programs

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

Transcription:

Assembly Language Programming

Integer Constants Optional leading + or sign Binary, decimal, hexadecimal, or octal digits Common radix characters: h hexadecimal d decimal b binary r encoded real q/o - octal Examples: 30d, 6Ah, 42, 1101b, 33q, 22o 2 [{+ -}] digits [radix] If no radix mentioned, then default is decimal Hexadecimal number that beginning with letter must have a leading zero to prevent the assembler interpreting it as an identifier : 0A5h

Character and String Constants Enclose character in single or double quotes 'A', "x" ASCII character = 1 byte Enclose strings in single or double quotes "ABC" 'xyz' Each character occupies a single byte Embedded quotes: 'Say "Goodnight," Gracie' 3

Reserved Words Reserved words cannot be used as identifiers Instruction mnemonics (MOVE, ADD, MUL) Directives (INCLUDE) type attributes (BYTE, WORD) Operators (+, -) predefined symbols (@data) 4 Directives command understood by the assembler not part of Intel instruction set case insensitive

Identifiers Identifiers (programmer-chosen name) May contain 1-247 characters, including digits Not case-sensitive (by default) first character must be a letter (A..Z,a..z), _, @,? or $ Subsequent character may also be a digit Identifier cannot be the same as assembler reserved word. common sense create identifier names that easy to understand 5 Avoid using single @ sign as first character. Ex : Valid identifier Var1, Count, MAX, $first, open_file, _12345, @@myfile,_main

Directives Commands that are recognized / understood and acted upon by the assembler Not part of the Intel instruction set Used to declare code, data areas, select memory model, declare procedures, etc. Not case sensitive (.data,.data,.data they are the same) Different assemblers have different directives NASM!= MASM, for example Examples :.DATA identify area of a program that contains variables.code identify area of a program that contains instructions PROC identify beginning of a procedure. name PROC 6

Instructions Assembled into machine code by assembler Executed at runtime by the CPU after the program has been loaded into memory and started. Member of the Intel IA-32 instruction set Parts : Label (optional) Mnemonic (required) Operand (required) Comment (optional) 7

Example: Standard Format for Instruction 8 Label Mnemonics Operands ; Comments Instruction 1 mov eax, 1000h ; eax=1000h Instruction 2 target: add ebx, 4000h ;ebx=ebx+4000h

Labels Act as place marker marks the address (offset) of code and data Follow identifier rules Data label must be unique example: myarray mov ax, myarray Code label target of jump and loop instructions example: L1: 9 target : mov ax,bx jmp target

Mnemonics and Operands Instruction Mnemonics is a short word that identifies the operation carried out by an instruction (useful name). Examples : MOV ADD SUB MUL INC DEC JMP CALL 10 move (assign one value to another) add two values subtract one value from another multiply two values increment decrement jump to a new location call a procedure

Operands An stc assembly ;set carry language flag can no have operand between zero to three operands (depending on the type of instruction) Types of operand : constant (immediate value) Example: 96, 2005h, 101011010b constant expression Example: 2+4 register 11 inc eax ; add 1 to eax 1 operand mov count, ebx ; move content of reg ebx into count Example: EAX, EBX, AX, AH memory (data label) Example: count

Comments Comments are good. explain the program's purpose when it was written, and by whom revision information tricky coding techniques application-specific explanations Single-line comments begin with semicolon (;) Example: ; this is a comment for add BX to AX Multi-line comments begin with COMMENT directive and a programmer-chosen character end with the same programmer-chosen character 13

Example: Multi-line Comments 14 COMMENT! This is a comment This line is also a comment Everything here are comments but after exclamation character it is no longer a comment!

Example: Program Template 15 TITLE Budget Calculator (budget.asm) ; Program Description: ; Author: ; Date Created: ; Last Modification Date: INCLUDE Irvine32.inc.data (insert variables here) val1 dword 10000h :.code main PROC (insert executable instructions here) mov ax,@data ; initialize DS exit ; exit to o/s main ENDP (insert additional procedures here) END main :

Example: Adding and Subtracting Integers TITLE Add and Subtract (AddSub.asm) ; This program adds and subtracts 32-bit integers. INCLUDE Irvine32.inc.code main PROC INCLUDE directive copies necessary definitions and setup info from text file named Irvine32.inc mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main ; EAX = 10000h ; EAX = 50000h ; EAX = 30000h Calls a procedure that displays the current values of the CPU registers.

Example: Adding and Subtracting Integers EAX: 0ABC 0000h mov eax,10000h ; EAX = 10000h EAX: 0001 0000h MOV - Instruction move from source to dest. op. EAX destination operand 10000h source operand add eax,40000h ; EAX = 10000 + 40000 = 50000h EAX: 0005 0000h sub eax,20000h ; EAX = 50000 20000 = 30000h EAX: 0003 0000h

Example Output Program output, showing registers and flags: 19 EAX=00030000 EBX=7FFDF000 ECX=00000101 EDX=FFFFFFFF ESI=00000000 EDI=00000000 EBP=0012FFF0 ESP=0012FFC4 EIP=00401024 EFL=00000206 CF=0 SF=0 ZF=0 OF=0

Registers 1 32 dword word EAX AX AH byte AL

Writing and executing a program 1. write program in Textpad

Writing and executing a program 2. Save and build 32

Writing and executing a program 2. Save and build 32

Writing and executing a program 3. Run MASM

Writing and executing a program 4. View results

Activity 1: Lets Try 1) Set EAX with the value of 123400h 2) Add the content of EAX with 567800h 3) Subtract content of EAX with 77700h 4) Store content of EAX to a variable named TOTAL (declare as DWORD) 27

TOTAL dword? Try changing to TOTAL word? And see what happens.

TITLE Class Exercise 1 (CLASSEXE.asm) ; This program adds and subtracts 32-bit integers. INCLUDE Irvine32.inc.data TOTAL dword 0.code main PROC mov eax,123400h add eax,567800h sub eax,77700h mov total, eax mov ebx, total call DumpRegs exit main ENDP END main

Intrinsic Data Types 30 BYTE, SBYTE (signed-byte) 8-bit unsigned integer; 8-bit signed integer WORD, SWORD 16-bit unsigned & signed integer DWORD, SDWORD 32-bit unsigned & signed integer QWORD (quadword) 64-bit integer TBYTE (tenbyte) 80-bit integer

Data Definition Statement A data definition statement sets aside storage in memory for a variable. May optionally assign a name (label) to the data Syntax: [name] directive initializer [,initializer]... Nilai1 BYTE 34 Nilai2 BYTE A Nilai3 BYTE 27h, 4Ch, 6Ah 31 All initializers become binary data in memory example : 00110010b, 32h, 50d

Defining BYTE and SBYTE Data (8 bits) Each of the following defines a single byte of storage: 32

Example: Sequences of Bytes 33

Example: Defining Bytes list1 BYTE 10,20,30,40 list2 BYTE 10,20,30,40 BYTE 50,60,70,80 BYTE 81,82,83,84 list3 BYTE?,32,41h,00100010b list4 BYTE 0Ah,20h, A,22h 34

Defining Strings A string is implemented as an array of characters For convenience, it is usually enclosed in quotation marks It usually has a null byte at the end Examples: 35

Defining Strings End-of-line character sequence: 0Dh = carriage return 0Ah = line feed Example: 36

Using the DUP (duplicate) Operator Use DUP to allocate (create space for) an array or string. Counter and argument must be constants or constant expressions. 37

Defining WORD and SWORD Data (2 bytes) Define storage for 16-bit integers or double characters single value or multiple values 38

BYTE vs WORD 39

WORD 40

Defining DWORD and SDWORD Data (4 bytes) Storage definitions for signed and unsigned 32-bit integers: 41

Defining QWORD (8 bytes), TBYTE (10 bytes), Real Data Storage definitions for quadwords, tenbyte values, and real numbers: 42

Little Endian Order 43 All data types larger than a byte store their individual bytes in reverse order. The least significant byte (LSB) occurs at the first (lowest) memory address.

Example 1 MyData DWORD 3740625, 2 DUP (2ABCCDEh) 44

Example 2 MyData2 WORD B, 25, 3 DUP (4FAh) 45

Adding Variables to AddSub 46

name = expression Equal-Sign Directive expression is a 32-bit integer (expression or constant) may be redefined name is called a symbolic constant good programming style to use symbols 47

EQU Directive 48