Assembly Language for Intel-Based Computers, 5th Edition

Slides:



Advertisements
Similar presentations
Introduction to Assembly Language
Advertisements

Assembly Language for Intel-Based Computers, 4 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify and.
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Assembly Programming Notes for Practical2 Munaf Sheikh
Assembly Language for x86 Processors 6th Edition
CS2422 Assembly Language and System Programming Assembly Language Fundamentals Department of Computer Science National Tsing Hua University.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Symbolic Constants Equal-Sign Directive Calculating.
Outline Learning Assembly by an Example.  Program Formats  Some Simple Instructions  Assemble and Execute Learning Another Example  Data Definition.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Intrinsic Data Types (1 of 2) BYTE, SBYTE 8-bit unsigned.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved.
1 Lecture 3: Assembly Language Fundamentals Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Assembly Language for Intel-Based Computers Chapter 3: Assembly Language Fundamentals Kip Irvine.
Introduction to Assembly Language
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify.
Coding.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved. You.
CS2422 Assembly Language and System Programming High-Level Language Interface Department of Computer Science National Tsing Hua University.
CS2422 Assembly Language & System Programming September 26, 2006.
Joseph L. Lindo Assembly Programming Sir Joseph Lindo University of the Cordilleras.
Chapter 2 Software Tools and Assembly Language Syntax.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify.
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Assembly Language for x86 Processors 7th Edition
Chapter 3: Assembly Language Fundamentals
Assembly Fundamentals Computer Organization and Assembly Languages Yung-Yu Chuang 2005/10/13 with slides by Kip Irvine.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals Assembling, Linking and Running Programs Example Programs.
Assembly Language for x86 Processors 7th Edition
1/2002JNM1 Basic Elements of Assembly Language Integer Constants –If no radix is given, the integer is assumed to be decimal. Int 21h  Int 21 –A hexadecimal.
Assembly Language for x86 Processors 6th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved. You may modify.
Intel x86 Assembly Fundamentals Computer Organization and Assembly Languages Yung-Yu Chuang with slides by Kip Irvine.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved.
Introduction to 8086 Assembly Language Assembly Language Programming University of Akron Dr. Tim Margush.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Summer 2014 Chapter 3: Assembly Language Fundamentals.
Chapter Five–80x86 Assembly program development Principles of Microcomputers 2015年11月15日 2015年11月15日 2015年11月15日 2015年11月15日 2015年11月15日 2015年11月15日 1.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
CSC 221 Computer Organization and Assembly Language
Sahar Mosleh California State University San MarcosPage 1 Introduction to Assembly language Data Definition Reserve words Labels Instruction Mnemonic Hello.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (c) Pearson Education, 2002.
New/Old Assembler Directives Data Definition Statement syntax –[name] directive initializer [,initializer]… At least one initializer is required in a.
Assembly Language for x86 Processors 6th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved. You may modify.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
CS2422 Assembly Language and System Programming 0 Week 7 & 8 Intro. To Assembly Programming.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved. You.
1 Chapter 3 Assembly Language Fundamentals Assembly Language for Intel-Based Computers, 4th edition Revised 3/11/05.
2/27/2003 Lecture 6: Assembly Language Fundamentals Assembly Language for Intel-Based Computers 4th edition Kip R. Irvine.
CSC 221 Computer Organization and Assembly Language
Assembly Language for x86 Processors 6th Edition
Format of Assembly language
Assembly Lab 3.
Data Transfers, Addressing, and Arithmetic
Assembly Language programming
x86 Assembly Language Fundamentals
Assembly Language Ms. V.Anitha AP/CSE SCT
Assembly Language Fundamentals
Microprocessor and Assembly Language
Assembly Language for Intel-Based Computers, 5th Edition
Assembly Language for Intel-Based Computers
Intel x86 Assembly Fundamentals
Assembler Directives end label end of program, label is entry point
Computer Organization and Assembly Language
Hardware & Software Architecture
Introduction to 8086 Assembly Language
Computer Architecture and System Programming Laboratory
BASIC SYNTAX OF ASSEMBLY LANGUAGE “HELLO WORLD” PROGRAM.
Presentation transcript:

COS2014 Assembly Language Fundamentals

Assembly Language for Intel-Based Computers, 5th Edition Kip Irvine

Chapter Overview Basic Elements of Assembly Language Example: Adding and Subtracting Integers Assembling, Linking, and Running Programs Defining Data Symbolic Constants Real-Address Mode Programming

Starting with an Example TITLE Add and Subtract (AddSub.asm) ; Adds and subtracts three 32-bit integers ; (10000h + 40000h + 20000h) INCLUDE Irvine32.inc .code main PROC mov eax,10000h ; EAX = 10000h add eax,40000h ; EAX = 50000h sub eax,20000h ; EAX = 30000h call DumpRegs ; display registers exit main ENDP END main Title/header Include file Code section

Chapter Overview Basic Elements of Assembly Language Integer constants and expressions Character and string constants Reserved words and identifiers Directives and instructions Labels Mnemonics and Operands Comments Example: Adding and Subtracting Integers Assembling, Linking, and Running Programs Defining Data Symbolic Constants Real-Address Mode Programming

Reserved Words, Directives TITLE: Define program listing title Reserved word of directive Reserved words Instruction mnemonics, directives, type attributes, operators, predefined symbols See MASM reference in Appendix A Directives: Commands for assembler TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc .code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main

Directive vs Instruction Directives: tell assembler what to do Commands that are recognized and acted upon by the assembler, e.g. declare code, data areas, select memory model, declare procedures, etc. Not part of the Intel instruction set Different assemblers have different directives Instructions: tell CPU what to do Assembled into machine code by assembler Executed at runtime by the CPU Member of the Intel IA-32 instruction set Format: LABEL (option), Mnemonic, Operands, Comment

Comments Single-line comments Multi-line comments TITLE Add and … begin with semicolon (;) Multi-line comments begin with COMMENT directive and a programmer-chosen character, end with the same character, e.g. COMMENT ! Comment line 1 Comment line 2 ! TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc .code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main

Include Files INCLUDE directive: TITLE Add and … Copies necessary definitions and setup information from a text file named Irvine32.inc, located in the assembler’s INCLUDE directory (see Chapt 5) TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc .code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main

Code Segment .code directive: TITLE Add and … Marks the beginning of the code segment, where all executable statements in a program are located TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc .code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main

Procedure Definition Procedure defined by: TITLE Add and … [label] PROC [label] ENDP Label: Place markers: marks the address (offset) of code and data Assigned a numeric address by assembler Follow identifier rules Data label: must be unique, e.g. myArray Code label: target of jump and loop instructions, e.g. L1: TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc .code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main

Identifiers Identifiers: TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc .code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main Identifiers: A programmer-chosen name to identify a variable, a constant, a procedure, or a code label 1-247 characters, including digits not case sensitive first character must be a letter, _, @, ?, or $

Integer Constants Optional leading + or – sign Binary, decimal, hexadecimal, or octal digits Common radix characters: h – hexadecimal d – decimal b – binary r – encoded real Examples: 30d, 6Ah, 42, 1101b Hexadecimal beginning with letter: 0A5h

Instructions Destination operand Source operand immediate values [label:] mnemonic operand(s) [;comment] Instruction mnemonics: help to memorize examples: MOV, ADD, SUB, MUL, INC, DEC Operands: constant constant expression register memory (data label, register) TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc .code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main Destination operand Source operand immediate values

Instruction Format Examples No operands stc ; set Carry flag nop ; no operation One operand inc eax ; register inc myByte ; memory Two operands add ebx,ecx ; register, register sub myByte,25 ; memory, constant add eax,36 * 25 ; register, constant-expr.

I/O Not easy, if program by ourselves Two steps: call DumpRegs: TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc .code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main Not easy, if program by ourselves Will use the library provided by the author Two steps: Include the library (Irvine32.inc) in your code Call the subroutines call DumpRegs: Calls the procedure to displays current values of processor registers

Remaining exit: END main: TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc .code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main exit: Halts the program Not a MSAM keyword, but a command defined in Irvine32.inc END main: Marks the last line of the program to be assembled Identifies the name of the program’s startup procedure

Example Program Output Program output, showing registers and flags 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

Alternative Version of AddSub TITLE Add and Subtract (AddSubAlt.asm) ; adds and subtracts 32-bit integers .386 .MODEL flat,stdcall .STACK 4096 ExitProcess PROTO, dwExitCode:DWORD DumpRegs PROTO .code main PROC mov eax,10000h ; EAX = 10000h add eax,40000h ; EAX = 50000h sub eax,20000h ; EAX = 30000h call DumpRegs INVOKE ExitProcess,0 main ENDP END main

Explanations .386 directive: .MODEL directive: PROTO directives: Minimum processor required for this code .MODEL directive: Generate code for protected mode program Stdcall: enable calling of Windows functions PROTO directives: Prototypes for procedures ExitProcess: Windows function to halt process INVOKE directive: Calls a procedure or function Calls ExitProcess and passes it with a return code of zero

Suggested Program Template TITLE Program Template (Template.asm) ; Program Description: ; Author: ; Creation Date: ; Revisions: ; Date: Modified by: INCLUDE Irvine32.inc .data ; (insert variables here) .code main PROC ; (insert executable instructions here) exit main ENDP ; (insert additional procedures here) END main

What's Next Basic Elements of Assembly Language Example: Adding and Subtracting Integers Assembling, Linking, and Running Programs Defining Data Symbolic Constants Real-Address Mode Programming

Assemble-Link-Execute Cycle Steps from creating a source program through executing the compiled program http://kipirvine.com/asm/gettingStarted/index.htm

Download, Install, and Run MASM 6.15 (with all examples of textbook) Masm615.zip: download from the course web site Unzip the archive and run setup.exe Choose the installation directory Suggest using the default directory See index.htm in the archive for details Go to C:\Masm615 (if installed default) Write assembly source code TextPad, NotePad++, UltraEdit or … make32 xxx (where xxx is your file name)

Suggestion Study make32.bat and make16.bat To know where assembling stage and linking stage are Think about linking with other language (ex: C or C++ or …) Understand that MASM is only one of the assemblers, and there are still many other assemblers to use Try to use NASM or TASM Try to use high‐level compiler to generate assembly codes gcc or visual c++ or turbo c or …

Listing File Use it to see how your program is compiled Contains source code addresses object code (machine language) segment names symbols (variables, procedures, and constants) Example: addSub.lst

Listing File 00000000 .code 00000000 main PROC 00000000 B8 00010000 mov eax,10000h 00000005 05 00040000 add eax,40000h 0000000A 2D 00020000 sub eax,20000h 0000000F E8 00000000E call DumpRegs exit 00000014 6A 00 * push +000000000h 00000016 E8 00000000E * call ExitProcess 0000001B main ENDP END main memory content address

What's Next Basic Elements of Assembly Language Example: Adding and Subtracting Integers Assembling, Linking, and Running Programs Defining Data Intrinsic Data Types Data Definition Statement Defining BYTE, SBYTE, WORD, SWORD, DWORD, SDWORD, QWORD, TBYTE Defining Real Number Data Little Endian Order Symbolic Constants Real-Address Mode Programming

Intrinsic Data Types BYTE 8-bit unsigned integer SBYTE 8-bit signed integer WORD 16-bit unsigned integer SWORD 16-bit signed integer DWORD 32-bit unsigned integer SDWORD 32-bit signed integer FWORD 48-bit integer (Far pointer in protected mode) QWORD 64-bit integer TBYTE 80-bit (10-byte) integer REAL4 32-bit (4-byte) IEEE short real REAL8 64-bit (8-byte) IEEE long real REAL10 80-bit (10-byte) IEEE extended real

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] . . . value1 BYTE 10 All initializers become binary data in memory Use ? if no initialization necessary Example: Var1 BYTE ?

Defining BYTE and SBYTE Data Each of following defines a single byte of storage: value1 BYTE 'A' ; character constant value2 BYTE 0 ; smallest unsigned byte value3 BYTE 255 ; largest unsigned byte value4 SBYTE -128 ; smallest signed byte value5 SBYTE +127 ; largest signed byte value6 BYTE ? ; uninitialized byte MASM does not prevent you from initializing a BYTE with a negative value, but it is considered poor style If you declare a SBYTE variable, the Microsoft debugger will automatically display its value in decimal with a leading sign

Defining Byte Arrays Examples that use multiple initializers: 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

Defining Strings An array of characters Usually enclosed in quotation marks Will often be null-terminated To continue a single string across multiple lines, end each line with a comma str1 BYTE "Enter your name",0 str2 BYTE 'Error: halting program',0 str3 BYTE 'A','E','I','O','U' greeting BYTE "Welcome to the Encryption Demo program " BYTE "created by Kip Irvine.",0 menu BYTE "Checking Account",0dh,0ah,0dh,0ah, "1. Create a new account",0dh,0ah, "2. Open an existing account",0dh,0ah, "Choice> ",0 End-of-line sequence: 0Dh = carriage return 0Ah = line feed Is str1 an array?

Using the DUP Operator Use DUP to allocate (create space for) an array or string Syntax: counter DUP (argument) Counter and argument must be constants or constant expressions var1 BYTE 20 DUP(0) ; 20 bytes, all equal to zero var2 BYTE 20 DUP(?) ; 20 bytes, uninitialized var3 BYTE 4 DUP("STACK"); 20 bytes, ; "STACKSTACKSTACKSTACK" var4 BYTE 10,3 DUP(0),20 ; 5 bytes

Defining WORD and SWORD Define storage for 16-bit integers or double characters single value or multiple values word1 WORD 65535 ; largest unsigned value word2 SWORD –32768 ; smallest signed value word3 WORD ? ; uninitialized, unsigned word4 WORD "AB" ; double characters myList WORD 1,2,3,4,5 ; array of words array WORD 5 DUP(?) ; uninitialized array

Defining Other Types of Data Storage definitions for 32-bit integers, quadwords, tenbyte values, and real numbers: val1 DWORD 12345678h ; unsigned val2 SDWORD –2147483648 ; signed val3 DWORD 20 DUP(?) ; unsigned array val4 SDWORD –3,–2,–1,0,1 ; signed array quad1 QWORD 1234567812345678h val1 TBYTE 1000000000123456789Ah rVal1 REAL4 -2.1 rVal2 REAL8 3.2E-260 rVal3 REAL10 4.6E+4096 ShortArray REAL4 20 DUP(0.0)

Adding Variables to AddSub TITLE Add and Subtract, Version 2 (AddSub2.asm) ; This program adds and subtracts 32-bit unsigned ; integers and stores the sum in a variable. INCLUDE Irvine32.inc .data val1 DWORD 10000h val2 DWORD 40000h val3 DWORD 20000h finalVal DWORD ? .code main PROC mov eax,val1 ; start with 10000h add eax,val2 ; add 40000h sub eax,val3 ; subtract 20000h mov finalVal,eax ; store the result (30000h) call DumpRegs ; display the registers exit main ENDP END main

Listing File 00000000 .data 00000000 00010000 val1 DWORD 10000h 0000000C 00000000 finalVal DWORD ? 00000000 .code 00000000 main PROC 00000000 A1 00000000 R mov eax,val1 ; start with 10000h 00000005 03 05 00000004 R add eax,val2 ; add 40000h 0000000B 2B 05 00000008 R sub eax,val3 ; subtract 20000h 00000011 A3 0000000C R mov finalVal,eax; store result 00000016 E8 00000000 E call DumpRegs ; display registers exit 00000022 main ENDP

C vs Assembly main() .data { val1 DWORD 10000h int val1=10000h; int finalVal; finalVal = val1 + val2 - val3; } .data val1 DWORD 10000h val2 DWORD 40000h val3 DWORD 20000h finalVal DWORD ? .code main PROC mov eax,val1 add eax,val2 sub eax,val3 mov finalVal,eax call DumpRegs exit main ENDP

What's Next Basic Elements of Assembly Language Example: Adding and Subtracting Integers Assembling, Linking, and Running Programs Defining Data Symbolic Constants Equal-Sign Directive Calculating the Sizes of Arrays and Strings EQU Directive TEXTEQU Directive Real-Address Mode Programming

Equal-Sign Directive name = expression expression is a 32-bit integer (expression or constant) may be redefined name is called a symbolic constant Also OK to use EQU good programming style to use symbols COUNT = 500 … mov al,COUNT

Calculating the Size of Arrays Current location counter: $ Size of a byte array Subtract address of list and difference is the number of bytes Size of a word array Divide total number of bytes by 2 (size of a word) list BYTE 10,20,30,40 ListSize = ($ - list) list WORD 1000h,2000h,3000h,4000h ListSize = ($ - list) / 2

EQU Directive Define a symbol as either an integer or text expression Cannot be redefined OK to use expressions in EQU: Matrix1 EQU 10 * 10 Matrix1 EQU <10 * 10> No expression evaluation if within < > EQU accepts texts too PI EQU <3.1416> pressKey EQU <"Press any key to continue",0> .data prompt BYTE pressKey

TEXTEQU Directive Define a symbol as either an integer or text expression Called a text macro Can be redefined continueMsg TEXTEQU <"Do you wish to continue (Y/N)?"> rowSize = 5 .data prompt1 BYTE continueMsg count TEXTEQU %(rowSize * 2) ; evaluates expression setupAL TEXTEQU <mov al,count> .code setupAL ; generates: "mov al,10"

What's Next Basic Elements of Assembly Language Example: Adding and Subtracting Integers Assembling, Linking, and Running Programs Defining Data Symbolic Constants Real-Address Mode Programming (skipped)

Summary Integer expression, character constant Directive – interpreted by the assembler Instruction – executes at runtime Code, data, and stack segments Source, listing, object, map, executable files Data definition directives: BYTE, SBYTE, WORD, SWORD, DWORD, SDWORD, QWORD, TBYTE, REAL4, REAL8, and REAL10 DUP operator, location counter ($) Symbolic constant EQU and TEXTEQU