Escolar Documentos
Profissional Documentos
Cultura Documentos
Third Edition
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
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)
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
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
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.
Overlapping Segments
Start 00000 00020 00030 Stop 00010 0002F 0012F Length 00011 00010 00100 Name _TEXT _DATA STACK Class CODE DATA STACK
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
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
Operator () +, *, / , m od +, -
Level 1 2 3 4 Parentheses
Description
Positive and negative signs (unary) Multiplication, Division, Modulus Addition, Subtraction
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
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
; ; ; ;
1 2 4 8
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)
; 20 ; 5
SIZE
Returns TYPE multiplied by LENGTH:
.data myByte myWord
db 20 dup(?) dw 5 dup(0)
; 20 (20 * 1) ; 10 (5 * 2)
JMP Example
Unconditional Transfer of control to a label:
LOOP Instruction
Automatically uses CX as the counter
decrements it automatically
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
Indirect Addressing
Indirect Operands
[si]. [di], [bx], [bp]
Base-Index Operands
[bx+si], [bx+di]
A B C D E F G
aString [bx]
...........
; AL = 10h ; AL = 30h
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
1000 [bx]
2000 C3000
(sum)
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]
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
0200
02 16 04 22 13 19 42 64 44
array
[BX]
(BX = 0005)
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...
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
[BX]
[BX + SI]
Debugging Workshop
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
; FEh
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
EXTRN directive
notifies the assembler that a procedure, constant, or variable is located outside the current program module EXTRN name:type
Readchar
Readkey
Readstring
Scroll
Writeint
Writeint_signed Writestring
.code extrn Clrscr:proc, Crlf:proc, Gotoxy:proc, \ Readint:proc, Readstring:proc, Scroll:proc, \ Readkey:proc, Writeint:proc, Writestring:proc
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
The End
title
text
.code
title
text
.code