Page 1 of 1

assembly question

Posted: Fri Jan 17, 2003 8:38 pm
by Madmonky1
Is there anything I could do in nasm assembly to get something to point to another functions location in memory? for example: if I was making an Idt could I do something like this:

Code: Select all

;interrupt 0
dw 0x0000
dw 0x10
dw <memory location of isr0>
dw 0x20
Without knowing where isr0 will be located in memory? Or is ther anyway I could figure out where it would be in memory??

EDIT: Or even better, is there a way to do it in c?

Re:assembly question

Posted: Fri Jan 17, 2003 10:32 pm
by nullify
You could do:

unsigned addr = (unsigned) isr0;

I think.

Re:assembly question

Posted: Sat Jan 18, 2003 3:51 am
by Schol-R-LEA
In NASM, a label is just a name for an offset. Thus, all you would need to do in order to get a function's address is [tt]mov <dest>, dword function_name[/tt]; without the square brackets, the label is treated as an immediate value, rather than an address, you see.

However, descriptors present a problem, in that the offset is split between two non-adjacent words. To deal with this, you have to do something like this:

Code: Select all

   ; initialize interrupt 0 descriptor
  mov eax, isr0  ; without the brackets, this moves the address into eax
   mov [int0_descriptor], ax ; save lower half of offset value
   shr eax, 16
   mov [int0_descriptor + 6], ax ; save upper half of offset value

 <additional code>

isr0:
   <code>
   iret

<more code and data>

;interrupt 0
int0_descriptor:
dw 0x0000  ; lower half of offset
dw 0x0001  ; segment selector
db 0x00    ; dword count 
db 0x0E    ; P/DPL/DT/type
dw 0x0000  ; upper half of offset


Note that I haven't tried this, and I'm not very familiar with descriptors, so I'm likely to be mistaken on the details of this. I assumed a single code segment in descriptor 0x0001, kernel-level priority (priority 0), and no stack parameters.

BTW it may be wiser to use a struct (or perhaps more than one) in defining descriptors, and perhaps use macros to deal with initializing those parts which can be easily computed at assembly time.