Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DS,CS,SS,ES???

Status
Not open for further replies.

syskplim

Technical User
Nov 17, 2003
46
0
0
MY
ES:0000 always point to the beginning and
SS:SP always point to the end of the program in memory

is this the truth?

as i know, the sequence of the segments is DS,CS,SS,ES?

.


---->ASM fresher need help!<----

With kind regards.
syskplim@streamyx.com
 
Hi syskplim!

DS is called data segment register. It points to the segment of the data used by the running program. You can point this to anywhere you want as long as it contains the desired data. ES is called extra segment register. It is usually used with DI and doing pointers things. The couple DS:SI and ES:DI are commonly used to do string operations. SS is called stack segment register. It points to stack segment.

The register SI and DI are called index registers. These registers are usually used to process arrays or strings. SI is called source index and DI is destination index. As the name follows, SI is always pointed to the source array and DI is always pointed to the destination. This is usually used to move a block of data, such as records (or structures) and arrays. These register is commonly coupled with DS and ES.

The register BP, SP, and IP are called pointer registers. BP is base pointer, SP is stack pointer, and IP is instruction pointer. Usually BP is used for preserving space to use local variables. SP is used to point the current stack. Although SP can be modified easily, you must be cautious. It's because doing the wrong thing with this register could cause your program in ruin. IP denotes the current pointer of the running program. It is always coupled with CS and it is NOT modifiable. So, the couple of CS:IP is a pointer pointing to the current instruction of running program. You can NOT access CS nor IP directly.

Bye
OSProgrammer
 
Basically agree but a few comments:
(1) You can read cs directly like any other segment register. You don't need to set cs directly using the same instructions as you'd set other registers, because instructions such as ret, jsr, jmp are the equivalents of pop, push/mov, and mov, for cs and ip.
(2) Where the segments are is up to your operating system. For instance in old .com applications for dos, the code segment and data segment were the same (cs=ds). If you don't like the current arrangement, you can change it. For instance, a small piece of embedded assembly in a larger high-level language program can be coded as though it were a .com program and bracketed by
push ds
push cs
pop ds
......
pop ds
to set up its own local data segment.
(3) Although es is usually used with di, and ds with si, this is something brought over from the string handling op codes, and there is no particular reason why you shouldn't mix and match these segment registers with any index register according to your needs. The bx register is also frequently used in pointer arithmetic.
(4) From 386 there are also spare segment registers, fs and gs. Unlike es, they need a segment prefix to the op code, so they are slightly slower. You don't need to worry about the prefix; your assembler will code it for you.
(5) Remember that protected world and dos-world are totally different in their attitude to segment registers. In dos 16 bit world the segment registers hold a physical address shifted 4 bits. You can even use them to hold a number that isn't an address at all, and there won't be complaints! In protected mode the segment registers hold an index to a table entry, and if you try to set them to an invalid reference there will be loud complaints from the operating system.
(6) Even in dos-16-bit world it's handy to be aware of scaled indexing (mov eax, ds:[si*4] etc.). It works, and although it loses efficiency in 16bit world, being a 32bit instruction, it can be useful in 386+ systems.
 
Since the stack-segment is always the last segment in an EXE-file, ES:0000 points to the beginning and SS:SP to the end of the program in memory. Through this the length of the program can be calculated
and

Code:
setfree proc near 

mov bx,ss ;first subtract the two segment addresses 
mov ax,es ;from each other. The result is 
sub bx,ax ;number of paragraphs from PSP 
          ;to the beginning of the stack 
mov ax,sp ;since the stackpointer is at the end of 
mov cl,4  ;the stack segment, its content indicates 
shr ax,cl ;the length of the stack 
add bx,ax ;add to current length 
inc bx    ;as precaution add another paragraph 
mov ah,4ah;pass new length to DOS 
int 21h 
ret       ;back to caller 

setfree endp


as the code shown above, seems that ES is at higher location than SS, so SS-ES can get the length of paragraph.
it this always the truth? ES always in lower address? how abt the other? CS and DS locations? (higher location will get lower address)

i get stuck here! :(


.

---->ASM fresher need help!<----

With kind regards.
syskplim@streamyx.com
 
I think this indicates the initial state of the registers when the EXE program is loaded by the Dos Loader.

According to Ray Duncan's Advanced Msdos Programming,

at entry to an EXE program

ds and es address the PSP segment

ss addresses the stack segment, with sp addressing the top of stack.

so sutracting es from ss will give the number of paragraphs in the PSP, code and data segments.

Dividing sp by 16 gives the number of paragraphs in the stack segment.

And adding the number of paragraphs in the stack segment to the number of paragraphs in the data, code, and PSP gives the length of the complete EXE program.

Your code above also gives an extra one paragraph just to make sure that the top of stack doesn't get overwritten by something that gets loaded above this EXE file (since int 21h function 4ah resizes the memory block of this EXE).

hope this helps
rgds
Zeit.
 
Zeit,

may i waste ur time to make me clear? ;-)

1. why?? and what is this in above code?
Code:
mov cl,4  ;the stack segment, its content indicates 
shr ax,cl ;the length of the stack


2. this is my debug code:

Code:
file1.exe
Ax=0000  Bx=0000  Cx=000a  Dx=0000  Sp=0400  Bp=Si=Di=0000
Ds=0b22  Es=0b22  Ss=0b33  Cs=0b32  Ip=0000  Flag Reg

file2.exe
Ax=0000  Bx=0000  Cx=0012  Dx=0000  Sp=0400  Bp=Si=Di=0000
Ds=0b22  Es=0b22  Ss=0b34  Cs=0b32  Ip=0000  Flag Reg

file3.exe
Ax=0000  Bx=0000  Cx=0038  Dx=0000  Sp=0400  Bp=Si=Di=0000
Ds=0b22  Es=0b22  Ss=0b36  Cs=0b32  Ip=0000  Flag Reg

can i find out the EXE size in memory after the program loaded?




---->ASM fresher need help!<----

With kind regards.
syskplim@streamyx.com
 
Zeit,

Dividing sp by 16 gives the number of paragraphs in the stack segment.

how long is a paragraph?



---->ASM fresher need help!<----

With kind regards.
syskplim@streamyx.com
 
So far as I understand it, a paragraph is 16 bytes.

The reason for defining the length of the program in paragraphs is that Int 21h funtion 4ah, resize memory block, requires the new size of the memory block in paragraphs, contained in the BX register.

What Ray Duncan is saying is relevant when the program is initially loaded into memory, and before instructions to change the segment registers have executed.

Your subroutine above is obviously executed after this.

rgds
Zeit.
 
Further to the above.

On entry, from the dos loader, ES and DS point to the PSP.

The order of DS and CS depends on how MASM is instructed to treat them.

If the file contains

DATA SEGMENT

DATA ENDS

CODE SEGMENT

CODE ENDS

will generate the segments in that order unless told otherwise.

This is not the recommended DOS order of segments, which is

CODE SEGMENT

CODE ENDS

DATA SEGMENT

DATA ENDS

STACK SEGMENT

STACK ENDS

In masm 6, there's a directive DOSSEG which forces this.

The linker can also force this using /DOSSEG as a command line parameter.

Actually you can probably write a book about this.

So I'll stop here.

rgds
Zeit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top