stack and its usage in assembly language

17

Upload: usman-bin-saad

Post on 22-Jan-2018

722 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Stack and its usage in assembly language
Page 2: Stack and its usage in assembly language

Stacks

Usman Bin Saad FA14-BSE-4C-118

Shahid Iqbal FA14-BSE-4C-160

Haseeb Ahmad FA14-BSE-4C-129

Submitted to

Miss Arubah

Page 3: Stack and its usage in assembly language

Stack

• A stack is a data structure that stores data in such a way that the last piece of data stored, is the first one retrieved i.e. Last in, First out LIFO / First in Last out FILO

• Only access to the stack is the top element

• Anything added to the stack goes on the “top” of the stack

• Anything removed from the stack is taken from the “top” of the stack

• Things are removed in the reverse order from that in which they were inserted

Page 4: Stack and its usage in assembly language
Page 5: Stack and its usage in assembly language

Basic Stack Operations

• There are three Basic Stack Operations which are discussed below

• Push

• Pop

• Stack Top

Page 6: Stack and its usage in assembly language

Push

• The operation to place a new item at the top of the stack

Page 7: Stack and its usage in assembly language

Pop

• The operation to remove the next item from the top of the stack

Page 8: Stack and its usage in assembly language
Page 9: Stack and its usage in assembly language

Implementing a Stack

• At least three different ways to implement a stack• array

• vector

• linked list

Page 10: Stack and its usage in assembly language

Stack in Assembly Language

• The stack segment register holds the starting address of the stack segment in the memory. Majorly SS is used for the following purposes namely:

1.To hold the temporary results.

2.To hold the return address of the subroutine.

3.Finally to handle the interrupts

• SS (Stack Segment) Register: holds stack Segment Address

• SP (Stack Pointer) Register: initialized to the value specified with the .STACK directive, represents empty stack position

• When stack is not empty, SP represents the top of the Stack

Page 11: Stack and its usage in assembly language

Stack (cont ..)

• In order to save the contents of a register during the execution, so that it can be used later for purposes.

• To do so, the microprocessor has a area of memory where the contents of specified registers or memory location can be saved temporarily, and is called STACK.

• It is a top-down data structure whose elements are accessed using the pointer that is implemented using the SP and SS registers.

• As we go on storing the data words onto the stack, the pointer goes on decrementing.

Page 12: Stack and its usage in assembly language

PUSH And PUSHF

• PUSH: Used to add a new source (16-bit register or memory word) on stack

Syntax:

PUSH source

• Execution of PUSH causes:

SP is decreased by 2

A copy of source contents is moved to the address specified by SS:SP.

Source remains unchanged

• PUSHF has no operand and it pushes the contents of FLAG register onto the stack.• Usually this is done whenever the processor is interrupted

Syntax: PUSHF

Page 13: Stack and its usage in assembly language
Page 14: Stack and its usage in assembly language

POP And POPF

• POP: Used to remove top item from stack to destination (i.e. a 16-bit register or memory word).

Syntax:

POP destination

• Execution of POP causes:

The contents of SS:SP (top of the stack) is moved to the destination.

SP is increased by 2

• POPF has no operand and pops the top of the stack into FLAG register.• SP is incremented by 2 after executing this instant.

Syntax: POPF

Page 15: Stack and its usage in assembly language
Page 16: Stack and its usage in assembly language
Page 17: Stack and its usage in assembly language