views:

403

answers:

5

Hello everyone! I am learning x86 assembly language, and I understand the purpose and usage of segments. Segments hold vital data, and can also be used to store extra data (ie. Memory Segmentation Model). Here is my question though. If segments can be used to store extra data, how can I make sure that my storing data in them won't overwrite any existing data?

For example, the CS register points to the Code Segment. The Code Segment contains the program's code. If I used the CS register with an offset to store some data, how would I know where to put my data so as not to overwrite the code that it is storing?

Please let me know. I using Intel syntax assembly and assembling with NASM.

Thanks

A: 

Hi,

Are you talking about segment overlapping?

Regards.

ATorras
+2  A: 

Segments never store any data. The segment registers are just "base" address pointers which are used to create 20-bit pointers using only 16-bit registers. For example:

MOV DS, 0001
MOV DI, 0013
MOV AL, DS:[DI]  ' this reads from address x00023 in memory

MOV DS, 0002
MOV DI, 0003
MOV AL, DS:[DI]  ' this too reads from address x00023 in memory

MOV DS, 0000
MOV DI, 0023
MOV AL, DS:[DI]  ' this too reads from address x00023 in memory

As for your question how to make sure that you don't overwrite code with data: it's entirely up to you make sure that you know exactly where in memory you store your code and data!

danbystrom
Ok thanks for that clarification. But how can I know where in memory my code and data lies?
QAH
I'm sorry, but I realise as I type this that a full explanation will be way way toooo loooong... :-( All I can say: keep studying. You'll probaby have a great experience digging into this concept!
danbystrom
By the way, there is no instruction for moving an immediate value to a segment register. Instead of MOV DS, 1you'll need to do go through another register, like MOV AX, 1 MOV DS, AXor the stack, like PUSH 1 POP DS
I. J. Kennedy
I stand corrected. :-)
danbystrom
A: 

It's assembly, so you are responsible for everything. To keep your code and data separate is just as easy as keeping two numbers separate. Just use each memory location at most once.

MSalters
A: 
PhiS
So when you assemble software, is ES filled with anything or does that stay empty for our messing around with?
QAH
@QAH: It's your own responsibility to set up the segment registers to point to anything useful. I don't know about NASM and it's a long time since I did 16-bit MASM assembly. IIRC, MASM had the "ASSUME" directive which told the assembler to set up the segment registers for you.
PhiS
A: 

As they already mentioned, segment register hold only a 16 bit pointer. This pointer is internally multiplied by 16 so that CPU can address 20-bit big address memory space.

1) If you have enough memory than you can choose 64Kb of RAM for stack, 64Kb for data memory and rest for code memory. Let say that SS (stack segment register) is 0x0400 and DS (data segment register) is 0x0800 and CS (code segment register) is 0x1B00. In this case your code can't override any other memory segment. If you need another 64K of data memory than you can simple extend it with use of ES segment and ES prefix.

2) If you don't have enough memory space (compact program) than you must predict the memory boundaries.

3) If your program use external calls with memory pointers than you must check boundaries. For this purpose in x86 mnemonic exist BOUND instruction.

GJ