Pages

Friday 25 January 2013

8086 microprocessor Programming (Part 3)


THE STACK

The stack is a memory buffer used as a holding area for addresses and data. The stack resides in the stack segment. Each 16-bit location on the stack is pointed by the SP register. SP register is called stack pointer register. The stack pointer holds the address of the last data element to be added to or pushed on the stack. The last value added to a stack is the first one to be removed or popped from the stack.

PUSH OPERATION

The SP is decremented before a new value is pushed on the stack. Consider the following push instruction

mov ax, 00a2          ; move 00a2 to ax register
push ax                   ; push ax on the stack

The push instruction does not change the contents of AX; instead it copies the contents of AX on to the stack.

As we push more values stack, the stack grows downward in the memory. Let’s assume that BX and CX registers contain the values 0001 and 0002. The following instructions are used to push them on the stack;

push bx        ; push bx on the stack
push cx        ; push cx on the stack


POP OPERATION

A pop operation removes a value from the stack and places it in a register or a variable. After a value is popped from the stack, the stack pointer is incremented to point to a previous value on the stack. Consider the following instruction;

pop ax       ; pop the top of stack into ax 


After the pop has taken place, AX contains the number that was at the top of the stack.
The popped value is no longer available, and the area of stack below the stack pointer is available for future use.

Stack makes a temporary save area for the registers if we need to preserve their values.
When a subroutine is called, the program saves the return address on the stack, the location of the program to which the sub routine is to return.

  • Assemble a short program using debug that performs the following tasks in order:

Load the data registers with following values
AX= 1240     , BX= 0023       , CX=0003 , DX=0004
Push each register on the stack in following order
AX, BX, CX, DX
Pop each register from the stack in the following order
AX, BX, CX, DX
Trace each instruction and explain why the registers values (AX, BX, CX, DX )   have been changed.

REFERENCES:


1. Kip R. Irvine, Assembly Language for the IBM-PC Macmillan Publishing Company, 1990. 
2. Avtrar Singh and Walter A Triebel, The 8086 and 80286 Microprocessors, Hardware, Software, and Interfacing. Prentice- Hall Inc. 1990.

No comments:

Post a Comment