Assembly Language Fundamentals
Dr. Nwe Nwe Myint Thein
Associate Professor
Variable Declaration
Irvine, Kip R. Assembly Language for
2 Faculty of Computer Science
Intel-Based Computers 8/e
Outcomes
• classify variable declaration rule
• observe different data types
Irvine, Kip R. Assembly Language for
3 Faculty of Computer Science
Intel-Based Computers 8/e
Basic Elements of Assembly Language
• Integer constants
• Integer expressions
• Character and string constants
• Reserved words and identifiers
• Directives and instructions
• Labels
• Mnemonics and Operands
• Comments
Irvine, Kip R. Assembly Language for
4 Faculty of Computer Science
Intel-Based Computers 8/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
• 1-247 characters, including digits
• not case sensitive
• first character must be a letter, _, @, ?, or $
Irvine, Kip R. Assembly Language for
5 Faculty of Computer Science
Intel-Based Computers 8/e
Directives
• Commands that are recognized 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.
• case insensitive In MASM
Example: .data, .DATA, .Data, .code, .stack 100h
• Different assemblers have different directives: NASM not the same as MASM, for
example
myVar DWORD 26 ; DWORD directive, set aside
; enough space for double word
mov eax, myVar ; MOV instruction
Irvine, Kip R. Assembly Language for
6 Faculty of Computer Science
Intel-Based Computers 8/e
Instructions
• An instruction is a statement that becomes executable when a program is
assembled.
• Assembled into machine code by assembler
• Executed at runtime by the CPU
• We use the Intel IA-32 instruction set
• An instruction contains:
• Label (optional)
• Mnemonic (required)
• Operand (depends on the instruction)
• Comment (optional)
• Basic syntax
[label:] mnemonic [operands] [ ; comment]
Irvine, Kip R. Assembly Language for
7 Faculty of Computer Science
Intel-Based Computers 8/e
Labels
• Act as place markers for instructions and data target:
• marks the address (offset) of code and data mov ax, bx
…
• Follow identifier rules
jmp target
• Data label
• must be unique
• example: myArray DWORD 1024, 2048 (not followed by colon)
• Code label
• target of jump and loop instructions
• example: L1: (followed by colon)
Irvine, Kip R. Assembly Language for
8 Faculty of Computer Science
Intel-Based Computers 8/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
Irvine, Kip R. Assembly Language for
9 Faculty of Computer Science
Intel-Based Computers 8/e
Mnemonics and Operands - Examples
• No operands
stc ; set Carry flag
• 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-expression
mov count, ebx ; move EBX to count
; first operation is destination
; second is the source
• Three operands
imul eax, ebx, 5 ; ebx multiplied by 5, product in EAX
Irvine, Kip R. Assembly Language for
10 Faculty of Computer Science
Intel-Based Computers 8/e
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 (;)
• Multi-line comments
• begin with COMMENT directive and a programmer-chosen character
• end with the same programmer-chosen character
Irvine, Kip R. Assembly Language for
11 Faculty of Computer Science
Intel-Based Computers 8/e
Comments - Examples
• Single line comment
inc eax ; single line at end of instruction
; single line at beginning of line
• Multiline comment
COMMENT !
This line is a comment
This line is also a comment
!
COMMENT &
This is a comment
This is also a comment
&
Irvine, Kip R. Assembly Language for
12 Faculty of Computer Science
Intel-Based Computers 8/e
NOP instruction
• Doesn’t do anything
• Takes up one byte
• Sometimes used by compilers and assemblers to align code to even-address
boundaries.
• The following MOV generates three machine code bytes. The NOP aligns the
address of the third instruction to a doubleword boundary (even multiple of
4)
00000000 66 8B C3 mov ax, bx
00000003 90 nop ;align next instruction
00000004 8B D1 mov edx, ecx
Irvine, Kip R. Assembly Language for
13 Faculty of Computer Science
Intel-Based Computers 8/e
Example Program
main PROC
mov eax, 5 ; move 5 to the EAX register
add eax, 6 ; add 6 to the EAX register
call WriteInt ; display value in EAX
Exit ; quit
main ENDP
Add two numbers and displays the result
Irvine, Kip R. Assembly Language for
14 Faculty of Computer Science
Intel-Based Computers 8/e
Suggested Coding Standards (1 of 2)
• Some approaches to capitalization
• capitalize nothing
• capitalize everything
• capitalize all reserved words, including instruction mnemonics and register names
• capitalize only directives and operators
• Other suggestions
• descriptive identifier names
• spaces surrounding arithmetic operators
• blank lines between procedures
Irvine, Kip R. Assembly Language for
15 Faculty of Computer Science
Intel-Based Computers 8/e
Suggested Coding Standards (2 of 2)
• 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
16 Faculty of Computer Science
Intel-Based Computers 8/e
32-bit Assembly Language template
Program Template (Template.asm)
; Program Description:
; Author:, Creation Date:, Revisions:, Date: Modified by:
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
.data
; declare variables here
.code
main PROC
; write your code here
INVOKE ExitProcess,0
main ENDP
; (insert additional procedures here)
END main
Irvine, Kip R. Assembly Language for
17 Faculty of Computer Science
Intel-Based Computers 8/e
64-bit Assembly Language template
; 64-bit Assembly Language template
; Use this when not calling subroutines.
ExitProcess proto
.data
; Declare your variables here.
.code
main proc
mov ecx,0 ; assign a process return code
call ExitProcess ; terminate the program
main endp
end
Irvine, Kip R. Assembly Language for
18 Faculty of Computer Science
Intel-Based Computers 8/e
• MASM supports 64-bit programming, although the following directives are not permitted:
• INVOKE, ADDR, .model, .386, .stack
• The following lines are not needed:
.386
.model flat,stdcall
.stack 4096
• INVOKE is not supported.
• CALL instruction cannot receive arguments
• Use 64-bit registers when possible
Irvine, Kip R. Assembly Language for
19 Faculty of Computer Science
Intel-Based Computers 8/e
The First Program
; AddTwo.asm - adds two 32-bit integers.
.386
.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword
.code
main proc
mov eax,5
add eax,6
invoke ExitProcess,0
main endp
end main
Irvine, Kip R. Assembly Language for
20 Faculty of Computer Science
Intel-Based Computers 8/e
32-Bit Version of AddTwoSum
; AddTwoSum.asm
.386
.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword
.data
sum dword 0
.code
main proc
mov eax,5
add eax,6
mov sum,eax
invoke ExitProcess,0
main endp
end main
Irvine, Kip R. Assembly Language for
21 Faculty of Computer Science
Intel-Based Computers 8/e
64-Bit Version of AddTwoSum
; AddTwoSum_64.asm
ExitProcess proto
• The following lines are not needed:
.data .386
sum qword 0 .model flat,stdcall
.stack 4096
.code
main proc • INVOKE is not supported.
mov rax,5 • CALL instruction cannot receive arguments
add rax,6 • Use 64-bit registers when possible
mov sum,rax
mov ecx,0
call ExitProcess
main endp
end
Irvine, Kip R. Assembly Language for
22 Faculty of Computer Science
Intel-Based Computers 8/e
Example: adds and subtracts integers
TITLE Add and Subtract (AddSub.asm)
; This program adds and subtracts 32-bit integers.
; Without include.386
.model flat, stdcall
.stack 4096 Defining Segments One important function of assembler
ExitProcess PROTO, dwExitCode:DWORD directives is to define program sections, or segments.
DumpRegs PROTO The .DATA directive identifies the area of a program
containing variables:
.code
.data
main PROC
mov eax,10000h ; EAX = 10000h
The .CODE directive identifies the area of a program
add eax,40000h ; EAX = 50000h containing instructions:
sub eax,20000h ; EAX = 30000h .code
call DumpRegs ; display registers The .STACK directive identifies the area of a program
INVOKE ExitProcess, 0 holding the runtime stack, setting its size:
exit .stack 100h
main ENDP
END main
Irvine, Kip R. Assembly Language for
23 Faculty of Computer Science
Intel-Based Computers 8/e
❖ The TITLE directive marks the entire line as a comment. You can put anything you want on this line.
❖ The INCLUDE directive copies necessary definitions and setup information from a text file named Irvine32.inc,
located in the assembler’s INCLUDE directory
❖ The .code directive marks the beginning of the code segment, where all executable statements in a program are
located.
❖ The PROC directive identifies the beginning of a procedure.
❖ The MOV instruction moves (copies) the integer 10000h to the EAX register.
❖ The ADD instruction adds 40000h to the EAX register.
❖ The SUB instruction subtracts 20000h from the EAX register.
❖ The CALL statement calls a procedure that displays the current values of the CPU registers.
❖ The exit statement (indirectly) calls a predefined MS-Windows function that halts the program.
❖ The ENDP directive marks the end of the main procedure
❖ The END main directive marks the last line of the program to be assembled.
Irvine, Kip R. Assembly Language for
24 Faculty of Computer Science
Intel-Based Computers 8/e
Defining Data
Irvine, Kip R. Assembly Language for
25 Faculty of Computer Science
Intel-Based Computers 8/e
Defining Data (Data Types)
• Data Types Essential Characteristics:
• Size in bits: 8, 16, 32, 48, 64, and 80.
• Signed and Unsigned.
• Pointer.
• Integral or Floating-point.
• Data Definition Statement
• A data definition has the following syntax:
[name] directive initializer [,initializer]...
• Name The optional name assigned to a variable must conform to the rules for identifiers, When you declare an
integer variable by assigning a label to a data allocation directive, the assembler allocates memory space for the
integer. The variable’s name becomes a label for the memory space.
• Directive The directive in a data definition statement can be BYTE, WORD, DWORD, SBYTE, SWORD, or
any of the types listed in the following table
Irvine, Kip R. Assembly Language for
26 Faculty of Computer Science
Intel-Based Computers 8/e
In addition, it can be any of the
legacy data definition directives
shown in the following table.
27 Faculty of Computer Science
❖ Initializer At least one initializer, initializers, if any, are separated by commas.
❖ For integer data types, initializer variable’s type, such as BYTE or WORD.
The Initializers and their description are shown in the following table:
Type Number of bits Range (inclusive)
tbyte, DT 80 10-byte integers if the initializer has a radix number.
qdword, DQ 64 8-byte integers used with 8087 family coprocessor instructions.
Normally used only as pointer variables on the 80386/486
fword, DF 48
processors.
sdword 32 –2,147,483,648 to +2,147,483,647
Dword, DD 32 0 to 4,294,967,295
sword 16 –32,768 to +32,767
Word, DW 16 0 to +65,535
sbyte 8 –128 to +127
byte, DB 8 0 to 255
Faculty of Computer Science
Data Initialization
❖ You can initialize variables when you declare them with constants or expressions that
evaluate to constants. The assembler generates an error if you specify an initial value too
large for the variable type.
❖ A ? in place of an initializer indicates you do not require the assembler to initialize the
variable. The assembler allocates the space but does not write in it.
• Use ? for buffer areas or variables your program will initialize at run time.
Faculty of Computer Science
• Intrinsic Data Types
• Data Definition Statement
• Defining BYTE and SBYTE Data
• Defining Strings
• Defining WORD and SWORD Data
• Defining DWORD and SDWORD Data
• Defining QWORD Data
• Defining TBYTE Data
• Defining Real Number Data
• Little Endian Order
• Adding Variables to the AddSub Program
• Declaring Uninitialized Data
• Mixing Code and Data
• Defining
Irvine, Kip R. Assembly Language for
30 Faculty of Computer Science
Intel-Based Computers 8/e
Integer Constants
[{+ | -}] digits [radix]
• Optional leading + or – sign
• binary, decimal, hexadecimal, or octal digits
• Common radix characters:
h – hexadecimal
q | o – octal
d – decimal
b – binary
r – encoded real
• If no radix given, assumed to be decimal
Examples: 30d, 6Ah, 42, 1101b
• Hexadecimal beginning with letter: 0A5h
Irvine, Kip R. Assembly Language for
31 Faculty of Computer Science
Intel-Based Computers 8/e
Integer Expressions
• integer values and arithmetic operators
• Operators and precedence levels:
Must evaluate to an integer that can be stored in
32 bits
These can be evaluated at assembly time – they are
not runtime expressions
Precedence Examples:
4+5*2 Multiply, add
12 – 1 MOD 5 Modulus, subtract
-5 + 2 Unary minus, add
(4 + 2) * 6 Add, multiply
Irvine, Kip R. Assembly Language for
32 Faculty of Computer Science
Intel-Based Computers 8/e
Intrinsic Data Types
• BYTE, SBYTE • REAL4
• 8-bit unsigned integer; 8-bit signed integer • 4-byte IEEE short real
• WORD, SWORD
• REAL8
• 16-bit unsigned & signed integer • 8-byte IEEE long real
• DWORD, SDWORD
• 32-bit unsigned & signed integer • REAL10
• QWORD • 10-byte IEEE extended real
• 64-bit integer
• TBYTE
• 80-bit integer
Irvine, Kip R. Assembly Language for
33 Faculty of Computer Science
Intel-Based Computers 8/e
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
Irvine, Kip R. Assembly Language for
34 Faculty of Computer Science
Intel-Based Computers 8/e
Defining BYTE and SBYTE Data
• The BYTE (define byte) and SBYTE (define signed byte) directives allocate storage for one or more unsigned or
signed values:
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
• A question mark (?) initializer leaves the variable uninitialized, implying it will be assigned a value at runtime:
value6 BYTE ?
• The optional name is a label marking the variable’s offset from the beginning of its enclosing segment. For
example, if value7 is located at offset 0000 in the data segment, value8 is automatically located at offset 0001:
Value7 BYTE 10h
Value8 BYTE 20h
• The DB legacy directive can also define an 8
val1 DB 255 ; unsigned byte
val2 DB 35 -128 ; signed byte Faculty of Computer Science
Irvine, Kip R. Assembly Language for
Intel-Based Computers 8/e
Defining Byte Arrays (Multiple Initializers)
• If multiple initializers are used in the same data definition, its label refers only to the offset
of the first initialize, for example
list BYTE 10,20,30,40
• Assume list is located at offset 0000. If so, the value 10 is at offset 0000, 20 is at offset
0001, 30 is at offset 0002, and 40 is at offset 0003
• Not all data definitions require labels
list BYTE 10,20,30,40
BYTE 50,60,70,80
BYTE 81,82,83,84
• Within a single data definition, its initializers can use different radixes. Character and
string constants can be freely mixed.
list1 BYTE ?, 32, 41h, 00100010b
list2 BYTE 0Ah, 20h, 'A', 22h
Irvine, Kip R. Assembly Language for
36 Faculty of Computer Science
Intel-Based Computers 8/e
Defining Character and String Constants
• Enclose character in single or double quotes
• 'A', "x"
• ASCII character = 1 byte
char grade1; grade1 byte ?
char grade2 = ’A’; grade2 byte 'A’
• A string is implemented as an array of characters
• To define a string of characters, enclose them in single or double quotation marks.
• null-terminated string
greeting1 BYTE "Good afternoon",0
greeting2 BYTE 'Good night',0
• Embedded quotes:
• 37
'Say "Goodnight," Gracie' Faculty of Computer Science
Irvine, Kip R. Assembly Language for
Intel-Based Computers 8/e
• Strings are an exception to the rule that byte values must be exception, greeting1 would
have to be defined as
greeting1 BYTE 'G','o','o','d'....etc.
• A string can be spread across multiple lines without having to supply a label for each line:
greeting1 BYTE "Welcome to the Encryption Demo program "
BYTE "created by Kip Irvine.",0dh,0ah
BYTE "If you wish to modify this program, please
"
BYTE "send me a copy.",0dh,0ah,0
Irvine, Kip R. Assembly Language for
38 Faculty of Computer Science
Intel-Based Computers 8/e
• Strings are often terminated with a zero taking up 1 byte to indicate the end of the string
• The hexadecimal codes 0Dh and 0Ah are alternately called end-of-line characters.
• End-of-line character sequence:
• 0Dh = carriage return
• 0Ah = line feed
str1 BYTE "Enter your name: BYTE ",0Dh,0Ah
"Enter your address: ",0
newLine BYTE0Dh,0Ah,0
Idea: Define all strings used by your program in the same area of the data segment.
Irvine, Kip R. Assembly Language for
39 Faculty of Computer Science
Intel-Based Computers 8/e
DUP Operator
• The DUP operator allocates storage for multiple data items. Counter and argument must be constants or
constant expressions
• It is particularly useful when allocating space for a string or array, and can be used with initialized or
uninitialized data:
BYTE 20 DUP(0) ; 20 bytes, all equal to zero
BYTE 20 DUP(?) ; 20 bytes, uninitialized
BYTE 4 DUP("STACK") ; 20 bytes: "STACKSTACKSTACKSTACK"
Irvine, Kip R. Assembly Language for
40 Faculty of Computer Science
Intel-Based Computers 8/e
Defining WORD and SWORD Data
• Define storage for 16-bit integers • Storage definitions for signed and
unsigned 32-bit integers:
• or double characters
• single value or multiple values
word1 WORD 65535 ; largest unsigned value val1 DWORD 12345678h ; unsigned
word2 SWORD –32768 ; smallest signed value val2 SDWORD –2147483648 ; signed
word3 WORD ? ; uninitialized, unsigned val3 DWORD 20 DUP(?) ; unsigned
word4 WORD "AB" ; double characters array
val4 SDWORD –3,–2,–1,0,1 ; signed array
myList WORD 1,2,3,4,5 ; array of words
array WORD 5 DUP(?) ; uninitialized array
The legacy DW directive can also be used:
val1 DW 65535 ; unsigned
val2 DW -32768 ; signed
• Array of Words: Create an array of words by listing the elements or using the DUP operator.
myList WORD 1,2,3,4,5
array WORD 5 DUP(?) ; 5 values, uninitialized Irvine, Kip R. Assembly Language for
41 Faculty of Computer Science
Intel-Based Computers 8/e
Defining DWORD and SDWORD Data
val1 DWORD 12345678h
val2 SDWORD −2147483648
val3 DWORD 20 DUP(?)
The legacy DD directive can also be used:
val1 DD 12345678h
val2 DD −2147483648
Array of Doublewords
myList DWORD 1,2,3,4,5
array DWORD 5 DUP(?) ; 5 values, uninitialized
Defining QWORD Data
quad1 QWORD 1234567812345678h
quad1 DQ 1234567812345678h
Irvine, Kip R. Assembly Language for
42 Faculty of Computer Science
Intel-Based Computers 8/e
Defining QWORD, TBYTE, Real Data
• Storage definitions for quadwords, tenbyte values, and real numbers:
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)
Irvine, Kip R. Assembly Language for
43 Faculty of Computer Science
Intel-Based Computers 8/e
Defining TBYTE Data
❖ This data type is primarily for the storage of binary-coded decimal numbers (packed BCD Integers
in 10-byte package)
❖ Each byte (exept the highest) contains two BCD numbers.
❖ In the highest byte, the highest bit indicates the number’s sign (if the highest byte =80h, the number
is negative; if the highest byte = 00h, the number is positive).
❖ The integer range is -999,999,999,999,999,999 to +999,999,999,999,999,999
val1 TBYTE 1000000000123456789Ah
val1 DT 1000000000123456789Ah
❖ Constant initializers must be in hexa numbers
BCDVal1 TBYTE 80000000000000001234h ; valid
BCDVal2 TBYTE -1234 ; invalid
Real Number Constants
• Represented as decimal reals or encoded (hexadecimal) reals
• Decimal real contains optional sign followed by integer, decimal point, and optional
integer that expresses a fractional and an optional exponent
[sign] integer.[integer] [exponent]
Sign {+, -}
Exponent E[{+, -}] integer
• Examples
• 2.
• +3.0
• -44.2E+05
• 26.E5
Irvine, Kip R. Assembly Language for
45 Faculty of Computer Science
Intel-Based Computers 8/e
Defining Real Number Data
REAL4 defines a 4-byte single-precision real variable.
REAL8 defines an 8-byte double-precision real,
REAL10 defines a 10-byte double extended-precision real.
rVal1 REAL4 -1.2
rVal2 REAL8 3.2E
rVal3 REAL10 4.6E+4096
ShortArray REAL4 20 DUP(0.0)
The legacy DD, DQ, and DT directives can define real numbers:
rVal1 DD -1.2 ; short real
rVal2 DQ 3.2ErVal3 ; long real
DT 4.6E+4096 ; extended-precision real
Programming Example: AddSub2.asm
Irvine, Kip R. Assembly Language for
46 Faculty of Computer Science
Intel-Based Computers 8/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 12345678h
Irvine, Kip R. Assembly Language for
47 Faculty of Computer Science
Intel-Based Computers 8/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
Irvine, Kip R. Assembly Language for
48 Faculty of Computer Science
Intel-Based Computers 8/e
Declaring Unitialized Data
• Use the .data? directive to declare an unintialized data segment:
• .data?
• Within the segment, declare variables with "?" initializers:
• smallArray DWORD 10 DUP(?)
• When defining a large block of uninitialized data, the .DATA? directive reduces the size of a
compiled program.
• Advantage: the program's EXE file size is reduced.
Irvine, Kip R. Assembly Language for
49 Faculty of Computer Science
Intel-Based Computers 8/e
❖ the following code is declared efficiently:
.data
smallArray DWORD 10 DUP(0) ; 40 bytes
.data?
bigArray DWORD 5000 DUP(?) ; 20,000 bytes, not initialized
The following code, on the other hand, produces a compiled program 20,000 bytes larger:
.data
smallArray DWORD 10 DUP(0) ; 40 bytes
bigArray DWORD 5000 DUP(?) ; 20,000 bytes
Mixing Code and Data
The assembler lets you switch back and forth between code and data in your programs.
.code
mov eax,ebx
.data
temp DWORD ?
.code
mov temp,eax
. . . Irvine, Kip R. Assembly Language for
50 Faculty of Computer Science
Intel-Based Computers 8/e
Symbolic Constants and Real-Address
Mode Programming
Irvine, Kip R. Assembly Language for
51 Faculty of Computer Science
Intel-Based Computers 8/e
• Symbolic Constants
• Equal-Sign Directive
• Calculating the Sizes of Arrays and Strings.
• EQU Directive
• TEXTEQU Directive
•
Irvine, Kip R. Assembly Language for
52 Faculty of Computer Science
Intel-Based Computers 8/e
Symbolic Constants
1. Equal-Sign Directive
• name = expression
• expression is a 32-bit integer (expression or constant)
• may be redefined
• name is called a symbolic constant
• good programming style to use symbols
COUNT = 500
mov ax,COUNT
Irvine, Kip R. Assembly Language for
53 Faculty of Computer Science
Intel-Based Computers 8/e
Equal-Sign Directive Example 2 (Using the DUP Operator
name = expr The counter used by DUP should be a symbolic constant
name is called a symbolic constant Count = 5
expression is a 32-bit integer (expression or constant) array DWORD COUNT DUP(0)
Good programming style to use symbols May be redefined.
o Example 1 (Keyboard Definitions A symbol defined with _ can be redefined within the same
program.
Esc_key = 27
mov al, Esc_key ;good style COUNT = 5
mov al,COUNT ; AL = 5
Rather than COUNT = 10
mov al,COUNT ; AL = 10
mov al,27 ; poor style COUNT = 100
mov al,COUNT ; AL = 100
Irvine, Kip R. Assembly Language for
54 Faculty of Computer Science
Intel-Based Computers 8/e
• 2. Calculating the Sizes of Arrays and Strings
Uses a constant named ListSize
list BYTE 10,20,30,40 Calculating the Size of a Word Array
ListSize = 4
current location counter: $
A better way to handle this situation would be to let the
assembler automatically calculate ListSize
o subtract address of list
o difference is the number of bytes
The $ operator (current location counter) returns the offset
o divide by 2 (the size of a word)
associated with the current program statement
list WORD 1000h,2000h,3000h,4000h
list BYTE 10,20,30,40
ListSize = ($ - list) / 2
ListSize = ($ - list)
ListSize must follow immediately after list.
list BYTE 10,20,30,40
var2 BYTE 20 DUP(?)
ListSize = ($ - list) ;incorrect Irvine, Kip R. Assembly Language for
55 Faculty of Computer Science
Intel-Based Computers 8/e
Calculating the Size of a Doubleword Array
current location counter: $
o subtract address of list
o difference is the number of bytes
o divide by 4 (the size of a doubleword)
list DWORD 1,2,3,4
ListSize = ($ - list) / 4
Calculating the Size of a string
Rather than calculating the length of a string manually, let the assembler do it:
myString BYTE "This is a long string, containing"
BYTE "any number of characters"
myString_len = ($ − myString)
Irvine, Kip R. Assembly Language for
56 Faculty of Computer Science
Intel-Based Computers 8/e
3. EQU Directive
❖ The EQU directive associates a symbolic name with an integer expression or some arbitrary text.
❖ There are three formats:
name EQU expression
name EQU symbol
name EQU <text>
o expression must be a valid integer expression
o symbol is an existing symbol name, already defined with = or EQU.
o text is any text may appear within the brackets <. . .>
❖ EQU can be useful when defining a value that does not evaluate to an integer:
PI EQU <3.1416>
associate a symbol with a character string
pressKey EQU <"Press any key to continue...",0>
.data
prompt BYTE pressKey
associate a symbol with an expression
matrix1 EQU 10 * 10
matrix2 EQU <10 * 10>
.data
M1 WORD matrix1
M2 WORD
57
matrix2 Faculty of Computer Science
Irvine, Kip R. Assembly Language for
Intel-Based Computers 8/e
Cannot be redefined
4. TEXTEQU Directive
Define a symbol as either an integer or text expression called a text macro.
There are three different formats
name TEXTEQU <text>
name TEXTEQU textmacro
name TEXTEQU %constExpr
Example 1
continueMsg TEXTEQU <"Do you wish to continue (Y/N)?">
.data
prompt1 BYTE continueMsg
Example 2
continueMsg TEXTEQU <"Do you wish to continue (Y/N)?">
rowSize = 5
.data
prompt1 BYTE continueMsg
count TEXTEQU %(rowSize * 2) ;
move TEXTEQU <mov>
setupAL TEXTEQU <move al,count> Programming Example: Constants.asm
.code
setupAL ; generates: "mov al,10"
TEXTEQU Can be redefined. Irvine, Kip R. Assembly Language for
58 Faculty of Computer Science
Intel-Based Computers 8/e
Real-Address Mode Programming
Generate 16-bit MS-DOS Programs
• Advantages
o enables calling of MS-DOS and BIOS functions
o no memory access restrictions
• Disadvantages
o must be aware of both segments and offsets
o cannot call Win32 functions (Windows 95 onward)
o limited to 640K program memory
• Requirements
o INCLUDE Irvine16.inc
o Two additional instructions are inserted at the beginning of the startup procedure (main )
• Initialize DS to the data segment using predefined MASM constant @data::
mov ax,@data
mov ds,ax
Irvine, Kip R. Assembly Language for
59 Faculty of Computer Science
Intel-Based Computers 8/e
Add and Subtract, 16-Bit Version
TITLE Add and Subtract, Version 2 (AddSub2.asm)
INCLUDE Irvine16.inc
.data
val1 DWORD 10000h
val2 DWORD 40000h
val3 DWORD 20000h
finalVal DWORD ?
.code
main PROC
mov ax,@data ; 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
60 Faculty of Computer Science
Intel-Based Computers 8/e