Академический Документы
Профессиональный Документы
Культура Документы
LOGICAL INSTRUCTIONS
AND
DESTINATION, SOURCE
Logical ANDs each bit in the source with the corresponding bit in the
destination
CF and OF both become zero PF, SF and ZF affected
AF undefined
||ly OR XOR
NOT
DESTINATION
Complements each bit in the destination and stores the result back into the
destination
No Flags Affected
NOT AL
NOT BX NOT BYTEPTR[SI]
NEG
DESTINATION
Does 2s complement on the data in the destination and stores the result back
AL BX BYTEPTR[SI]
8086-80486
STRING INSTRUCTIONS
STRING INSTRUCTIONS
80x86 is equipped with special instructions to handle string operations String: A series of data words (or bytes) that reside in consecutive memory locations
MOVS/MOVSB/MOVSW/MOVSD
Copies a byte or word or double-word from a location in the data segment to a location in the extra segment Source DS:SI Destination ES:DI No Flags Affected
D=0
COPY A BLOCK OF DATA FROM ONE MEMORY AREA TO ANOTHER MEMORY AREA- 50 DATA
.model tiny
0ah ,bch ,deh ,0f5h ,11h, 56h ,78h ,0ffh ,0ffh ,23h 4ah, 50 dup(0) cx,32h si,array1 di,array2 BL,[SI] [DI],BL SI DI CX X1
X1:
rep
8086-80486
STRING INSTRUCTIONS
STRING INSTRUCTIONS
80x86 is equipped with special instructions to handle string operations String: A series of data words (or bytes) that reside in consecutive memory locations
D=0
LODS
Loads AL or AX or EAX with the data stored at the data segment Offset address indexed by SI register After loading contents of SI inc if D = 0 & dec if D = 1 LODSB LODSW LODSD ; AL = DS:[SI]; SI = SI 1 ; AX = DS:[SI]; SI = SI 2 ; EAX = DS:[SI]; SI = SI4
STOS
Stores AL, or AX or EAX into the extra segment memory at offset address indexed by DI register
STOSW ;
ES:[DI] = AX
DI = DI 2
ES:[DI] = EAX DI = DI 4
Write an ALP to fill a set of 100 memory locations starting at displacement DIS1 with the value F6H
DAT1
.CODE
DB 100 DUP(?)
.STARTUP
MOV
MOV MOV
CLD
REP STOSB .EXIT
END
SCAS
Compares the AL with a byte of data in memory Compares the AX with a word of data in memory Compares the EAX with a double word of data in memory Memory is ES: DI Operands not affected - flags affected (subtraction)
SCASB SCASW
SCASD
Can be used with prefix REPNE SCASB
Write an ALP to find the displacement at which the data 0DH is present from an array of data stored from location DAT1. The number of bytes of data in the array is 80.
.MODEL TINY .DATA DAT1 .CODE .STARTUP MOV MOV DI, OFFSET DAT1 AL, 0DH
PROGRAM DB 80 DUP(?)
MOV
CLD
CX, 80
Scanning is repeated as long as bytes are not equal & the end of the string not reached If 0DH is found DI will point to the next address
CMPS
Compares a byte in one string with a byte in another string or a word /dword in
with ES: DI
Flags affected Direction flag used for auto increment or decrement Can be used with Prefix
PROGRAM SEGMENT
MOV MOV CLD MOV REPE CX, 100 CMPSB SI, OFFSET STRING FIRST DI, OFFSET STRING SECOND
CMPSB
Source
1000 1001 1002 67 56 4A
Destination
2000 2001 2002 67 56 4A
1003
1004
67
AA
2003
2004
67
AA
CMPSB
Source
1000 1001 1002 67 56 67
Destination
2000 2001 2002 67 56 4A
1003
1004
67
AA
2003
2004
67
AA
8086-80486
ROTATE OPERATIONS
USE CL FOR COUNT greater than 1 (if 80386 count greater than 1 can be
specified directly)
rol ror
ax,1 byteptr[si], 1
mov
rol ror
cl,04h
ax,cl byteptr[si], cl
rol
ecx,12
Target Register/Memory
ROR
CF
0 1
0 1
1 0
1 1
0 1
1 1
1 0
1 0
OF-1 OF-0
EXAMPLE
ROL
BL, 04
Swap Nibbles
Target Register/Memory
RCR
CF
WRITE A PROGRAM THAT COUNTS THE NUMBER OF 1S IN A BYTE IN LOCATION DATA1 AND WRITES IT INTO LOCATION RES1
.Model Tiny .data DATA1 DB 0A7H RES1 DB ? .code .startup SUB BL, BL MOV DL, 8 MOV AL,DATA1 AGAIN: ROL AL,1 JNC NEXT INC BL NEXT: DEC DL JNZ AGAIN MOV RES1,BL .exit end
;clear BL - no. of 1s ;rotate total of 8 times ;rotate it once ;check for 1 ;if CF=1 then inc count ;go through this 8 times ;if not finished go back
SAL/SHL
EXAMPLES
SAL MOV SAL SAL SAL
Flags Affected : CF, ZF,SF OF If MSB changes single bit rotate PF - Affected but has meaning only if 8-bit operation AF - Undefined
SHR
Shift Operand bits right. Put Zero in MSBs SHR Destination, Count SHR MOV AND MOV MOV BP, 1 BL, AL BL, 0FH CL, 04H BH, AL
SHR AL, CL
SAR
MOV
SAR
CL, 02H
WORD PTR[BP], CL
MUL/IMUL SOURCE
MUL/IMUL SOURCE
Result for Byte multiplication in AX Result for Word multiplication in DX :AX Result for Dword Multiplication EDX:EAX
CBW/CWD
Convert Byte to Word 80 FF80 27- 0027 Convert Word to Double Word 5643 00005643
9100- FFFF9100
..
MOV
MUL MOV
AX, MULTIPLICAND
MULTIPLIER PRODUCT1, AX
MOV
.EXIT END
PRODUCT2, DX
SPECIAL IMUL
WORD
BYTE
DWORD
QWORD
WORD
DWORD
AL- quotient
AH- reminder
AX- Quotient
DX- Reminder
Location
EAX- Quotient EDX- Reminder All Flags undefined Sign of remainder same as dividend In Case of signed ops
Jump Instructions
Conditional Jump and Unconditional Jump instructions
Near and Far jumps are further divided into Direct or Indirect
Direct Indirect - Destination address specified as a part of the instruction - Destination address specified in a register or memory location
Short jump
Displacement 8 bits +127 to -128 locations.
Near Jump
Displacement 16 bits +32, 767 to 32, 768 locations
0100H 0102H
back:
add nop
al, 03H
0103H
0104H
nop
jmp back
0106H
- 6 is the displacement FAH
EB 90 90
THER:
Displacement = 02H
Intersegment Direct
JMP Offset Base
JZ/JNZ
JP/JNP JS/JNS JO/JNO JCXZ
Zero
Parity Sign Overflow CX =0
8086 Conditional Jump Instructions Unsigned numbers: JA JAE JB JBE Comp Operands Dest > Src CF 0 ZF 0
Dest = Src
Dest < Src
0
1
1
0
CF 0
SF,OF SF = OF
0 1
X
SF OF
LOOP Jump to a specified label if CX 0 after auto- dec of CX Used to repeat a series of instructions some number of times. The no. of times sequence is to be repeated is loaded in CX Each time Loop inst executes CX automatically decs by 1
LOOP X1
DEC CX JNZ X1
IF CX 0 execution will jump to destination Specified by a label in the instruction If CX = 0 after auto dec execution will go on to the next instruction after LOOP Destination address is of type SHORT LOOP affects no FLAGs
Add a data in one block of memory with data in another block of memory using LOOP Size of block -100 words
Y1 Y2 = = = X 1 + Y1 X2 + Y2 Xn + Yn
Yn=
DW DW DW
X1:
.EXIT END
CX, COUNT SI, OFFSET BLOCK1 DI, OFFSET BLOCK 2 AX, [DI] X1
Conditional LOOPs LOOPE/LOOPZ ( LOOP while equal) LOOP while CX 0 and ZF = 1 Each time the LOOP instr executes - CX decremented If CX 0 & ZF = 1 execution will jump to destn specified
MOV
DEC
MOV NEXT: INC CMP LOOPE
BX
CX, 100 BX [BX], 0FFH NEXT
LOOPNE/LOOPNZ
Loop While CX 0 and ZF = 0 MOV DEC MOV INC CMP LOOPNE BX, OFFSET ARRAY BX CX, 100 BX [BX], 0DH NEXT
NEXT:
Store and retrieve data from LIFO stack memory 2/4 bytes involved Whenever 16-bit data pushed into stack MSB moves into memory [SP-1] LSB moves into memory [SP-2]
Contents of SP register decremented by 2
Push
Push
PUSH PUSH PUSH PUSH PUSHF PUSHFD PUSHA PUSHAD PUSH PUSH PUSHD
AX BX SI WORD PTR[BX]
70050
7004FH
7004EH 7004DH
AH
AL BH
SP SP-2 7004EH AX
[004EH]
SP SP-2
7004CH BX SP SP-2 7004AH SI SP SP-2
[004CH]
[004AH] [0048H] [0046H]
7004CH
7004BH 7004AH
BL
SI(High) SI(Low)
70049H
70048H 70047H 70046H SP:0050H
Mem(high)
Mem(low) FLR(high) FLR(low)
70048H MEM
SPSP-2 70046H FLAGS
SS:7000H
POP
POP performs inverse of PUSH Takes data from stack to register, Memory Data popped in 16 bits/32 bits First byte from stack to lower register Second byte to higher register SP = SP+2
POP POP POP AX CX WORD PTR[BX]
00046H 00047H
SP = 0050H
7004A 7004A
7004B 7004B
7004C 7004C 7004D 7004D 7004E 7004E 7004F 7004F 70050 70050
7F
AB 12 56 34
8086-80486
Subroutines
Subroutines
CALL Instruction CALL instruction in the main line program loads the IP& in some cases also CS registers - the starting address of the procedure Next instruction fetched will be the first inst of the procedure
RET instruction at the procedure end sends the execution Back to mail line program
CALL
RET
Call
Intra-segment (near) Inter-segment (far) In-Direct Direct
Absolute
Call DWord Ptr [SI]
Absolute
Call IP CS
Direct
Relative Call disp
In-Direct
Absolute Call BX Call Word Ptr [SI]
CALL Stores the address of the instruction after call into stack ( return address) near CALL or far CALL (IP saved) (CS and IP saved)
RET inst retrieves the next address after CALL Back to IP or ( IP and CS)
CALL RET
CALL _____ _____ SUMS PROC ADD ADD ADD RET ENDP
SUMS
SUMS
db db dw label
x1:
mask x1
.exit
mask
proc near
and stosb ret al,0fh
mask
endp
end
STACK
Call SUMS
8086-80486
Subroutines & Stacks
RET instruction at the procedure end sends the execution Back to mail line program
CALL
RET
db db dw label
x1:
mask1 x1
.exit
mask1
proc near
and stosb ret al,0fh
mask1
endp
end
0100 0101
8D 26
0110
0111 0112 0113 0114 0115
E8
06 00 E2 FA B4 4C CD 21 24
CALL 0119
2E 01 8D 36
1E 01 8D 3E 22 01 B9 04 00 AC
0F AA C3
31 32
STOSB
RET
0122 0121
33 34
0 0 0 0
X X X X X X X X TOP_STACK
33 34 01 0 0
0 0
X X X X
X X 13 X 01 X
8086-80486
SUBROUTINES- PARAMETER PASSING
Parameters are data values or addresses made available for the procedures for
use
Passed by
Registers Dedicated memory locations accessed by name With pointers passed in registers With stack
EX: BCD to Binary Conversion BCD_BIN PROC PUSHF PUSH PUSH MOV AND AND MOV ROR MOV MUL ADD POP POP POPF RET BCD_BIN ENDP
NEAR
BCD 16
Bin/hex -10h
(01 * 0ah ) + 6
MOV
AL, BCDINPUT
Using reg
CALL BCD_BIN BCD_BIN PROC NEAR MOV AL, BCDINPUT MOV SI, OFFSET BCDINPUT CALL BCD_BIN BCD_BIN PROC NEAR MOV AL, [SI] Using Mem
Using Pointers
MOV AX, 30 PUSH AX CALL BCD_BIN BCD_BIN PROC PUSH MOV MOV .. POP RET NEAR BP BP, SP AX, [BP+4]
BP
Using stack