Page 1 of 1
Far pointers in assembly
Posted: Thu Aug 12, 2010 2:05 am
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.
Re: Far pointers in assembly
Posted: Thu Aug 12, 2010 3:43 am
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.
Re: Far pointers in assembly
Posted: Thu Aug 12, 2010 6:35 am
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?
Re: Far pointers in assembly
Posted: Fri Aug 13, 2010 2:20 am
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?
Re: Far pointers in assembly
Posted: Fri Aug 13, 2010 2:35 am
by a5498828
quotient overflow?
Re: Far pointers in assembly
Posted: Fri Aug 13, 2010 2:41 am
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
Re: Far pointers in assembly
Posted: Fri Aug 13, 2010 2:43 am
by gerryg400
You should use bitwise operators for this.
Re: Far pointers in assembly
Posted: Fri Aug 13, 2010 2:50 am
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.
Re: Far pointers in assembly
Posted: Fri Aug 13, 2010 2:56 am
by gerryg400
shr etc.
Re: Far pointers in assembly
Posted: Fri Aug 13, 2010 2:58 am
by chibicitiberiu
gerryg400 wrote:shr etc.
shr and shl right? Thanks a lot
now it works