What is the diff between CS and DS

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
codemastersnake

What is the diff between CS and DS

Post by codemastersnake »

Hello every one!

Can ne one tell me the diff between code segment and data segment a-z....

also is there any stack in real mode environment
AR

Re:What is the diff between CS and DS

Post by AR »

CS stands for Code Segment, it is the segment that the code is executed from. DS is the Data Segment and is where the CPU will attempt to read memory accesses from. This allows you to confuse yourself by having CS and DS at different locations, eg.

Code: Select all

.org 0x5000
ljmp 0, $Start  # Set CS=0, IP=0x5005
Start:
mov $0x0700, %ax
mov %ax, %ds # Set DS=0x700

mov (0x8), %ax # Access memory implicitly through DS, meaning at 0x7008
The stack in realmode is in the SS segment, stack pointer is SP.
codemastersnake

Re:What is the diff between CS and DS

Post by codemastersnake »

OK then how much amount of stack I can asign in rm....

can I do something like this:

--------

unsigned char _STACK[1024]; //1KB... can I asign any value...
asm mov sp, [_STACK];

------
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:What is the diff between CS and DS

Post by Solar »

A segment in Real Mode is limited to 64k, so that's the maximum size of your stack segment.
Every good solution is obvious once you've found it.
Visitor..

Re:What is the diff between CS and DS

Post by Visitor.. »

Codemaster Snake wrote: --------

unsigned char _STACK[1024]; //1KB... can I asign any value...
asm mov sp, [_STACK];

------
The stack grows downwards so you'd better do it like this:

Code: Select all

const size_t STACK_SIZE = 1024;
unsigned char stack[STACK_SIZE];
asm mov sp, [stack+STACK_SIZE]
AR

Re:What is the diff between CS and DS

Post by AR »

Or more accurately:

Code: Select all

mov sp, stack+STACK_SIZE
That is unless you want to set SP to zero/garbage.
codemastersnake

Re:What is the diff between CS and DS

Post by codemastersnake »

what about ss si and other stack regs
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:What is the diff between CS and DS

Post by Solar »

Codemaster Snake wrote: what about ss si and other stack regs
Sounds like you should give the Intel manuals another go. ;)

SS is a segment register, i.e. it should point at the segment base address (with the SP being interpreted as offset from SS). The only other "stack register" I know of is BP, which is by default considered to be based on SS too.

SI is the string (source) index, and is interpreted as relative to the DS segment register. It's counterpart is DI (destination index, usually interpreted as relative to the ES segment register.
Every good solution is obvious once you've found it.
Post Reply