Know how to formulate assembly language instructions, using valid syntax o One important function of assembler directives is to define program section,

94 KB – 22 Pages

PAGE – 1 ============
CMPS293&290 Class Notes (Chap 03) Kuo-pao Yang Page 1 / 22Chapter 3 Assembly Language Fundamentals 3.1 Basic Elements of Assembly Language 51 3.1.1 Integer Constants 52 3.1.2 Integer Expressions 52 3.1.3 Real Number Constants 53 3.1.4 Character Constants 54 3.1.5 String Constants 54 3.1.6 Reserved Words 54 3.1.7 Identifiers 54 3.1.8 Directives 55 3.1.9 Instructions 55 3.1.10 The NOP (No Operations) Instruction 57 3.1.11 Section Review 58 3.2 Example: Adding Three Integers 58 3.2.1 Alternative Version of AddSub 60 3.2.2 Program Template 61 3.2.3 Section Review 61 3.3 Assembling, Linking, and Running Programs 62 3.3.1 The Assemble-Link-Execute Cycle 62 3.3.2 Section Review 64 3.4 Defining Data 64 3.4.1 Intrinsic Data Types 64 3.4.2 Data Definition Statement 64 3.4.3 Defining BYTE and SBYTE Data 66 3.4.4 Defining WORD and SWORD Data 67 3.4.5 Defining DWORD and SDWORD Data 68 3.4.6 Defining QWORD Data 69 3.4.7 Defining TBYTE Data 69 3.4.8 Defining Real Number Data 69 3.4.9 Little Endian Order 69 3.4.10 Adding Variables to the AddSub Program 70 3.4.11 Declaring Uninitialized Data 71 3.4.12 Section Review 71 3.5 Symbolic Constants 72 3.5.1 Equal-Sign Directive 72 3.5.2 Calculating the Sizes of Arrays and Strings 73 3.5.3 EQU Directive 74 3.5.4 TEXTEQU Directive 74 3.5.5 Section Review 75 3.6 Real-Address Mode Programming (Optional) 75 3.6.1 Basic Changes 75 3.7 Chapter Summary 76 3.8 Programming Exercises 77

PAGE – 2 ============
CMPS293&290 Class Notes (Chap 03) Kuo-pao Yang Page 2 / 22Chapter 3 Assembly Language Fundamentals Objectives After reading this Chapter, you should be able to understand or do each of the following: Know how to represent integer constants, expressi ons, real number constants, character constants, and string constants in assembly language Know how to formulate assembly language instructions, using valid syntax Understand the difference between instructions and directives Be able to code, assemble, and execute a pr ogram that adds and subtracts integers Be able to create variables using all standard assembly language data types Be able to define symbolic constants Be able to calculate the size of arrays at assembly time 3.1 Basic Elements of Assembly Language 51 3.1.1 Integer Constants 52 Syntax: [{+ | -}] digits [radix] Microsoft syntax notation is used throughout this chapter o Elements within square brackets [ ] are optional o Elements within { –| –|–} requires a choice of the enclosed elements o Elements in italics denote items which have known definitions or descriptions Optional leading + or Œ sign binary, decimal, hexadecimal, or octal digits Common radix characters: o h Œ hexadecimal o d Œ decimal o b Œ binary o r Œ encoded real Examples: 30d, 6A h, 42, 1101 b Hexadecimal beginning w ith letter must have leading 0 : 0A5h If no radix is given, the integer constant is assumed to be decimal

PAGE – 3 ============
CMPS293&290 Class Notes (Chap 03) Kuo-pao Yang Page 3 / 223.1.2 Integer Expressions 52 An integer expression is a mathematical expr ession involving integer value and arithmetic operators. Operators and precedence levels: TABLE 3-1 Arithmetic Operators (Precedence). Examples: 3.1.3 Real Number Constants 53 Syntax: [sign] integer.[integer][ exponent] sign {+ | -} exponent E[{+ | -}]integer Examples: 2., +3.0, -44.26E+05, 26.E-5 3.1.4 Character Constants 54 Enclose character in single or double quotes o ASCII character = 1 byte Examples: ‘A’, “x”

PAGE – 4 ============
CMPS293&290 Class Notes (Chap 03) Kuo-pao Yang Page 4 / 223.1.5 String Constants 54 Enclose strings in single or double quotes o Each character occupies a single byte Examples: ‘xyz’, “ABC” Embedded quotes: ‘Say “Goodnight,” Gracie’ 3.1.6 Reserved Words 54 Reserved words have special meaning in MASM and can only be used in their context. There are different types of reserved words : o Instruction mnemonics: such as MOV, ADD, and MUL o Directives: Tell MSAM how assemble programs, such as .DATA and .CODE o Attributes: Provide size and usage information fo r variables and operands, such as BYTE and WORD o Operators: used in constant expressions, such as 10 * 10 o Predefined symbols : such as @data, which return constant integer values at assembly time. Reserved words cannot be used as identifiers See MASM reference in Appendix A (Page 600) 3.1.7 Identifiers 54 Identifiers Œ a programmer-choice name o 1-247 characters, including digits o not case sensitive o The first character must be a letter (A..Z, a..z), underscore (_), @, ?, or $. Subsequent character may also be digits. o An identifier cannot be the same as an assembler reserved word. Examples: var1, Count, $first, _main, MAX , open_file, xVal

PAGE – 5 ============
CMPS293&290 Class Notes (Chap 03) Kuo-pao Yang Page 5 / 223.1.8 Directives 55 Commands that are recognized and acted upon by the assembler o Not part of the Intel instruction set Directives do not execute at r un time, whereas instructions do. Example myVar DWORD 26 ; DWORD directive move ax, myVar ; MOV instruction o Used to declare code, data areas, select memory model, declare procedures, etc. o not case sensitive: It recognizes .data, .DATA, and .Data as equivalent. Defining Segments: o One important function of assembler directives is to define program section, or segments. o The .DATA directive identifies the area of a program containing variables: .data o The .CODE directive identifies the area of a program contai ning instructions: .code o The .STACK directive identifies the area of a program holding the runtime stack, setting its size: .stack 1000h Different assemblers have different directives o NASM not the same as MASM o See MASM Directives in Appendix A.5 (Page 604)

PAGE – 6 ============
CMPS293&290 Class Notes (Chap 03) Kuo-pao Yang Page 6 / 223.1.9 Instructions 55 An instruction is a statement that becomes executable when a program is assembled. Instructions are translated by the assembler into machine language bytes, which are loaded and executed by the CPU at run time. We use the Intel IA-32 instruction set Syntax: [label] mnemonic operand(s) [; comment] label optional instruction mnemonic required: such as MOV, ADD, SUB, MUL operands usually required comment optional An instruction contains: o Labels (optional) Act as place markers marks the address (offset) of code and data Follow identifer rules Data label must be unique example: count (not followed by colon ) count DWORD 100 Code label target of jump and loop instructions example: target: (followed by colon ) target: MOV ax, bx – JMP target o Mnemonics (required) Instruction Mnemonics memory aid examples: MOV, ADD, SUB, MUL, CALL MOV Move (assign) one value to another ADD Add two values SUB Subtract one value from another MUL Multiply two values JMP Jump to a new location CALL Call a procedure

PAGE – 8 ============
CMPS293&290 Class Notes (Chap 03) Kuo-pao Yang Page 8 / 223.1.10 The NOP (No Operations) Instruction 57 The safest instruction you can write is called NOP (no operation). It takes up 1 byte of program storage and does not do any work. I t is sometimes used by compilers and assemb lers to align code to even-address boundaries. Example: o In the following example, the NOP instruction a ligns the address of third instruction to a double word boundary (even multiple of 4). 0000 0000 66 8B C3 mov ax, bx 0000 0003 90 nop ; align next instruction 0000 0004 8B D1 mov edx, ecx o IA-32 processors are designe d to load code and data more quickly from even double word address.

PAGE – 9 ============
CMPS293&290 Class Notes (Chap 03) Kuo-pao Yang Page 9 / 223.2 Example: Adding Three Integers 58 Program listing 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 Program Output: showin g registers and flags EAX=00030000 EBX=7FFDF000 ECX=00000101 EDX=FFFFFFFF ESI=00000000 EDI=00000000 EBP=0012FFF0 ESP=0012FFC4 EIP=00401024 EFL=00000206 CF=0 SF=0 ZF=0 OF=0 Program Description o The TITLE directive marks the entire line as a comment o The INCLUDE directive copies necessary definitions and setup information from a test file ( Irvine32.inc) located in assembler™s INCLUDE directory o The .code directive marks the beginning of the code segment o The PROC directive identifies the beginning of a procedure o The MOVE instruction moves (copi es) the second operand (source operand) to the first operand (destination operator) o The ADD instruction add second operand to the first operand o The SUB instruction subtracts second operand from the from operand o The CALL statement calls a procedure. DumpRegs: Irvine32 procedure o The exit statement calls a predefined MS-Window function that halts the program o The ENDP directive marks the end of the procedure o The END directive marks the last line of the pr ogram to be assembled. It identifies the name of the program™s startup procedure (the procedure that starts the program execution.) Procedure main is the startup procedure. Segments Œ organize the program o The code segment (.code ) contains all of the program™s executable instruction o The data segment ( .data) holds variable o The stack ( .stack) holds procedure parameters and local variables

PAGE – 10 ============
CMPS293&290 Class Notes (Chap 03) Kuo-pao Yang Page 10 / 22 Suggested Coding Standards o This approach is used in this book , except that lowercase is used for the .code, .stack, .mode, and .data directives. Capitalize only directives and operators Use mixed case for identifiers Lower case everything else 3.2.1 Alternative Version of AddSub 60 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 The .386 directive identifies the mini mum CPU required for this program ( Intel386). The .MODEL directive instructs the assembler to generate code for a protected mode program, and STDCALL enables the calling of MS-Windows functions. Two PROTO directives declare prototypes for procedures used by this program: o ExitProcess is an MS-Windows function that halts the current program (called a process), and o DumpRegs is a procedure from the Irvine32 link library that displays registers. INVOKE is an assembler directive th at calls a procedure or function. o This program ends by calling the ExitProcess function, passing it a return code of zero .

PAGE – 11 ============
CMPS293&290 Class Notes (Chap 03) Kuo-pao Yang Page 11 / 223.2.2 Program Template 61 Program 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

94 KB – 22 Pages