90edea92771dabce78ad81eb09f9e565.ppt
- Количество слайдов: 52
CS 2422 Assembly Language and System Programming Assembly Language Fundamentals Department of Computer Science National Tsing Hua University
Assembly Language for Intel. Based Computers, 5 th Edition CS 2422 Assembly Language and System Programming Kip Irvine Chapter 3: Assembly Language Fundamentals Slides prepared by the author Revision date: June 4, 2006 (c) Pearson Education, 2006 -2007. All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.
Chapter Overview u u u Basic Elements of Assembly Language Example: Adding and Subtracting Integers Assembling, Linking, and Running Programs Defining Data Symbolic Constants Real-Address Mode Programming 2
Starting with an Example TITLE Add and Subtract (Add. Sub. asm) ; Adds and subtracts three 32 -bit integers ; (10000 h + 40000 h + 20000 h) INCLUDE Irvine 32. inc. code Title/header main PROC Include file mov eax, 10000 h ; EAX = 10000 h add eax, 40000 h ; EAX = 50000 h sub eax, 20000 h ; EAX = 30000 h call Dump. Regs ; display registers exit main ENDP Code section END main 3
Meanings of the Code Assembly code MOV EAX, 10000 h Machine code B 8 00010000 (Move 10000 h into EAX) Operand in instruction ADD EAX, 40000 h 05 00040000 (Add 40000 h to EAX) SUB EAX, 20000 h 2 D 00020000 (SUB 20000 h from EAX) 4
Fetched MOV EAX, 10000 h Register Memory EAX EBX data … ALU IR B 8 00010000 B 8 00 01 00 00 05 00 04 00 00 MOV EAX, 10000 h ADD EAX, 40000 h SUB EAX, 20000 h PC 0000011 address … 5
Execute MOV EAX, 10000 h Register EAX EBX Memory 00010000 data … ALU IR B 8 00010000 B 8 00 01 00 00 05 00 04 00 00 MOV EAX, 10000 h ADD EAX, 40000 h SUB EAX, 20000 h PC 0000011 address … 6
Fetched ADD EAX, 40000 h Register EAX EBX Memory 00010000 data … ALU IR 05 00040000 B 8 00 01 00 00 05 00 04 00 00 MOV EAX, 10000 h ADD EAX, 40000 h SUB EAX, 20000 h PC 0001000 address … 7
Execute ADD EAX, 40000 h Register EAX EBX Memory 00010000 00050000 data … ALU IR 05 00040000 B 8 00 01 00 00 05 00 04 00 00 MOV EAX, 10000 h ADD EAX, 40000 h SUB EAX, 20000 h PC 0001000 address … 8
Chapter Overview u Basic Elements of Assembly Language l l l l u u u Integer constants and expressions Character and string constants Reserved words and identifiers Directives and instructions Labels Mnemonics and Operands Comments Example: Adding and Subtracting Integers Assembling, Linking, and Running Programs Defining Data Symbolic Constants Real-Address Mode Programming 9
Reserved Words, Directives TITLE Add and … ; Adds and subtracts ; (10000 h + … INCLUDE Irvine 32. inc. code main PROC mov eax, 10000 h add eax, 40000 h sub eax, 20000 h call Dump. Regs exit main ENDP END main u u u TITLE: l Define program listing title l Reserved word of directive Reserved words l Instruction mnemonics, directives, type attributes, operators, predefined symbols l See MASM reference in Appendix A Directives: l Commands for assembler 10
Directive vs Instruction u Directives: tell assembler what to do l l l u Commands that are recognized and acted upon by the assembler, e. g. declare code, data areas, select memory model, declare procedures, etc. Not part of the Intel instruction set Different assemblers have different directives Instructions: tell CPU what to do l l Assembled into machine code by assembler Executed at runtime by the CPU Member of the Intel IA-32 instruction set Format: LABEL (option), Mnemonic, Operands, Comment 11
Comments TITLE Add and … ; Adds and subtracts ; (10000 h + … INCLUDE Irvine 32. inc. code main PROC mov eax, 10000 h add eax, 40000 h sub eax, 20000 h call Dump. Regs exit main ENDP END main u Single-line comments l u begin with semicolon (; ) Multi-line comments l begin with COMMENT directive and a programmer-chosen character, end with the same character, e. g. COMMENT ! Comment line 1 Comment line 2 ! 12
Include Files TITLE Add and … ; Adds and subtracts ; (10000 h + … INCLUDE Irvine 32. inc. code main PROC mov eax, 10000 h add eax, 40000 h sub eax, 20000 h call Dump. Regs exit main ENDP END main u INCLUDE directive: l Copies necessary definitions and setup information from a text file named Irvine 32. inc, located in the assembler’s INCLUDE directory (see Chapt 5) 13
Code Segment TITLE Add and … ; Adds and subtracts ; (10000 h + … INCLUDE Irvine 32. inc. code main PROC mov eax, 10000 h add eax, 40000 h sub eax, 20000 h call Dump. Regs exit main ENDP END main u . code directive: l Marks the beginning of the code segment, where all executable statements in a program are located 14
Procedure Definition TITLE Add and … ; Adds and subtracts ; (10000 h + … INCLUDE Irvine 32. inc. code main PROC mov eax, 10000 h add eax, 40000 h sub eax, 20000 h call Dump. Regs exit main ENDP END main u u Procedure defined by: l [label] PROC l [label] ENDP Label: l Place markers: marks the address (offset) of code and data l Assigned a numeric address by assembler l Follow identifier rules l Data label: must be unique, e. g. my. Array l Code label: target of jump and loop instructions, e. g. L 1: 15
Identifiers TITLE Add and … ; Adds and subtracts ; (10000 h + … INCLUDE Irvine 32. inc. code main PROC mov eax, 10000 h add eax, 40000 h sub eax, 20000 h call Dump. Regs exit main ENDP END main u Identifiers: l l A programmer-chosen name to identify a variable, a constant, a procedure, or a code label 1 -247 characters, including digits not case sensitive first character must be a letter, _, @, ? , or $ 16
Integer Constants u u u Optional leading + or – sign Binary, decimal, hexadecimal, or octal digits Common radix characters: l l u u h – hexadecimal d – decimal b – binary r – encoded real Examples: 30 d, 6 Ah, 42, 1101 b Hexadecimal beginning with letter: 0 A 5 h 17
Instructions [label: ] mnemonic operand(s) TITLE Add and … [; comment] ; Adds and subtracts u Instruction mnemonics: ; (10000 h + … l help to memorize INCLUDE Irvine 32. inc l examples: MOV, ADD, . code SUB, MUL, INC, DEC main PROC u Operands: l constant mov eax, 10000 h l constant expression add eax, 40000 h l register sub eax, 20000 h l memory (data label, call Dump. Regs register) exit main ENDP Destination Source immediate values END main operand 18
Instruction Format Examples u No operands l l u ; set Carry flag ; no operation One operand l l u stc nop inc eax inc my. Byte ; register ; memory Two operands l l l add ebx, ecx sub my. Byte, 25 add eax, 36 * 25 ; register, register ; memory, constant ; register, constant-expr. 19
I/O TITLE Add and … ; Adds and subtracts ; (10000 h + … INCLUDE Irvine 32. inc. code main PROC mov eax, 10000 h add eax, 40000 h sub eax, 20000 h call Dump. Regs exit main ENDP END main u Not easy, if program by ourselves l u Two steps: l l u Will use the library provided by the author Include the library (Irvine 32. inc) in your code Call the subroutines call Dump. Regs: l Calls the procedure to displays current values of processor registers 20
Remaining TITLE Add and … ; Adds and subtracts ; (10000 h + … INCLUDE Irvine 32. inc. code main PROC mov eax, 10000 h add eax, 40000 h sub eax, 20000 h call Dump. Regs exit main ENDP END main u exit: l l u Halts the program Not a MSAM keyword, but a command defined in Irvine 32. inc END main: l l Marks the last line of the program to be assembled Identifies the name of the program’s startup procedure 21
Example Program Output u Program output, showing registers and flags EAX=00030000 EBX=7 FFDF 000 ECX=00000101 EDX=FFFF ESI=0000 EDI=0000 EBP=0012 FFF 0 ESP=0012 FFC 4 EIP=00401024 EFL=00000206 CF=0 SF=0 ZF=0 OF=0 22
Alternative Version of Add. Sub TITLE Add and Subtract (Add. Sub. Alt. asm) ; adds and subtracts 32 -bit integers. 386. MODEL flat, stdcall. STACK 4096 Exit. Process PROTO, dw. Exit. Code: DWORD Dump. Regs PROTO. code main PROC mov eax, 10000 h ; EAX = 10000 h add eax, 40000 h ; EAX = 50000 h sub eax, 20000 h ; EAX = 30000 h call Dump. Regs INVOKE Exit. Process, 0 main ENDP END main 23
Explanations u . 386 directive: l u . MODEL directive: l l u Generate code for protected mode program Stdcall: enable calling of Windows functions PROTO directives: l l u Minimum processor required for this code Prototypes for procedures Exit. Process: Windows function to halt process INVOKE directive: l l Calls a procedure or function Calls Exit. Process and passes it with a return code of zero 24
Suggested Program Template TITLE Program Template (Template. asm) ; Program Description: ; Author: ; Creation Date: ; Revisions: ; Date: Modified by: INCLUDE Irvine 32. inc. data ; (insert variables here). code main PROC ; (insert executable instructions here) exit main ENDP ; (insert additional procedures here) END main 25
What's Next u u u Basic Elements of Assembly Language Example: Adding and Subtracting Integers Assembling, Linking, and Running Programs Defining Data Symbolic Constants Real-Address Mode Programming 26
Assemble-Link-Execute Cycle u Steps from creating a source program through executing the compiled program http: //kipirvine. com/asm/getting. Started/index. htm 27
MASM History u v 6. 11 l u v 6. 15 l u Visual C++. NET 2002 v 7. 1 l u Visual C++ 6. 0 Processor Pack v 7. 0 l u Independent product Visual C++. NET 2003 v 8. 0 l Visual C++. NET 2005 28
Download, Install, and Run u MASM 6. 15 (with all examples of textbook) l u u Unzip the archive and run setup. exe Choose the installation directory l l u Masm 615. zip: download from the course web site Suggest using the default directory See index. htm in the archive for details Go to C: Masm 615 (if installed default) l Write assembly source code ‒ Text. Pad, Note. Pad++, Ultra. Edit or … l make 32 xxx (where xxx is your file name) 29
Suggestion u Study make 32. bat and make 16. bat l u u Think about linking with other language (ex: C or C++ or …) Understand that MASM is only one of the assemblers, and there are still many other assemblers to use l u To know where assembling stage and linking stage are Try to use NASM or TASM Try to use high‐level compiler to generate assembly codes l gcc or visual c++ or turbo c or … 30
Listing File u u Use it to see how your program is compiled Contains l l l u source code addresses object code (machine language) segment names symbols (variables, procedures, and constants) Example: add. Sub. lst 31
Listing File 00000000 00000005 0000000 A 0000000 F . code main PROC B 8 00010000 05 00040000 2 D 00020000 E 8 0000 E mov eax, 10000 h add eax, 40000 h sub eax, 20000 h call Dump. Regs exit 00000014 6 A 00 * push +00000 h 00000016 E 8 0000 E * call Exit. Process 0000001 B main ENDP END main memory address content 32
What's Next u u Basic Elements of Assembly Language Example: Adding and Subtracting Integers Assembling, Linking, and Running Programs Defining Data l l l u u Intrinsic Data Types Data Definition Statement Defining BYTE, SBYTE, WORD, SWORD, DWORD, SDWORD, QWORD, TBYTE Defining Real Number Data Little Endian Order Symbolic Constants Real-Address Mode Programming 33
Intrinsic Data Types BYTE SBYTE WORD SWORD DWORD SDWORD FWORD QWORD TBYTE REAL 4 REAL 8 REAL 10 8 -bit unsigned integer 8 -bit signed integer 16 -bit unsigned integer 16 -bit signed integer 32 -bit unsigned integer 32 -bit signed integer 48 -bit integer (Far pointer in protected mode) 64 -bit integer 80 -bit (10 -byte) integer 32 -bit (4 -byte) IEEE short real 64 -bit (8 -byte) IEEE long real 80 -bit (10 -byte) IEEE extended real 34
Data Definition Statement u u u 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 u u All initializers become binary data in memory Use ? if no initialization necessary l Example: Var 1 BYTE ? 35
Defining BYTE and SBYTE Data u Each of 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 is considered poor style • If you declare a SBYTE variable, the Microsoft debugger will automatically display its value in decimal with a leading sign 36
Defining Byte Arrays u 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 37
Defining Strings u An array of characters l l l Usually enclosed in quotation marks Will often be null-terminated To continue a single string across multiple lines, end each line with a comma 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 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, "Choice> ", 0 End-of-line sequence: Is str 1 an array? • 0 Dh = carriage return 38 • 0 Ah = line feed
Using the DUP Operator u u u 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 39
Defining WORD and SWORD u Define storage for 16 -bit integers l l word 1 word 2 word 3 word 4 my. List array or double characters single value or multiple values WORD SWORD WORD 65535 – 32768 ? "AB" 1, 2, 3, 4, 5 5 DUP(? ) ; ; ; largest unsigned value smallest signed value uninitialized, unsigned double characters array of words uninitialized array 40
Defining Other Types of Data u Storage definitions for 32 -bit integers, quadwords, tenbyte values, and real numbers: val 1 DWORD 12345678 h ; unsigned val 2 SDWORD – 2147483648 ; signed val 3 DWORD 20 DUP(? ) ; unsigned array val 4 SDWORD – 3, – 2, – 1, 0, 1 ; signed array 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) 41
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 42
Listing File 00000000 00010000000400000008 00020000000 C 0000 . data val 1 DWORD 10000 h val 2 DWORD 40000 h val 3 DWORD 20000 h final. Val DWORD ? 00000000 00000005 0000000 B 00000011 00000016 . code main PROC A 1 0000 R mov eax, val 1 ; start with 10000 h 03 05 00000004 R add eax, val 2 ; add 40000 h 2 B 05 00000008 R sub eax, val 3 ; subtract 20000 h A 3 0000000 C R mov final. Val, eax; store result E 8 0000 E call Dump. Regs ; display registers exit 00000022 main ENDP 43
C vs Assembly main() { int int val 1=10000 h; val 2=40000 h; val 3=20000 h; final. Val; final. Val = val 1 + val 2 - val 3; } . 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 add eax, val 2 sub eax, val 3 mov final. Val, eax call Dump. Regs exit main ENDP 44
What's Next u u u Basic Elements of Assembly Language Example: Adding and Subtracting Integers Assembling, Linking, and Running Programs Defining Data Symbolic Constants l l u Equal-Sign Directive Calculating the Sizes of Arrays and Strings EQU Directive TEXTEQU Directive Real-Address Mode Programming 45
Equal-Sign Directive u name = expression l l u expression is a 32 -bit integer (expression or constant) may be redefined name is called a symbolic constant Also OK to use EQU good programming style to use symbols COUNT = 500 … mov al, COUNT 46
Calculating the Size of Arrays u u Current location counter: $ Size of a byte array l Subtract address of list and difference is the number of bytes list BYTE 10, 20, 30, 40 List. Size = ($ - list) u Size of a word array l Divide total number of bytes by 2 (size of a word) list WORD 1000 h, 2000 h, 3000 h, 4000 h List. Size = ($ - list) / 2 47
EQU Directive u u u Define a symbol as either an integer or text expression Cannot be redefined OK to use expressions in EQU: l l u u Matrix 1 EQU 10 * 10 Matrix 1 EQU <10 * 10> No expression evaluation if within < > EQU accepts texts too PI EQU <3. 1416> press. Key EQU <"Press any key to continue", 0>. data prompt BYTE press. Key 48
TEXTEQU Directive u u u 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 expression setup. AL TEXTEQU <mov al, count>. code setup. AL ; generates: "mov al, 10" 49
What's Next u u u Basic Elements of Assembly Language Example: Adding and Subtracting Integers Assembling, Linking, and Running Programs Defining Data Symbolic Constants Real-Address Mode Programming (skipped) 50
Summary u u u Integer expression, character constant Directive – interpreted by the assembler Instruction – executes at runtime Code, data, and stack segments Source, listing, object, map, executable files Data definition directives: l l u BYTE, SBYTE, WORD, SWORD, DWORD, SDWORD, QWORD, TBYTE, REAL 4, REAL 8, and REAL 10 DUP operator, location counter ($) Symbolic constant l EQU and TEXTEQU 51
90edea92771dabce78ad81eb09f9e565.ppt