1 / 17

PUSH and POP Instruction

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

vivi
Télécharger la présentation

PUSH and POP Instruction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PUSH and POP Instruction

  2. PUSH ( add element to top) • The push instruction adds a new element to the top. The syntax isPUSH source  ; no destination defined because destination is always TOP • A few things to notice is that the PUSH operation works as follows • A copy of the source content is copied to the address specified by the TOP      • SP or TOP is decreased

  3. 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.

  4. 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).

  5. 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.

  6. Before memory After memory Address ? 02FFB ? 02FFB 02FFC ? ? 02FFC 02FFD ? ? 02FFD SP 34 ? 02FFE 02FFE 56 02FFF ? 02FFF 03000 ? ? SP 03000 AX 56 34 AX 56 34 Execution of PUSH AX

  7. POP ( remove element to top) • The pop instruction removes the top element. The syntax isPOP destination  ; no source defined because source is always TOP. • A few things to notice is that the PUSH operation works as follows • SP or TOP isincreased    • The content of the TOP is copied to the destination      

  8. 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

  9. 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.

  10. After memory Before memory Address Address 03FFF 10 10 03FFB SP 20 20 04000 04000 40 04001 40 04001 04002 50 SP 50 04002 BX ? ? BX 40 20 Execution of Pop BX

  11. 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 registers push ecx push ebx mov esi,OFFSET dwordVal ; starting OFFSET mov ecx,LENGTHOF dwordVal ; number of units mov ebx,TYPE dwordVal ; size of a doubleword call DumpMem ; display memory pop ebx ; opposite order pop ecx pop esi

  12. 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

  13. Cont.., WHILE:        CMP AL, 0DH        JE END_WHILE        PUSH AX        INC CX        INT 21H     JMP WHILEEND_WHILE:        MOV AH, 2        MOV DL, 0DH        INT 21H        MOV DL, 0AH        INT 21H    JCXZ EXIT TOP:      POP DX     INT  21H     LOOP TOPEXIT:      MOV  AH, 4CH      INT 21HMAIN  ENDPEND MAIN

  14. 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

  15. The End

More Related