17
PUSH and POP Instruction

PUSH and POP Instruction

  • Upload
    vivi

  • View
    50

  • Download
    8

Embed Size (px)

DESCRIPTION

PUSH and POP Instruction. PUSH ( add element to top). The push instruction adds a new element to the top. The syntax is PUSH source  ; no destination defined because destination is always TOP A few things to notice is that the PUSH operation works as follows - PowerPoint PPT Presentation

Citation preview

Page 1: PUSH and POP Instruction

PUSH and POP Instruction

Page 2: PUSH and POP Instruction

PUSH ( add element to top)

• The push instruction adds a new element to the top. The syntax is

PUSH source  ; no destination defined because destination is always TOP

• A few things to notice is that the PUSH operation works as follows

1. A copy of the source content is copied to the address specified by the TOP     

2. SP or TOP is decreased

Page 3: PUSH and POP Instruction

PUSH Operation (1 of 2)

• A 32-bit push operation decrements the stack pointer by 4 and

copies a value into the location pointed to by the stack pointer.

Page 4: PUSH and POP Instruction

PUSH Operation (2 of 2)

• This is the same stack, after pushing two more integers:

• The stack grows downward. The area below ESP is always available

(unless the stack has overflowed).

Page 5: PUSH and POP Instruction
Page 6: PUSH and POP Instruction

Example• PUSH AX

• When this instruction is executed the content of the AX register is pushed into the top of the

stack. The stack pointer(SP) is decremented by two. So the SP points to the top of the stack.

• The AX register is the combination of AH and AL registers.

• After performing PUSH operation the stack contents are as follows.

• (SP) - 1 AH

• (SP) – 2 AL

• SP (SP) - 2

• Initially SP points to the address location 03000. Reg AX holds the value 5634H.

Page 7: PUSH and POP Instruction

?

?

?

?

?

?

?

?

?

34

56

?

Before memory After memory

Address

02FFE

02FFC

02FFD

02FFB

02FFF

SP03000

02FFB

02FFC

02FFD

SP02FFE

02FFF03000

56 34 56 34AX AX

Execution of PUSH AX

Page 8: PUSH and POP Instruction

POP ( remove element to top)

• The pop instruction removes the top element. The syntax is

POP destination  ; no source defined because source

is always TOP.

• A few things to notice is that the PUSH operation works as

follows

1. SP or TOP is increased    

2. The content of the TOP is copied to the destination      

Page 9: PUSH and POP Instruction

POP Operation

• Copies value at stack[ESP] into a register or variable.

• Adds n to ESP, where n is either 2 or 4.

– depends on the attribute of the operand receiving the data

Page 10: PUSH and POP Instruction
Page 11: PUSH and POP Instruction

Example

• POP BX

• When this instruction is executed the content of the stack top is

retrieved and stored in the BX register. The operations are,

• BL (SP)

• BH (SP) + 1

• SP (SP) +2

• After performing pop operation the contents of BX register are as

follows.

Page 12: PUSH and POP Instruction

Before memory After memory

Execution of Pop BX

Address

10

20

40

50

40 20BX

10

20

40

50

? ?BX

Address03FFF

03FFBSP

SP

04000 04000

04001

04002

04001

04002

Page 13: PUSH and POP Instruction

Using PUSH and POP

• Save and restore registers when they contain important values. Note that

the PUSH and POP instructions are in the opposite order:

push esi ; push registerspush ecxpush ebx

mov esi,OFFSET dwordVal ; starting OFFSETmov ecx,LENGTHOF dwordVal ; number of unitsmov ebx,TYPE dwordVal ; size of a doublewordcall DumpMem ; display memory

pop ebx ; opposite orderpop ecxpop esi

push esi ; push registerspush ecxpush ebx

mov esi,OFFSET dwordVal ; starting OFFSETmov ecx,LENGTHOF dwordVal ; number of unitsmov ebx,TYPE dwordVal ; size of a doublewordcall DumpMem ; display memory

pop ebx ; opposite orderpop ecxpop esi

Page 14: PUSH and POP Instruction

Example ( Reversing a line of text )

• The program shall take one character at a time and PUSH it to the stack until its a

carriage return. When "Enter" is pressed the characters are POPed out and printed.

• CODE:

.MODEL SMALL

.STACK 100H

.CODE

 MAIN PROC

        MOV AH, 2

        MOV DL, ‘?’

        INT 21H

 XOR CX, CX

        MOV AH, 1

        INT 21H

Page 15: PUSH and POP Instruction

Cont..,

WHILE:

        CMP AL, 0DH

        JE END_WHILE

        PUSH AX

        INC CX

        INT 21H

     JMP WHILE

END_WHILE:

        MOV AH, 2

        MOV DL, 0DH

        INT 21H

        MOV DL, 0AH

        INT 21H

    JCXZ EXIT

TOP:

     POP DX

     INT  21H

     LOOP TOP

EXIT:

      MOV  AH, 4CH

      INT 21H

MAIN  ENDP

END MAIN

Page 16: PUSH and POP Instruction

External Links

• http://programminbasics.blogspot.com/2010/01/assembly-language-part-8.html

• http://www.slidefinder.net/a/assembly_language_intel_based_computers/11087109

• http://teaching.idallen.com/dat2343/01f/assembler_programming.htm

Page 17: PUSH and POP Instruction

The End