Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assembly Language for x86 Processors 6th Edition

Similar presentations


Presentation on theme: "Assembly Language for x86 Processors 6th Edition"— Presentation transcript:

1 Assembly Language for x86 Processors 6th Edition
Kip Irvine Chapter 3: Assembly Language Fundamentals Slides prepared by the author Revision date: 2/15/2010 (c) Pearson Education, All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.

2 Directives and Instructions
Assembly language statements are either directives or instructions Instructions are executable statements. They are translated by the assembler into machine instructions. Ex: call MySub ;transfer of control mov ax,5 ;data transfer Directives tells the assembler how to generate machine code, allocate storage, or define segments. They do not execute at run time. Ex: count BYTE 50 ;creates 1 byte ;of storage ;initialized to 50

3 A Template for Assembly Language Programs
TITLE Program Template (Template.asm) ; Program Description: anything after the ‘;’ is ignored ; Author: ; Creation Date: ; Revisions: ; Date: Modified by: INCLUDE Irvine32.inc ; contains library procedures for IA-32 ; for 32-bit protected mode programs .data ; data segment, read and write ; (insert variable declarations here) .code ; code segment, read-only main PROC ; (insert executable instructions here) exit main ENDP ; (insert additional procedures here) END main This is the template to follow in all your programs Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

4 A Template for ASM if Irvine32.inc is not Included
Irvine32.inc: library procedures and setup information for IA-32 .386: identifies as required processor. Use .586 for Pentium. .model: set the running mode to 32-bit protected mode and use the MS-Windows calling convention main PROC: label of the entry point of the program first instruction to execute END: marks the end of the program and identifies the program’s startup procedure exit: macro that halts the program then returns the control to the caller (here the Win32 console) .data and .code: beginning of the data segment and code segment .386 .model flat, stdcall .stack 4096 ExitProcess PROTO, dwExitCode: DWORD DumpRegs PROTO .data ;data declarations .code main PROC … ;instructions here call DumpRegs INVOKE ExitProcess, 0 main ENDP END main

5 The FLAT Memory Model The .model flat directive tells the assembler to generate code that will run in protected mode and in 32-bit mode Also ask the assembler to do whatever is needed in order that code, stack, and data share the same 32-bit memory segment All the segment registers will be loaded with the correct values at load time and do not need to be changed by the programmer Only the offset part of a logical address becomes relevant Each data byte (or instruction) is referred to only by a 32-bit offset address The directives .code and .data mark the beginning of the code and data segments. They are used only for protection .code is read-only .data is read and write

6 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 mov eax,10000h ; EAX = 10000h add eax,40000h ; EAX = 50000h sub eax,20000h ; EAX = 30000h call DumpRegs ; display registers exit main ENDP END main Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

7 Assemble-Link Execute Cycle (Steps to Produce an Executable File)
The following diagram describes the steps from creating a source program through executing the compiled program. If the source code is modified, Steps 2 through 4 must be repeated. All 4 steps performed via the Visual Studio environment. No need to use command lines in a window. See Getting started with MASM and Visual Studio 2012 at for instruction on assembling, linking and running ASM programs using Microsoft Visual Studio Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

8 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 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

9 Map File Information about each program segment:
starting address ending address size segment type Example: addSub.map (16-bit version) Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

10 Integer Constants Optional leading + or – sign
binary, decimal, hexadecimal, or octal digits Common radix characters: h – hexadecimal 1011h q/o – octal 1011q or 1011o d – decimal 1011d or 1011 (base 10 is the default) b – binary 1011b r – encoded real 3F800000r = +1.0 (topic of Chap 12) real E5+05 , 2. , +3.0 , … More examples: 30d, 6Ah, -42, 1101b Hexadecimal beginning with letter: 0A5h A5h is not a number (must start with digit 0) Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

11 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‘ , “123” (this is a string, not a number) Each character occupies a single byte Embedded quotes: 'Say "Goodnight," Gracie' Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

12 Reserved Words and Identifiers
Reserved words cannot be used as identifiers Instruction mnemonics, directives, type attributes, operators, predefined symbols See MASM reference in Appendix A Identifiers are programmer-chosen names Variable, constant, procedure, code label 1 to 247 characters, including digits not case sensitive first character must be a letter, ?, or $ Cannot be the same as an assembler reserved word Avoid using as first character since many keywords start with it. Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

13 Directives Commands that are recognized and acted upon by the assembler Not part of the Intel instruction set; but used by the assembler (i.e. the compiler) to direct the OS to perform certain tasks. Used to declare code, data areas, select memory model, declare procedures, etc. not case sensitive Different assemblers have different directives NASM not the same as MASM, for example Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

14 Instructions Assembled into machine code by assembler
Executed at runtime by the CPU We use the Intel IA-32 instruction set Always INCLUDE Irvine32.inc in your programs An instruction contains: Label (optional) Mnemonic (required) Operand (depends on the instruction) Comment (optional) Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

15 Mnemonics and Operands
Instruction Mnemonics memory aid examples: MOV, ADD, SUB, MUL, INC, DEC Operands constant constant expression register memory (data label) Constants and constant expressions are often called immediate values Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

16 Instruction Format Examples
No operands stc ; set Carry flag One operand inc eax ; register dec myByte ; memory Two operands (there are also 3-operand instructions, but they are rare) add ebx, ecx ; register, register sub myByte, 25 ; memory, constant add eax, 36 * 25 ; register, constant-expression All instructions are in the .code segment of programs Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

17 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 mov eax,10000h ; EAX = 10000h add eax,40000h ; EAX = 50000h sub eax,20000h ; EAX = 30000h call DumpRegs ; display content of registers exit main ENDP END main Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

18 Program output, showing registers and flags:
Example Output Program output, showing registers and flags: EAX= EBX=7FFDF000 ECX= EDX=FFFFFFFF ESI= EDI= EBP=0012FFF0 ESP=0012FFC4 EIP= EFL= CF=0 SF=0 ZF=0 OF=0 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

19 Suggested Coding Standards
Indentation and spacing code and data labels – no indentation executable instructions – indent 4-5 spaces comments: right side of page, aligned vertically 1-3 spaces between instruction and its operands ex: mov ax, bx 1-2 blank lines between procedures Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

20 Alternative Version of AddSub (If not including Irvine32.inc)
TITLE Add and Subtract (AddSubAlt.asm) ; This program 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 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

21 Data Definition Statement
A data definition statement declares a variable and allocates memory for the variable. The allocation directive defines the type of the variable. May optionally assign a name (label) to the data Syntax: [name] directive initializer [,initializer] var1 BYTE 10 var2 SWORD AFh, ?, -2, +7, 0BC9h All initializers become binary data in memory All variable declarations are in the .data segment of programs Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

22 Defining BYTE and SBYTE Data
8-bit unsigned integer and 8-bit signed integer type Each of the 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's considered poor style. If you declare a SBYTE variable, the Microsoft debugger will automatically display its value in decimal with a leading sign. Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

23 Defining [S]Byte Arrays
Examples that use multiple initializers: A question mark (?) in the initializer leaves the initial value of the variable undefined. Ex: c SBYTE ? ; c is undefined 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, b list4 BYTE 0Ah,20h,‘A’,22h Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

24 Defining Strings (1 of 3) A string is implemented as an array of characters For convenience, it is usually enclosed in quotation marks It often will be null-terminated Character type is BYTE Examples: 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 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

25 Defining Strings (2 of 3) To continue a single string across multiple lines, end each line with a comma: menu BYTE "Checking Account",0dh,0ah,0dh,0ah, "1. Create a new account",0dh,0ah, "2. Open an existing account",0dh,0ah, "3. Credit the account",0dh,0ah, "4. Debit the account",0dh,0ah, "5. Exit",0ah,0ah, "Choice> ",0 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

26 Defining Strings (3 of 3) End-of-line character sequence:
0Dh = carriage return 0Ah = line feed Line continuation character (\) Concatenates two source code lines into a single statement greeting1 BYTE “Welcome to the Encryption Demo Program”,0 greeting1 \ BYTE “Welcome to the Encryption Demo Program”,0 str1 BYTE "Enter your name: ",0Dh,0Ah BYTE "Enter your address: ",0 newLine BYTE 0Dh,0Ah,0 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

27 Using the DUP Operator Use DUP to allocate (create space for) an array of any type or for a string. Syntax: [var_name] TYPE counter DUP ( argument ) Counter and argument must be constants or constant expressions. DUP must be used only with data allocation directives. 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 Var5 BYTE 2 DUP( ‘a’ , 2 DUP ( ‘b’ ) ) ; 6 bytes : ‘abbabb’ Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

28 Defining WORD and SWORD Data
16-bit unsigned & signed integer type Define storage for 16-bit integers or double characters single value or multiple values word1 WORD ; 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 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

29 Defining DWORD and SDWORD Data
32-bit unsigned & signed integer type Storage definitions for signed and unsigned 32-bit integers: val1 DWORD h ; unsigned val2 SDWORD – ; signed val3 DWORD 20 DUP(?) ; unsigned array val4 SDWORD –3,–2,–1,0,1 ; signed array Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

30 Defining QWORD, TBYTE, Real Data
64-bit integer, 80-bit integer, and real types Storage definitions for quadwords, tenbyte values, and real numbers: quad1 QWORD h val1 TBYTE Ah rVal1 REAL ; 4-byte single-precision rVal2 REAL8 3.2E ; 8-byte double-precision rVal3 REAL10 4.6E ; 10-byte extended precision ShortArray REAL4 20 DUP(0.0) Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

31 Offset Address of Variables and Data
The optional variable name is a label marking its address in the data segment. The (offset) address of a variable is the address of its first byte. Ex: If the following data segment starts at address 0. .data Var1 BYTE “ABC” Var2 BYTE “DEFG” The address of Var1 is 0 = the address of ‘A’ The address of ‘B’ is 1 The address of ‘C’ is 2 The address of Var2 is 3 The address of ‘E’ is 4 …

32 Little Endian Order All data types larger than a byte store their individual bytes in reverse order. The least significant byte occurs at the first (lowest) memory address. Example: val1 DWORD h Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

33 Little Endian Order Ex: A WORD 1234h, 5678h ; allocates 2 words
Intel’s x86 are little endian processors: the lowest order byte (of a word or double word) is always stored at the lowest address. Ex: if variable A (above) is located at address 0, we have: address: value: 34h 12h 78h 56h

34 Little Endian Order Ex: B DWORD 12345678h ;allocates 1 double word
If variable B is located at address of 0, we have: address: value: 78h 56h 34h 12h If a value fits into a byte, it will be stored in the lowest ordered byte available. Ex: V WORD ‘A’ the value will be stored as: address: 0 1 value: 41h 00h

35 Legacy Data Directives
Legacy data directives are also supported by NASM and TASM. Var1 DB ; 8-bit integer type (signed or unsigned) Var2 DW ; 16-bit integer type (signed or unsigned) Var3 DD 1.2 ; 32-bit integer/real (signed or unsigned) Var4 DQ 3.2E-260 ; 64-bit integer/real (signed or unsigned) Var5 DT 4.6E+4096 ; 80-bit integer/real (signed or unsigned) The Legacy Data Directives do not distinguish between signed or unsigned data

36 Adding Variables to AddSub
TITLE Add and Subtract, Version (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 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

37 Equal-Sign Directive name = integer expression
expression is a 32-bit integer (expression or constant) may be redefined name is called a symbolic constant No memory is allocated for a constant ASM substitutes name with value (of expression) in each occurrence of name good programming style to use symbols Good for DUP COUNT = ; this is a constant, not a variable mov ax, COUNT ; AX ← 500 A = (-3 * 8) + 2 B = (A+2)/ ; constants can be defined in terms of another constants Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

38 Calculating the Size of a Word Array
Divide total number of bytes by 2 (the size of a word) The $ operator (current location counter) returns the offset associated with the current program statement The constant must follow immediately after the array whose size you want to calculate Works for any type: BYTE, DWORD, QWORD, … etc list WORD 1000h,2000h,3000h,4000h ListSize = ($ - list) / 2 ; ListSize is a constant ; evaluated at runtime Difference ($ - list) is the number of bytes Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

39 EQU Directive Define a symbol as either an integer or text expression.
Cannot be redefined PI EQU <3.1416> ; text expression Mat1 EQU 10 * 10 ; integer expression Mat2 EQU <10*10> ; text expression pressKey EQU <"Press any key to continue...",0> .data prompt BYTE pressKey ; prompt ← “Press any …”,0 “ M1 WORD Mat1 ; M1 WORD 100 M2 WORD Mat2 ; M2 WORD 10 * 10 P REAL4 PI ; P REAL Text must be enclosed with <...> (useful for real value) Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

40 TEXTEQU Directive Define a symbol as either an integer or text expression. Called a text macro Can be redefined at any time continueMsg TEXTEQU <"Do you wish to continue (Y/N)?"> rowSize = 5 .data prompt1 BYTE continueMsg ; assigns the content of a textmacro count TEXTEQU %(rowSize * 2) ; evaluates the integer expression setupAL TEXTEQU <mov al,count> ; assigns text .code setupAL ; generates: "mov al,10" Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

41 Exercise 1 Suppose that the following data segment starts at address 0
A WORD 1,2 B WORD 6ABCh Z EQU 232 C BYTE 'ABCD' A) Find the address of variable A. B) Find the address of variable B. C) Find the address of variable C. D) Find the address of character ‘C’.

42 4C E Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

43 Real-Address Mode Programming (1 of 2)
Generate 16-bit MS-DOS Programs Advantages enables calling of MS-DOS and BIOS functions no memory access restrictions Disadvantages must be aware of both segments and offsets cannot call Win32 functions (Windows 95 onward) limited to 640K program memory Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

44 Real-Address Mode Programming (2 of 2)
Requirements INCLUDE Irvine16.inc Initialize DS to the data segment: mov mov ds,ax Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

45 Add and Subtract, 16-Bit Version
TITLE Add and Subtract, Version (AddSub2r.asm) INCLUDE Irvine16.inc .data val1 DWORD 10000h val2 DWORD 40000h val3 DWORD 20000h finalVal DWORD ? .code main PROC mov ; initialize DS mov ds,ax mov eax,val1 ; get first value add eax,val2 ; add second value sub eax,val3 ; subtract third value mov finalVal,eax ; store the result call DumpRegs ; display registers exit main ENDP END main Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

46 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 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

47 4C E Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.


Download ppt "Assembly Language for x86 Processors 6th Edition"

Similar presentations


Ads by Google