
Instruction Set
4-247
XCHG
XCHG
This example performs a bubble sort on a list of unsigned numbers in memory. The
microcontroller rearranges the list from smallest to largest.
Tips
To exchange two components that are both stored in memory, use MOV to copy the first
component to a register, use XCHG to exchange the register with the second component,
and then use MOV again to copy the register to the first component.
X
CHG requires both operands to be the same size. To convert an 8-bit integer to its 16-
bit equivalent, use CBW. To convert a 16-bit integer to its 32-bit equivalent, use CWD. To
convert another type of component to its extended equivalent, use MOV to copy 0 to the
high byte or word.
You cannot use XCHG to exchange a word with a segment register. To copy a segment
address to a segment register, use MOV to copy the segment address to a general register,
and then use MOV to copy the value in the general register to the segment register.
Related Instructions
If you want to
See
Copy a component to a register or a location in memory
MOV
LIST
DB
3,5,2,9,7
L_LENGTH
EQU
5
; sort unsigned numbers
MOV
SI,0
; set up list index
MOV
DX,L_LENGTH - 1
; get length of list
MOV
CX,DX
; set up counter
SORT:
MOV
AL,LIST[SI]
; copy this number
CMP
AL,LIST[SI]+1
; is this number <= next number?
JLE
NEXT
; if so, then jump
XCHG
AL,LIST[SI]+1
; write larger number to next byte
MOV
LIST[SI],AL
; write smaller number to this byte
NEXT:
INC
SI
; point to next number
LOOP
SORT
; while CX is not zero, jump
; to top of loop
DEC
DX
; set up length of sublist
MOV
SI,0
; reset sublist index
MOV
CX,DX
; set up sublist counter
LOOP
SORT
; while CX is not zero, jump
; to top of loop
Summary of Contents for Am186 Series
Page 1: ...Am186 and Am188 Family Instruction Set Manual February 1997...
Page 10: ...Table of Contents x...
Page 18: ...Programming 1 8...
Page 40: ...Instruction Set Listing 3 14...