News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

A question on stack

Started by wincry, August 27, 2008, 05:20:27 PM

Previous topic - Next topic

wincry

Hi Everybody,

  I am completely new to assembly programming, 
  i have a question on stack...... plz help me out.
     
     question :
        
      Why  when a stack segment (ss) is setup it points to the base of the
        segment ie. at 0 and sp points to the opposite end of the stack segment ie.
         at the higher memory space.   

Neil

The Stack Segment (SS) is the 64k segment in which the stack resides, the stack pointer (SP) points at the top of the stack which grows downwards in memory, which is why it points at the highest address within the SS.

wincry


Neil I understand that,
 
   but i like to know is there will be any problem if ss points to the higher memory space
   and sp points to the lower memory space and let sp grow at the higher memory space.

Neil

I don't quite understand your question, but I'll try. Where the stack segment resides in memory is decided by the assembler you have no control over this, all you have control over is the offset within the segment & if you start changing the stack pointer then your program will crash. It's best to leave things as they are.

MichaelW

The programmer can control where the stack segment is within the program memory, but the OS (at least normally) controls where in system memory the program memory will be. In real mode the value in SS is the base segment address of the stack segment, and the segment extends upwards (from lower addresses to higher addresses). By replacing push, pop, call, ret, etc one could effectively change the direction of stack "growth", but considering that the value in SP is interpreted as a positive integer, it could not point to a lower address than SS.
eschew obfuscation

tenkey

SS is a segment register, and like the other segment registers, it sets the lower limit of a 64K memory region. Even though these regions are 64K, they begin at any 16 byte "boundary".

So when you set SS to the value (hex) 1234, your stack region (segment) starts at address (hex) 12340. The stack pointer, SP, as an offset, allows you to read and write data at address (hex) 12340 and any of the (64K - 1) bytes following it.

This is how the processor handles 20-bit addresses using only 16-bit registers.

At the upper and lower limits of the stack region, addresses will "wrap around" because of the 16-bit limit.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8