Page 1 of 1

LSS and sp

Posted: Wed Jul 26, 2006 6:48 am
by joke
I've just received the intel hardcopies. But I still have a real mode problem
Can anybody please help me with the LSS instruction. Suppose a real mode stack segment A (A=hexademical)

If I do mov ss,A
will the sp register automatically point to the top of ss? If not, then how can I load sp with the correct values? The intel manuals mention the LSS instruction, but how do we use it?



Thanks[/quote]

Posted: Wed Jul 26, 2006 9:03 am
by JAAman
If I do mov ss,A
you cant -- there is no mov segment, immediate
will the sp register automatically point to the top of ss? If not, then how can I load sp with the correct values?
no, SP is not altered unless you write to it, and all your writing to is SS

normally, this is done like this:
mov ax, stackSegment
mov ss,ax
mov sp,TopOfStack

the CPU will automatically block interupts for 1 instruction whenever the SS register is modified, insuring that there should never be an invalid stack, however, many people prefer to CLI first anyway, just to be certain
The intel manuals mention the LSS instruction, but how do we use it?
im not sure, never really used the LxS instructions, but it looks simple enough

create a pointer in memory (16bit selector, followed by a 16bit offset, i think), then use LSS SP, PointerToPointer

Posted: Wed Jul 26, 2006 9:52 am
by joke
i'm new to asm so i don't know how to use pointers in asm

how can i make sp to point at the top of my stack?
mov ax,ss
mov sp,ax ? is this correct? :?:

thanks

Posted: Wed Jul 26, 2006 6:58 pm
by SpooK
joke wrote:i'm new to asm so i don't know how to use pointers in asm

how can i make sp to point at the top of my stack?
mov ax,ss
mov sp,ax ? is this correct? :?:

thanks
No, using the value of SS to fill in the value for SP has no bearing... and will probably result in crashes.

The use of SS:SP combine to make the entire stack address in real mode. SS:ESP combine to make the entire stack address in protected mode (32-bit).

What you need to do, is define what the "top of the stack" is for your OS. Most people initially set it to memory locations like 000A:0000 (top of usable conventional memory) or 0000:7C00 (right under the address the MBR is loaded to). Just remember to adhere to memory segmentation while in real mode... this means that 0000:7C00 could also be 07C0:0000... and 000A:0000 would actually be A000:0000.

From there, while in Ring 0 with no multi-tasking, you should not have to change SP at all... push/pop and other stack-sensative instructions work automatically. Multi-tasking, context switching and implementing API calls all deviate from this "simplicity", but take one thing at a time ;)