代写代考 DWORD 26 ; DWORD directive

Assembly Language for x86 Processors
CHAPTER 3: ASSEMBLY LANGUAGE FUNDAMENTALS

Copyright By PowCoder代写 加微信 powcoder

Chapter Overview
Basic Elements of Assembly Language
Example: Adding and Subtracting Integers
Assembling, Linking, and Running Programs
Defining Data
Symbolic Constants
64-Bit Programming
IRVINE, KIP R. ASSEMBLY LANGUAGE FOR X86 PROCESSORS 7/E, 2015.

IRVINE, KIP R. ASSEMBLY LANGUAGE FOR X86 PROCESSORS 7/E, 2015.

Integer Constants
Optional leading + or – sign
Binary, decimal, hexadecimal, or octal digits
Common radix characters:
h – hexadecimal
d – decimal
b – binary
r – encoded real
[sign]integer.[integer][exponent]
2. , +3.0, -44.2E+05, 26.E5
At least one digit and a decimal point are required.
Examples: 30d, 6Ah, 42, 1101b
Hexadecimal beginning with letter: 0A5h
IRVINE, KIP R. ASSEMBLY LANGUAGE FOR X86 PROCESSORS 7/E, 2015.

IRVINE, KIP R. ASSEMBLY LANGUAGE FOR X86 PROCESSORS 7/E, 2015.

Integer Expressions
Operators and precedence levels:

Character and String Constants
Enclose character in single or double quotes
ASCII character = 1 byte
Enclose strings in single or double quotes
Each character occupies a single byte
Embedded quotes:
‘Say “Goodnight,” Gracie’

Reserved Words and Identifiers
Reserved words cannot be used as identifiers
Instruction mnemonics, such as MOV, ADD, and MUL
Register names
Directives, which tell MASM how to assemble programs
Attributes, which provide size and usage information for variables and operands. Examples are BYTE and WORD
Operators, used in constant expressions (+,-,*,….)
Predefined symbols, such as @data, which return constant integer values at assembly time
See MASM reference in Appendix A
Microsoft Macro Assembler Reference
Identifiers
1-247 characters, including digits
not case sensitive
first character must be a letter, _, @, ?, or $
IRVINE, KIP R. ASSEMBLY LANGUAGE FOR X86 PROCESSORS 7/E, 2015.

IRVINE, KIP R. ASSEMBLY LANGUAGE FOR X86 PROCESSORS 7/E, 2015.

Directives
Commands that are recognized and acted upon by the assembler
Not part of the Intel instruction set
Used to declare code, data areas, select memory model, declare procedures, etc.
not case sensitive
Different assemblers have different directives
NASM not the same as MASM, for example
The .DATA directive identifies the area of a program containing variables:
The .CODE directive identifies the area of a program containing executable instructions:
The .STACK directive identifies the area of a program holding the runtime stack, setting its size:
.stack 100h

Directives
myVar DWORD 26 ; DWORD directive
mov eax,myVar ; MOV instruction
The DWORD directive tells the assembler to reserve space in the program for a doubleword variable.
The MOV instruction, on the other hand, executes at runtime, copying the contents of myVar to the EAX register:

IRVINE, KIP R. ASSEMBLY LANGUAGE FOR X86 PROCESSORS 7/E, 2015.

IRVINE, KIP R. ASSEMBLY LANGUAGE FOR X86 PROCESSORS 7/E, 2015.

Instructions
Assembled into machine code by assembler
Executed at runtime by the CPU
We use the Intel IA-32 instruction set
An instruction contains:
Label (optional)
Mnemonic (required)
Operand (depends on the instruction)
Comment (optional)

[Label:] Mnemonic Operand(s) [; Comment]
IRVINE, KIP R. ASSEMBLY LANGUAGE FOR X86 PROCESSORS 6/E, 2010.

IRVINE, KIP R. ASSEMBLY LANGUAGE FOR X86 PROCESSORS 6/E, 2010.

Act as place markers
marks the address (offset) of code and data
Follow identifer rules
Data label
Data Labels A data label identifies the location of a variable, providing a convenient way to reference the variable in code. The following, for example, defines a label named count:
count DWORD 100
must be unique
Code label
in the code area of a program (where instructions are located) (followed by colon)
target of jump and loop instructions
jmp target

Mnemonics and Operands
Instruction Mnemonics
memory aid
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

Mnemonics and Operands
Assembly language can have between zero and three operands
constant expression
memory (data label)
Constants and constant expressions are often called immediate values

Instruction Format Examples
No operands
stc ; set Carry flag
One operand
inc eax ; register
inc myByte ; memory
Two operands
add ebx,ecx ; register, register
sub myByte,25 ; memory, constant
add eax,36 * 25 ; register, constant-expression
mov count,ebx ; move EBX to count
Three operand
imul eax,ebx,5 ; EBX multiplied by 5, and the result stored in EAX.

Comments are good!
explain the program’s purpose
when it was written, and by whom
revision information
tricky coding techniques
application-specific explanations
Single-line comments
begin with semicolon (;)
Multi-line comments
begin with COMMENT directive and a programmer-chosen character
end with the same programmer-chosen character

;Here is the comment
mov ax, bx

;Here is the comment
mov ax, bx

Example: Adding and Subtracting Integers
; AddTwo.asm – adds two 32-bit integers

.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
mov eax,5 ; move 5 to the EAX register
add eax,6 ; add 6 to the EAX register

INVOKE ExitProcess,0

Example Output
Showing registers and flags in the debugger:
EAX = 0000000B EBX = 7EFDE000 ECX = 00000000 EDX = 0040100A ESI = 00000000 EDI = 00000000 EIP = 00401054 ESP = 0018FF8C EBP = 0018FF94 EFL = 00000202

OV = 0 UP = 0 EI = 1 PL = 0 ZR = 0 AC = 0 PE = 0 CY = 0

16-bit FLAGS Register

Bit0: CF – Carry Flag Bit1: always a 1
Bit2: PF – Parity Flag Bit3: always a 0
Bit4: AF – Auxiliary (Carry) Bit5: always a 0
Bit6: ZF – Zero Flag Bit7: SF – Sign Flag
Bit8: TF – Trap Flag (no use) Bit9: IF – Interrupt Flag
Bit10: DF – Direction Flag Bit11: OF – Overflow

Reference: The Flag Register at A Guide to DEBUG

http://thestarman.pcministry.com/asm/debug/debug.htm

Bit 11 10 9 8 7 6 5 4 3 2 1 0
Txt OF DF IF SF ZF AF PF CF
VS OV UP EI PL ZR AC PE CY

Suggested Coding Standards (1 of 2)
Some approaches to capitalization

capitalize nothing
capitalize everything
capitalize all reserved words, including instruction mnemonics and register names
capitalize only directives and operators
Other suggestions

descriptive identifier names
spaces surrounding arithmetic operators
blank lines between procedures

Suggested Coding Standards (2 of 2)
Indentation and spacing

code and data labels – no indentation
executable instructions – indent 4-5 spaces
comments: right side of page, aligned vertically
1-3 spaces between instruction and its operands
ex: mov ax,bx
1-2 blank lines between procedures

Program Template
; Program Template (Template.asm)

; Description:
; Creation Date:
; Revisions:
; Modified by:
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD

; declare variables here

; write your code here
INVOKE ExitProcess,0

; (insert additional procedures here)

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.

The assembler contains a preprocessor to process directives, etc.

reads the executable file
into memory and branches
the CPU to the program’s
starting address
Checks if there is any calls to procedures in a link
reads the executable file
into memory and branches
the CPU to the program’s
starting address

Listing File
Listing file suitable for printing, and contains a copy of the program’s
source code,
with line numbers,
offset addresses,
segment names
translated machine code,
and a symbol table
Use it to see how your program is compiled
Example: addSub.lst

Information about each program segment:
starting address
ending address
segment type
Example: addSub.map (16-bit version)

The NOP Instruction
It takes up 1 byte of program storage and doesn’t do any work. It is sometimes used by compilers and assembler
Code alignment: Align the code to even-address boundaries
Check from a list file
Debug from Disassembly window:
00000000 66 8B C3 mov ax,bx
00000003 90 nop ; align next instruction
00000004 8B D1 mov edx,ecx

Intrinsic Data Types (1 of 2)
Describes a set of values that can be assigned to variables and expressions of the given type.
BYTE, SBYTE
8-bit unsigned integer; 8-bit signed integer
WORD, SWORD
16-bit unsigned & signed integer
DWORD, SDWORD
32-bit unsigned & signed integer
64-bit integer
80-bit integer

Intrinsic Data Types (2 of 2)

4-byte IEEE short real

8-byte IEEE long real

10-byte IEEE extended real

Data Definition Statement
A data definition statement sets aside storage in memory for a variable.
May optionally assign a name (label) to the data
[name] directive initializer [,initializer] . . .

value1 BYTE 10

All initializers become binary data in memory

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

Defining 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 ;different radixes
Examples that use multiple initializers:

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 (null byte containing 0)
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 .”,0

Defining Strings (2 of 3)
To continue a single string across multiple lines, end each line with a comma:

menu BYTE “Checking Account”,0dh,0ah,0dh,0ah,
“1. Create a new account”,0dh,0ah,
“2. Open an existing account”,0dh,0ah,
“3. Credit the account”,0dh,0ah,
“4. Debit he account”,0dh,0ah,
“5. Exit”,0ah,0ah,
“Choice> “,0

Defining Strings (3 of 3)
End-of-line character sequence:

0Dh = carriage return
0Ah = line feed
CR/LF: When written to standard output, they move the cursor to the left column of the line following the current line.

str1 BYTE “Enter your name: “,0Dh,0Ah
BYTE “Enter your address: “,0

newLine BYTE 0Dh,0Ah,0

Using the DUP Operator
Use DUP to allocate (create space for) an array or string. Syntax: counter DUP ( argument )
Counter and argument must be constants or constant expressions

var1 BYTE 20 DUP(0) ; 20 bytes, all equal to zero
var2 BYTE 20 DUP(?) ; 20 bytes, uninitialized
var3 BYTE 4 DUP(“STACK”) ; 20 bytes: “STACKSTACKSTACKSTACK”
var4 BYTE 10,3 DUP(0),20 ; 5 bytes

Defining 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

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

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

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.

val1 DWORD 12345678h

Big Endian Order
All data types larger than a byte store their individual bytes in “usual” order. The most significant byte occurs at the first (lowest) memory address.

val1 DWORD 12345678h

Adding Variables to AddSub

; This program adds and subtracts 32-bit unsigned
; integers and stores the sum in a variable.
INCLUDE Irvine32.inc
val1 DWORD 10000h
val2 DWORD 40000h
val3 DWORD 20000h
finalVal DWORD ?
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

Declaring Unitialized Data
Use the .data? directive to declare an unintialized data segment:

Within the segment, declare variables with “?” initializers:

smallArray DWORD 10 DUP(?)
Advantage: the program’s EXE file size is reduced.

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 ax,COUNT

Redefine with Equal-Sign
mov al, count

count = 100
mov al, count

Count = “This is a count!”

Calculating the Size of a Byte Array
current location counter: $

subtract address of list
difference is the number of bytes
list BYTE 10,20,30,40
ListSize = ($ – list)

Calculating the Size of a Word Array
Divide total number of bytes by 2 (the size of a word)
($ – list) is byte count

list WORD 1000h,2000h,3000h,4000h
ListSize = ($ – list) / 2

Calculating the Size of a Doubleword Array
Divide total number of bytes by 4 (the size of a doubleword)
list DWORD 1,2,3,4
ListSize = ($ – list) / 4

EQU Directive
Define a symbol as either a number or text expression.
Cannot be redefined

PI EQU <3.1416>
pressKey EQU <"Press any key to continue...",0>
prompt BYTE pressKey
Matrix1 EQU 10*10
Matrix2 EQU <10*10>
M1 word Matrix1
M2 word Matrix2

M1 word 100
M2 word 10*10

TEXTEQU Directive
Define a symbol as either an integer or text expression.
Called a text macro
Can be redefined
continueMsg TEXTEQU <"Do you wish to continue (Y/N)?">
rowSize = 5
prompt1 BYTE continueMsg
count TEXTEQU %(rowSize * 2) ; evaluates the expression
setupAL TEXTEQU

setupAL ; generates: “mov al,10”

64-Bit Programming
MASM supports 64-bit programming, although the following directives are not permitted:

INVOKE, ADDR, .model, .386, .stack

64-Bit Version of AddTwoSum
1: ; AddTwoSum_64.asm – Chapter 3 example.
3: ExitProcess PROTO
6: sum DWORD 0
9: main PROC
10: mov eax,5
11: add eax,6
12: mov sum,eax
13: mov ecx,0
14: call ExitProcess
15: main ENDP

Things to Notice About the Previous Slide
The following lines are not needed:

.model flat,stdcall
.stack 4096
INVOKE is not supported.
CALL instruction cannot receive arguments
Use 64-bit registers when possible

First Test
IRVINE, KIP R. ASSEMBLY LANGUAGE FOR X86 PROCESSORS 7/E, 2015.
TEST 1, 4th February

IRVINE, KIP R. ASSEMBLY LANGUAGE FOR X86 PROCESSORS 7/E, 2015.

Executable
Step 1: text editor

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com