Você está na página 1de 51

Chapter 4: Using the Assembler

Slides to Accompany Assembly Language for Intel-Based Computers,

Third Edition

Irvine, Kip R. Assembly Language For Intel-Based Computers

Overview
More about the assembler and linker Operators and expressions JMP and LOOP instructions Indirect addressing Debugging workshop More 80386 and 80486 instructions Using a link library

Irvine, Kip R. Assembly Language For Intel-Based Computers

More About the Assembler and Linker


Topics: Source Listing Map File Memory models Overlapping segments Target processor directives

Irvine, Kip R. Assembly Language For Intel-Based Computers

Source Listing
Exchange Two Variables 3 0000 .model small 4 0000 .stack 100h 5 6 0000 .code 7 0000 main proc 8 0000 B8 0000s 9 0003 8E D8 10 0005 swap: 11 0005 A0 0000r 12 0008 86 06 0001r 13 000C A2 0000r 14 000F B8 4C00 15 0012 CD 21 16 0014 main endp 17 18 0014 .data 19 0000 0A value1 20 0001 14 value2 21 22 end main (SAMPLE.ASM)

mov mov mov xchg mov mov int

ax,@data ; initialize DS register ds,ax al,value1 al,value2 value1,al ax,4C00h 21h ; ; ; ; load the AL register exchange AL, value2 store new value of AL return to DOS

db 0Ah db 14h

Irvine, Kip R. Assembly Language For Intel-Based Computers

Map File
Segments always begin on even-paragraph boundaries.
Start 00000H 00020H 00030H Stop 00013H 00021H 0012FH Length 00014H 00002H 00100H Name _TEXT _DATA STACK Class CODE DATA STACK

Program entry point at 0000:0000

Irvine, Kip R. Assembly Language For Intel-Based Computers

Memory Models
Model Tiny Small Medium Compact Large Huge Flat Description Code and data combined must be less than 64K. Code <= 64K, data <= 64K. One code segment, one data segment. Data <= 64K, code any size. Multiple code segments, one data segment. Code <= 64K, data any size. One code segment, multiple data segments. Code >64K, data > 64K. Multiple code and data segments. Same as the Large model, except that individual variables such as arrays may be larger than 64K. No segments. 32-bit addresses are used for both code and data. Protected mode only.

Irvine, Kip R. Assembly Language For Intel-Based Computers

Overlapping Segments
Start 00000 00020 00030 Stop 00010 0002F 0012F Length 00011 00010 00100 Name _TEXT _DATA STACK Class CODE DATA STACK

Program entry point at 0000:0000

Offset 00 20 30 code data stack

130 (64K) (64K) (64K)

Irvine, Kip R. Assembly Language For Intel-Based Computers

Target Processor Directives


Directive .8086 Description Enables assembly of 8086 and 8088 instructions. Disables assembly of instructions for the 80186 and later processors. Also enables 8087 instructions. Enables assembly of 80186 instructions and disables assembly of instructions for all later processors. Enables assembly of nonprivileged 80286 instructions and disables assembly of instructions for all later processors. Enables assembly of nonprivileged 80386 instructions and disables assembly of instructions for all later processors. Enables assembly of nonprivileged 80486 instructions and disables assembly of instructions for the Pentium. Enables assembly of nonprivileged Pentium instructions. Enables assembly of floating-point instructions for the 80287 math coprocessor. Enables assembly of floating-point instructions for the 80387 math coprocessor.

.186 .286 .386 .486 .586 .287 .387

Irvine, Kip R. Assembly Language For Intel-Based Computers

Assembler Operators
Operator .TYPE +, -, *, / AND, OR, NOT EQ, NE, LT, LE, GT, GE HIGH HIGHWORD LENGTH LOW LOWWORD MASK MOD OFFSET PTR Description Returns a byte that defines the mode and scope of an expression. The result is bit mapped and is used to show whether a label or variable is programrelated, data-related, undefined, or external in scope. Addition, subtraction, multiplication, division of integers. Bitwise operations on constant integers. Relational operators: Assembler returns a value of 0FFFFh when a relation is true or 0 when it is false. Returns the high 8 bits of a constant expession. Returns the high 16 bits of a 32-bit operand (MASM only). Returns the number of byte, word, dword, qword, or tenbyte elements in a variable. This is meaningful only if the variable is initialized with the DUP operator. Returns the low 8 bits of a constant expression. Returns the low 16 bits of a 32-bit operand (MASM only). Returns a bit mask for the bit positions in a field within a variable. A bit mask preserves just the important bits, setting all others equal to zero. The variable must be defined with the RECORD directive. Modulus operator: returns the integer remainder of a division operation. Returns the offset of a label or variable from the beginning of its segment. Specifies the size of an operand, particularly when its size is not clear from context. Language For Intel-Based Computers R.the Assembly

Irvine, Kip

MASK MOD OFFSET PTR SEG SHORT SIZE

Assembler Operators (continued) Modulus operator: returns the integer remainder of a division operation.

Returns a bit mask for the bit positions in a field within a variable. A bit mask preserves just the important bits, setting all others equal to zero. The variable must be defined with the RECORD directive.

Returns the offset of a label or variable from the beginning of its segment. Specifies the size of an operand, particularly when its size is not clear from the context. Returns the segment value of an expression, whether it be a variable, a segment/group name, a label, or any other symbol. Sets a labels attribute to SHORT. Often used in JMP instructions, as in: JM P SHO RT Label 1. Returns the total number of bytes allocated for a variable. This is calculated as the LENGTH multiplied by the TYPE. The name following (.) identifies a field within a predefined structure by adding the offset of the field to the offset of the variable. The format is variable.field. Creates an operand of a specified type at the current program location. The type can be any of those used with the PTR operator or the LABEL directive. Returns an integer that represents either the size of a variable or its type. For example, the TYPE of a word variable is 2. Returns the number of bits of a given field within a variable that has been declared with the RECORD directive.

Field (.)

THIS

TYPE WIDTH

Irvine, Kip R. Assembly Language For Intel-Based Computers

Operator Precedence Table

Operator () +, *, / , m od +, -

Level 1 2 3 4 Parentheses

Description

Positive and negative signs (unary) Multiplication, Division, Modulus Addition, Subtraction

Irvine, Kip R. Assembly Language For Intel-Based Computers

Offset, Seg, Ptr, Label, and Even


OFFSET returns the 16-bit address (offset) of a label SEG returns the segment portion of a label's address PTR overrides the default size of an operand LABEL redefines the size attribute of a data label EVEN aligns the next instruction to a 16-bit boundary EVENDATA aligns the next data label on a 16-bit boundary
Irvine, Kip R. Assembly Language For Intel-Based Computers

TYPE and SIZE Operators


TYPE
returns the size, in bytes of a single element of a data label (variable)

LENGTH
returns a count of the number of individual elements in a data label that uses the DUP operator

SIZE
returns the product of TYPE * LENGTH

Irvine, Kip R. Assembly Language For Intel-Based Computers

TYPE
TYPE returns the size attribute:
.data myByte myWord myDouble myQuad
db dw dd dq 1,2,3,4 1000h,2000h,3000h 12345678h 1,2,3

.code mov ax,TYPE mov ax,TYPE mov ax,TYPE mov ax,TYPE

myByte myWord myDouble myQuad

; ; ; ;

1 2 4 8

Irvine, Kip R. Assembly Language For Intel-Based Computers

LENGTH
Returns a count of the number of individual elements in a data label that uses the DUP operator:
.data myByte myWord db 20 dup(?) dw 5 dup(0)

.code mov ax,LENGTH myByte mov ax,LENGTH myWord

; 20 ; 5

Irvine, Kip R. Assembly Language For Intel-Based Computers

SIZE
Returns TYPE multiplied by LENGTH:
.data myByte myWord

db 20 dup(?) dw 5 dup(0)

.code mov ax,SIZE myByte mov ax,SIZE myWord

; 20 (20 * 1) ; 10 (5 * 2)

Irvine, Kip R. Assembly Language For Intel-Based Computers

JMP and LOOP Instructions


JMP is an unconditional jump to a code label LOOP creates a counting loop, using CX as the default counter
LOOPD uses ECX as the counter register LOOPW uses CX as the counter register (only necessary in 32-bit mode programming)

Irvine, Kip R. Assembly Language For Intel-Based Computers

JMP: Distance Modifiers


JMP SHORT destination +/- 127 bytes JMP NEAR PTR destination same code segment (default in the small and compact memory models) JMP FAR PTR destination different code segment (default in the large, medium, and huge memory models)

Irvine, Kip R. Assembly Language For Intel-Based Computers

JMP Example
Unconditional Transfer of control to a label:

Label1: . . jmp Label1

Irvine, Kip R. Assembly Language For Intel-Based Computers

LOOP Instruction
Automatically uses CX as the counter
decrements it automatically

If CX > 0, LOOP transfers control to a label


otherwise, excecution drops through

Irvine, Kip R. Assembly Language For Intel-Based Computers

LOOP Example
Task: sum the integers { 1,2,3,4,5 }
mov mov mov L1: cx,5 bx,1 ax,0 ; loop counter ; value to be added ; holds the sum

add ax,bx inc bx Loop L1


; AX=000F, BX=0006, CX=0000

Irvine, Kip R. Assembly Language For Intel-Based Computers

Indirect Addressing
Indirect Operands
[si]. [di], [bx], [bp]

Based and Indexed Operands


array[si], array[di], array[bx]

Base-Index Operands
[bx+si], [bx+di]

Base-Index with Displacement


array[bx+si], array[bx+di]

Irvine, Kip R. Assembly Language For Intel-Based Computers

Indirect Operand Example


.data aString db "ABCDEFG .code mov bx,offset aString add bx,5 mov dl,[bx]
0200 0205

A B C D E F G
aString [bx]

...........

Irvine, Kip R. Assembly Language For Intel-Based Computers

Adding 8-bit Integers


.data aList db 10h,20h,30h sum db 0 .code mov bx,offset aList mov al,[bx] inc bx add al,[bx] inc bx add al,[bx] mov si,offset sum mov [si],al

; AL = 10h ; AL = 30h

; AL = 60h ; get offset of sum ; store the sum

If you want to paste a code example such as this into a program, remember that the code segment must always begin with the following statements: mov ax,@data mov ds,ax

Irvine, Kip R. Assembly Language For Intel-Based Computers

Adding 16-bit Integers


.data wordList dw 1000h,2000h,3000h, 0 .code mov bx,offset wordList mov ax,[bx] ; first number add ax,[bx+2] ; second number add ax,[bx+4] ; third number mov [bx+6],ax ; store the sum

1000 [bx]

2000 C3000

(sum)

[bx+2] [bx+4] [bx+6]

Irvine, Kip R. Assembly Language For Intel-Based Computers

32-Bit Registers
The .386 directive permits any of the following registers to be used as indirect operands: EAX, EBX, ECX, EDX, ESI, EDI, EBP
.386 mov ax,[ebx+3] mov dl,string[edx] mov ecx,[esi] mov ebx,[eax]

Irvine, Kip R. Assembly Language For Intel-Based Computers

Displaying a String
.data string db "This is a string." COUNT = ($string) ; calculate string length .code mov mov L1: mov mov int inc Loop cx,COUNT ; loop counter si,offset string ah,2 dl,[si] 21h si L1 ; ; ; ; ; DOS function: display char get character from array display it now point to next character decrement CX, repeat until 0

Irvine, Kip R. Assembly Language For Intel-Based Computers

Summing an Integer Array


.data intarray dw 0100h,0200h,0300h,0400h COUNT = ($ intarray) / (TYPE intarray) .code mov mov mov L1: add add Loop ax,0 ; zero accumulator di,offset intarray ; address of array cx,COUNT ; loop counter ax,[di] di,TYPE intarray L1 ; add an integer ; point to next integer ; repeat until CX = 0

Irvine, Kip R. Assembly Language For Intel-Based Computers

Based and Indexed Operands


The microsoft assembler permits the same address expression to be notated in various ways:

Register Added to an Offset


m ov dx, ar r ay[ bx] m ov dx, [ di + ar r ay] m ov dx, [ ar r ay+si ]

Register Added to a Constant


m ov ax, [ bx + R O W VAL] m ov dx, [ bp+4] m ov dx, 2[ si ]

Irvine, Kip R. Assembly Language For Intel-Based Computers

Two-Dimensional Array Example


Each row of this table contains five bytes. BX points to the beginning of the second row:
.data ROWSIZE = 5 array db 2h, 16h, 4h, 22h, 13h db 19h, 42h, 64h, 44h, 88h .code mov bx,ROWSIZE mov al,array[bx] ; AL = 19h

0200

0205 88 ...... ......... .

02 16 04 22 13 19 42 64 44

array

[BX]

(BX = 0005)

Irvine, Kip R. Assembly Language For Intel-Based Computers

Based-Index Operands
Add the value of a base register to an index register, producing an effective address of 0157:
BX = 0155, SI = 0002
0150 0155 0157 A0 B0 C0 D0 E0 F0 10 20 30 40 50 60 70 80 90

[BX]

[BX + SI]

Example...

Irvine, Kip R. Assembly Language For Intel-Based Computers

Base-Index Example
.data ROWSIZE = 5 array db 10h, 20h, 30h, 40h, 50h db 60h, 70h, 80h, 90h,0A0h db 0B0h,0C0h,0D0h,0E0h,0F0h .code mov bx,offset array add bx,ROWSIZE mov si,2 mov al,[bx + si]

; ; ; ;

point to the array at 0150 choose second row choose third column get the value at 0157

Irvine, Kip R. Assembly Language For Intel-Based Computers

Base-Index with Displacement


.data ROWSIZE = 5 array db 10h, 20h, 30h, 40h, 50h db 60h, 70h, 80h, 90h,0A0h db 0B0h,0C0h,0D0h,0E0h,0F0h

.code mov bx,ROWSIZE mov si,2 mov dl,array[bx + si]


0150 0155 0157 10 20 30 40 50 60 70 80 90

; row 1 ; column 2 ; DL = 80h


A0 B0 C0 D0 E0 F0

[BX]

[BX + SI]

Irvine, Kip R. Assembly Language For Intel-Based Computers

Debugging Workshop

Irvine, Kip R. Assembly Language For Intel-Based Computers

Mismatching Operand Sizes


1: title Mismatching Operand Sizes 2: 3: .model small 4: .stack 100h 5: .code 6: main proc 7: mov ax,@data 8: mov ds,ax 9: mov ax,value1 10: mov ah,value2 11: mov ax,4C00h 12: int 21h 13: main endp 14: 15: .data 16: value1 db 0Ah 17: value2 dw 1000h 18: end main (9): warning A4031: Operand types must match (10): warning A4031: Operand types must match

Irvine, Kip R. Assembly Language For Intel-Based Computers

Miscellaneous Errors
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: title Miscellaneous Errors Program .model small .stack 100h .code main proc mov ax,@data mov ds,ax mov ax,bx * cx mov bx,value1 * 2 mov byte ptr value3,al mov cx,ax mov cs,ds mov ax,4C00h int 21h main endp .data value1 db 0Ah value2 db 14h value3 dw 1000h end main

Irvine, Kip R. Assembly Language For Intel-Based Computers

Intel386 and Intel486 Instructions


MOVZX - Move with zero-extend
moves 8-bit operand into a 16-bit register, fills upper bits with zeros moves 16-bit operand into a 32-bit register, fills upper bits with zeros

MOVSX - Move with sign-extend


moves 8-bit operand into a 16-bit register, sign extends into upper bits moves 16-bit operand into a 32-bit register, sign extends into upper bits

Irvine, Kip R. Assembly Language For Intel-Based Computers

MOVZX and MOVSX Examples


.data var16 dw 1234h var8 db -2 .code mov movzx movzx movsx

; FEh

bl,22h ax,bl edx,var16 cx,var8

; AX = 0022h ; EDX = 00001234h ; CX = FFFEh

Irvine, Kip R. Assembly Language For Intel-Based Computers

XADD Instruction
XADD op-left, op-right

Adds operands and stores sum in op-left. Saves the starting value of op-left and stores it in op-right.
.code mov ax,1000h mov bx,2000h xadd ax,bx

; AX = 1000h, BX = 2000h ; AX = 3000h, BX = 1000h

Irvine, Kip R. Assembly Language For Intel-Based Computers

Using a Link Library


IRVINE.LIB is supplied with the book's sample programs
Complete list of procedures is in Appendix E

EXTRN directive
notifies the assembler that a procedure, constant, or variable is located outside the current program module EXTRN name:type

Irvine, Kip R. Assembly Language For Intel-Based Computers

Data Types for the EXTRN Directive


Type ABS PROC NEAR FAR BYTE WORD DWORD FWORD QWORD TBYTE Description A constant defined with EQU or = Default type for a procedure Name is in the same segment Name is in a different segment Size is 8 bits Size is 16 bits Size is 32 bits Size is 48 bits Size is 64 bits Size is 10 bytes

Irvine, Kip R. Assembly Language For Intel-Based Computers

Selected Procedures in the Link Library


Procedure Clrscr Crlf Get_time Gotoxy Random_range Random32 Randomize Readint Description Clear the screen and locate the cursor at the upper left corner. Write a carriage return and line feed to standard output. Retrieve the current time of day. Input: DS:SI points to a TimeRecord structure. Requires the console.inc include file. Locate the cursor at a specified row and column on the screen. Input: DH = row (0-24), DL = column (0-79). Generate a pseudorandom integer in EAX between 0 and n1. Input: EAX contains n. Generate a 32-bit pseudorandom integer in the range 0 to FFFFFFFFh. Output: EAX contains the number. Automatically seed the random number generator with a random value, based on the current time of day. Read a signed ASCII decimal string from standard input and store it as a 16-bit binary integer. Output: AX contains the value. Try to read a character from standard input without echoing the character on the screen. Output: If a character is waiting, ZF = 0 and AL contains the character. If no character is waiting, ZF = 1. Wait for a single key to be pressed on the keyboard. Cannot be redirected. Output: AH = key scan code, AL = ASCII code.

Readchar

Readkey

Irvine, Kip R. Assembly Language For Intel-Based Computers

Selected Procedures in the Link Library


Procedure Readlong Description Read a signed ASCII decimal string from standard input and store it as a 32-bit binary integer. Output: EAX contains the value. Read a string of characters from standard input and store them in a null-terminated string. Input: DX points to the string, CX = maximum character count. Output: AX = number of characters typed. Scroll a window on the screen with a chosen color. Input: CH, CL = upper left corner row and column; DH, DL = lower right row and column; BH = attribute (color) of the scrolled lines. Return the number of seconds that have passed since midnight. Output: EAX contains the seconds. Write a given time to standard output. Input: DS:SI points to a TimeRecord structure. Wait for a character to be read from standard input. Does not echo the character on the console. Output: AL contains the character. Write a single character to standard output. Input: AL contains the character. Write an unsigned 16-bit integer to standard output in ASCII binary, decimal, octal, or hexadecimal format. Input: AX = the integer to display, and BX = radix value (2, 8, 10, or 16). Write a 16-bit integer to standard output in signed decimal ASCII format. Input: AX = the integer to display. Write a null-terminated string to standard output. Input: DX points to the string.

Readstring

Scroll

Seconds_today Show_time Waitchar Writechar

Writeint

Writeint_signed Writestring

Irvine, Kip R. Assembly Language For Intel-Based Computers

Link Library Demo Program


Title Link Library Demo Program (lnkdemo.asm) ; This program calls various I/O procedures ; in the link library. .model small .stack 100h WhiteOnBlue = 1Fh GreetingLoc = 0400h .data greeting db "Link Library Demo Program" db 0dh,0ah,0dh,0ah db "What is your name? ",0 numberPrompt db 0dh,0ah db "Please enter a 16bit integer: ",0 userName db 50 dup(0) pressAnyKey db 0dh,0ah,0dh,0ah db "Press any key...",0

.code extrn Clrscr:proc, Crlf:proc, Gotoxy:proc, \ Readint:proc, Readstring:proc, Scroll:proc, \ Readkey:proc, Writeint:proc, Writestring:proc

Irvine, Kip R. Assembly Language For Intel-Based Computers

Link Library Demo, continued


main proc mov ax,@data mov ds,ax ; Clear the screen, scroll a blue window. call mov mov mov call Clrscr cx,0400h dx,0B28h bh,WhiteOnBlue Scroll ; upperleft corner ; lowerright corner

; Display a greeting and ask for the ; users name.


mov call mov call mov call dx,GreetingLoc Gotoxy dx,offset greeting Writestring dx,offset userName Readstring

Irvine, Kip R. Assembly Language For Intel-Based Computers

Link Library Demo, continued


; Ask the user to enter a signed decimal integer. ; Redisplay the number in hexadecimal and binary. mov call call call mov call call mov call mov call call call dx,offset numberPrompt Writestring Readint ; input an integer Crlf bx,16 ; display in hexadecimal Writeint Crlf bx,2 ; display in binary Writeint dx,offset pressAnyKey Writestring Readkey Clrscr ; end program

mov ax,4c00h int 21h main endp end main

Irvine, Kip R. Assembly Language For Intel-Based Computers

Generating Random Integers


Generate 20 random integers between 0 and 999.
.code extrn Randomize:proc, Random_range:proc extrn WriteInt:proc, Crlf:proc call Randomize mov cx,20
L1: mov call mov call call Loop eax,1000 Random_range bx,10 WriteInt Crlf L1

; EAX = random integer ; decimal radix

Irvine, Kip R. Assembly Language For Intel-Based Computers

Delay_seconds Procedure
Pause for a specified number of seconds.
extrn Seconds_today:proc Delay_seconds proc pusha mov ecx,eax call Seconds_today mov ebx,eax DLY1: call sub cmp jb Seconds_today eax,ebx eax,ecx DLY1

; delay, in seconds
; save start time ; ; ; ; get the time subtract from start delay finished yet? if not, continue loop

popa ret Delay_seconds endp

Irvine, Kip R. Assembly Language For Intel-Based Computers

The End

Irvine, Kip R. Assembly Language For Intel-Based Computers

title
text
.code

Irvine, Kip R. Assembly Language For Intel-Based Computers

title
text
.code

Irvine, Kip R. Assembly Language For Intel-Based Computers

Você também pode gostar