Friday, December 25, 2009

Assembly Language Part 8

Stack operations in Assembly

A stack is a one dimensional data structure. The elements of the stack is added and removed from one end. That is why it is also known as
Last-In-First-Out or LIFO system. That end of the stack is called TOP.

You can have the Idea by watching the stack of papers to the right. It is only possible to add or remove new paper holders from the TOP.

The add operation on a stack is called push and the remove operation on the stack is called pop.

As we mentioned earlier the operations are only done at the top.



Stack Operations Push and Pop

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





                                             Push                          Pop

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                                     


The PUSH and the POP instructions take 16 bit registers only. So
PUSH DL
and
PUSH 3
are both illegal

PUSHF ( a variation of PUSH)
There are no operand of PUSHF. It just copies the FLAGS register to the stack.

POPF ( a variation of POP)
There are no operand of POPF either , it does the reverse of PUSHF. Executing it restores the FLAGS register from the stack TOP.

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


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

No comments:

Post a Comment

- - Important Notice - -





Programming google group is up. If you want to join request personally to any of the managers!




Message from DIUCSP




This blog is open for all students of CS, CSE, CIS of Daffodil International University.


Join This blog to stay on touch with other students. If you have a gmail account then just sign in as the g-mail user and become a follower of the blog. You can then submit comments on our posts.

You will find information on different subjects of our course, discuss problems and have solution. Try to maintain the subjectwise discussion for early response. And remember this blog is not only for the genius or more than average CS, CSE, CIS students with great computer skills so dont feel shy in submitting your simple daily life problems (even the silliest ones) because I believe if you feel shy in learning now then you will feel shy all your life for not learning it at all.


Please be conscious about sharing your personal Data, DaffodilCSE blog moderator is not responsible for any risk you take, If you feel safe post comment as your own will, If you do not want to share your mail, comment anonymously. Thanks




They are with us

There was an error in this gadget

BlogCatalog

Earn from a cool looking gadget..! It works with Google adsense and any other advertising programs!
Grab now !