| Table of Contents |
This appendix covers the most commonly used instructions for moving data. The instructions discussed include the MOV, PUSH, POP, IN, and OUT.
The MOV statement is generally used to move data in and out of registers. It can also be used to move data from memory to memory and to load memory or registers with immediate data. When moving data, always define the destination first, then the source.
The following are some examples of MOV statements.
;load register AX with value 55
mov AX,55
;this loads AX with contents of BX
mov ax,bx
;this loads AX with contents of memory word indexed by DS:BX
mov ax,[bx]
;this loads AL with contents of memory byte indexed
;by SS:BP+BX
mov al,[bp+bx]
;define a word of memory for use as a variable v_1
v_1 dw 0
;load AX with data from v_1
mov AX,v_1
;load AX with data from v_1
mov AX,[v_1]
;move AX to memory location v_1
mov v_1,AX
;move 55 to memory location v_1
mov v_1,55
There are special instructions to move data from one memory location to another memory location. These are the string move instructions: MOVSB is used for byte movements and MOVSW is used for word movements. These instructions can be used with or without the REP (repeat) instruction. If used with the REP instruction, the direction flag bit must be set along with the count in register CX before executing the REP. If CX is set to zero at the start of a repeat, then it will loop 65536 times. The string move instructions must use DS:SI as the source memory location and ES:DI as the destination memory location. After each byte or word move with a repeat instruction, the SI and DI registers are altered to index the next location.
cld ;clear direction bit for backward
std ;set direction bit for forward
;setup index pointers and segments
mov es,destination_segment
lea si,source_string
lea di,destination_string
;move string byte from SI to DI
movsb
;move string word from SI to DI
movsw ;move string word from SI to DI, dec CX if
;set up counter for repeat
mov cx,10
;move 10 words from SI to DI
rep movsb
The IN and OUT instructions are used by the 80X86 to address peripheral devices such as interrupt controllers, video controllers, communications ports, etc. When addressing an I/O port, the DX register is commonly used as the port address index. If an eight bit port number (0 - 255) is used, it can be addressed directly. The basic 8086 can only IN and OUT eight bits of data at a time and the AL register is always either the source or destination. Other processors in the 80X86 line may use 16 bits at a time. The following is an example of I/O port addressing.
IN al,20H
OUT 20H,al
;For all 16 bit port addresses,
; the DX register must be used as the index.
mov dx 3BDH
in al,dx
mov al,40H
out dx,al
PUSH and POP are used to save the contents of registers into a temporary stacking area for recall at a later time. This works like a LIFO (Last In First Out) structure which allows for systematic nesting of data variables. This can be important to routines that must not destroy the original contents of the registers during their execution and also for routines that need to be reentrant. Hardware interrupting routines are an example of this.
Many high level languages use stack frame structures to store data. To address data in these stack frame structures, the BP register can be used because it is the natural stack data index. The following code displays how this may be done.
Start proc near
push bp
mov bp,sp
;to access the last word pushed on stack before a near call
; was made to this procedure use offset of 4 to BP
mov ax,bp+4
Start proc far
push bp
mov bp,sp
;to access the last word pushed on stack before a far call
; was made to this procedure use offset of 6 to BP
mov ax,bp+6
Start proc near
push bp
mov bp,sp
sub sp,8
The subtract instruction opens up four words of data space for the procedure to use as temporary variable space. The data can be addressed by using the offsets of BP-2 to BP-8. On exit from these routines, you must remember to reset the stack pointer back before you can execute the return instruction correctly.
The exchange instruction is used as a quick way to swap data between registers or registers and memory.
XCHG ax,bx
XCHG bx,data
The translate instruction (XLAT) is used to translate eight-bit data codes. You can translate EBCDIC to ASCII with the correct translation table set up. The BX register is used to index the base address of a 256-byte data block. The AL register is added to BX to get a byte address of the data to load into the AL register.
The load effective address instruction is used to get the address of a data location or an execution routine. This is very useful when you want to get the address of some data variable to pass to another routine for processing.
variable_string db This is a variable data string,0
LEA si,variable_string
| Table of Contents |