Could someone briefly say what the default segment registers are for the base
and index registers, and when segment override is necessary? I assume using
overrides increases the code size. Since I've been unsure, I put the segment
register on everything, and would like to go through and delete all that.
I think this is what you need:
Register Default segment register
-------- ------------------------
AX,CX,DX illegal for indexing
BX,SI DS
DI ES
BP SS
SP SS
IP CS
William Zandvliet
Yes, that's what I need. Thank you. Is this automatic? Does the assembler do
anything secret based on the PROC/ENDP the code resides within? I wouldn't think
so, but I'm just curious. How many extra bytes or machine cycles or used with
segment overrides?
My previous reply was incomplete and
contained a mistake. It leaves room for
misunderstanding. I would like to offer
my sincere apologies and a better explanation:
Only BX,SI,DI, and BP can be used as INDEX.
The default segment BX,SI, and DI point at is DS.
The default segment BP points at is SS
e.g. MOV AX,[BX] fills AX with the value at DS:BX
MOV DX,[BP] fills DX with the value at SS:BP
The default segment register for DI is ES
when using STOS, SCAS, and CMPS
e.g. STOSW stores AX at ES:DI
The default segment register for SI is DS
when using LODS and CMPS
e.g. LODSW loads AX with the value at DS:SI
e.g. CMPSW compares the word at DS:SI with
the word at ES:DI
SP points to the location in SS (stack segment)
where the next value is POPped from. If a value
is POPped, SP increases. If a value is PUSHed,
SP decreases.
IP (instruction pointer) points to the location
of the next instruction in CS.
I forgot one thing to ask. What is the default segment register for memory
addressing, such as
mov ax,bufhead
It's probably DS. That's where I use most of the segment overrides. I put CS: in
front of everything since it's all in the same segment. Surely I could figure it
out through trial and error, but I haven't taken the time to do that yet.
Thanks
>Could someone briefly say what the default segment registers are for the base
>and index registers, and when segment override is necessary?
Code uses always CS, cannot be overridden.
Stack instructions (PUSH, POP, CALL, RET) uses SS. I don't know
whether this can be overridden, but you shouldn't want to.
References using BP or EBP use SS, this can be overridden: They are:
[BP] [SI+BP] [DI+BP] [EBP+d]
[BP+d] [SI+BP+d] [DI+BP+d]
Instructions with implicit use of [DI] or {EDI} use ES. This cannot be
overridden. They are:
STOS SCAS INS
Instructions with implicit use of [DI] or [EDI] and [SI] or [ESI] use
ES with the destination and DS with the source. The latter can be
overridden, the former cannot. They are:
CMPS MOVS
All other instructions use DS. This can be overridden.
>overrides increases the code size.
Yes, it does, although a smart assembler might see that the prefix is
redundant, and delete it.
-----------
Taal Effect
Gerda en Feico Nater
Beukweg 24, 7556 DE Hengelo
tel 074 2506673
fax 074 2506675
In normal instructions the default segment is SS if BP is used in
indexing. Otherwise it is DS. In string instructions ES is used with
SCAS, STOS and as destination for MOVS and CMPS. One cannot change that with
any override. DS is used with LODS and for source with MOVS and CMPS.
FS and GS (386+) do not have any default uses.
Yes, a segment override increases the instruction by one byte. But with
then you might avoid the need to constantly change and restore DS. Also
keeping DS to point at the date segment as much as possible is a good way
to avoid bugs.
Osmo
NONONONONO! DI defaults to DS... the only time it defaults to ES is when
one of the relevant string instructions is used. ALSO sp is not a general
purpose index register... although if you use 32-bit addressing modes
EAX,ECX,EDX,ESP all become general-purpose index registers.
Likewise IP is not a general-purpose index register either... in fact
you can never get at it directly, it only shows up in stack frames. BTW
a neat trick is:
call myfunc
db mydata
proc myfunc
pop esi
do something with data
push esi
ret
I used to use it all the time but it makes debugging hell! Early versions
of turbo pascal really used it a lot...
Note that by use of the ASSUME keyword you can get the assembler to
automatically do the work of figuring out what segment overrides
are needed.
:
: William Zandvliet
David G. McDivitt wrote:
>> a question about over-rides>
>William Zandvliet <william....@pi.net> responded
>I think this is what you need:
>Register Default segment register
>-------- ------------------------
>AX,CX,DX illegal for indexing
On 86
>BX,SI DS
>DI ES
In Stos,etc. but not in Mov ax,[di] for example.
>BP SS
Except as stated below.
>SP SS
>IP CS
This is genarally true for the 86 but wrong in the greater sense for
it's incompleteness.
Di is default as below by Fieco Nater (DS) in common instruction forms
but default in specialized instructions.
32 bit and SIB encoding allows ax,cx and dx as index registers. It is
also possible to have default DS with a complex 32 bit + SIB when using
BP.
Paul
----------------
Feico Nater wrote:
> References using BP or EBP use SS, this can be overridden: They are:
> [BP] [SI+BP] [DI+BP] [EBP+d]
> [BP+d] [SI+BP+d] [DI+BP+d]
Also 32 bit and SIB options should be included but I hate to type, the
manuals have all the permutations. It is hundred all together. Also note
that BP in combination can have a default DS in some odd cases.
> Instructions with implicit use of [DI] or {EDI} use ES. This cannot be
> overridden. They are:
> STOS SCAS INS
> Instructions with implicit use of [DI] or [EDI] and [SI] or [ESI] use
> ES with the destination and DS with the source. The latter can be
> overridden, the former cannot. They are:
> CMPS MOVS
> All other instructions use DS. This can be overridden.
This is correct.
Additionally the following:
The XLAT instruction also has implied use of the DS register with Bx and
can also be over-ridden.
Paul
---------
>(David G. McDivitt) added
>I forgot one thing to ask. What is the default segment register for
>memory addressing, such as
> mov ax,bufhead
An instruction of the form Mov reg,[offset] is DS.
Paul
It is a feature of the processor. The assembler has nothing to do with
it. The assembler on the other hand can generate overrides on basis of
what you have told it on the segment registers. That means for example
that if you have ASSUME CS:CODE,DS:DATA and the have a variable in the
code segment then the assembler automatically generates an override for it.
IT is a good thing to change the assumptions when you change segment
registers (most typically DS). In that way the assembler can detect
errors for you.
>I wouldn't think
>so, but I'm just curious. How many extra bytes or machine cycles or used with
>segment overrides?
>
That depends on the CPU. I think it is one on 386 and that or less on
newer ones. In general it is not something gone should terribly worry. A
good thing is just to use DS for data segment and ES, FS, GS for others.
Osmo
That is incorrect. Only SI or DI can be used as index and only BX or BP
can be used as base. One can use [base+index+displacement] or any subset
of it. That means [BP+BX] and [SI+DI] are illegal.
>The default segment BX,SI, and DI point at is DS.
>The default segment BP points at is SS
>
> e.g. MOV AX,[BX] fills AX with the value at DS:BX
> MOV DX,[BP] fills DX with the value at SS:BP
>
Actually SS is used if the base is BP. Otherwise DS is used.
Osmo
Default at what level? On machine code level the default is DS. On
assembler level it depends on what segment the variable is in. If it can be
accessed with DS then DS is used (that is no override). Otherwise proper
override is created or an error message is generated.
>I put CS: in
>front of everything since it's all in the same segment.
Why? Why not just make sure that DS points to the same segment. Remember
that using overrides is the exception. Also if you handle assumptions
correctly you do not have to worry about overrides in cases like this
(that is labels instead of numeric offsets.)
> Surely I could
>figure it out through trial and error, but I haven't taken the time to
>do that yet.
>
>Thanks
>
>--
>http://cust.iamerica.net/mcdivitt/
Osmo
: Register Default segment register
: -------- ------------------------
: AX,CX,DX illegal for indexing
: BX,SI DS
: DI ES
: BP SS
: SP SS
: IP CS
ES is only the base segment when it is used in string operations.
Otherwise it is DS.
Segment overrides are needed when ever the default segments (above)
need to be altered.
Is there ever a CS:IP override? Not even sure what that would mean.
--
<---->
It would mean that you access info in the code set at whatever offset
the top of the stack is at. What else would it mean?
Dan Ramage
http://www.geocities.com/SiliconValley/9577/
DS for all everything except and string destination (ES), and
stack access (SS), and anything using ESP, or (E)BP. Example:
mov ax, [1234] ;DS
lods ;DS
cmp [bx+si], di ;DS
string destination:
stos ;ES
cmps ;ES
movs ;ES
scas ;ES
stack access:
mov ax, [bp + 12] ;SS
cmp [bp + si], ax ;SS
mov [esp + 1234], 1 ;SS
>and when segment override is necessary?
Whenever the default segment is not the segment to which you
want to refer (note that you can't override string
destination - it's always ES).
example: change default from DS to CS, for move:
mov ax, cs:[1234]
>I assume using overrides increases the code size.
One byte per override.
> Since I've been unsure, I put the segment register on
>everything
A good assembler will realise when your override matches the
default, and remove it during assembly.
You are absolutely right. What I meant was that only
BX,SI,DI, and BP can be used as a MEMORY POINTER.
I used the wrong term.
Another thing I stated that is not entirely correct
was this:
The default segment register for DI is ES
when using string instructions (STOS, SCAS, CMPS, MOVS)
It should have been:
The segment register for DI when using string
instructions is ALWAYS ES. It can NOT be overridden.
William
> DS for all everything except and string destination (ES), and
> stack access (SS), and anything using ESP, or (E)BP. Example:
[... snipp ...]
EBP used as an index register defaults to DS, not to SS as one
might think.
Regards
Wuschel
Dan Ramage (dra...@worldnet.att.net) wrote:
: It would mean that you access info in the code set at whatever offset
: the top of the stack is at. What else would it mean?
What has the stack to do with this?
The confusion comes from the possible use of overrides to perform
intersegmeng jumps/calls, or their use to "possibly" override jumps/calls
from tables held in alternate segments.