Professional Documents
Culture Documents
COMP-261 Computer Organization and Assembly Language
COMP-261 Computer Organization and Assembly Language
Lectur
Stack Operations and String Data Transfer Instruction
Dr Hashim Al
Fall - 2021
PUSH Instructions
• The source of data may be any internal 16-bit register, immediate data, any segment
register or, any two bytes of memory data
• Whenever data are pushed onto the stack, the rst (most-signi cant) data byte moves
to the stack segment memory location addressed by SP-1. The second (least
signi cant) data byte moves into the stack segment memory location addressed by
SP-2.
2
fi
.
fi
fi
.
• PUSH
- PUSHA (Push All) instruction
copies the registers to the stack in
the following order:
‣ AX, CX, DX, BX, SP, BP, SI and
D
• PUSH
- PUSHF (Push Flag) instruction
copies the contents of the ag
register to the stack.
3
I
fl
POP Instructions
4
fi
fi
.
• POP
- POPA (Pop All) instruction removes 16 bytes of
data from stack and places them into the
following registers, in the order:
‣ DI, SI, BP, SP, BX, DX, CX and A
• POP
- The POPF (Pop Flag) instruction removes 2-byte
number from the stack and places it into the ag
register.
5
A
fl
Problem — PUSH/POP
• An assembly language program is given below, where assume that, SS=2000H and SP=2009H; Flag register,
F=FFCDH
MOV AX, 7645
MOV BX, 4477
MOV CX, 8899
MOV DX, B
PUSH D
PUSH A
PUSH B
PUSH
POP C
PUSH 1000
POP D
POP AX
• Find Out
a) The physical address
b) The nal value of SS and SP after the end of program
c) The value of AX, BX, CX, DX and Flag register F after the end of program
d) Draw the memory map in details.
6
fi
F
Solution — PUSH/POP
a) Physical Address = SS*10+SP = 2000*10+2009 = 22009
• Direction ag
- Direction ag selects auto-increment (D=0) or the auto-decrement
(D=1) operation for the DI and SI registers during string
operations
- CLD clears D ag (D=0) ; i.e. CLD selects auto-increment mode
- STD set D ag (D=1); i.e. STD selects auto-decrement mode
• DI and SI:
- DI offset address accesses data in the extra segment for all string
instructions
- SI offset address accesses data, by default, in the data segment.
8
fl
fl
fl
.
fl
:
9
)
Example
• LODSW ; AX=DS:
[SI]; SI=SI+2 (if D=0)
or, SI=SI-2 (if D=1)
10
)
• Example
- STOSB ; ES:[DI]=AL ; DI=DI+1 (if D=0), DI=DI-1 (if D=1
- STOSW ; ES:[DI]=AX ; DI=DI+2 (if D=0), DI=DI-2 (if D=1)
11
:
XCHG
• XCHG
- XCHG (exchange) instruction exchanges the contents of
a register with the content of any other register or
memory location
- The XCHG instruction can not exchange segment
register or, memory to memory data
- Example
‣ XCHG AX, BX ; Exchanges the content of AX with B
‣ XCHG [DI], AL ; Exchanges the content of memory
location [DI] with AL
12
Problem:
13
Solution:
Instruction AX BX CX DX SI DI D-FLAG
MOV CX, 34A1H — — 34AIH — — —
MOV BX, A202H — A202H 34AIH — — —
MOV SI, 2005H — A202H 34AIH — 2005H —
MOV DI, 2007H — A202H 34AIH — 2005H 2007H
STD — A202H 34AIH — 2005H 2007H D=1
LODSW 2003H A202H 34AIH — 2003H 2007H
XCHG AX, BX A202H 2003H 34AIH — 2003H 2007H
CLD A202H 2003H 34AIH — 2003H 2007H D=0
STOSB A202H 2003H 34AIH — 2003H 2008H
MOV CH, AL A202H 2003H 02A1H — 2003H 2008H
STD A202H 2003H 02A1H — 2003H 2008H D=1
LODSB A207H 2003H 02A1H — 2002H 2008H
MOV CL, AL A207H 2003H 0207H — 2002H 2008H
MOV DX, AX A207H 2003H 0207H A207H 2002H 2008H
STOSW A207H 2003H 0207H A207H 2002H 2006H
D = 0 (Auto Increment)
D = 1 (Auto Decrement)
14