Shifting the argument of a macro in NASM?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
NOTNULL

Shifting the argument of a macro in NASM?

Post by NOTNULL »

Hi all,
I have defined a macro to install an exception in assembly. The arguments for the macro will be the function address, segment selector and DPL of an exception. How do i shift the higher word of the function address to fill the "offset(16-31)" field in the IDT? NASM supports shifting only for scalar values. How do i shift the argument of a macro? Here is my macro,

Code: Select all

%macro INSTALL_EXCEPTION 3
        dw      %1
        dw      %2
        db      00h
   %if %3=0
        db      0x8E
   %else
        db      0xEE
   %endif
        dw      (%1 >> 16)   ; Here is the problem...
%endmacro
Any help appreciated.
hgb

Re:Shifting the argument of a macro in NASM?

Post by hgb »

I think is about the nasm version you are using?? (what is the problem you see???)

This work OK for me...

Code: Select all

%macro SH 1
   %assign _xx_ %1 << 16
   dw %1 << 16
   %error _xx_
%endmacro

%macro S 1
   %assign _xx_ %1 >> 16
   %assign __xx %1 & 0xFFff0000
   %assign __xx __xx >> 16
   dw __xx
   dw %1 >> 16
   %error _xx_ :::: __xx
%endmacro


SH 2
SH 5
SH 6
S 2
S 5
S 6
S 65536
S 65537
S 65536+1
S 0xAAbb8833
S 0x1100FEDC
43707 => 0xAA00
4352 => 0x1100

I have a relative old one? NASM version 0.98.38 compiled on Sep 12 2003.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:Shifting the argument of a macro in NASM?

Post by Brendan »

Hi,

With NASM (all versions) it won't allow operations to be performed on labels, as the labels are relative to something rather than usable (scalar) values. To convert a label into a scalar value you need to do something like:

Code: Select all

%define codeBaseAddress 0x012345

    org codeBaseAddress

    dw ( ((%1-$$)+codeBaseAddress) >> 16)
This works because "(%1 - $$)" is the same as "offset_within_section - offset_within_section", which is a scalar number, and "(scalar_difference + scalar_value)" is also a scalar number. It's messy, but you end up shifting a scalar number rather than the offset within a section :).

In the NASM manual, see section "10.1.4 TIMES Doesn't Work". It's not an obvious place to look but describes the same problem and the same solution (note: the manual is talking above code where ORG=0, or where the "codeBAseAddress" above wouldn't be needed).

@rae: your macro isn't shifting the address of a label - it works because it's shifting a scalar value.


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.
Post Reply