Confusion about seg:offset addressing

Programming, for all ages and all languages.
Post Reply
User avatar
siavoshkc
Member
Member
Posts: 40
Joined: Wed Feb 19, 2014 11:10 am

Confusion about seg:offset addressing

Post 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?
Check out my FSB Data Integrity Tester at http://fsbdit.sourceforge.net/.

Siavosh
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Confusion about seg:offset addressing

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Confusion about seg:offset addressing

Post 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.
User avatar
siavoshkc
Member
Member
Posts: 40
Joined: Wed Feb 19, 2014 11:10 am

Re: Confusion about seg:offset addressing

Post 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.
Check out my FSB Data Integrity Tester at http://fsbdit.sourceforge.net/.

Siavosh
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Confusion about seg:offset addressing

Post 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.
User avatar
mathematician
Member
Member
Posts: 437
Joined: Fri Dec 15, 2006 5:26 pm
Location: Church Stretton Uk

Re: Confusion about seg:offset addressing

Post 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].
The continuous image of a connected set is connected.
freecrac
Member
Member
Posts: 69
Joined: Thu Sep 20, 2012 5:11 am
Location: germany hamburg

Re: Confusion about seg:offset addressing

Post 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
User avatar
siavoshkc
Member
Member
Posts: 40
Joined: Wed Feb 19, 2014 11:10 am

How to use segment selectors

Post 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.
Check out my FSB Data Integrity Tester at http://fsbdit.sourceforge.net/.

Siavosh
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Confusion about seg:offset addressing

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
siavoshkc
Member
Member
Posts: 40
Joined: Wed Feb 19, 2014 11:10 am

Re: Confusion about seg:offset addressing

Post 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
Last edited by siavoshkc on Thu Feb 20, 2014 8:59 am, edited 1 time in total.
Check out my FSB Data Integrity Tester at http://fsbdit.sourceforge.net/.

Siavosh
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: Confusion about seg:offset addressing

Post by VolTeK »

User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: Confusion about seg:offset addressing

Post 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)
User avatar
siavoshkc
Member
Member
Posts: 40
Joined: Wed Feb 19, 2014 11:10 am

Re: Confusion about seg:offset addressing

Post by siavoshkc »

Do we have any instruction in real mode having two memory operands?
Check out my FSB Data Integrity Tester at http://fsbdit.sourceforge.net/.

Siavosh
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Confusion about seg:offset addressing

Post by Combuster »

There's MOVSx for that.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply