Download presentation
Presentation is loading. Please wait.
1
Assembly Language Fundamentals
2
Basic Elements Directives
Embedded in the source code that is recognized and acted upon by the assembler Do not execute at run time Define variables, macros, and procedures For example TITLE INCLUDE .CODE PROC ENDP END
3
Instructions Translated by the assembler into machine language bytes, which are loaded and executed by the CPU at run time. Format: [label] mnemonic operand(s) [;comment] For example: L1: mov ax, bx ; copy data from bx to ax mov eax, 10000h call DumpRegs
4
Integer Constants Format: [{+|-}] digits [radix] For example:
Radix: h, Hexadecimal q/o, Octal d/t, Decimal b/y, Binary r, Encoded real For example: 26 Decimal 26d Decimal b Binary 42q Octal 42o Octal 1Ah Hexadecimal 0A3h Hexadecimal, note the 0 before A
5
Integer Expressions Use (, ), +, -, *, /, MOD
Precedence: (, ) > +, - (unary) > *, / > MOD > +, - For example: 16/5 value = 3 -(3+4) * (6-1) value = -35 -3 + 4*6 -1 value = 20 25 mod 3 value = 1 Note: The integer expressions are not instructions. They are processed by the assembler, but not executed by the CPU at the running time
6
Real Number Constants Format: [{+,-}]integer.[integer][exponent]
exponent: E[{+,-}]integer For example: 2. +3.0 -44.2E+05 26.E5
7
Character Constants String Constants
Enclosed in single or double quotes For example: ‘A’ “d” String Constants ‘ABC’ “Good night, Gracie” “This isn’t a test” ‘Say “Good night,” Gracie” Note: Not like C, null byte is not automatically added after the double quotes
8
Reserved Words Instruction mnemonics, such as MOV, ADD, MUL.
Directives Attributes providing size and usage information for variables and operands, such as BYTE and WORD. Operators Predefined symbols, such
9
Identifiers Contain between 1 and 127 characters Not case sensitive
The first character must be a letter (A..Z, a..z), underscore ?, or $. Cannot be reserved words. For example: var1 $first _main open_file _12345
10
Label An identifier that acts as a place marker for instruction or data Data label, for example count DWORD 100 array DWORD 1024, 2048 DWORD 4096, 8192 Code label L1: mov ax, bx ; copy data from bx to ax
11
Comments Single-line comments, beginning with a semicolon character (;) Block comments, beginning with the COMMENT directive and a user-specified symbol and with the same user-specified symbol For example: COMMENT ! This line is a comment This line is also a omment ! COMMENT & &
12
NOP instruction Takes up 1 byte of program storage and doesn’t do any work For example: mov ax, bx nop mov edx, ecx
13
Example: Adding Three Integers
TITLE Add and Subtract (AddSub.asm) ; This program adds and subtracts 32-bit integers. ; Last update: 06/01/2006 INCLUDE Irvine32.inc .code main PROC mov eax,10000h ; EAX = 10000h add eax,40000h ; EAX = 50000h sub eax,20000h ; EAX = 30000h call DumpRegs exit main ENDP END main
14
TITLE Add and Subtract (AddSub.asm)
The TITLE directive marks the entire line as a comment ; This program adds and subtracts 32-bit integers. ; Last update: 06/01/2006 Comments can be put after a semicolon INCLUDE Irvine32.inc The INCLUDE directive copies necessary definitions and setup information from a text file named Irvine32.inc .code The .code directive marks the beginning of the code segment, where all executable statements in a program are located.
15
main PROC The PROC directive identifies the beginning of a procedure. The name of the procedure here is main. mov eax,10000h ; EAX = 10000h The MOV instruction copies the integer 10000h to the EAX register. add eax,40000h ; EAX = 50000h The ADD instruction adds 40000h to the EAX register. sub eax,20000h ; EAX = 30000h The SUB instruction subtracts 20000h from the EAX register. call DumpRegs The CALL instruction calls a procedure DumpRegs.
16
exit The exit macro (indirectly) calls a predefined MS-Windows function that halts the program main ENDP The ENDP directive marks the end of the main procedure. END main The END directive marks the last line of the program to be assembled. It identifies the name of the program’s startup procedure.
17
Alternative Version of AddSub
TITLE Add and Subtract (AddSubAlt.asm) ; This program adds and subtracts 32-bit integers. ; 32-bit Protected mode version ; Last update: 06/01/2006 .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
18
.386 The .386 directive identifies the minimum CPU required for this program .MODEL flat,stdcall The . MODEL directive instructs the assembler to generate code for a protected mode program, and STDCALL enables the calling of MS-Windows functions .STACK 4096 Reserve 4086 bytes of stack space
19
ExitProcess PROTO,dwExitCode:DWORD
DumpRegs PROTO Two PROTO directives declare prototypes for procedures used by this program. ExitProcess is an MS-Windows function. DumpRegs is a procedure from the Irvine32 link library INVOKE ExitProcess,0 INVOKE is an assembler directive that calls a procedure or function
20
Progrm Template TITLE Program Template (template.asm)
; Program Description: ; Author: ; Date Created: ; Last Modification Date: INCLUDE Irvine32.inc ; (insert symbol definitions here) .data ; (insert variables here) .code main PROC ; (insert executable instructions here) exit ; exit to operating system main ENDP ; (insert additional procedures here) END main
21
Assembling, Linking, and Running Programs
22
Assemble-Link Execute Cycle
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. Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
23
Listing File Microsoft (R) Macro Assembler Version /27/06 22:12:24 Add and Subtract (AddSub.asm) Page 1 - 1 TITLE Add and Subtract (AddSub.asm) ; This program adds and subtracts 32-bit integers. ; Last update: 06/01/2006 INCLUDE Irvine32.inc C ; Include file for Irvine32.lib (Irvine32.inc) C
24
C INCLUDE SmallWin.inc ; MS-Windows prototypes, structures, and constants
C .NOLIST C .LIST C code main PROC B mov eax,10000h ; EAX = 10000h add eax,40000h ; EAX = 50000h A 2D sub eax,20000h ; EAX = 30000h F E E call DumpRegs exit B main ENDP END main
25
Microsoft (R) Macro Assembler Version 6.15.8803 09/27/06 22:12:24
Add and Subtract (AddSub.asm) Symbols 2 - 1 Structures and Unions: N a m e Size Offset Type CONSOLE_CURSOR_INFO dwSize DWord bVisible Byte CONSOLE_SCREEN_BUFFER_INFO dwCursorPos DWord wAttributes Word srWindow A QWord maxWinSize DWord COORD X Word Y Word
26
FILETIME loDateTime DWord hiDateTime DWord SMALL_RECT Left Word Top Word Right Word Bottom Word SYSTEMTIME wYear Word wMonth Word wDayOfWeek Word wDay Word wHour Word wMinute A Word wSecond C Word wMilliseconds E Word
27
Segments and Groups: N a m e Size Length Align Combine Class FLAT GROUP STACK Bit DWord Stack 'STACK' _DATA Bit DWord Public 'DATA' _TEXT Bit B DWord Public 'CODE' Procedures, parameters and locals: N a m e Type Value Attr CloseHandle P Near FLAT Length= External STDCALL ClrScr P Near FLAT Length= External STDCALL CreateFileA P Near FLAT Length= External STDCALL
28
Crlf . . . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL
Delay P Near FLAT Length= External STDCALL DumpMem P Near FLAT Length= External STDCALL DumpRegs P Near FLAT Length= External STDCALL ExitProcess P Near FLAT Length= External STDCALL FlushConsoleInputBuffer P Near FLAT Length= External STDCALL GetCommandTail P Near FLAT Length= External STDCALL GetConsoleCP P Near FLAT Length= External STDCALL GetConsoleCursorInfo P Near FLAT Length= External STDCALL GetConsoleMode P Near FLAT Length= External STDCALL GetConsoleScreenBufferInfo P Near FLAT Length= External STDCALL GetDateTime P Near FLAT Length= External STDCALL GetLocalTime P Near FLAT Length= External STDCALL
29
GetMseconds . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL
GetNumberOfConsoleInputEvents . P Near FLAT Length= External STDCALL GetStdHandle P Near FLAT Length= External STDCALL GetSystemTime P Near FLAT Length= External STDCALL GetTickCount P Near FLAT Length= External STDCALL Gotoxy P Near FLAT Length= External STDCALL IsDigit P Near FLAT Length= External STDCALL PeekConsoleInputA P Near FLAT Length= External STDCALL Random P Near FLAT Length= External STDCALL RandomRange P Near FLAT Length= External STDCALL Randomize P Near FLAT Length= External STDCALL ReadChar P Near FLAT Length= External STDCALL ReadConsoleA P Near FLAT Length= External STDCALL
30
ReadConsoleInputA. P Near. 00000000 FLAT
ReadConsoleInputA P Near FLAT Length= External STDCALL ReadFile P Near FLAT Length= External STDCALL ReadHex P Near FLAT Length= External STDCALL ReadInt P Near FLAT Length= External STDCALL ReadString P Near FLAT Length= External STDCALL SetConsoleCursorInfo P Near FLAT Length= External STDCALL SetConsoleCursorPosition P Near FLAT Length= External STDCALL SetConsoleMode P Near FLAT Length= External STDCALL SetConsoleScreenBufferSize P Near FLAT Length= External STDCALL SetConsoleTextAttribute P Near FLAT Length= External STDCALL SetConsoleTitleA P Near FLAT Length= External STDCALL SetConsoleWindowInfo P Near FLAT Length= External STDCALL SetFilePointer P Near FLAT Length= External STDCALL
31
SetTextColor . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL
Sleep P Near FLAT Length= External STDCALL Str_compare P Near FLAT Length= External STDCALL Str_copy P Near FLAT Length= External STDCALL Str_length P Near FLAT Length= External STDCALL Str_trim P Near FLAT Length= External STDCALL Str_ucase P Near FLAT Length= External STDCALL SystemTimeToFileTime P Near FLAT Length= External STDCALL WaitMsg P Near FLAT Length= External STDCALL WriteBin P Near FLAT Length= External STDCALL WriteChar P Near FLAT Length= External STDCALL WriteConsoleA P Near FLAT Length= External STDCALL WriteConsoleOutputAttribute . . P Near FLAT Length= External STDCALL
32
WriteConsoleOutputCharacterA. P Near. 00000000 FLAT
WriteConsoleOutputCharacterA . . P Near FLAT Length= External STDCALL WriteDec P Near FLAT Length= External STDCALL WriteFile P Near FLAT Length= External STDCALL WriteHex P Near FLAT Length= External STDCALL WriteInt P Near FLAT Length= External STDCALL WriteString P Near FLAT Length= External STDCALL main P Near _TEXT Length= B Public STDCALL
33
Symbols: N a m e Type Value Attr @CodeSize Number h @DataSize Number h @Interface Number h @Model Number h @code Text _TEXT @data Text FLAT @fardata? Text FLAT @fardata Text FLAT @stack Text FLAT CREATE_ALWAYS Number h CREATE_NEW Number h CreateFile Text CreateFileA DO_NOT_SHARE Number h ENABLE_ECHO_INPUT Number h ENABLE_LINE_INPUT Number h ENABLE_MOUSE_INPUT Number h ENABLE_PROCESSED_INPUT Number h ENABLE_PROCESSED_OUTPUT Number h
34
ENABLE_WINDOW_INPUT . . . . . . Number 00000008h
ENABLE_WRAP_AT_EOL_OUTPUT Number h FALSE Number h FILE_APPEND_DATA Number h FILE_ATTRIBUTE_ARCHIVE Number h FILE_ATTRIBUTE_COMPRESSED Number h FILE_ATTRIBUTE_DEVICE Number h FILE_ATTRIBUTE_DIRECTORY Number h FILE_ATTRIBUTE_ENCRYPTED Number h FILE_ATTRIBUTE_HIDDEN Number h FILE_ATTRIBUTE_NORMAL Number h FILE_ATTRIBUTE_NOT_CONTENT_INDEXED . Number h FILE_ATTRIBUTE_OFFLINE Number h FILE_ATTRIBUTE_READONLY Number h FILE_ATTRIBUTE_REPARSE_POINT . . Number h FILE_ATTRIBUTE_SPARSE_FILE Number h FILE_ATTRIBUTE_SYSTEM Number h FILE_ATTRIBUTE_TEMPORARY Number h FILE_BEGIN Number h FILE_CURRENT Number h FILE_DELETE_CHILD Number h FILE_END Number h FILE_READ_DATA Number h
35
FILE_SHARE_DELETE . . . . . . . Number 00000004h
FILE_SHARE_READ Number h FILE_SHARE_WRITE Number h FILE_WRITE_DATA Number h FOCUS_EVENT Number h GENERIC_ALL Number h GENERIC_EXECUTE Number h GENERIC_READ Number h GENERIC_WRITE Number h INVALID_HANDLE_VALUE Number h KEY_EVENT Number h MENU_EVENT Number h MOUSE_EVENT Number h NULL Number h OPEN_ALWAYS Number h OPEN_EXISTING Number h PeekConsoleInput Text PeekConsoleInputA ReadConsoleInput Text ReadConsoleInputA ReadConsole Text ReadConsoleA STD_INPUT_HANDLE Number Ah STD_OUTPUT_HANDLE Number Bh SetConsoleTitle Text SetConsoleTitleA
36
TRUE Number h TRUNCATE_EXISTING Number h WINDOW_BUFFER_SIZE_EVENT Number h WriteConsoleOutputCharacter . . Text WriteConsoleOutputCharacterA WriteConsole Text WriteConsoleA black Number h blue Number h brown Number h cyan Number h exit Text INVOKE ExitProcess,0 gray Number h green Number h lightBlue Number h lightCyan Number Bh lightGray Number h lightGreen Number Ah lightMagenta Number Dh lightRed Number Ch magenta Number h red Number h white Number Fh yellow Number Eh 0 Warnings 0 Errors
37
Map File AddSub Timestamp is 4523cd3d (Wed Oct 04 23:03:25 2006)
Preferred load address is Start Length Name Class 0001: c40H .text CODE 0002: H .rdata DATA 0002: H .edata DATA 0003: e03H .data DATA 0003:00000e H .bss DATA 0004: H .idata$ DATA 0004: H .idata$ DATA 0004: cH .idata$ DATA 0004: cH .idata$ DATA 0004: dH .idata$ DATA
38
Address Publics by Value Rva+Base Lib:Object
0001: f AddSub.obj 0001: f irvine32:Irvine32.obj 0001: f irvine32:Irvine32.obj 0001:000000a a0 f irvine32:Irvine32.obj 0001:000000a a9 f irvine32:Irvine32.obj 0001: f irvine32:Irvine32.obj 0001: f irvine32:Irvine32.obj 0001: b b f irvine32:Irvine32.obj 0001: b b f irvine32:Irvine32.obj 0001:000003f f4 f irvine32:Irvine32.obj 0001: a a f irvine32:Irvine32.obj 0001: f irvine32:Irvine32.obj
39
0001:00000453 _Random32@0 00401453 f irvine32:Irvine32.obj
0001: e e f irvine32:Irvine32.obj 0001: f irvine32:Irvine32.obj 0001:000004e e4 f irvine32:Irvine32.obj 0001: f irvine32:Irvine32.obj 0001: f irvine32:Irvine32.obj 0001: f irvine32:Irvine32.obj 0001: c c f irvine32:Irvine32.obj 0001:000006b b6 f irvine32:Irvine32.obj 0001:000006d d9 f irvine32:Irvine32.obj 0001:000006f f3 f irvine32:Irvine32.obj 0001: f irvine32:Irvine32.obj
40
0001:00000742 _WaitMsg@0 00401742 f irvine32:Irvine32.obj
0001: b b f irvine32:Irvine32.obj 0001:000007cf cf f irvine32:Irvine32.obj 0001:000007fe fe f irvine32:Irvine32.obj 0001: f f f irvine32:Irvine32.obj 0001: f irvine32:Irvine32.obj 0001:000008f f7 f irvine32:Irvine32.obj 0001: f kernel32:KERNEL32.dll 0001: a a f kernel32:KERNEL32.dll 0001: f kernel32:KERNEL32.dll 0001: f kernel32:KERNEL32.dll 0001: c c f kernel32:KERNEL32.dll 0001:000009a a2 f kernel32:KERNEL32.dll
41
0001:000009a8 _GetSystemTime@4 004019a8 f kernel32:KERNEL32.dll
0001:000009ae ae f kernel32:KERNEL32.dll 0001:000009b b4 f kernel32:KERNEL32.dll 0001:000009ba ba f kernel32:KERNEL32.dll 0001:000009c c0 f kernel32:KERNEL32.dll 0001:000009c c6 f kernel32:KERNEL32.dll 0001:000009cc cc f kernel32:KERNEL32.dll 0001:000009d d2 f kernel32:KERNEL32.dll 0004: __IMPORT_DESCRIPTOR_KERNEL kernel32:KERNEL32.dll 0004: __NULL_IMPORT_DESCRIPTOR kernel32:KERNEL32.dll 0004: kernel32:KERNEL32.dll 0004: kernel32:KERNEL32.dll 0004: c c kernel32:KERNEL32.dll
42
0004:000000a0 __imp__GetConsoleMode@8 004060a0 kernel32:KERNEL32.dll
0004:000000a a4 kernel32:KERNEL32.dll 0004:000000a a8 kernel32:KERNEL32.dll 0004:000000ac ac kernel32:KERNEL32.dll 0004:000000b b0 kernel32:KERNEL32.dll 0004:000000b b4 kernel32:KERNEL32.dll 0004:000000b b8 kernel32:KERNEL32.dll 0004:000000bc bc kernel32:KERNEL32.dll 0004:000000c c0 kernel32:KERNEL32.dll 0004:000000c c4 kernel32:KERNEL32.dll 0004:000000c c8 kernel32:KERNEL32.dll 0004:000000cc \177KERNEL32_NULL_THUNK_DATA cc kernel32:KERNEL32.dll entry point at :
43
Defining Data Intrinsic Data Types Data Definition Statement
Defining BYTE and SBYTE Data 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 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
44
Intrinsic Data Types (1 of 2)
BYTE, SBYTE 8-bit unsigned integer; 8-bit signed integer WORD, SWORD 16-bit unsigned & signed integer DWORD, SDWORD 32-bit unsigned & signed integer QWORD 64-bit integer TBYTE 80-bit integer Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
45
Intrinsic Data Types (2 of 2)
REAL4 4-byte IEEE short real REAL8 8-byte IEEE long real REAL10 10-byte IEEE extended real Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
46
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 Intel-Based Computers, 2007.
47
Defining BYTE and SBYTE Data
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 Intel-Based Computers, 2007.
48
Defining Byte Arrays Examples that use multiple initializers:
list1 BYTE 10,20,30,40 list2 BYTE 10,20,30,40 BYTE 50,60,70,80 BYTE 81,82,83,84 list3 BYTE ?,32,41h, b list4 BYTE 0Ah,20h,‘A’,22h Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
49
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 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 Intel-Based Computers, 2007.
50
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 Intel-Based Computers, 2007.
51
Defining Strings (3 of 3) End-of-line character sequence:
0Dh = carriage return 0Ah = line feed str1 BYTE "Enter your name: ",0Dh,0Ah BYTE "Enter your address: ",0 newLine BYTE 0Dh,0Ah,0 Idea: Define all strings used by your program in the same area of the data segment. Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
52
Using the DUP Operator Use DUP to allocate (create space for) an array or string. Syntax: counter DUP ( argument ) Counter and argument must be constants or constant expressions var1 BYTE 20 DUP(0) ; 20 bytes, all equal to zero var2 BYTE 20 DUP(?) ; 20 bytes, uninitialized var3 BYTE 4 DUP("STACK") ; 20 bytes: "STACKSTACKSTACKSTACK" var4 BYTE 10,3 DUP(0),20 ; 5 bytes Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
53
Defining WORD and SWORD Data
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 Intel-Based Computers, 2007.
54
Defining DWORD and SDWORD Data
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 Intel-Based Computers, 2007.
55
Defining QWORD, TBYTE, Real Data
Storage definitions for quadwords, tenbyte values, and real numbers: quad1 QWORD h val1 TBYTE Ah rVal1 REAL rVal2 REAL8 3.2E-260 rVal3 REAL10 4.6E+4096 ShortArray REAL4 20 DUP(0.0) Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
56
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 Intel-Based Computers, 2007.
57
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 Intel-Based Computers, 2007.
58
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(?) Advantage: the program's EXE file size is reduced. Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
59
What's Next Basic Elements of Assembly Language
Example: Adding and Subtracting Integers Assembling, Linking, and Running Programs Defining Data Symbolic Constants Real-Address Mode Programming Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
60
Symbolic Constants Equal-Sign Directive
Calculating the Sizes of Arrays and Strings EQU Directive TEXTEQU Directive Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
61
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 al,COUNT Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
62
Calculating the Size of a Byte Array
current location counter: $ subtract address of list difference is the number of bytes list BYTE 10,20,30,40 ListSize = ($ - list) Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
63
Calculating the Size of a Word Array
Divide total number of bytes by 2 (the size of a word) list WORD 1000h,2000h,3000h,4000h ListSize = ($ - list) / 2 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
64
Calculating the Size of a Doubleword Array
Divide total number of bytes by 4 (the size of a doubleword) list DWORD 1,2,3,4 ListSize = ($ - list) / 4 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
65
EQU Directive Define a symbol as either an integer or text expression.
Cannot be redefined PI EQU <3.1416> pressKey EQU <"Press any key to continue...",0> .data prompt BYTE pressKey Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
66
TEXTEQU Directive Define a symbol as either an integer or text expression. Called a text macro Can be redefined continueMsg TEXTEQU <"Do you wish to continue (Y/N)?"> rowSize = 5 .data prompt1 BYTE continueMsg count TEXTEQU %(rowSize * 2) ; evaluates the expression setupAL TEXTEQU <mov al,count> .code setupAL ; generates: "mov al,10" Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
67
Real-Address Mode Programming
TITLE Add and Subtract, Version (AddSub2r.asm) ; This program adds and subtracts 32-bit integers ; and stores the sum in a variable. (From page 94.) ; Last update: 06/01/2006 INCLUDE Irvine16.inc ; new .data val1 dword h val2 dword h val3 dword h finalVal dword ? .code main PROC mov ; initialize DS mov ds,ax ; new
68
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
69
Exercise TITLE Data Definitions (DataDef.asm)
; Examples showing how to define data. ; Last update: 06/01/2006 INCLUDE Irvine32.inc ; Byte Values .data value1 BYTE 'A' value2 BYTE 0 .code main PROC ; (insert instructions here) exit main ENDP END main
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.