Far pointers in assembly

Programming, for all ages and all languages.
Post Reply
chibicitiberiu
Member
Member
Posts: 68
Joined: Thu May 28, 2009 11:46 pm

Far pointers in assembly

Post by chibicitiberiu »

Some bios interrupts need stored in [es:di] (like get vesa information).
How do I store a pointer like this, es = segment, di = offset?

This doesn't work in nasm. If I remove 'offset' it says that the output file doesn't support segment or something:
mov ax, seg value
mov es, ax
mov di, offset value

Note that the code is part of the 2nd stage bootloader, and it's mixed 16 and 32 bit code.

How is it correctly done? I can't find any good example online.
Tibi,
Currently working on the Lux Operating System
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: Far pointers in assembly

Post by Combuster »

if you know the seg and offset, you can store it to any other register or memory location, which will thus allow you to pass it around.

Basically a 16-bit far pointer is a 32-bit struct containing two 16 bit unsigned values.
"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 ]
chibicitiberiu
Member
Member
Posts: 68
Joined: Thu May 28, 2009 11:46 pm

Re: Far pointers in assembly

Post by chibicitiberiu »

Okay, now the actual address in Segment:Offset looks like this, right?

Address = Segment * 0x10 + Offset;

So this means that:

Segment = (one of the many possibilities) Address / 10h;
Offset = Address % 10h;

Is this correct?
Tibi,
Currently working on the Lux Operating System
chibicitiberiu
Member
Member
Posts: 68
Joined: Thu May 28, 2009 11:46 pm

Re: Far pointers in assembly

Post by chibicitiberiu »

Okay so I tried to do this:

Code: Select all

      xor         eax, eax
      xor         ebx, ebx
      xor         edx, edx
      mov         dword [TemporaryStorage], vbeControllerInfo
      mov         ax, word [TemporaryStorage+2]
      mov         dx, word [TemporaryStorage]
      mov         bx, 10h
      div         bx
      mov         es, ax
      mov         di, dx
      mov         ax, 0x4f00
      int         10h
      cmp         ax, 0x004F
It freezes when it reaches the div instruction. What could be the problem?
Tibi,
Currently working on the Lux Operating System
a5498828
Member
Member
Posts: 99
Joined: Thu Aug 12, 2010 7:25 am

Re: Far pointers in assembly

Post by a5498828 »

quotient overflow?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Far pointers in assembly

Post by Brendan »

chibicitiberiu wrote:It freezes when it reaches the div instruction. What could be the problem?
You've got the high word and low word around the wrong way, which is probably causing an overflow error (e.g. dx:ax divided by 16 doesn't fit in 16-bits).

Also, you probably shouldn't be using division in the first place. You could use shifts instead (which is faster and easier), you may be able to get the assembler to calculate it (which is even faster), and the segment register could/should already be correct (which is extremely fast because you don't need to do anything).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Far pointers in assembly

Post by gerryg400 »

You should use bitwise operators for this.
If a trainstation is where trains stop, what is a workstation ?
chibicitiberiu
Member
Member
Posts: 68
Joined: Thu May 28, 2009 11:46 pm

Re: Far pointers in assembly

Post by chibicitiberiu »

I searched yesterday all the net for bitwise operators in assembly, and found nothing other than AND, OR, XOR...
How do I do bit shifts? Because that was my first idea, just couldn't find the right instructions to do it.
Tibi,
Currently working on the Lux Operating System
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Far pointers in assembly

Post by gerryg400 »

shr etc.
If a trainstation is where trains stop, what is a workstation ?
chibicitiberiu
Member
Member
Posts: 68
Joined: Thu May 28, 2009 11:46 pm

Re: Far pointers in assembly

Post by chibicitiberiu »

gerryg400 wrote:shr etc.
shr and shl right? Thanks a lot :) now it works
Tibi,
Currently working on the Lux Operating System
Post Reply