what is the difference between ESP register and SS register?

user188276 picture user188276 · Dec 27, 2011 · Viewed 7.6k times · Source

I'm just a beginner in Assembly language. As I know, ESP and SS both refer to stack registers but not quite understand the differences between them.

Answer

Mike Nakis picture Mike Nakis · Dec 27, 2011

The ESP register is the 32-bit version of the 16-bit SP register, but in the 32-bit architecture, SS is irrelevant. So, let's talk about 16-bit first. A note about 32-bit is at the end of the post.

In the 16-bit Intel x86 architecture:

  • SS is the stack-segment register. It identifies the block of memory that will be used for the stack.

  • SP is the stack pointer register. It points to the precise location within the stack segment which is at any given moment the 'top' of the stack.


The 16-bit Intel architecture had a clunky mechanism for implementing 20-bit wide addresses by means of 16-bit 'segments' plus 16-bit 'offsets', so the SS register would point to the stack segment, and the SP register would hold the actual offset into the stack. We would say that the current stack location was at SS:SP.

Naturally, you might wonder how come they were only able to have 20-bit wide addresses instead of 32-bit wide addresses, given that the segment register was 16-bit wide, and the offset register was another 16-bits wide. Well, this is part of why the architecture was clunky: the actual address represented by the SS:SP pair was not calculated as (SS << 16) + SP, instead it was (SS << 4) + SP. This means that the segments had a very high degree of overlap: even though each segment was 65536 bytes long, its start was only 16 bytes away from the start of the previous segment. So, the segment:offset address 0:0 represented absolute address 0, while the 1:0 address represented absolute address 16. (Apparently they did not believe that anyone would ever need to address more than 20 bits of address space.)


32-bit

In the 32-bit architecture, none of that matters, because the ESP register is large enough to be capable of addressing the entire 32-bit memory address space by itself, with no need for any segment register. So if you are using the ESP register you don't have to worry about the SS register at all.