Assembly Language Fundamentals Basic Elements Directives l l

Assembly Language Fundamentals

Basic Elements ¡ Directives l l 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 l l l 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: ¡ L 1: mov ax, bx ; copy data from bx to ax ¡ mov eax, 10000 h ¡ call Dump. Regs
![¡ Integer Constants l l Format: [{+|-}] digits [radix] ¡ Radix: h, Hexadecimal ¡ ¡ Integer Constants l l Format: [{+|-}] digits [radix] ¡ Radix: h, Hexadecimal ¡](http://slidetodoc.com/presentation_image_h/1bd95c5081004b1d86014910a36d0630/image-4.jpg)
¡ Integer Constants l l Format: [{+|-}] digits [radix] ¡ Radix: h, Hexadecimal ¡ q/o, Octal ¡ d/t, Decimal ¡ b/y, Binary ¡ r, Encoded real For example: ¡ 26 Decimal ¡ 26 d Decimal ¡ 11010011 b Binary ¡ 42 q Octal ¡ 42 o Octal ¡ 1 Ah Hexadecimal ¡ 0 A 3 h Hexadecimal, note the 0 before A

¡ Integer Expressions l l 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 l l l Format: [{+, -}]integer. [integer][exponent] exponent: E[{+, -}]integer ¡ Real Number Constants l l l Format: [{+, -}]integer. [integer][exponent] exponent: E[{+, -}]integer](http://slidetodoc.com/presentation_image_h/1bd95c5081004b1d86014910a36d0630/image-6.jpg)
¡ Real Number Constants l l l Format: [{+, -}]integer. [integer][exponent] exponent: E[{+, -}]integer For example: 2. ¡ +3. 0 ¡ -44. 2 E+05 ¡ 26. E 5 ¡

¡ Character Constants l l ¡ Enclosed in single or double quotes For example: ¡ ‘A’ ¡ “d” String Constants l l l 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 l l l 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 l l l 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: ¡ var 1 ¡ $first ¡ _main ¡ open_file ¡ @@myfile ¡ _12345

¡ Label l l 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 ¡ l Code label ¡ L 1: mov ax, bx ; copy data from bx to ax

¡ Comments l l 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 l l 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 ¡ ¡ ; This program adds and subtracts 32 -bit integers. ; Last update: 06/01/2006 ¡ INCLUDE Irvine 32. inc ¡ . code main PROC ¡ ¡ ¡ ¡ mov add sub call exit main ENDP END main eax, 10000 h eax, 40000 h eax, 20000 h Dump. Regs (Add. Sub. asm) ; EAX = 10000 h ; EAX = 50000 h ; EAX = 30000 h

¡ TITLE Add and Subtract (Add. Sub. 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 Irvine 32. inc The INCLUDE directive copies necessary definitions and setup information from a text file named Irvine 32. 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, 10000 h ; EAX = 10000 h The MOV instruction copies the integer 10000 h to the EAX register. ¡ add eax, 40000 h ; EAX = 50000 h The ADD instruction adds 40000 h to the EAX register. ¡ ¡ ¡ sub eax, 20000 h ; EAX = 30000 h The SUB instruction subtracts 20000 h from the EAX register. call Dump. Regs The CALL instruction calls a procedure Dump. Regs.

¡ 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 Add. Sub ¡ TITLE Add and Subtract ¡ ; This program adds and subtracts 32 -bit integers. ; 32 -bit Protected mode version ; Last update: 06/01/2006 ¡ ¡ ¡ ¡ (Add. Sub. Alt. asm) . 386. MODEL flat, stdcall. STACK 4096 Exit. Process PROTO, dw. Exit. Code: DWORD Dump. Regs PROTO. code main PROC mov add sub call eax, 10000 h eax, 40000 h eax, 20000 h Dump. Regs INVOKE Exit. Process, 0 main ENDP END main ; EAX = 10000 h ; EAX = 50000 h ; EAX = 30000 h

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

¡ ¡ ¡ ¡ Exit. Process PROTO, dw. Exit. Code: DWORD Dump. Regs PROTO Two PROTO directives declare prototypes for procedures used by this program. Exit. Process is an MS-Windows function. Dump. Regs is a procedure from the Irvine 32 link library INVOKE Exit. Process, 0 INVOKE is an assembler directive that calls a procedure or function

Progrm Template ¡ TITLE Program Template ¡ ¡ ; ; ¡ INCLUDE Irvine 32. inc ¡ ; (insert symbol definitions here) ¡ . data ; (insert variables here) ¡ ¡ ¡ (template. asm) Program Description: Author: Date Created: Last Modification Date: ¡ . 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. Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Listing File ¡ ¡ Microsoft (R) Macro Assembler Version 6. 15. 8803 09/27/06 22: 12: 24 Add and Subtract (Add. Sub. asm) Page 1 - 1 TITLE Add and Subtract (Add. Sub. asm) ; This program adds and subtracts 32 -bit integers. ; Last update: 06/01/2006 ¡ ¡ (Irvine 32. inc) INCLUDE Irvine 32. inc C ; Include file for Irvine 32. lib C

C INCLUDE Small. Win. inc ; MS-Windows prototypes, structures, and constants C. NOLIST C ¡ ¡ ¡ ¡ 00000000 B 8 00010000 eax, 10000 h 00000005 05 00040000 eax, 40000 h 0000000 A 2 D 00020000 eax, 20000 h 0000000 F E 8 0000 E ¡ ¡ 0000001 B ¡ ¡ � . code main PROC ; EAX = 10000 h ; EAX = 50000 h mov add sub ; EAX = 30000 h call Dump. Regs exit main ENDP END main

¡ ¡ ¡ ¡ ¡ Microsoft (R) Macro Assembler Version 6. 15. 8803 09/27/06 22: 12: 24 Add and Subtract (Add. Sub. asm) Symbols 2 - 1 Structures and Unions: Name Size Offset Type CONSOLE_CURSOR_INFO. . . 00000005 dw. Size. . . 0000 DWord b. Visible. . . 00000004 Byte CONSOLE_SCREEN_BUFFER_INFO. . . 00000016 dw. Size. . . 0000 DWord dw. Cursor. Pos. . 00000004 DWord w. Attributes. . 00000008 Word sr. Window. . . 0000000 A QWord max. Win. Size. . 00000012 DWord COORD. . . 00000004 X. . . 0000 Word Y. . . 00000002 Word

¡ ¡ ¡ ¡ ¡ FILETIME. . . lo. Date. Time. . hi. Date. Time. . SMALL_RECT. . . Left. . . Top. . . Right. . . Bottom. . . SYSTEMTIME. . . w. Year. . . w. Month. . . w. Day. Of. Week. . w. Day. . . w. Hour. . . w. Minute. . . w. Second. . . w. Milliseconds. . . . 00000008 0000 DWord 00000004 DWord 00000008 0000 Word 00000002 Word 00000004 Word 00000006 Word 00000010 0000 Word 00000002 Word 00000004 Word 00000006 Word 00000008 Word 0000000 A Word 0000000 C Word 0000000 E 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 0000001 B DWord Procedures, parameters and locals: Name Type Value Attr Close. Handle. . P Near 0000 FLAT Length= 0000 External STDCALL Clr. Scr. . . P Near 0000 FLAT Length= 0000 External STDCALL Create. File. A. . P Near 0000 FLAT Length= 0000 External STDCALL

¡ ¡ ¡ ¡ Crlf. . . P Near 0000 FLAT Length= 0000 External STDCALL Delay. . . P Near 0000 FLAT Length= 0000 External STDCALL Dump. Mem. . . P Near 0000 FLAT Length= 0000 External STDCALL Dump. Regs. . . P Near 0000 FLAT Length= 0000 External STDCALL Exit. Process. . P Near 0000 FLAT Length= 0000 External STDCALL Flush. Console. Input. Buffer. . P Near 0000 FLAT Length= 0000 External STDCALL Get. Command. Tail. . P Near 0000 FLAT Length= 0000 External STDCALL Get. Console. CP. . P Near 0000 FLAT Length= 0000 External STDCALL Get. Console. Cursor. Info. . . P Near 0000 FLAT Length= 0000 External STDCALL Get. Console. Mode. . P Near 0000 FLAT Length= 0000 External STDCALL Get. Console. Screen. Buffer. Info. . . P Near 0000 FLAT Length= 0000 External STDCALL Get. Date. Time. . P Near 0000 FLAT Length= 0000 External STDCALL Get. Local. Time. . P Near 0000 FLAT Length= 0000 External STDCALL

¡ ¡ ¡ ¡ Get. Mseconds. . P Near 0000 FLAT Length= 0000 External STDCALL Get. Number. Of. Console. Input. Events. P Near 0000 FLAT Length= 0000 External STDCALL Get. Std. Handle. . P Near 0000 FLAT Length= 0000 External STDCALL Get. System. Time. . P Near 0000 FLAT Length= 0000 External STDCALL Get. Tick. Count. . P Near 0000 FLAT Length= 0000 External STDCALL Gotoxy. . . P Near 0000 FLAT Length= 0000 External STDCALL Is. Digit. . . P Near 0000 FLAT Length= 0000 External STDCALL Peek. Console. Input. A. . . . P Near 0000 FLAT Length= 0000 External STDCALL Random 32. . . P Near 0000 FLAT Length= 0000 External STDCALL Random. Range. . P Near 0000 FLAT Length= 0000 External STDCALL Randomize. . . P Near 0000 FLAT Length= 0000 External STDCALL Read. Char. . . P Near 0000 FLAT Length= 0000 External STDCALL Read. Console. A. . P Near 0000 FLAT Length= 0000 External STDCALL

¡ ¡ ¡ ¡ Read. Console. Input. A. . . . P Near 0000 FLAT Length= 0000 External STDCALL Read. File. . . P Near 0000 FLAT Length= 0000 External STDCALL Read. Hex. . . P Near 0000 FLAT Length= 0000 External STDCALL Read. Int. . . P Near 0000 FLAT Length= 0000 External STDCALL Read. String. . . P Near 0000 FLAT Length= 0000 External STDCALL Set. Console. Cursor. Info. . . P Near 0000 FLAT Length= 0000 External STDCALL Set. Console. Cursor. Position. . P Near 0000 FLAT Length= 0000 External STDCALL Set. Console. Mode. . P Near 0000 FLAT Length= 0000 External STDCALL Set. Console. Screen. Buffer. Size. . . P Near 0000 FLAT Length= 0000 External STDCALL Set. Console. Text. Attribute. . P Near 0000 FLAT Length= 0000 External STDCALL Set. Console. Title. A. . . . P Near 0000 FLAT Length= 0000 External STDCALL Set. Console. Window. Info. . . P Near 0000 FLAT Length= 0000 External STDCALL Set. File. Pointer. . P Near 0000 FLAT Length= 0000 External STDCALL

¡ ¡ ¡ ¡ Set. Text. Color. . P Near 0000 Length= 0000 External STDCALL Sleep. . . P Near 0000 Length= 0000 External STDCALL Str_compare. . P Near 0000 Length= 0000 External STDCALL Str_copy. . . P Near 0000 Length= 0000 External STDCALL Str_length. . . P Near 0000 Length= 0000 External STDCALL Str_trim. . . P Near 0000 Length= 0000 External STDCALL Str_ucase. . . P Near 0000 Length= 0000 External STDCALL System. Time. To. File. Time. . . P Near 0000 Length= 0000 External STDCALL Wait. Msg. . . P Near 0000 Length= 0000 External STDCALL Write. Bin. . . P Near 0000 Length= 0000 External STDCALL Write. Char. . . P Near 0000 Length= 0000 External STDCALL Write. Console. A. . P Near 0000 Length= 0000 External STDCALL Write. Console. Output. Attribute. . P Near 0000 Length= 0000 External STDCALL FLAT FLAT FLAT FLAT

¡ ¡ ¡ ¡ Write. Console. Output. Character. A. . P Near 0000 FLATLength= 0000 External STDCALL Write. Dec. . . P Near 0000 FLAT Length= 0000 External STDCALL Write. File. . . P Near 0000 FLAT Length= 0000 External STDCALL Write. Hex. . . P Near 0000 FLAT Length= 0000 External STDCALL Write. Int. . . P Near 0000 FLAT Length= 0000 External STDCALL Write. String. . P Near 0000 FLAT Length= 0000 External STDCALL main. . . P Near 0000 _TEXT Length= 0000001 B Public STDCALL

¡ ¡ ¡ ¡ ¡ Symbols: Name Type Value @Code. Size. . . Number @Data. Size. . . Number @Interface. . . Number @Model. . . Number @code. . . Text @data. . . Text @fardata? . . . Text @fardata. . . Text @stack. . . Text CREATE_ALWAYS. . Number CREATE_NEW. . . Number Create. File. . . Text DO_NOT_SHARE. . Number ENABLE_ECHO_INPUT. . . . ENABLE_LINE_INPUT. . . . Number ENABLE_MOUSE_INPUT. . . . ENABLE_PROCESSED_INPUT. . . ENABLE_PROCESSED_OUTPUT. . Attr 00000000 h 00000003 h 00000007 h _TEXT FLAT 00000002 h 00000001 h Create. File. A 0000 h Number 00000004 h 00000002 h Number 00000010 h Number 00000001 h

¡ ¡ ¡ ¡ ¡ ¡ ENABLE_WINDOW_INPUT. . . Number 00000008 h ENABLE_WRAP_AT_EOL_OUTPUT. . . Number 00000002 h FALSE. . . Number 0000 h FILE_APPEND_DATA. . . . Number 00000004 h FILE_ATTRIBUTE_ARCHIVE. . . Number 00000020 h FILE_ATTRIBUTE_COMPRESSED. . . Number 00000800 h FILE_ATTRIBUTE_DEVICE. . . Number 00000040 h FILE_ATTRIBUTE_DIRECTORY. . Number 00000010 h FILE_ATTRIBUTE_ENCRYPTED. . Number 00004000 h FILE_ATTRIBUTE_HIDDEN. . . Number 00000002 h FILE_ATTRIBUTE_NORMAL. . . Number 00000080 h FILE_ATTRIBUTE_NOT_CONTENT_INDEXED. Number 00002000 h FILE_ATTRIBUTE_OFFLINE. . . Number 00001000 h FILE_ATTRIBUTE_READONLY. . Number 00000001 h FILE_ATTRIBUTE_REPARSE_POINT. . Number 00000400 h FILE_ATTRIBUTE_SPARSE_FILE. . . Number 00000200 h FILE_ATTRIBUTE_SYSTEM. . . Number 00000004 h FILE_ATTRIBUTE_TEMPORARY. . Number 00000100 h FILE_BEGIN. . . Number 0000 h FILE_CURRENT. . Number 00000001 h FILE_DELETE_CHILD. . . . Number 00000040 h FILE_END. . . Number 00000002 h FILE_READ_DATA. . Number 00000001 h

¡ ¡ ¡ ¡ ¡ ¡ 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 Peek. Console. Input. . . . Text Read. Console. . Text STD_INPUT_HANDLE. . . . STD_OUTPUT_HANDLE. . . . Set. Console. Title. . . . Text 00000004 h 00000001 h 00000002 h 00000010 h 10000000 h 20000000 h -80000000 h 40000000 h Number -00000001 h 00000008 h 00000002 h 0000 h 00000004 h 00000003 h Peek. Console. Input. A Read. Console. A Number -0000000 Ah Number -0000000 Bh Set. Console. Title. A

¡ ¡ ¡ ¡ ¡ ¡ TRUE. . . Number TRUNCATE_EXISTING. . . . WINDOW_BUFFER_SIZE_EVENT. . Write. Console. Output. Character. A Write. Console. . Text black. . . Number blue. . . Number brown. . . Number cyan. . . Number exit. . . Text gray. . . Number green. . . Number light. Blue. . . Number light. Cyan. . . Number light. Gray. . . Number light. Green. . . Number light. Magenta. . Number light. Red. . . Number magenta. . . Number red. . . Number white. . . Number yellow. . . Number 0 Warnings 0 Errors 00000001 h Number 00000005 h Number 00000004 h Text Write. Console. A 0000 h 00000001 h 00000006 h 00000003 h INVOKE Exit. Process, 0 00000008 h 00000002 h 00000009 h 0000000 Bh 00000007 h 0000000 Ah 0000000 Dh 0000000 Ch 00000005 h 00000004 h 0000000 Fh 0000000 Eh

Map File ¡ Add. Sub ¡ Timestamp is 4523 cd 3 d (Wed Oct 04 23: 03: 25 2006) ¡ Preferred load address is 00400000 ¡ Start Length Name 0001: 00001 c 40 H. text 0002: 00000121 H. rdata 0002: 00000121 0000 H. edata 0003: 00000 e 03 H. data 0003: 00000 e 04 00000224 H. bss 0004: 00000014 H. idata$2 0004: 00000014 H. idata$3 0004: 00000028 0000006 c. H. idata$4 0004: 00000094 0000006 c. H. idata$5 0004: 00000100 0000022 d. H. idata$6 ¡ ¡ ¡ Class CODE DATA DATA DATA

¡ Address ¡ 0001: 00000010 _main@0 00401010 f Add. Sub. obj 0001: 00000034 _Clr. Scr@0 00401034 f irvine 32: Irvine 32. obj 0001: 00000083 _Crlf@0 00401083 f irvine 32: Irvine 32. obj 0001: 000000 a 0 _Delay@0 004010 a 0 f irvine 32: Irvine 32. obj 0001: 000000 a 9 _Dump. Mem@0 004010 a 9 f irvine 32: Irvine 32. obj 0001: 00000179 _Dump. Regs@0 00401179 f irvine 32: Irvine 32. obj 0001: 00000355 _Get. Command. Tail@0 00401355 f irvine 32: Irvine 32. obj 0001: 0000036 b _Get. Date. Time@4 0040136 b f irvine 32: Irvine 32. obj 0001: 0000039 b _Get. Mseconds@0 0040139 b f irvine 32: Irvine 32. obj 0001: 000003 f 4 _Gotoxy@0 004013 f 4 f irvine 32: Irvine 32. obj 0001: 0000042 a _Is. Digit@0 0040142 a f irvine 32: Irvine 32. obj 0001: 00000437 _Random. Range@0 00401437 f irvine 32: Irvine 32. obj ¡ ¡ ¡ Publics by Value Rva+Base Lib: Object

¡ ¡ ¡ ¡ 0001: 00000453 _Random 32@0 irvine 32: Irvine 32. obj 0001: 0000046 e _Randomize@0 irvine 32: Irvine 32. obj 0001: 00000487 _Read. Char@0 irvine 32: Irvine 32. obj 0001: 000004 e 4 _Read. Hex@0 irvine 32: Irvine 32. obj 0001: 00000544 _Read. Int@0 irvine 32: Irvine 32. obj 0001: 00000601 _Read. String@0 irvine 32: Irvine 32. obj 0001: 00000667 _Set. Text. Color@0 irvine 32: Irvine 32. obj 0001: 0000068 c _Str_compare@8 irvine 32: Irvine 32. obj 0001: 000006 b 6 _Str_copy@8 irvine 32: Irvine 32. obj 0001: 000006 d 9 _Str_length@4 irvine 32: Irvine 32. obj 0001: 000006 f 3 _Str_trim@8 irvine 32: Irvine 32. obj 0001: 00000720 _Str_ucase@4 irvine 32: Irvine 32. obj 00401453 f 0040146 e f 00401487 f 004014 e 4 f 00401544 f 00401601 f 00401667 f 0040168 c f 004016 b 6 f 004016 d 9 f 004016 f 3 f 00401720 f

¡ ¡ ¡ ¡ 0001: 00000742 _Wait. Msg@0 00401742 f irvine 32: Irvine 32. obj 0001: 0000079 b _Write. Bin@0 0040179 b f irvine 32: Irvine 32. obj 0001: 000007 cf _Write. Char@0 004017 cf f irvine 32: Irvine 32. obj 0001: 000007 fe _Write. Dec@0 004017 fe f irvine 32: Irvine 32. obj 0001: 0000083 f _Write. Hex@0 0040183 f f irvine 32: Irvine 32. obj 0001: 00000896 _Write. Int@0 00401896 f irvine 32: Irvine 32. obj 0001: 000008 f 7 _Write. String@0 004018 f 7 f irvine 32: Irvine 32. obj 0001: 00000984 _Exit. Process@4 00401984 f kernel 32: KERNEL 32. dll 0001: 0000098 a _Flush. Console. Input. Buffer@4 0040198 a f kernel 32: KERNEL 32. dll 0001: 00000990 _Get. Command. Line. A@0 00401990 f kernel 32: KERNEL 32. dll 0001: 00000996 _Get. Console. Mode@8 00401996 f kernel 32: KERNEL 32. dll 0001: 0000099 c _Get. Local. Time@4 0040199 c f kernel 32: KERNEL 32. dll 0001: 000009 a 2 _Get. Std. Handle@4 004019 a 2 f kernel 32: KERNEL 32. dll

¡ ¡ ¡ ¡ 0001: 000009 a 8 _Get. System. Time@4 004019 a 8 f kernel 32: KERNEL 32. dll 0001: 000009 ae _Read. Console. A@20 004019 ae f kernel 32: KERNEL 32. dll 0001: 000009 b 4 _Set. Console. Cursor. Position@8 004019 b 4 f kernel 32: KERNEL 32. dll 0001: 000009 ba _Set. Console. Mode@8 004019 ba f kernel 32: KERNEL 32. dll 0001: 000009 c 0 _Set. Console. Text. Attribute@8 004019 c 0 f kernel 32: KERNEL 32. dll 0001: 000009 c 6 _Sleep@4 004019 c 6 f kernel 32: KERNEL 32. dll 0001: 000009 cc _System. Time. To. File. Time@8 004019 cc f kernel 32: KERNEL 32. dll 0001: 000009 d 2 _Write. Console. A@20 004019 d 2 f kernel 32: KERNEL 32. dll 0004: 0000 __IMPORT_DESCRIPTOR_KERNEL 32 00406000 kernel 32: KERNEL 32. dll 0004: 00000014 __NULL_IMPORT_DESCRIPTOR 00406014 kernel 32: KERNEL 32. dll 0004: 00000094 __imp__Exit. Process@4 00406094 kernel 32: KERNEL 32. dll 0004: 00000098 __imp__Flush. Console. Input. Buffer@4 00406098 kernel 32: KERNEL 32. dll 0004: 0000009 c __imp__Get. Command. Line. A@0 0040609 c kernel 32: KERNEL 32. dll

¡ ¡ ¡ ¡ 0004: 000000 a 0 __imp__Get. Console. Mode@8 004060 a 0 kernel 32: KERNEL 32. dll 0004: 000000 a 4 __imp__Get. Local. Time@4 004060 a 4 kernel 32: KERNEL 32. dll 0004: 000000 a 8 __imp__Get. Std. Handle@4 004060 a 8 kernel 32: KERNEL 32. dll 0004: 000000 ac __imp__Get. System. Time@4 004060 ac kernel 32: KERNEL 32. dll 0004: 000000 b 0 __imp__Read. Console. A@20 004060 b 0 kernel 32: KERNEL 32. dll 0004: 000000 b 4 __imp__Set. Console. Cursor. Position@8 004060 b 4 kernel 32: KERNEL 32. dll 0004: 000000 b 8 __imp__Set. Console. Mode@8 004060 b 8 kernel 32: KERNEL 32. dll 0004: 000000 bc __imp__Set. Console. Text. Attribute@8 004060 bc kernel 32: KERNEL 32. dll 0004: 000000 c 0 __imp__Sleep@4 004060 c 0 kernel 32: KERNEL 32. dll 0004: 000000 c 4 __imp__System. Time. To. File. Time@8 004060 c 4 kernel 32: KERNEL 32. dll 0004: 000000 c 8 __imp__Write. Console. A@20 004060 c 8 kernel 32: KERNEL 32. dll 0004: 000000 cc 177 KERNEL 32_NULL_THUNK_DATA 004060 cc kernel 32: KERNEL 32. 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 Add. Sub 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) • REAL 4 • 4 -byte IEEE short real • REAL 8 • 8 -byte IEEE long real • REAL 10 • 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]. . . value 1 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: value 1 BYTE 'A' ; character constant value 2 BYTE 0 ; smallest unsigned byte value 3 BYTE 255 ; largest unsigned byte value 4 SBYTE -128 ; smallest signed byte value 5 SBYTE +127 ; largest signed byte value 6 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: list 1 BYTE 10, 20, 30, 40 list 2 BYTE 10, 20, 30, 40 BYTE 50, 60, 70, 80 BYTE 81, 82, 83, 84 list 3 BYTE ? , 32, 41 h, 0010 b list 4 BYTE 0 Ah, 20 h, ‘A’, 22 h 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: str 1 BYTE str 2 BYTE str 3 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", 0 dh, 0 ah, "1. Create a new account", 0 dh, 0 ah, "2. Open an existing account", 0 dh, 0 ah, "3. Credit the account", 0 dh, 0 ah, "4. Debit the account", 0 dh, 0 ah, "5. Exit", 0 ah, "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: • 0 Dh = carriage return • 0 Ah = line feed str 1 BYTE "Enter your name: ", 0 Dh, 0 Ah BYTE "Enter your address: ", 0 new. Line BYTE 0 Dh, 0 Ah, 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 var 1 BYTE 20 DUP(0) ; 20 bytes, all equal to zero var 2 BYTE 20 DUP(? ) ; 20 bytes, uninitialized var 3 BYTE 4 DUP("STACK") ; 20 bytes: "STACKSTACK" var 4 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 word 1 word 2 word 3 word 4 my. List array WORD SWORD 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: val 1 val 2 val 3 val 4 DWORD SDWORD 12345678 h – 2147483648 20 DUP(? ) – 3, – 2, – 1, 0, 1 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. ; ; unsigned array Web site Examples

Defining QWORD, TBYTE, Real Data Storage definitions for quadwords, tenbyte values, and real numbers: quad 1 QWORD 12345678 h val 1 TBYTE 100000123456789 Ah r. Val 1 REAL 4 -2. 1 r. Val 2 REAL 8 3. 2 E-260 r. Val 3 REAL 10 4. 6 E+4096 Short. Array REAL 4 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: val 1 DWORD 12345678 h Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. Web site Examples

Adding Variables to Add. Sub TITLE Add and Subtract, Version 2 (Add. Sub 2. asm) ; This program adds and subtracts 32 -bit unsigned ; integers and stores the sum in a variable. INCLUDE Irvine 32. inc. data val 1 DWORD 10000 h val 2 DWORD 40000 h val 3 DWORD 20000 h final. Val DWORD ? . code main PROC mov eax, val 1 ; start with 10000 h add eax, val 2 ; add 40000 h sub eax, val 3 ; subtract 20000 h mov final. Val, eax ; store the result (30000 h) call Dump. Regs ; 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: small. Array 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 List. Size = ($ - 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 1000 h, 2000 h, 3000 h, 4000 h List. Size = ($ - 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 List. Size = ($ - 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> press. Key EQU <"Press any key to continue. . . ", 0>. data prompt BYTE press. Key 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 continue. Msg TEXTEQU <"Do you wish to continue (Y/N)? "> row. Size = 5. data prompt 1 BYTE continue. Msg count TEXTEQU %(row. Size * 2) ; evaluates the expression setup. AL TEXTEQU <mov al, count>. code setup. AL 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 ¡ ¡ ; This program adds and subtracts 32 -bit integers ; and stores the sum in a variable. (From page 94. ) ; Last update: 06/01/2006 ¡ INCLUDE Irvine 16. inc ; new ¡ . data val 1 dword val 2 dword val 3 dword final. Val dword ¡ ¡ ¡ ¡ ¡ . code main PROC mov (Add. Sub 2 r. asm) 10000 h 40000 h 20000 h ? ax, @data ds, ax ; new ; initialize DS

¡ ¡ ¡ ¡ mov eax, val 1 add eax, val 2 sub eax, val 3 mov final. Val, eax call Dump. Regs exit main ENDP END main ; ; ; start with 10000 h add 40000 h subtract 20000 h store the result (30000 h) display the registers

Exercise ¡ TITLE Data Definitions ¡ ¡ ; Examples showing how to define data. ; Last update: 06/01/2006 ¡ INCLUDE Irvine 32. inc ¡ ; --------- Byte Values --------. data value 1 BYTE 'A' value 2 BYTE 0 ¡ ¡ . code main PROC ¡ ; (insert instructions here) ¡ exit main ENDP END main ¡ ¡ ¡ (Data. Def. asm)
- Slides: 69