Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assembly Language for x86 Processors 6th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, 2010. All rights reserved. You may modify.

Similar presentations


Presentation on theme: "Assembly Language for x86 Processors 6th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, 2010. All rights reserved. You may modify."— Presentation transcript:

1 Assembly Language for x86 Processors 6th Edition Chapter 3: Assembly Language Fundamentals (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. Slides prepared by the author Revision date: 2/15/2010 Kip Irvine

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

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

7 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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 StudioGetting started with MASM and Visual Studio 2012

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

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

10 Irvine, Kip R. Assembly Language for x86 Processors 6/e, Integer Constants Optional leading + or – sign binary, decimal, hexadecimal, or octal digits Common radix characters: h – hexadecimal1011h q/o – octal1011q or 1011o d – decimal1011d or 1011 (base 10 is the default) b – binary1011b r – encoded real3F800000r = +1.0 (topic of Chap 12) real-26.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)

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

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

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

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

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

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

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

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

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

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

21 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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 10var2 SWORD AFh, ?, -2, +7, 0BC9h All initializers become binary data in memory All variable declarations are in the.data segment of programs

22 Irvine, Kip R. Assembly Language for x86 Processors 6/e, Defining BYTE and SBYTE Data 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 8-bit unsigned integer and 8-bit signed integer type Each of the following defines a single byte of storage: 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.

23 Irvine, Kip R. Assembly Language for x86 Processors 6/e, Defining [S]Byte Arrays 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 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

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

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

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

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

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

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

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

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

33 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:0123  value:34h12h78h56h

34 34 Little Endian Order  Ex: B DWORD h ;allocates 1 double word  If variable B is located at address of 0, we have:  address:0123  value:78h56h34h12h  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:01 value:41h00h

35 35 Legacy Data Directives  Legacy data directives are also supported by NASM and TASM.  Var1 DB -128; 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 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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

37 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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 COUNT = 500 ; this is a constant, not a variable mov ax, COUNT ; AX ← 500 A = (-3 * 8) + 2 B = (A+2)/2 ; constants can be defined in terms of another constants

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

39 Irvine, Kip R. Assembly Language for x86 Processors 6/e, EQU Directive Define a symbol as either an integer or text expression. Cannot be redefined PI EQU ; text expression Mat1 EQU 10 * 10; integer expression Mat2 EQU ; text expression pressKey EQU.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)

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

41 41 Exercise 1  Suppose that the following data segment starts at address 0.data A WORD1,2 B WORD6ABCh 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 Irvine, Kip R. Assembly Language for x86 Processors 6/e, C E

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

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

45 Irvine, Kip R. Assembly Language for x86 Processors 6/e, Add and Subtract, 16-Bit Version TITLE Add and Subtract, Version 2 (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

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

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


Download ppt "Assembly Language for x86 Processors 6th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, 2010. All rights reserved. You may modify."

Similar presentations


Ads by Google