
Instruction Set
4-182
PUS H
PUS H
Examples
This example copies a string of 16-bit integers in one segment to a string in another
segment. The microcontroller copies the words and changes their sign—one by one, from
first to last—before storing them in the other string. Before setting up the registers for the
string operation, this example exchanges DS for ES in order to address the destination
string using ES.
; defined in SEG_S segment
SOURCE
; defined in SEG_D segment
DESTINATIONDW
DW
16 DUP ()
LENGTH SOURCE DUP ()
; notify assembler: DS and ES specify different segments
ASSUME DS:SEG_D, ES:SEG_S
; set up segment registers with different segments
MOV
AX,SEG_D
MOV
DS,AX
MOV
AX,SEG_S
MOV
ES,AX
; load one segment into DS
; DS points to SEG_D, destination
; load another segment into ES
; ES points to SEG_S, source
; initialize and use source string
...
; exchange DS for ES: the microcontroller does not allow
; you to override the segment register it uses to address
; the destination string (ES)
PUSH
ES
; ES points to SEG_S, source
PUSH
DS
; DS points to SEG_D, destination
POP
ES
; ES points to SEG_D, destination
POP
DS
; DS points to SEG_S, source
; set up registers and flags
LEA
SI,SOURCE
LEA
DI,DESTINATION
MOV
CX,LENGTH SOURCE
CLD
; load source offset (segment = DS)
; load dest. offset (segment = ES)
; set up counter
; process string low to high
LOAD:
; load integers, change their sign, and store them
LODSW
NEG
AX
STOSW
LOOP
LOAD
; copy integer from source to AX
; change sign of integer in AX
; copy integer from AX to dest.
; while CX is not zero,
; jump to top of loop
; exchange DS for ES
PUSH
ES
PUSH
DS
POP
ES
POP
DS
; ES points to SEG_D, destination
; DS points to SEG_S, source
; ES points to SEG_S, source
; DS points to SEG_D, destination