47
Assembly Language for x86 Processors 6th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, 2010. 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

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

Embed Size (px)

Citation preview

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

Assembly Language for x86 Processors 6th Edition

Chapter 3: Assembly Language Fundamentals

(c) Pearson Education, 2010. 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

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

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 controlmov 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

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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 3

A Template for Assembly Language ProgramsTITLE 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-onlymain PROC

; (insert executable instructions here)exit

main ENDP; (insert additional procedures here)

END main

This is the template to follow in all your programs

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

4

A Template for ASM if Irvine32.inc is not Included Irvine32.inc: library

procedures and setup information for IA-32 .386: identifies 80386 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 4096ExitProcess PROTO, dwExitCode: DWORDDumpRegs PROTO

.data ;data declarations.code main PROC … ;instructions here call DumpRegs INVOKE ExitProcess, 0main ENDPEND main

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

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

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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 6

Example: Adding and Subtracting Integers

TITLE Add and Subtract (AddSub.asm)

; This program adds and subtracts 32-bit integers.

INCLUDE Irvine32.inc.codemain PROC

mov eax,10000h ; EAX = 10000hadd eax,40000h ; EAX = 50000hsub eax,20000h ; EAX = 30000hcall DumpRegs ; display registersexit

main ENDPEND main

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

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 http://www.asmirvine.com/ for instruction on assembling, linking and running ASM programs using Microsoft Visual Studio

SourceFile

ObjectFile

ListingFile

LinkLibrary

ExecutableFile

MapFile

Output

Step 1: text editor

Step 2:assembler

Step 3:linker

Step 4:OS loader

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

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

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

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)

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

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 -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)

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

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'

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

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.

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

13Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

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

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

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)

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

15Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

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

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

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

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

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.codemain PROC

mov eax,10000h ; EAX = 10000hadd eax,40000h ; EAX = 50000hsub eax,20000h ; EAX = 30000hcall DumpRegs ; display content of registersexit

main ENDPEND main

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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 18

Example 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

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

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

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

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:DWORDDumpRegs PROTO

.codemain PROC

mov eax,10000h ; EAX = 10000hadd eax,40000h ; EAX = 50000hsub eax,20000h ; EAX = 30000hcall DumpRegsINVOKE ExitProcess,0

main ENDPEND main

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

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

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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 22

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.

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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 23

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,00100010b

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

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

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

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

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

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

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

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

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’

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

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 65535 ; largest unsigned valueword2 SWORD –32768 ; smallest signed valueword3 WORD ? ; uninitialized, unsignedword4 WORD "AB" ; double charactersmyList WORD 1,2,3,4,5 ; array of wordsarray WORD 5 DUP(?) ; uninitialized array

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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 29

Defining DWORD and SDWORD Data

val1 DWORD 12345678h ; unsignedval2 SDWORD –2147483648 ; signedval3 DWORD 20 DUP(?) ; unsigned arrayval4 SDWORD –3,–2,–1,0,1 ; signed array

32-bit unsigned & signed integer type

Storage definitions for signed and unsigned 32-bit integers:

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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 30

Defining QWORD, TBYTE, Real Data

quad1 QWORD 1234567812345678hval1 TBYTE 1000000000123456789AhrVal1 REAL4 -2.1 ; 4-byte single-precisionrVal2 REAL8 3.2E-260 ; 8-byte double-precisionrVal3 REAL10 4.6E+4096 ; 10-byte extended precisionShortArray REAL4 20 DUP(0.0)

64-bit integer, 80-bit integer, and real types

Storage definitions for quadwords, tenbyte values, and real numbers:

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

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 …

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

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

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

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: 0 1 2 3 value: 34h 12h 78h 56h

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

34

Little Endian Order

Ex:

B DWORD 12345678h ;allocates 1 double word

If variable B is located at address of 0, we have: address: 0 1 2 3 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 1value: 41h 00h

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

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 +32768 ; 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

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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 36

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.dataval1 DWORD 10000hval2 DWORD 40000hval3 DWORD 20000hfinalVal DWORD ?.codemain PROC

mov eax,val1 ; start with 10000hadd eax,val2 ; add 40000hsub eax,val3 ; subtract 20000hmov finalVal,eax ; store the result (30000h)call DumpRegs ; display the registersexitmain ENDPEND main

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

37Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

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

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

38Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

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,4000hListSize = ($ - list) / 2 ; ListSize is a constant

; evaluated at runtime

Difference ($ - list) is the number of bytes

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

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 REAL4 3.1416

Text must be enclosed with <...>

(useful for real value)

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

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"

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

41

Exercise 1

Suppose that the following data segment starts at address 0

.dataA WORD 1,2B WORD 6ABCh Z EQU 232C 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’.

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

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

4C 61 46 69 6E

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

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

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

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 ax,@datamov ds,ax

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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 45

Add and Subtract, 16-Bit Version

TITLE Add and Subtract, Version 2 (AddSub2r.asm)INCLUDE Irvine16.inc.dataval1 DWORD 10000hval2 DWORD 40000hval3 DWORD 20000hfinalVal DWORD ?.codemain PROC

mov ax,@data ; initialize DSmov ds,ax mov eax,val1 ; get first valueadd eax,val2 ; add second valuesub eax,val3 ; subtract third valuemov finalVal,eax ; store the resultcall DumpRegs ; display registersexitmain ENDPEND main

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

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

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

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

4C 61 46 69 6E