NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: SmileyFace on March 03, 2013, 12:24:59 PM

Title: Perform arimethic/logical operations with labels in the sourcecode (flatbinary)
Post by: SmileyFace on March 03, 2013, 12:24:59 PM
Hello, :)
I have the following problem:
I want to split the address of any function in the upper two bytes and the lower two bytes, while creating a structure in sourcecode:

Code: [Select]
struc Structure
         Structure.FunctionAddressLow: resw 1
         Structure.AnyValue: resd 1
         Structure.FunctionAddressHigh: resw 1
endstruc

StructureImage:
istruc Structure
         at Structure.FunctionAddressLow, dw (function & 0xffff)
         at Structure.AnyValue, dd 'axyz' ;doesnt matter
         at Structure.FunctionAddressHigh, dw ((function >> 16) & 0xffff)
iend

function:
;do something
ret


After assembling it:
 "nasm -f bin -o example.bin example.asm",
NASM complains:
 "& operator may only be applied for scalar values"
 and
 ">> operator may only be applied for scalar value"
=>So my question is how to solve this problem without using code like this

Code: [Select]
mov eax, function
mov ebx, StructureImage

mov [ebx + Structure.FunctionAddressLow], ax
shr eax, 16
mov [ebx + Structure.FunctionAddressHigh], ax

But initialize the structure correct
Thanks in advance
Title: Re: Perform arimethic operations with labels in the sourcecode (flatbinary)
Post by: Keith Kanios on March 03, 2013, 04:37:53 PM
If you are using a single section flat binary, you can get away with something like this:

Code: [Select]
%define LO(x) ((x-$$+START) & 0xffff)
%define HI(x) (((x-$$+START) >> 16) & 0xffff)

%define START 0

ORG START

struc Structure
         Structure.FunctionAddressLow: resw 1
         Structure.AnyValue: resd 1
         Structure.FunctionAddressHigh: resw 1
endstruc

StructureImage:
istruc Structure
         at Structure.FunctionAddressLow, dw LO(function)
         at Structure.AnyValue, dd 'axyz' ;doesnt matter
         at Structure.FunctionAddressHigh, dw HI(function)
iend

function:
;do something
ret

If you are using multiple sections, look into doing the same by utilizing the start/vstart section attribute.
Title: Re: Perform arimethic operations with labels in the sourcecode (flatbinary)
Post by: SmileyFace on March 03, 2013, 05:18:00 PM
That works fantastic!!!
Thank you very much. :)