OSDev.org
https://forum.osdev.org/

Shifting the argument of a macro in NASM?
https://forum.osdev.org/viewtopic.php?f=1&t=10537
Page 1 of 1

Author:  NOTNULL [ Fri May 27, 2005 2:54 am ]
Post subject:  Shifting the argument of a macro in NASM?

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:
%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.

Author:  hgb [ Fri May 27, 2005 1:39 pm ]
Post subject:  Re:Shifting the argument of a macro in NASM?

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

This work OK for me...

Code:
%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.

Author:  Brendan [ Fri May 27, 2005 11:35 pm ]
Post subject:  Re:Shifting the argument of a macro in NASM?

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:
%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

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/