News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

real4 in invoke

Started by RedXVII, February 12, 2006, 12:30:37 AM

Previous topic - Next topic

RedXVII

Hey all,

i want to do this -

myfunction( -1.6f );

But i dont know the form it should be in, compiler isnt accepting anything i try - someone please enlighten me, syntax is annoying my like hell. Im going to need to do lots of different floats so i basically need it in the form

Invoke myfunction, ??[what to write here]??

Thx  :U

Ratch

RedXVII,
Quote
But i dont know the form it should be in, compiler isnt accepting anything i try

     You write compiler, do you really mean assembler?  As in MASM.  If so, you are out of luck.  MASM does not generate floating point constants.  It will generate floating point data words in REAL4 format, however.  Perhaps Pelle's new assembler will do what you want.  Hopefully.  Ratch

zooba

The easiest way to use floating point constants is to do this:

mov eax, 0BADF00Dh
ORG $-4
DWORD 123.456


or make a macro:

; this is untested
movfp MACRO dest, src
    mov dest, 0BADF00Dh
    ORG $-4
    DWORD src
ENDM


Then use eax in the invoke statement.

RedXVII

Ratch:

I dont think you understand matey - theres a comma there... but maybe this will clear it up

basically; correct this

.invoke myfunction, -1.5f

Zooba:

Hmm, that looks annoying. isnt there a better way of doing this.

Ive seen in compiled C programs in the debugger:

push BFC00000            ;push -1.5
call MyFunction


How about something like

.invoke MyFunction, REAL4(-1.5f)               ???


Im sure you get the idea now eh?

Cheers  :U

zooba

I haven't found a way to make MASM convert a floating point constant into it's hex equivalent at compile time.

Your second suggestion (invoke MyFunction, REAL4(-1.5f)) is doable if you store the value in memory and reference the memory location. You can do the mov... trick with EAX if you only have one parameter as macro parameters are all evaluated before pushing. Otherwise, you use a macro like this:

pushREAL4 MACRO src
    pushd 0
    ORG $-4
    REAL4 src
ENDM


but then you'll have to push-call.

AFAICT, there's no 'nice' way of putting floating point constants into an invoke statement.

raymond

If those are constants which would not change throughout your program, simply initialize them in your data section and use them from there. I can assume you don't need zillions of them. Ex.:


.data
  onep5   dd  -1.5

.code
  invoke function,onep5


If those floats are variables, store them in memory or in a register and do the same.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Ratch

RedXVII,
Quote
Ratch:

I dont think you understand matey - theres a comma there... but maybe this will clear it up

basically; correct this

.invoke myfunction, -1.5f

     Au contraire, I did and do understand what you want.  You want to push an immediate floating point number onto the stack without using the clever trick suggested by zooba.  The fact of the matter is that MASM was not written to generate immediate floating point constants directly.  What a shame, because it obviously has the floating point generator within if it can do floating point data words.  Due to the Will of the Bill, however, the immediate floating point constant feature was not added.  Sad.  Ratch

Biterider

Hi RedXVII
If you use the ObjAsm32 AddIn for RadASM, you can convert a float to it's hex representation and and use it as parameter for your procedure. To do it, press ObjAsm32 from the menu, Insert, Float. That's all.

Biterider

MichaelW

MASM32 already has macros that can do this. This uses FP8 because printf expects a double, but you could use FP4 in the same manner (I'm not sure about FP10).

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    invoke crt_printf,chr$("%f%c"),FP8(123.456),10
    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start
eschew obfuscation

Biterider

Hi
The difference is that these macros first allocate the float in a segment and the push the the symbol to the float.
Using the method I described, you directly push the float as if it were ad dword (or qword for real8).

Biterider

zooba

MichaelW,

The problem with MASM32's macros is that they use 4/8/10 bytes of data (and another 4 bytes of address). They don't directly return the hex interpretation of the number. This is fine for FPU stuff because it doesn't support immediate values anywhere but for parameter passing it is not ideal. It also has no way of nicely handling multiple definitions.

RedXVII,

Probably the best solution for your particular case is Raymond's. Define a naming convention for your values and define them all in your data section. My naming convention when I do this is as follows:

REAL4/DWORD - s  (for 'single')
REAL8/QWORD - d (for 'double' - I use 'dw' for DWORD)
REAL10/TBYTE - t (for 'tbyte' :wink )
Negative numbers - N
Divided by - o (for 'over')
Decimal point - p

For example:

s1p0    DWORD   1.0
sN1p0   DWORD  -1.0
d3o2    QWORD   1.5     ; = 3/2
t0p707  TBYTE   0.7071067811865475 ; = 2^(-2)


If you remain consistent then there should be no problem remembering what you named your variable. And then if it's not there when you compile, it's easy enough to fill it in :U

RedXVII

Sweet! Thanks to all who helped - i quite like zooba's pushd + ORG method :)


Cheers to all who helped  :U