Sunteți pe pagina 1din 14

Data Movement Instructions:

The data movement instructions copy values from


one location to another. Theseinstructions include
mov, xchg, lds, lea, les, push, pushf, pop,
popf.

The MOV Instruction:


The mov instruction takes several different forms:
Mov reg, reg
Mov mem, reg
Mov reg, mem
Mov mem, immediate data
Mov reg, immediate data
mov ax/al, mem
mov mem, ax/al
mov seg reg, mem16
movsegreg, reg16
mov mem16, segreg
mov reg16, segreg

The PUSH and POP Instructions:


The 80x86 push and pop instructions manipulate
data on the 80x86s hardware stack.
There are 8 varieties of the push and pop
instructions, they are
push reg16
pop reg16
push seg reg
pop seg reg (except CS)
push memory
pop memory
pushf
popf
The first two instructions push and pop a 16 bit
general purpose register. This is a compact (one
byte)version designed specifically for registers.

The second pair of push/pop instructions let you


push or pop an 80x86 segmentregister. You can
only push the csregister (popping the csregister
would create someinteresting program flow control
problems).
The third of push/pop instructions allow you to
push or pop the contents of amemory location. On
the 80286 and earlier, this must be a 16 bit value.
For memoryoperationswithout an explicit type
(e.g., [bx]) you must either use the pushw
mnemonic or explicitlystate the size using an
instruction like push word ptr [bx].
The pushfand popfinstructions allow you to
push/pop the processor status register (theflags).
Note that these two instructions provide a
mechanism to modify the 80x86s traceflag.
So what do these instructions do? The push
instructions move data onto the 80x86hardware
stack and the pop instructions move data from the
stack to memory or to a register.

The following is an algorithmic description of each


instruction:
push instructions (16 bits):
SP := SP - 2
[SS:SP] := 16 bit operand (store result at location
SS:SP.)
pop instructions (16 bits):
16-bit operand := [SS:SP]
SP := SP + 2
Notice three things about the 80x86 hardware
stack. First, it is always in the stack
segment(wherever ss points). Second, the stack
grows down in memory. That is, as you push
valuesonto the stack the CPU stores them into
successively lower memory locations.
Finally, the 80x86 hardware stack pointer (ss:sp)
always contains the address of the value onthe top
of the stack (the last value pushed on the stack).
You can use the 80x86 hardwarestack for

temporarily saving registers and variables, passing


parameters to a procedure,
allocating storage for local variables, and other
uses. The push and pop instructions areextremely
valuable for manipulating these items on the stack.
Most of the push and pop instructions do not affect
any of the flags in the 80x86processor status
register.

The XCHG Instruction:


The xchg (exchange) instruction swaps two values.
The general form is
xchg operand1, operand2
There are three specific forms of this instruction on
the 80x86:
Xchg reg, mem
Xchg reg, reg
xchg ax, reg16

Note that the order of the xchgs operands does


not matter. That is, you could enter xchg mem, reg
and get the same result as xchg reg, mem.

The LEA Instruction:


The lea (Load Effective Address) instruction is
another instruction used to preparepointer values.
The lea instruction takes the form:
Leadest, source
The specific forms on the 80x86 are
lea reg16, mem
It loads the specified 16 or 32 bit general purpose
register with the effective address of
the specified memory location. The effective
address is the final memory address obtainedafter
all addressing mode computations. For example,
lea ax, ds:[1234h] loads the ax registerwith the
address of memory location 1234h; here it just
loads the ax register with thevalue 1234h.
If you think about it for a moment, this isnt a very
exciting operation. After all, the mov ax, immediate

data instruction can do this. So why bother with the


lea instructionat all? Well, there are many other
forms of a memory operand besides
displacementonly operands. Consider the following
lea instructions:
lea ax, [bx]
leabx, 3[bx]
lea ax, 3[bx]
leabx, 4[bp+si]
lea ax, -123[di]
The lea ax, [bx] instruction copies the address of
the expression [bx] into the ax register.
Since the effective address is the value in the bx
register, this instruction copies bxsvalue into the
ax register. Again, this instruction isnt very
interesting because mov can dothe same thing,
even faster.
The lea bx,3[bx] instruction copies the effective
address of 3[bx] into the bx register.

Since this effective address is equal to the current


value of bx plus three, this lea
instructioneffectively adds three to the bx register.
There is an add instruction that will let you
addthree to the bx register, so again, the lea
instruction is superfluous for this purpose.

The LDS, LES, and LSS Instructions:


The lds, les, and lss instructions let you load a 16
bit general purpose registerand segment register
pair with a single instruction. On the 80286 and
earlier, the lds andles instructions are the only
instructions that directly process values larger than
32 bits.
The general form is LxSdest, source
These instructions take the specific forms:
lds reg16, mem32
les reg16, mem32
Keep in mind that these instructions load the four
bytes at a given memory locationinto the register
pair; they do not load the address of a variable into

the register pair (i.e.,this instruction does not have


an immediate mode).

STRING OPERATIONS:
_ In 8086 family of microprocessors there are a
group of instructions referred to as the string
instructions.
_ DI and SI registers are used to point the source
and the destination operands. Until nowbecausethe
ES(Extra segment) is not defined within the
programs both the DI and SI registers were
Usedthe offset of DS(Data segment).
_ When ES is defined as:
..
ASSUME CS:CODSEG, DS:DATSEG,
SS:STASEG,ES:DATSEG

MOV AX,DATSEG
MOV DS,AX
MOV ES,AX

then by default DI becomes the offset address of


ES and SI becomes the offset address ofDS.
_ In each of the string instructions the operands
can be byte or word. This is indicated byadding B
(byte) and W (word) to the end of the instruction
mnemonicDF, the direction flag: To process
operands in consecutive memory locations requires
thatthe pointer be incremented or decremented.
DF in string operations is used toindicate if SI and
DI pointers will increment or decrement
automatically.
_ It is the job of the programmer to specify the
choice of increment or decrement by settingthe
direction
flag high or low.

CLD :(clear direction flag)will reset (put to zero)


DF, indicating that the string instructionshould
increment the pointers (SI,DI) automatically. This
automaticincremination is sometimes referred as
auto increment.
STD :(set direction flag)will set (put to one) DF,
indicating that the string instructionshould
decrement the pointers (SI,DI) automatically.
REP prefix: The REP (repeat) allows a string
instruction to perform the operation repeatedly.
REP assumesthat CX holds the number of times
that the instruction should be repeated. As the
operation continues theCX register is decremented
until it becomes zero.
REPE/ REPNE prefix :REPE allows a string
instruction to perform the operation repeatedly as
long as CX is not zero and the comparison gives
equality. REPNE allows a string instruction to
perform the operation repeatedly as long as CX is
notzero and the comparison gives inequality.

Ex: Using the string instructions, write a program


that transfers a block of 20 bytes of data.Inthe data
segment:
DATA1 DB ABCDEFGHIJKLMNOPQRST
ORG 30H
DATA2 DB 20 DUP (?)
In the code segment:
ASSUME CS:CODESEG,DS:DATSEG,SS:STASEG,
ES:DATSEG
MOV AX,DATSEG
MOV DS,AX ; initialize the data segment
MOV ES,AX ; initialize the extra segment
CLD ;clear direction flag (auto increment)
MOV SI,OFFSET DATA1 ;load the source
pointer
MOV DI,OFFSET DATA2 ;load the destination
pointer
MOV CX,20 ;load the counter
REP MOVSB ;repeat until CX becomes zero

_ After the transfer of every byte by the MOVSB


instruction, both SI and DIregisters are
incremented automatically once only (notice CLD).
_ The REP (repeat) prefix causes the CX counter to
be decremented and MOVSBis repeated until CX
becomes zero.
Ex: Assuming that there is a spelling of Europe in
an electronic dictionary and auser types in
Euorope, write a program that compares these
two and displays thefollowing message, depending
on a result:
1. If they are equal, display The spelling is
correct
2. If they are not equal, display Wrong Spelling
;from the data segment
DAT_DIC DB Europe
DAT_TYPED DB Euorope
MESSAGE1 DB The spelling is correct,$
MESSAGE2 DB Wrong spelling,$
;from code segment:

.
CLD ;DF=0 for increment
MOV SI,OFFSET DAT_DIC ;SI=offset of
DAT_DIC
MOV DI,OFFSET DAT_TYPED ;DI=offset of
DAT_TYPED
MOV CX,06 ;load the counter
REPE CMPSB ;repeat as long as equal or until
CX=0
JE OVER ;if ZF=1 then display MESSAGE1
MOV DX,OFFSET MESSAGE2 ;if ZF=0 then
display MESSAGE2
JMP DISPLAY
OVER: MOV DX,OFFSET MESSAGE1
DISPLAY: MOV AH,09
INT 21H

S-ar putea să vă placă și