About me
Home
Resumé
CV
Pictures

Course Work
CSE300-FA98

Teaching
Portfolio
CSE240-SP99
CSE207-FA03

Misc
eHarmony
Documents

Tools

Additional Instructions

As you start working with the stack at some point you will unbalance it and have "interesting" behavior from your program. To fix these types of bugs, you will most likely need to draw a diagram of what is on your stack. To do this correctly, you need to know how a few instructions will affect your program.

Near calls push one items onto the stack - specifically IP. Far calls push two items onto the stack, CS and IP. Int pushes three items onto the stack which are flags, CS, and IP.


    call Near   call FAR     Int n
    |      |    |      |    |      |
    |------|    |------|    |------|
    |      |    |      |    |      |
    |------|    |------|    |------|
    |      |    |      |    |      |
    |------|    |------|    |------|
    |      |    |      |    |  IP  |
    |------|    |------|    |------|
    |      |    |  IP  |    |  CS  |
    |------|    |------|    |------|
    |  IP  |    |  CS  |    | flags|
     ------      ------      ------ 

The compliment of these instructions is ret, retf, and iret. Ret and Retf do not need to be specified since the assembler knows if you have declared your procedure as NEAR or FAR. Using ret, it will pick the correct form for you, but it is a good idea to be explicit in your coding to avoid confusion. Iret, you must always specify when you want that functionality.

Now lets see what happens when we unbalance the stack. Say we have the following program...

    myProc PROC NEAR
       push AX
       ret
    ENDP

    call myProc

     after       after        after
   call myProc  push AX        ret
    |      |    |      |    |      |
    |------|    |------|    |------|
    |      |    |      |    |      |
    |------|    |------|    |------|
    |      |    |      |    |      |
    |------| => |------| => |------|
    |      |    |      |    |      |
    |------|    |------|    |------|
    |      |    |  AX  |    |      |
    |------|    |------|    |------|
    |  IP  |    |  IP  |    |  IP  |
     ------      ------      ------ 

Where did this code return to? It returned to CS:AX not CS:IP like we would have wanted it to. This is the typical looking problem when the stack becomes unbalanced - be careful, and draw pictures. Remember that the stack has to be balanced for every branch in your program, see common errors for an example of this.

Last Modified: January 26, 1999 - Barry E. Mapen