Page 1 of 1

Confusion about seg:offset addressing

Posted: Wed Feb 19, 2014 11:39 am
by siavoshkc
Greetings,
I am very glad to find this forum and related wiki.

I got into a strange confusion about segment:offset notation in Real-Mode assembly. I read many references but couldn't find the exact answer.

What I know:
In real mode we have a 16bits segment selector and 16bits offset. To create an effective address, processor multiplies segment by 16 and adds offset to it.

We have CS, DS, SS, and ES as segment selectors available. CS is for Code segment and others are used for stack and data.

Now the confusion:
If I want to access a variable in seg 12h and offset 4Ah:

Code: Select all

mov ds, 12h
mov ax, [4Ah]
Right?
So when will I use ds:ax notation? Does the code below work?

Code: Select all

mov ax, [12:4A]
When will the processor implicity use ss or es or ds to calculate the effective address?
Do I have to change DS each time I want to access from another segment? Is that enough?

Re: Confusion about seg:offset addressing

Posted: Wed Feb 19, 2014 12:38 pm
by Combuster
Does the code below work?
The forum rules wrote:Try it and see. If you did that, you'd (a) learn the answer, and (b) stop wasting my time.

Re: Confusion about seg:offset addressing

Posted: Wed Feb 19, 2014 1:26 pm
by qw
This is very basic information that can be easily found on many places on the web. Try the Intel manuals or the Art of Assembly Language.

Re: Confusion about seg:offset addressing

Posted: Wed Feb 19, 2014 3:10 pm
by siavoshkc
I have all the Intel programming manuals and AMD manuals (which are very similar). I read them. I read both genereal and system programming. Now I think I know all stuff about paging and ENTER instruction! But still can't find this simple thing explicitly noted anywhere.

Code: Select all

	mov ds,	usermem_start_segment
	mov cl,	usermem_start_offset	
	mov	[ds:cl], 0xFFFF
	mov ds,	0x7FE0
	mov cl,	0xFFFF
	mov  bx, [usermem_start_segment:usermem_start_offset] <<Error line
 
NASM says 1> error: invalid segment override

Code: Select all

	mov ds,	usermem_start_segment
	mov cl,	usermem_start_offset	
	mov	[ds:cl], 0xFFFF
	mov ds,	0x7FE0
	mov cl,	0xFFFF
	mov  bx, [ds:cl]  <<Error line
This time NASM says error: invalid effective address

[EDIT]
I am trying to find an assembly book for 8086. There I think I can find it.

Re: Confusion about seg:offset addressing

Posted: Wed Feb 19, 2014 4:42 pm
by iansjack
Table 2-1 in the Intel Manual Vol 2A details the allowable addressing modes in real mode. You will note that [cl] is not one of them (which makes sense as cl holds an 8-bit value; neither is [cx] which might seem more reasonable). The rules for segment overrides are fairly straightforward. Most instructions assume that the offset is relative to the DS segment. You can override this. A few instructions assume that the offset is relative to the ES or SS register; the description of the instruction in the manual will tell you which. You really shouldn't have a lot of need to override segments.

You cannot use the form xxxx:yyyy as an address; you must use a segment register. But it's far easier just to use the default DS, which you don't need to specify.

Hence your first error (you tried to use a value directly rather than a segment register) and your second error (you used an invalid addressing mode). I suggest that you read a simple tutorial on assembler programming as well as the manuals; all the information is in them but it can be difficult to see the wood for the trees.

Re: Confusion about seg:offset addressing

Posted: Wed Feb 19, 2014 8:27 pm
by mathematician
You only need to use a segment override if you are trying to access data in an unusual place.

mov ax, [bx] is the same as move ax, ds:[bx], because the ds register will be assumed unless you specify otherwise. If , for some reason, you had data poked away in the code segment, and you wanted to get at that, then you would have to use a segment override - mov ax, cs:[bx].

The bp register is a bit unusual, because it is the only one for which mov ax, [bp] does not default to mov ax, ds:[bp]. Instead it defaults to mov ax, ss:[bp]. Therefore, if you wanted to use the bp register to access something in the data segment, you would have to do, mov ax, ds:[bp].

Re: Confusion about seg:offset addressing

Posted: Thu Feb 20, 2014 1:22 am
by freecrac
siavoshkc wrote:

Code: Select all

mov ds, 0x7FE0
This instruction does not exist. We can not move directly immediate value into a segment register.
Only from a general purpose register, a memory location, or from poping the stack.

Code: Select all

mov ax, 0x7FE0
mov ds, ax
Dirk

How to use segment selectors

Posted: Thu Feb 20, 2014 1:56 am
by siavoshkc
Thank you.

So what I know now is that:
-Each memory Read/Write uses one segment selector implicitly.
-Instruction fetches use cs, stack access uses ss, data references use ds and string instructions use es
-We can override default selector register for each instructions but not all instructions support that. This will be done by putting a prefix for instruction which tells the processor to use the specified selector register instead of the default one.
-We can load segment selectors with lxs instructions, by mov instruction or a pop instruction.

The most comprehensive explanation for this topic I found so far is in section 5.3.3.1 of the Intel manual, vol 1, titled "SPECIFYING A SEGMENT SELECTOR"

Thanks again for rapid answers.

Re: Confusion about seg:offset addressing

Posted: Thu Feb 20, 2014 2:14 am
by Combuster
stack access uses ss
Not quite. accesses (implicitly) using *bp or *sp use SS
string instructions use es
Wrong. They use a mix of DS, ES, or both depending on the instruction.

Re: Confusion about seg:offset addressing

Posted: Thu Feb 20, 2014 7:04 am
by siavoshkc
Combuster wrote:
stack access uses ss
Not quite. accesses (implicitly) using *bp or *sp use SS
string instructions use es
Wrong. They use a mix of DS, ES, or both depending on the instruction.
So what about

Code: Select all

mov [bx], sp
?

[EDIT]
I admit this one was stupid because only [bx] needs address calculation. #-o

Re: Confusion about seg:offset addressing

Posted: Thu Feb 20, 2014 7:39 am
by VolTeK

Re: Confusion about seg:offset addressing

Posted: Thu Feb 20, 2014 8:45 am
by JAAman
siavoshkc wrote: So what about

Code: Select all

mov [bx], sp
?
you are not using sp to reference memory there, the memory reference is in bx, which means DS will be used

however all of these:

Code: Select all

mov [sp], bx
mov bx, [sp]
mov bx, [bp]
mov [bp], bx
would use SS instead of DS (by default, of course, you can use a DS: seg override to change this)

Re: Confusion about seg:offset addressing

Posted: Thu Feb 20, 2014 11:56 am
by siavoshkc
Do we have any instruction in real mode having two memory operands?

Re: Confusion about seg:offset addressing

Posted: Fri Feb 21, 2014 12:09 am
by Combuster
There's MOVSx for that.