Results 1 to 6 of 6

Thread: Problems visualing the stack in MIPS32![Resolved]

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Problems visualing the stack in MIPS32![Resolved]

    Hello everyone i'm having problems visualing this stack. I'm use to a stack that grows upward, First one in, last one out. But its the opposite in this case, the stack grows downward, not upward.

    We have the following code:

    int leaf_example (int g, int h, int i , int j)
    {

    int f;
    f = (g + h) - (i + j);
    return f;
    }

    WHat is the compiled IMPS asm code?

    THe parameter variables g, h, i , and j correspond to the argument registers $a0, $a1, $a2, and $a3, and f corresponds to $s0. The compiled program starts with the label:

    leaf_example:

    THe book says
    the next step is to save the registers used by the prodedure. Thus, we need to save 3 registers.
    $s0, $t0, and $t1.

    What I don't understand is, why don't u need to safe the arguments as well? like $a0, $a1, $a2, and $a3?

    This is the part that really confuses me though:
    We "push" the old values onto the stack by creating space for three words on the stack and then store them.

    addi $sp, $sp, -12 #adjust stack to make room for 3 items
    sw $t1, 8($sp) #save registers for use afterwards
    sw $t0, 4($sp)
    sw $s0, 0($sp)

    Why did u they add -12 to the stack pointer?
    Why not 12?
    I know each word is 4 bytes, so 4*3 = 12.

    but if the stack grows downward, does that mean this is what the stack looks like after addi $sp,$sp, -12
    [ ] <- $sp location -12
    [ ] -8
    [ ] -4
    [ ] 0

    but they don't use sw $t1, -8($sp) but instead use postive values, sw $t1,8($sp).

    It says in the book,
    Historical precedent, stacks grow from higher addresses to lower addresses. This convetion meanse that you push values onto the stack by subtracting from the stack pointer. Adding to the stack pointer shrinks the stac, thereby popping values off the stack.

    So when they say, you "push" values onto the stack by subtracting from the stack pointer, do thy mean, since the stack point is at location -12, if u add 8, ur really subtracting since -12 is negative. and your adding a postive number to it?




    [ ]
    Last edited by voidflux; Oct 6th, 2006 at 01:15 PM.
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width