Push & Pop in Assembly :: Stack

include irvine32.inc

.data

msg3 byte "Push character one by one in stack and moves ESP(32-bit-stack-pointer-register) downward:",0
msg byte "              Push: ",0
msg4 byte "Pop character one by one from stack and moves ESP(32-bit-stack-pointer-register) upward:",0
msg1 byte "              Pop: ",0
arr byte "usman siddique",0
asiz = ($-arr)-1
msg2 byte "---------------------------------------------------------------",0
msg5 byte "After perfoming the pop operation the name will print in reverse order.Now think about it, why it happens",0
msg6 byte "                      Name:  ",0
msg7 byte ".......................~~THE END~~.............................",0

.code
main proc
call clrscr
mov edx,offset msg3
call writestring
call crlf
mov ecx,asiz
mov esi,0

L1:
mov edx,offset msg
call writestring
movzx eax,arr[esi]
push eax ;PUSH:  In runtime stack ESP moves in downward direction instead of moving upward
                            ;but it does not matter what the  direction is,because stack follows LIFO structure in both(downward/upward) cases.
call writechar
call delay
call delay
call delay
call delay
call crlf
inc esi
loop L1

call crlf
mov edx,offset msg2
call writestring
call crlf
mov edx,offset msg4
call writestring
call crlf
mov ecx,asiz
mov esi,0
L2:
mov edx,offset msg1
call writestring
pop eax        ;POP:   In runtime stack ESP moves in upward direction instead of moving downward
                            ;but it does not matter what the direction is,because stack follows LIFO structure in both(downward/upward) cases.
mov arr[esi],al
call delay
call delay
call delay
call delay

call writechar
call crlf
inc esi
loop L2
call crlf
mov edx,offset msg5
call writestring
call crlf
call crlf
call crlf
call waitmsg ;show a msg: Press [Enter] to continue..
mov edx,offset msg6
call writestring
mov edx,offset arr
call writestring
call crlf
call crlf
call crlf
mov edx,offset msg7
call writestring
call crlf
call crlf
exit
main endp
end main

No comments

Powered by Blogger.