You are on page 1of 14

COMP-261 Computer Organization

and Assembly Language

Lectur
Stack Operations and String Data Transfer Instruction
Dr Hashim Al

Fall - 2021

Department of IT and Computer Scienc


Pak-Austria Fachhochschule: Institute of Applied Sciences
and Technology
1
e

PUSH Instructions

• PUSH instruction always transfers two byte of data to the stack

• 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

• The value for SP that is pushed onto


the stack is whatever it was before the
PUSHA in the stack

• PUSH
- PUSHF (Push Flag) instruction
copies the contents of the ag
register to the stack.

3
I

fl
POP Instructions

• POP instruction performs the inverse operation of a PUSH instruction


• The POP instruction removes data from stack and places it into the
target 16-bit register, segment register or, 16-bit memory location
• The least signi cant byte of data is removed from SP and most
signi cant byte is removed from stack segment memory location
addressed by SP+1.

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

b) Final value of SS = 2000H and SP = current SP – no. of PUSH*2 + no. of POP*2 =


2009-5*2+3*2 = 2005H
Instructions Operation
MOV AX, 7645H AX = 7645H
MOV BX, 4477H BX = 4477H
MOV CX, 8899H CX = 8899H c) AX = 4477
BX = 4477H
MOV DX, BX DX = 4477H
CX = FFCD
PUSH DX SP = 2007H DX = 1000
PUSH AX SP = 2005H F = FFCDH
PUSH BX SP = 2003H
PUSHF SP = 2001H
POP CX CX = FFCDH, SP = 2003H
PUSH 1000H SP = 2001H
POP DX DX = 1000H, SP = 2003H
POP AX AX = 4477H
7
H

String Data Transfer

Before discussing string data transfer instructions we should know the


followings:

• 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
:

LODS / LODSB / LODSW — String Data


Transfer
• The LODS (Load String) instruction loads AL or AX with
data stored at the data segment offset address index by the SI
register. (i.e. It loads contents of memory pointed by DS:[SI]
into AL or AX.

• After loading AL with a byte or, AX with a word, the content


of SI increment, if D=0; or decrement if D=1

• The LODSB (loads a Byte) instruction causes a byte to be


loaded into AL

• The LODSW (loads a Word) instruction causes a word to be


loaded into AX.

9
)

Example

• LODSB ; AL=DS: [SI]


; SI=SI+1 (if D=0) or
SI=SI-1 (if D=1

• LODSW ; AX=DS:
[SI]; SI=SI+2 (if D=0)
or, SI=SI-2 (if D=1)

10
)

STOS / STOSB / STOSW — String Data


Transfer
• The STOS (Store string) instruction stores AL or, AX at the extra
segment memory location addressed by the DI register. (i.e.
The content of AL or AX stored to memory pointed by ES:[DI]

• The STOSB (stores a byte) instruction stores the byte in AL at


the extra segment memory location addressed by DI

• The STOSW (stores a word) instruction stores AX in the extra


segment memory location addressed by DI

• 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:

Some assembly language instructions with a portion of memory location of Data


Segment and Extra Segment are given below. Find the content of AX, BX, CX, DX,
SI and DI after execution of instructions.

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

You might also like