Author Topic: Perform arimethic/logical operations with labels in the sourcecode (flatbinary)  (Read 5435 times)

Offline SmileyFace

  • Jr. Member
  • *
  • Posts: 2
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
« Last Edit: March 03, 2013, 07:45:23 PM by SmileyFace »

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
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.

Offline SmileyFace

  • Jr. Member
  • *
  • Posts: 2
That works fantastic!!!
Thank you very much. :)