Transcript
Page 1: Assembly Language Assembly Language Fundamentals

Assembly Language

Assembly Language Fundamentals

Page 2: 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

Page 3: Assembly Language Assembly Language Fundamentals

Instructions Translated by the assembler into machine languag

e 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

Page 4: Assembly Language Assembly Language Fundamentals

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 bef

ore A

Page 5: Assembly Language Assembly Language Fundamentals

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

Page 6: Assembly Language Assembly Language Fundamentals

Real Number Constants Format: [{+,-}]integer.[integer][exponent] exponent: E[{+,-}]integer For example:

2. +3.0 -44.2E+05 26.E5

Page 7: Assembly Language Assembly Language Fundamentals

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

Page 8: Assembly Language Assembly Language Fundamentals

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.

Page 9: Assembly Language Assembly Language Fundamentals

Identifiers Contain between 1 and 127 characters Not case sensitive The first character must be a letter (A..Z, a..z), unde

rscore (_), @, ?, or $. Cannot be reserved words. For example:

var1 $first _main open_file @@myfile _12345

Page 10: Assembly Language Assembly Language Fundamentals

Label An identifier that acts as a place marker for ins

truction 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

Page 11: Assembly Language Assembly Language Fundamentals

Comments Single-line comments, beginning with a semicolon

character (;) Block comments, beginning with the COMMENT dir

ective 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 &

Page 12: Assembly Language Assembly Language Fundamentals

NOP instruction Takes up 1 byte of program storage and do

esn’t do any work For example:

mov ax, bx nop mov edx, ecx

Page 13: Assembly Language Assembly Language Fundamentals

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

Page 14: Assembly Language Assembly Language Fundamentals

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 informati

on 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.

Page 15: Assembly Language Assembly Language Fundamentals

main PROC The PROC directive identifies the beginning of a procedure. The name o

f 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.

Page 16: Assembly Language Assembly Language Fundamentals

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. I

t identifies the name of the program’s startup procedure.

Page 17: Assembly Language Assembly Language Fundamentals

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

Alternative Version of AddSub

Page 18: Assembly Language Assembly Language Fundamentals

.386 The .386 directive identifies the minimum CPU required for this progra

m

.MODEL flat,stdcall The . MODEL directive instructs the assembler to generate code for a pr

otected mode program, and STDCALL enables the calling of MS-Windows functions

.STACK 4096 Reserve 4086 bytes of stack space

Page 19: Assembly Language Assembly Language Fundamentals

ExitProcess PROTO,dwExitCode:DWORD DumpRegs PROTO Two PROTO directives declare prototypes for procedures used by this p

rogram. 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

Page 20: Assembly Language Assembly Language Fundamentals

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

Page 21: Assembly Language Assembly Language Fundamentals

Assembling, Linking, and Running Programs

Page 22: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Assemble-Link Execute CycleAssemble-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.

Page 23: Assembly Language Assembly Language Fundamentals

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

INCLUDE Irvine32.inc C ; Include file for Irvine32.lib (Irvi

ne32.inc) C

Page 24: Assembly Language Assembly Language Fundamentals

C INCLUDE SmallWin.inc ; MS-Windows prototypes, structures, and constants

C .NOLIST C .LIST C C .NOLIST C .LIST C

00000000 .code 00000000 main PROC

00000000 B8 00010000 mov eax,10000h; EAX = 10000h

00000005 05 00040000 add eax,40000h; EAX = 50000h

0000000A 2D 00020000 sub eax,20000h; EAX = 30000h

0000000F E8 00000000 E call DumpRegs

exit 0000001B main ENDP END main

Page 25: Assembly Language Assembly Language Fundamentals

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

Page 26: Assembly Language Assembly Language Fundamentals

FILETIME . . . . . . . . . . . . 00000008 loDateTime . . . . . . . . . . 00000000 DWord hiDateTime . . . . . . . . . . 00000004 DWord SMALL_RECT . . . . . . . . . . . 00000008 Left . . . . . . . . . . . . . 00000000 Word Top . . . . . . . . . . . . . 00000002 Word Right . . . . . . . . . . . . 00000004 Word Bottom . . . . . . . . . . . . 00000006 Word SYSTEMTIME . . . . . . . . . . . 00000010 wYear . . . . . . . . . . . . 00000000 Word wMonth . . . . . . . . . . . . 00000002 Word wDayOfWeek . . . . . . . . . . 00000004 Word wDay . . . . . . . . . . . . . 00000006 Word wHour . . . . . . . . . . . . 00000008 Word wMinute . . . . . . . . . . . 0000000A Word wSecond . . . . . . . . . . . 0000000C Word wMilliseconds . . . . . . . . 0000000E Word

Page 27: Assembly Language Assembly Language Fundamentals

Segments and Groups:

N a m e Size Length Align Combine Class

FLAT . . . . . . . . . . . . . . GROUP STACK . . . . . . . . . . . . . 32 Bit 00001000 DWord Stack 'S

TACK' _DATA . . . . . . . . . . . . . 32 Bit 00000000 DWord Public 'DAT

A' _TEXT . . . . . . . . . . . . . 32 Bit 0000001B DWord Public 'COD

E'

Procedures, parameters and locals:

N a m e Type Value Attr

CloseHandle . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

ClrScr . . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

CreateFileA . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

Page 28: Assembly Language Assembly Language Fundamentals

Crlf . . . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

Delay . . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

DumpMem . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

DumpRegs . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

ExitProcess . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

FlushConsoleInputBuffer . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

GetCommandTail . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

GetConsoleCP . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

GetConsoleCursorInfo . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

GetConsoleMode . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

GetConsoleScreenBufferInfo . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

GetDateTime . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

GetLocalTime . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

Page 29: Assembly Language Assembly Language Fundamentals

GetMseconds . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

GetNumberOfConsoleInputEvents . P Near 00000000 FLAT Length= 00000000 External STDCALL

GetStdHandle . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

GetSystemTime . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

GetTickCount . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

Gotoxy . . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

IsDigit . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

PeekConsoleInputA . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

Random32 . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

RandomRange . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

Randomize . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

ReadChar . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

ReadConsoleA . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

Page 30: Assembly Language Assembly Language Fundamentals

ReadConsoleInputA . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

ReadFile . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

ReadHex . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

ReadInt . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

ReadString . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

SetConsoleCursorInfo . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

SetConsoleCursorPosition . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

SetConsoleMode . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

SetConsoleScreenBufferSize . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

SetConsoleTextAttribute . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

SetConsoleTitleA . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

SetConsoleWindowInfo . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

SetFilePointer . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

Page 31: Assembly Language Assembly Language Fundamentals

SetTextColor . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

Sleep . . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

Str_compare . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

Str_copy . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

Str_length . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

Str_trim . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

Str_ucase . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

SystemTimeToFileTime . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

WaitMsg . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

WriteBin . . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

WriteChar . . . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

WriteConsoleA . . . . . . . . . P Near 00000000 FLAT Length= 00000000 External STDCALL

WriteConsoleOutputAttribute . . P Near 00000000 FLAT Length= 00000000 External STDCALL

Page 32: Assembly Language Assembly Language Fundamentals

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

Page 33: Assembly Language Assembly Language Fundamentals

Symbols:

N a m e Type Value Attr

@CodeSize . . . . . . . . . . . Number 00000000h @DataSize . . . . . . . . . . . Number 00000000h @Interface . . . . . . . . . . . Number 00000003h @Model . . . . . . . . . . . . . Number 00000007h @code . . . . . . . . . . . . . Text _TEXT @data . . . . . . . . . . . . . Text FLAT @fardata? . . . . . . . . . . . Text FLAT @fardata . . . . . . . . . . . . Text FLAT @stack . . . . . . . . . . . . . Text FLAT CREATE_ALWAYS . . . . . . . . . Number 00000002h CREATE_NEW . . . . . . . . . . . Number 00000001h CreateFile . . . . . . . . . . . Text CreateFileA DO_NOT_SHARE . . . . . . . . . . Number 00000000h ENABLE_ECHO_INPUT . . . . . . . Number 00000004h ENABLE_LINE_INPUT . . . . . . . Number 00000002h ENABLE_MOUSE_INPUT . . . . . . . Number 00000010h ENABLE_PROCESSED_INPUT . . . . . Number 00000001h ENABLE_PROCESSED_OUTPUT . . . . Number 00000001h

Page 34: Assembly Language Assembly Language Fundamentals

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

Page 35: Assembly Language Assembly Language Fundamentals

FILE_SHARE_DELETE . . . . . . . Number 00000004h FILE_SHARE_READ . . . . . . . . Number 00000001h FILE_SHARE_WRITE . . . . . . . . Number 00000002h FILE_WRITE_DATA . . . . . . . . Number 00000002h FOCUS_EVENT . . . . . . . . . . Number 00000010h GENERIC_ALL . . . . . . . . . . Number 10000000h GENERIC_EXECUTE . . . . . . . . Number 20000000h GENERIC_READ . . . . . . . . . . Number -80000000h GENERIC_WRITE . . . . . . . . . Number 40000000h INVALID_HANDLE_VALUE . . . . . . Number -00000001h KEY_EVENT . . . . . . . . . . . Number 00000001h MENU_EVENT . . . . . . . . . . . Number 00000008h MOUSE_EVENT . . . . . . . . . . Number 00000002h NULL . . . . . . . . . . . . . . Number 00000000h OPEN_ALWAYS . . . . . . . . . . Number 00000004h OPEN_EXISTING . . . . . . . . . Number 00000003h PeekConsoleInput . . . . . . . . Text PeekConsoleInputA ReadConsoleInput . . . . . . . . Text ReadConsoleInputA ReadConsole . . . . . . . . . . Text ReadConsoleA STD_INPUT_HANDLE . . . . . . . . Number -0000000Ah STD_OUTPUT_HANDLE . . . . . . . Number -0000000Bh SetConsoleTitle . . . . . . . . Text SetConsoleTitleA

Page 36: Assembly Language Assembly Language Fundamentals

TRUE . . . . . . . . . . . . . . Number 00000001h TRUNCATE_EXISTING . . . . . . . Number 00000005h WINDOW_BUFFER_SIZE_EVENT . . . . Number 00000004h WriteConsoleOutputCharacter . . Text WriteConsoleOutputC

haracterA WriteConsole . . . . . . . . . . Text WriteConsoleA black . . . . . . . . . . . . . Number 00000000h blue . . . . . . . . . . . . . . Number 00000001h brown . . . . . . . . . . . . . Number 00000006h cyan . . . . . . . . . . . . . . Number 00000003h exit . . . . . . . . . . . . . . Text INVOKE ExitProcess,0 gray . . . . . . . . . . . . . . Number 00000008h green . . . . . . . . . . . . . Number 00000002h lightBlue . . . . . . . . . . . Number 00000009h lightCyan . . . . . . . . . . . Number 0000000Bh lightGray . . . . . . . . . . . Number 00000007h lightGreen . . . . . . . . . . . Number 0000000Ah lightMagenta . . . . . . . . . . Number 0000000Dh lightRed . . . . . . . . . . . . Number 0000000Ch magenta . . . . . . . . . . . . Number 00000005h red . . . . . . . . . . . . . . Number 00000004h white . . . . . . . . . . . . . Number 0000000Fh yellow . . . . . . . . . . . . . Number 0000000Eh

0 Warnings 0 Errors

Page 37: Assembly Language Assembly Language Fundamentals

Map File

AddSub

Timestamp is 4523cd3d (Wed Oct 04 23:03:25 2006)

Preferred load address is 00400000

Start Length Name Class 0001:00000000 00001c40H .text CODE 0002:00000000 00000121H .rdata DATA 0002:00000121 00000000H .edata DATA 0003:00000000 00000e03H .data DATA 0003:00000e04 00000224H .bss DATA 0004:00000000 00000014H .idata$2 DATA 0004:00000014 00000014H .idata$3 DATA 0004:00000028 0000006cH .idata$4 DATA 0004:00000094 0000006cH .idata$5 DATA 0004:00000100 0000022dH .idata$6 DATA

Page 38: Assembly Language Assembly Language Fundamentals

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:Irvin

e32.obj 0001:0000036b _GetDateTime@4 0040136b f irvine32:Irvine32.

obj 0001:0000039b _GetMseconds@0 0040139b f irvine32:Irvine3

2.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:Irvine

32.obj

Page 39: Assembly Language Assembly Language Fundamentals

0001:00000453 _Random32@0 00401453 f irvine32:Irvine32.obj

0001:0000046e _Randomize@0 0040146e f irvine32:Irvine32.obj

0001:00000487 _ReadChar@0 00401487 f irvine32:Irvine32.obj

0001:000004e4 _ReadHex@0 004014e4 f irvine32:Irvine32.obj

0001:00000544 _ReadInt@0 00401544 f irvine32:Irvine32.obj

0001:00000601 _ReadString@0 00401601 f irvine32:Irvine32.obj

0001:00000667 _SetTextColor@0 00401667 f irvine32:Irvine32.obj

0001:0000068c _Str_compare@8 0040168c f irvine32:Irvine32.obj

0001:000006b6 _Str_copy@8 004016b6 f irvine32:Irvine32.obj

0001:000006d9 _Str_length@4 004016d9 f irvine32:Irvine32.obj

0001:000006f3 _Str_trim@8 004016f3 f irvine32:Irvine32.obj 0001:00000720 _Str_ucase@4 00401720 f irvine32:Irvine32.o

bj

Page 40: Assembly Language Assembly Language Fundamentals

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.o

bj 0001:00000984 _ExitProcess@4 00401984 f kernel32:KERNEL3

2.dll 0001:0000098a _FlushConsoleInputBuffer@4 0040198a f kernel32:K

ERNEL32.dll 0001:00000990 _GetCommandLineA@0 00401990 f kernel32:KE

RNEL32.dll 0001:00000996 _GetConsoleMode@8 00401996 f kernel32:KER

NEL32.dll 0001:0000099c _GetLocalTime@4 0040199c f kernel32:KERNE

L32.dll 0001:000009a2 _GetStdHandle@4 004019a2 f kernel32:KERNE

L32.dll

Page 41: Assembly Language Assembly Language Fundamentals

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

Page 42: Assembly Language Assembly Language Fundamentals

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

Page 43: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Defining DataDefining 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

Page 44: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Intrinsic Data Types Intrinsic Data Types (1 of 2)(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

Page 45: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Intrinsic Data Types Intrinsic Data Types (2 of 2)(2 of 2)

• REAL4• 4-byte IEEE short real

• REAL8• 8-byte IEEE long real

• REAL10• 10-byte IEEE extended real

Page 46: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Data Definition StatementData 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

Page 47: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Defining BYTE and SBYTE DataDefining 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

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 48: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Defining Byte ArraysDefining 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:

Page 49: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Defining StringsDefining Strings (1 of 3) (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

Page 50: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Defining StringsDefining Strings (2 of 3) (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 51: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Defining StringsDefining Strings (3 of 3) (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.

Page 52: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Using the DUP OperatorUsing 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

Page 53: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Defining WORD and SWORD DataDefining WORD and SWORD Data

• Define storage for 16-bit integers• or double characters• single value or multiple values

word1 WORD 65535 ; 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

Page 54: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Defining DWORD and SDWORD DataDefining DWORD and SDWORD Data

val1 DWORD 12345678h ; unsigned

val2 SDWORD –2147483648 ; signed

val3 DWORD 20 DUP(?) ; unsigned array

val4 SDWORD –3,–2,–1,0,1 ; signed array

Storage definitions for signed and unsigned 32-bit integers:

Page 55: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Defining QWORD, TBYTE, Real DataDefining QWORD, TBYTE, Real Data

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)

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

Page 56: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Little Endian OrderLittle 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 57: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Adding Variables to AddSubAdding 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 registersexit

main ENDPEND main

Page 58: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Declaring Unitialized DataDeclaring 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.

Page 59: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

What's NextWhat'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

Page 60: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Symbolic ConstantsSymbolic Constants

• Equal-Sign Directive• Calculating the Sizes of Arrays and Strings• EQU Directive• TEXTEQU Directive

Page 61: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Equal-Sign DirectiveEqual-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

Page 62: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Calculating the Size of a Byte ArrayCalculating the Size of a Byte Array

• current location counter: $• subtract address of list

• difference is the number of bytes

list BYTE 10,20,30,40ListSize = ($ - list)

Page 63: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Calculating the Size of a Word ArrayCalculating the Size of a Word Array

Divide total number of bytes by 2 (the size of a word)

list WORD 1000h,2000h,3000h,4000hListSize = ($ - list) / 2

Page 64: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Calculating the Size of a Doubleword ArrayCalculating the Size of a Doubleword Array

Divide total number of bytes by 4 (the size of a doubleword)

list DWORD 1,2,3,4ListSize = ($ - list) / 4

Page 65: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

EQU DirectiveEQU 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

Page 66: Assembly Language Assembly Language Fundamentals

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

TEXTEQU DirectiveTEXTEQU 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"

Page 67: Assembly Language Assembly Language Fundamentals

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 10000h val2 dword 40000h val3 dword 20000h finalVal dword ?

.code main PROC mov ax,@data ; initialize DS mov ds,ax ; new

Page 68: Assembly Language Assembly Language Fundamentals

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

Page 69: Assembly Language Assembly Language Fundamentals

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


Recommended