Download assembly2

Document related concepts
no text concepts found
Transcript
Assembly Language
Assembly Language Fundamentals
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

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

Integer Constants


Format: [{+|-}] digits [radix]
 Radix: h, Hexadecimal

q/o, Octal

d/t, Decimal

b/y, Binary

r, Encoded real
For example:
 26
Decimal
 26d
Decimal
 11010011b Binary
 42q
Octal
 42o
Octal
 1Ah
Hexadecimal
 0A3h
Hexadecimal, note the 0 before A

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

Real Number Constants



Format: [{+,-}]integer.[integer][exponent]
exponent: E[{+,-}]integer
For example:
2.
 +3.0
 -44.2E+05
 26.E5


Character Constants



Enclosed in single or double quotes
For example:
 ‘A’
 “d”
String Constants



Enclosed in single or double quotes
For example:
 ‘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

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 as @data.

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
 @@myfile
 _12345

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

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 &

This line is a comment

This line is also a omment
 &

NOP instruction


Takes up 1 byte of program storage
and doesn’t do any work
For example:
mov ax, bx
 nop
 mov edx, ecx

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
add
sub
call
exit
main ENDP
END main
eax,10000h
eax,40000h
eax,20000h
DumpRegs
; EAX = 10000h
; EAX = 50000h
; EAX = 30000h








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.









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.






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.
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
add
sub
call
eax,10000h
eax,40000h
eax,20000h
DumpRegs
INVOKE ExitProcess,0
main ENDP
END main
; EAX = 10000h
; EAX = 50000h
; EAX = 30000h






.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







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
Progrm Template

TITLE Program Template
(template.asm)

;
;
;
;

INCLUDE Irvine32.inc

; (insert symbol definitions here)





Program Description:
Author:
Date Created:
Last Modification Date:
.data
; (insert variables here)

.code
main PROC

; (insert executable instructions here)


exit
main ENDP

; (insert additional procedures here)

END main

; exit to operating system
Assembling, Linking, and Running
Programs
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.
Link
Library
Source
File
Step 1: text editor
Step 2:
assembler
Object
File
Listing
File
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
Step 3:
linker
Executable
File
Map
File
Web site
Examples
Step 4:
OS loader
Output
Listing File




Microsoft (R) Macro Assembler Version 6.15.8803
09/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




(Irvine32.inc)
INCLUDE Irvine32.inc
C ; Include file for Irvine32.lib
C













C INCLUDE SmallWin.inc
; MS-Windows prototypes, structures, and constants
C .NOLIST
C .LIST
C
C .NOLIST
C .LIST
C
00000000
00000000
00000000 B8 00010000
eax,10000h
00000005 05 00040000
eax,40000h
0000000A 2D 00020000
eax,20000h
0000000F E8 00000000 E




0000001B
.code
main PROC
; EAX = 10000h
; EAX = 50000h
mov
add
sub
; EAX = 30000h
call
DumpRegs
exit
main ENDP
END main

















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:
Name
Size
Offset
Type
CONSOLE_CURSOR_INFO . . . . . .
00000005
dwSize . . . . . . . . . . . .
00000000
DWord
bVisible . . . . . . . . . . .
00000004
Byte
CONSOLE_SCREEN_BUFFER_INFO . . .
00000016
dwSize . . . . . . . . . . . .
00000000
DWord
dwCursorPos . . . . . . . . .
00000004
DWord
wAttributes . . . . . . . . .
00000008
Word
srWindow . . . . . . . . . . .
0000000A
QWord
maxWinSize . . . . . . . . . .
00000012
DWord
COORD . . . . . . . . . . . . .
00000004
X . . . . . . . . . . . . . . 00000000
Word
Y . . . . . . . . . . . . . . 00000002
Word

















FILETIME . . . . . . . . . . . .
00000008
loDateTime . . . . . . . . . . 00000000
hiDateTime . . . . . . . . . . 00000004
SMALL_RECT . . . . . . . . . . . 00000008
Left . . . . . . . . . . . . .
00000000
Top . . . . . . . . . . . . .
00000002
Right . . . . . . . . . . . .
00000004
Bottom . . . . . . . . . . . .
00000006
SYSTEMTIME . . . . . . . . . . . 00000010
wYear . . . . . . . . . . . .
00000000
wMonth . . . . . . . . . . . .
00000002
wDayOfWeek . . . . . . . . . . 00000004
wDay . . . . . . . . . . . . .
00000006
wHour . . . . . . . . . . . .
00000008
wMinute . . . . . . . . . . .
0000000A
wSecond . . . . . . . . . . . 0000000C
wMilliseconds . . . . . . . . 0000000E
DWord
DWord
Word
Word
Word
Word
Word
Word
Word
Word
Word
Word
Word
Word











Segments and Groups:
Class
Name
FLAT . . . . . . . . . . . . . .
STACK . . . . . . . . . . . . .
Stack
'STACK'
_DATA . . . . . . . . . . . . .
Public 'DATA'
_TEXT . . . . . . . . . . . . .
Public 'CODE'
Size
Length
Align
Combine
GROUP
32 Bit
00001000 DWord
32 Bit
00000000 DWord
32 Bit
0000001B DWord
Procedures, parameters and locals:
Name
Type
CloseHandle . . . . . . . . . .
P
Length= 00000000 External
ClrScr . . . . . . . . . . . . .
P
Length= 00000000 External
CreateFileA . . . . . . . . . .
P
Length= 00000000 External
Value
Attr
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL













Crlf . . . . . . . . . . . . . .
P
Length= 00000000 External
Delay . . . . . . . . . . . . .
P
Length= 00000000 External
DumpMem . . . . . . . . . . . .
P
Length= 00000000 External
DumpRegs . . . . . . . . . . . .
P
Length= 00000000 External
ExitProcess . . . . . . . . . .
P
Length= 00000000 External
FlushConsoleInputBuffer . . . . P
Length= 00000000 External
GetCommandTail . . . . . . . . . P
Length= 00000000 External
GetConsoleCP . . . . . . . . . .
P
Length= 00000000 External
GetConsoleCursorInfo . . . . . . P
Length= 00000000 External
GetConsoleMode . . . . . . . . . P
Length= 00000000 External
GetConsoleScreenBufferInfo . . .
FLAT Length= 00000000 External
GetDateTime . . . . . . . . . .
P
Length= 00000000 External
GetLocalTime . . . . . . . . . .
P
Length= 00000000 External
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
P Near
00000000
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL













GetMseconds . . . . . . . . . .
P
Length= 00000000 External
GetNumberOfConsoleInputEvents
FLAT Length= 00000000 External
GetStdHandle . . . . . . . . . .
P
Length= 00000000 External
GetSystemTime . . . . . . . . .
P
Length= 00000000 External
GetTickCount . . . . . . . . . .
P
Length= 00000000 External
Gotoxy . . . . . . . . . . . . .
P
Length= 00000000 External
IsDigit . . . . . . . . . . . .
P
Length= 00000000 External
PeekConsoleInputA . . . . . . . P
Length= 00000000 External
Random32 . . . . . . . . . . . .
P
Length= 00000000 External
RandomRange . . . . . . . . . .
P
Length= 00000000 External
Randomize . . . . . . . . . . .
P
Length= 00000000 External
ReadChar . . . . . . . . . . . .
P
Length= 00000000 External
ReadConsoleA . . . . . . . . . .
P
Length= 00000000 External
Near
00000000 FLAT
STDCALL
.
P Near
00000000
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL













ReadConsoleInputA . . . . . . . P
Length= 00000000 External
ReadFile . . . . . . . . . . . .
P
Length= 00000000 External
ReadHex . . . . . . . . . . . .
P
Length= 00000000 External
ReadInt . . . . . . . . . . . .
P
Length= 00000000 External
ReadString . . . . . . . . . . .
P
Length= 00000000 External
SetConsoleCursorInfo . . . . . . P
Length= 00000000 External
SetConsoleCursorPosition . . . . P
Length= 00000000 External
SetConsoleMode . . . . . . . . .
P
Length= 00000000 External
SetConsoleScreenBufferSize . . .
FLAT Length= 00000000 External
SetConsoleTextAttribute . . . . P
Length= 00000000 External
SetConsoleTitleA . . . . . . . .
P
Length= 00000000 External
SetConsoleWindowInfo . . . . . . P
Length= 00000000 External
SetFilePointer . . . . . . . . .
P
Length= 00000000 External
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
P Near
00000000
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL
Near
00000000 FLAT
STDCALL













SetTextColor . . . . . . . . . .
P
Length= 00000000 External
Sleep . . . . . . . . . . . . .
P
Length= 00000000 External
Str_compare . . . . . . . . . .
P
Length= 00000000 External
Str_copy . . . . . . . . . . . .
P
Length= 00000000 External
Str_length . . . . . . . . . . .
P
Length= 00000000 External
Str_trim . . . . . . . . . . . .
P
Length= 00000000 External
Str_ucase . . . . . . . . . . .
P
Length= 00000000 External
SystemTimeToFileTime . . . . . . P
Length= 00000000 External
WaitMsg . . . . . . . . . . . .
P
Length= 00000000 External
WriteBin . . . . . . . . . . . .
P
Length= 00000000 External
WriteChar . . . . . . . . . . .
P
Length= 00000000 External
WriteConsoleA . . . . . . . . .
P
Length= 00000000 External
WriteConsoleOutputAttribute . .P
Length= 00000000 External
Near
00000000
STDCALL
Near
00000000
STDCALL
Near
00000000
STDCALL
Near
00000000
STDCALL
Near
00000000
STDCALL
Near
00000000
STDCALL
Near
00000000
STDCALL
Near
00000000
STDCALL
Near
00000000
STDCALL
Near
00000000
STDCALL
Near
00000000
STDCALL
Near
00000000
STDCALL
Near
00000000
STDCALL
FLAT
FLAT
FLAT
FLAT
FLAT
FLAT
FLAT
FLAT
FLAT
FLAT
FLAT
FLAT
FLAT







WriteConsoleOutputCharacterA . . P Near 00000000
FLAT Length= 00000000 External STDCALL
WriteDec . . . . . . . . . . . . P Near 00000000 FLAT
Length= 00000000 External STDCALL
WriteFile . . . . . . . . . . .
P Near 00000000 FLAT
Length= 00000000 External STDCALL
WriteHex . . . . . . . . . . . . P Near 00000000 FLAT
Length= 00000000 External STDCALL
WriteInt . . . . . . . . . . . .
P Near 00000000 FLAT
Length= 00000000 External STDCALL
WriteString . . . . . . . . . . P Near 00000000 FLAT
Length= 00000000 External STDCALL
main . . . . . . . . . . . . . .
P Near 00000000 _TEXT
Length= 0000001B Public STDCALL




















Symbols:
Name
Type
Value
@CodeSize . . . . . . . . . . .
Number
@DataSize . . . . . . . . . . .
Number
@Interface . . . . . . . . . . .
Number
@Model . . . . . . . . . . . . .
Number
@code . . . . . . . . . . . . .
Text
@data . . . . . . . . . . . . .
Text
@fardata? . . . . . . . . . . .
Text
@fardata . . . . . . . . . . . .
Text
@stack . . . . . . . . . . . . .
Text
CREATE_ALWAYS . . . . . . . . . Number
CREATE_NEW . . . . . . . . . . .
Number
CreateFile . . . . . . . . . . .
Text
DO_NOT_SHARE . . . . . . . . . . Number
ENABLE_ECHO_INPUT . . . . . . .
ENABLE_LINE_INPUT . . . . . . . Number
ENABLE_MOUSE_INPUT . . . . . . .
ENABLE_PROCESSED_INPUT . . . . .
ENABLE_PROCESSED_OUTPUT . . . .
Attr
00000000h
00000000h
00000003h
00000007h
_TEXT
FLAT
FLAT
FLAT
FLAT
00000002h
00000001h
CreateFileA
00000000h
Number 00000004h
00000002h
Number 00000010h
Number 00000001h
Number 00000001h























ENABLE_WINDOW_INPUT . . . . . .
Number 00000008h
ENABLE_WRAP_AT_EOL_OUTPUT . . .
Number 00000002h
FALSE . . . . . . . . . . . . .
Number 00000000h
FILE_APPEND_DATA . . . . . . . . Number 00000004h
FILE_ATTRIBUTE_ARCHIVE . . . . .
Number 00000020h
FILE_ATTRIBUTE_COMPRESSED . . .
Number 00000800h
FILE_ATTRIBUTE_DEVICE . . . . .
Number 00000040h
FILE_ATTRIBUTE_DIRECTORY . . . .
Number 00000010h
FILE_ATTRIBUTE_ENCRYPTED . . . .
Number 00004000h
FILE_ATTRIBUTE_HIDDEN . . . . .
Number 00000002h
FILE_ATTRIBUTE_NORMAL . . . . .
Number 00000080h
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED .
Number
00002000h
FILE_ATTRIBUTE_OFFLINE . . . . .
Number 00001000h
FILE_ATTRIBUTE_READONLY . . . .
Number 00000001h
FILE_ATTRIBUTE_REPARSE_POINT . .
Number 00000400h
FILE_ATTRIBUTE_SPARSE_FILE . . .
Number 00000200h
FILE_ATTRIBUTE_SYSTEM . . . . .
Number 00000004h
FILE_ATTRIBUTE_TEMPORARY . . . .
Number 00000100h
FILE_BEGIN . . . . . . . . . . .
Number 00000000h
FILE_CURRENT . . . . . . . . . .
Number 00000001h
FILE_DELETE_CHILD . . . . . . . Number 00000040h
FILE_END . . . . . . . . . . . .
Number 00000002h
FILE_READ_DATA . . . . . . . . . Number 00000001h






















FILE_SHARE_DELETE . . . . . . . Number
FILE_SHARE_READ . . . . . . . . Number
FILE_SHARE_WRITE . . . . . . . . Number
FILE_WRITE_DATA . . . . . . . . Number
FOCUS_EVENT . . . . . . . . . . Number
GENERIC_ALL . . . . . . . . . .
Number
GENERIC_EXECUTE . . . . . . . . Number
GENERIC_READ . . . . . . . . . . Number
GENERIC_WRITE . . . . . . . . . Number
INVALID_HANDLE_VALUE . . . . . .
KEY_EVENT . . . . . . . . . . .
Number
MENU_EVENT . . . . . . . . . . .
Number
MOUSE_EVENT . . . . . . . . . . Number
NULL . . . . . . . . . . . . . .
Number
OPEN_ALWAYS . . . . . . . . . . Number
OPEN_EXISTING . . . . . . . . . Number
PeekConsoleInput . . . . . . . .
Text
ReadConsoleInput . . . . . . . . Text
ReadConsole . . . . . . . . . .
Text
STD_INPUT_HANDLE . . . . . . . .
STD_OUTPUT_HANDLE . . . . . . .
SetConsoleTitle . . . . . . . .
Text
00000004h
00000001h
00000002h
00000002h
00000010h
10000000h
20000000h
-80000000h
40000000h
Number -00000001h
00000001h
00000008h
00000002h
00000000h
00000004h
00000003h
PeekConsoleInputA
ReadConsoleInputA
ReadConsoleA
Number -0000000Ah
Number -0000000Bh
SetConsoleTitleA
























TRUE . . . . . . . . . . . . . .
Number
TRUNCATE_EXISTING . . . . . . .
WINDOW_BUFFER_SIZE_EVENT . . . .
WriteConsoleOutputCharacter . .
WriteConsoleOutputCharacterA
WriteConsole . . . . . . . . . .
Text
black . . . . . . . . . . . . .
Number
blue . . . . . . . . . . . . . .
Number
brown . . . . . . . . . . . . .
Number
cyan . . . . . . . . . . . . . .
Number
exit . . . . . . . . . . . . . .
Text
gray . . . . . . . . . . . . . .
Number
green . . . . . . . . . . . . .
Number
lightBlue . . . . . . . . . . .
Number
lightCyan . . . . . . . . . . .
Number
lightGray . . . . . . . . . . .
Number
lightGreen . . . . . . . . . . .
Number
lightMagenta . . . . . . . . . .
Number
lightRed . . . . . . . . . . . .
Number
magenta . . . . . . . . . . . .
Number
red . . . . . . . . . . . . . .
Number
white . . . . . . . . . . . . .
Number
yellow . . . . . . . . . . . . .
Number
0 Warnings
0 Errors
00000001h
Number 00000005h
Number 00000004h
Text
WriteConsoleA
00000000h
00000001h
00000006h
00000003h
INVOKE ExitProcess,0
00000008h
00000002h
00000009h
0000000Bh
00000007h
0000000Ah
0000000Dh
0000000Ch
00000005h
00000004h
0000000Fh
0000000Eh
Map File

AddSub

Timestamp is 4523cd3d (Wed Oct 04 23:03:25 2006)

Preferred load address is 00400000












Start
Length
Name
0001:00000000 00001c40H .text
0002:00000000 00000121H .rdata
0002:00000121 00000000H .edata
0003:00000000 00000e03H .data
0003:00000e04 00000224H .bss
0004:00000000 00000014H .idata$2
0004:00000014 00000014H .idata$3
0004:00000028 0000006cH .idata$4
0004:00000094 0000006cH .idata$5
0004:00000100 0000022dH .idata$6
Class
CODE
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA














Address
Publics by Value
Rva+Base
Lib:Object
0001:00000010
_main@0
00401010 f
AddSub.obj
0001:00000034
_ClrScr@0
00401034 f
irvine32:Irvine32.obj
0001:00000083
_Crlf@0
00401083 f
irvine32:Irvine32.obj
0001:000000a0
_Delay@0
004010a0 f
irvine32:Irvine32.obj
0001:000000a9
_DumpMem@0
004010a9 f
irvine32:Irvine32.obj
0001:00000179
_DumpRegs@0
00401179 f
irvine32:Irvine32.obj
0001:00000355
_GetCommandTail@0
00401355 f
irvine32:Irvine32.obj
0001:0000036b
_GetDateTime@4
0040136b f
irvine32:Irvine32.obj
0001:0000039b
_GetMseconds@0
0040139b f
irvine32:Irvine32.obj
0001:000003f4
_Gotoxy@0
004013f4 f
irvine32:Irvine32.obj
0001:0000042a
_IsDigit@0
0040142a f
irvine32:Irvine32.obj
0001:00000437
_RandomRange@0
00401437 f
irvine32:Irvine32.obj













0001:00000453
_Random32@0
irvine32:Irvine32.obj
0001:0000046e
_Randomize@0
irvine32:Irvine32.obj
0001:00000487
_ReadChar@0
irvine32:Irvine32.obj
0001:000004e4
_ReadHex@0
irvine32:Irvine32.obj
0001:00000544
_ReadInt@0
irvine32:Irvine32.obj
0001:00000601
_ReadString@0
irvine32:Irvine32.obj
0001:00000667
_SetTextColor@0
irvine32:Irvine32.obj
0001:0000068c
_Str_compare@8
irvine32:Irvine32.obj
0001:000006b6
_Str_copy@8
irvine32:Irvine32.obj
0001:000006d9
_Str_length@4
irvine32:Irvine32.obj
0001:000006f3
_Str_trim@8
irvine32:Irvine32.obj
0001:00000720
_Str_ucase@4
irvine32:Irvine32.obj
00401453 f
0040146e f
00401487 f
004014e4 f
00401544 f
00401601 f
00401667 f
0040168c f
004016b6 f
004016d9 f
004016f3 f
00401720 f














0001:00000742
_WaitMsg@0
00401742 f
irvine32:Irvine32.obj
0001:0000079b
_WriteBin@0
0040179b f
irvine32:Irvine32.obj
0001:000007cf
_WriteChar@0
004017cf f
irvine32:Irvine32.obj
0001:000007fe
_WriteDec@0
004017fe f
irvine32:Irvine32.obj
0001:0000083f
_WriteHex@0
0040183f f
irvine32:Irvine32.obj
0001:00000896
_WriteInt@0
00401896 f
irvine32:Irvine32.obj
0001:000008f7
_WriteString@0
004018f7 f
irvine32:Irvine32.obj
0001:00000984
_ExitProcess@4
00401984 f
kernel32:KERNEL32.dll
0001:0000098a
_FlushConsoleInputBuffer@4 0040198a f
kernel32:KERNEL32.dll
0001:00000990
_GetCommandLineA@0
00401990 f
kernel32:KERNEL32.dll
0001:00000996
_GetConsoleMode@8
00401996 f
kernel32:KERNEL32.dll
0001:0000099c
_GetLocalTime@4
0040199c f
kernel32:KERNEL32.dll
0001:000009a2
_GetStdHandle@4
004019a2 f
kernel32:KERNEL32.dll














0001:000009a8
_GetSystemTime@4
004019a8 f
kernel32:KERNEL32.dll
0001:000009ae
_ReadConsoleA@20
004019ae f
kernel32:KERNEL32.dll
0001:000009b4
_SetConsoleCursorPosition@8 004019b4 f
kernel32:KERNEL32.dll
0001:000009ba
_SetConsoleMode@8
004019ba f
kernel32:KERNEL32.dll
0001:000009c0
_SetConsoleTextAttribute@8 004019c0 f
kernel32:KERNEL32.dll
0001:000009c6
_Sleep@4
004019c6 f
kernel32:KERNEL32.dll
0001:000009cc
_SystemTimeToFileTime@8 004019cc f
kernel32:KERNEL32.dll
0001:000009d2
_WriteConsoleA@20
004019d2 f
kernel32:KERNEL32.dll
0004:00000000
__IMPORT_DESCRIPTOR_KERNEL32
00406000
kernel32:KERNEL32.dll
0004:00000014
__NULL_IMPORT_DESCRIPTOR 00406014
kernel32:KERNEL32.dll
0004:00000094
__imp__ExitProcess@4
00406094
kernel32:KERNEL32.dll
0004:00000098
__imp__FlushConsoleInputBuffer@4
00406098
kernel32:KERNEL32.dll
0004:0000009c
__imp__GetCommandLineA@0 0040609c
kernel32:KERNEL32.dll













0004:000000a0
__imp__GetConsoleMode@8 004060a0
kernel32:KERNEL32.dll
0004:000000a4
__imp__GetLocalTime@4
004060a4
kernel32:KERNEL32.dll
0004:000000a8
__imp__GetStdHandle@4
004060a8
kernel32:KERNEL32.dll
0004:000000ac
__imp__GetSystemTime@4
004060ac
kernel32:KERNEL32.dll
0004:000000b0
__imp__ReadConsoleA@20
004060b0
kernel32:KERNEL32.dll
0004:000000b4
__imp__SetConsoleCursorPosition@8
004060b4
kernel32:KERNEL32.dll
0004:000000b8
__imp__SetConsoleMode@8 004060b8
kernel32:KERNEL32.dll
0004:000000bc
__imp__SetConsoleTextAttribute@8
004060bc
kernel32:KERNEL32.dll
0004:000000c0
__imp__Sleep@4
004060c0
kernel32:KERNEL32.dll
0004:000000c4
__imp__SystemTimeToFileTime@8
004060c4
kernel32:KERNEL32.dll
0004:000000c8
__imp__WriteConsoleA@20 004060c8
kernel32:KERNEL32.dll
0004:000000cc
\177KERNEL32_NULL_THUNK_DATA
004060cc
kernel32:KERNEL32.dll
entry point at
0001:00000010
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.
Web site
Examples
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.
Web site
Examples
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.
Web site
Examples
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.
Web site
Examples
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.
Web site
Examples
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,00100010b
list4 BYTE 0Ah,20h,‘A’,22h
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
Web site
Examples
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
str2 BYTE
str3 BYTE
greeting
"Enter your name",0
'Error: halting program',0
'A','E','I','O','U'
BYTE "Welcome to the Encryption Demo program "
BYTE "created by Kip Irvine.",0
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
Web site
Examples
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.
Web site
Examples
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.
Web site
Examples
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.
Web site
Examples
Defining WORD and SWORD Data
• Define storage for 16-bit integers
• or double characters
• single value or multiple values
word1
word2
word3
word4
myList
array
WORD
SWORD
WORD
WORD
WORD
WORD
65535
–32768
?
"AB"
1,2,3,4,5
5 DUP(?)
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
;
;
;
;
;
;
largest unsigned value
smallest signed value
uninitialized, unsigned
double characters
array of words
uninitialized array
Web site
Examples
Defining DWORD and SDWORD Data
Storage definitions for signed and unsigned 32-bit
integers:
val1
val2
val3
val4
DWORD
SDWORD
DWORD
SDWORD
12345678h
–2147483648
20 DUP(?)
–3,–2,–1,0,1
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
;
;
;
;
unsigned
signed
unsigned array
signed array
Web site
Examples
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 Intel-Based Computers, 2007.
Web site
Examples
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 Intel-Based Computers, 2007.
Web site
Examples
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 Intel-Based Computers, 2007.
Web site
Examples
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.
Web site
Examples
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.
Web site
Examples
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.
Web site
Examples
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.
Web site
Examples
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.
Web site
Examples
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.
Web site
Examples
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.
Web site
Examples
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.
Web site
Examples
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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007.
; generates: "mov al,10"
Web site
Examples
Real-Address Mode Programming

TITLE Add and Subtract, Version 2
(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
val2
dword
val3
dword
finalVal dword
.code
main PROC
mov
mov
10000h
40000h
20000h
?
ax,@data
ds,ax
; new
; initialize DS








mov
eax,val1
add
eax,val2
sub
eax,val3
mov
finalVal,eax
call DumpRegs
exit
main ENDP
END main
;
;
;
;
;
start with 10000h
add 40000h
subtract 20000h
store the result (30000h)
display the registers
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
Related documents