Вы находитесь на странице: 1из 2

Stack Tutorial

By Robert Coulier Stack functionality can be implemented in SPARC assembly language. To do so, use is made of a stack pointer. A stack pointer is simply %r14 (register 14) in SPARC but is typically referenced in code by using its alias %sp for stack pointer. It is referred to as a pointer because the register will be used to hold a memory address. STEP 1: Allocate memory for the Stack in the .data section of your assembly code. stack_top: stack_bot: .data .skip 1024 ! allocate memory. Same as .=.+1024

The .skip directive allocates a space of 1024 bytes to the stack. In this example, the beginning address is 2976 and the ending address is 4000. Notice that the stacks memory position is below the positions allocated for your programs code and data. The label stack_top is assigned the address of the memory pointer before the stack is allocated. The label stack_bot is assigned the address of the memory pointer after the stack is allocated. Therefore: stack_top 2976 stack_bot 4000

STEP 2: Set the stack pointer .text set stack_bot, %sp This command sets the stack pointer to the address corresponding to the bottom of the stack (address 4000). IMPORTANT: Always set the stack pointer to the bottom of the stack unless you are trying to implement a top down stack. STEP 3: Implement a PUSH operation on the stack add %r0, 1, %r1 sub %sp, 4, %sp st %r1, [%sp] ! r1 = 1 ! decrement stack pointer by 4 ! store r1 into the location pointed to by sp Note that for the bottom up stack we are implementing, the stack pointer has to be decremented. Registers in the SPARC emulator hold 32 bits (4 bytes). Therefore, in order to PUSH a register value onto the stack, room in the stack must be made to hold a 4 byte value. To do this we decrement the stack pointer by 4. Finally, register 1 is stored on the stack. Note that if the stack pointer were mistakenly initialized to point to stack_top, we would have overwritten a portion of our data section in memory.

For further illustration, we can push a second value onto the stack. add %r0, 2, %r1 sub %sp, 4, %sp st %r1, [%sp] ! r1 = 2 ! decrement stack pointer by 4 ! store r1 into the location pointed to by sp Once again, the stack pointer is decremented and a value stored. Notice that the top of the stack grows in the direction of decreasing memory addresses. A top down stack implementation would grow the stack from stack_top down in the direction of increasing memory addresses.

STEP 3: Implement a POP operation on the stack ld [%sp], %r1 add %sp, 4, %sp ! copy value on top of the stack to register 1 ! increment the stack pointer First load the value from the memory location contained in stack pointer. Next, move the stack pointer so that it points to the memory location of the next value in the stack. This effectively pops the previous value. In actuality, the previous value is still present in memory, we are simply not pointing to it with the stack pointer.

Summary: The key to a successful bottom up stack implementation is to accurately update the position of the stack pointer. Set the stack pointer to the bottom of the stack for a bottom up implementation. Increment and decrement the stack pointer appropriate to the size of the object you are pushing onto or popping from the stack.

Вам также может понравиться