Hi!

Cool to see that all the ASM gods are in this board :grin: ...
My problem is not so complicated though:

I am using MASM32 and I want to push an immediate REAL4 floating-point value (which is actually a DWORD) onto the stack.
Something like:

push 0.7

But I have to tell MASM to push the dword equivalent... I tried combinations of "REAL4 PTR" and "0.7f" ... but it won't work.

Now do I have to calculate it myself?!

Thanx

aweX <-
Posted on 2002-05-14 09:51:27 by aweX
Yes, or use a macro:
iPUSHr4 MACRO val:REQ

db 68h
REAL4 val
ENDM
Posted on 2002-05-14 10:10:35 by bitRAKE
Good macro!
How do you push real8 ?
so would a pointer to it?
Posted on 2002-05-14 22:37:41 by The Svin
Thanks, bitRAKE

Seems like that macro is the only way to keep it straight ... I also thought 'bout that but wanted to make sure I'm not missing an easier (direct) way.

@The Svin:
Well, that's not hard, is it? As pointers are always dword, it doesnt depend on the datatype they're pointing to.



.data
some_float REAL8 3.14

.code
?
?
?
push OFFSET some_float



aweX <-
Posted on 2002-05-15 16:46:02 by aweX
aweX,

Define a dd in the data definitions like this, for exemple:
xval real4 0.7
Then, in the code do:
push xval
That simple,
Rudy.
Posted on 2002-05-15 18:51:16 by Rudy Penteado
Sorry but isn't it kind of inefficient, defining a seperate dword just for pushing it as immediate value ?

The macro-solution is the best way unless there's a method in MASM to push an immediate REAL4.


aweX <-
Posted on 2002-05-15 19:11:30 by aweX
aweX,

It is not an immediate value. What you push is the contents of a doubleword defined as a short float.
MASM32 does not allow you to push an immediate float value.
Remember also that depending on who is going to use the pushed value, that could represent a value or a pointer. It is up to the user of the pushed value to interpret what you are pushing.

Best regards,
Rudy.
Posted on 2002-05-15 20:02:15 by Rudy Penteado
Of course, you are right in both cases... :grin:

But what I meant was that I actually know the value, it is static... in that case, it is not very efficient to waste space for it in the data section.

Only when I need a pointer to it I have to define it there.



aweX <-
Posted on 2002-05-15 20:06:41 by aweX
aweX,

You are a nice guy, but tell me what is the difference of using a macro which defines the value in the code section or a 'dd' assignment which defines the value in the data section? What is the difference?
OK, I will tell you what the difference is: When you define the value as 'dd' you don?t have to turn on your hex calculator to decide what is 0.7 in float point notation. MASM32 will decide that for you.
But remember. you are not saving any space or execution time by using the 2 methods. They are the same.
You are a bold guy for a beginner. That's very good because it takes to be a bold guy to be an assembler winner. My best wishes for you.

Rudy.
Posted on 2002-05-15 20:24:03 by Rudy Penteado
Rudy, I'm not sure I understand your last post 100%, so excuse
me if I got you wrong...

but using bitrake's macro will save space, since you use only an
immediate push (5 bytes), instead of 4 bytes for the float plus
6 bytes for the indirect dword push.
Posted on 2002-05-15 20:32:53 by f0dder
FOdder,

Yes, of course you are right about the number of bytes used in the executable, but let me tell you what I thing about efficiency and size:
Any code that is executed once only, can have any size that make it convenient to write. The efficiency here, is to get the code correct and easy to write.
Any code that is looped and executed hundreds of times have to be as lean as possible and could require the best of your efforts to get is small and fast.

If your immediate push is inside a critical loop, of course you should code it as a 5 byte ?push 03F333333h?.
If it is not, and most of the times they are in tenths or hundreds of them, you should define that as ?dummy dd 0.7? and push them with 6 bytes ?push dummy?.

The error here is that I assumed by the naive question, that aweX was a beginner in assembler coding and those efficiency matters were secondary. My mistake, I apologize.

Rudy.
Posted on 2002-05-15 21:02:31 by Rudy Penteado
I assembled both versions and those are the resulting operations of the push:

#1 Rudy's version:


FF3500304000 push dword ptr [00403000]



#2 bitRAKE's version:


683333333F push 3F333333


So version #1 is indeed inefficient because, firstly, it results in a six-bytes instruction where #2 has only five, and secondly (which is the point I was talking about!!) it reserves space (four bytes) for the REAL4 value inside the data section (at memory address 403000)!

#2 should also be faster in execution because the CPU doesn't have to fetch the value from memory, but has it right inside the instruction.

Oh and by the way, Rudy: You are also very nice... but I am far away from being a beginner, since I am programming for a total of 6 years now and for 3 years in asm ... :tongue:

aweX <-
Posted on 2002-05-15 21:06:18 by aweX
As pointers are always dword, it doesnt depend on the datatype they're pointing to

:)
I meant the same question as you but of qwords.
How to create and push real8 value as imm. Without initialising it as some memory var in data section.
And the question was precize -
so that is a pointer to it.
For example for 1234.5678

push 40934A45h
push 6D5CFAADh
fld
;st0 = 1234.5678
Posted on 2002-05-15 21:26:21 by The Svin
aweX

Yes,
Programming in assembler for 3 years and still doesn't know that MAS32 won't translate a literal for you for short float?
You ask a question and then start to tell what is good and what is not? You don't know how to lisen when you ask? I think you don't need help. You are looking for something alse. Hope you find it.

Best,
Rudy.
Posted on 2002-05-15 21:42:43 by Rudy Penteado
@Rudy:

Probably, you didn't read carefully my reply to bitRAKE's macro:
There I wrote:
> I also thought 'bout that but wanted to make sure I'm not missing an easier (direct) way

That means I definitely know about the opcode-method and therefore I can hardly be a beginner ;)

You can say I am a beginner when I asked something like
"Why doesnt mov eax, bx work?"

But here... :confused:
I never dealt with floats until now and as normal values can be written immediately (push 3), it was legitimate to ask if that's not also possible with floats.

Why not?



aweX <-

P.S.: You hope I find "it"? Well, I already found the solution in the first reply to my question. So what?
Posted on 2002-05-15 22:00:07 by aweX
aweX,
I apologize,
You won't see me again on your threads.
Rudy.
Posted on 2002-05-15 22:18:31 by Rudy Penteado
Basically, I just pointed out that you wrote something wrong ("no difference in code size and in speed"). Isn't that allowed, even when I asked something? Do I have to be thankful for every reply I get and not allowed to doubt its correctness?

Well, anyways... You are still invited to all my future threads.
Why not being friendly? :alright:

Everything's cleared out already on this topic so let's leave it alone now :grin:

aweX <-
Posted on 2002-05-15 22:30:13 by aweX

If your immediate push is inside a critical loop, of course you should code it as a 5
byte ?push 03F333333h?. If it is not, and most of the times they are in tenths or hundreds
of them, you should define that as ?dummy dd 0.7? and push them with 6 bytes ?push dummy?.

Why? If I had to manually calc the dword representation of floats every time I need
to do "push imm real4", I would probable save the immediate version for stuff where
it really mattered. But since I can use bitRAKEs macro and do "iPUSHr4 42.424242",
why would I ever use the indirect form?

Also, it's pretty silly that masm (not masm32, that's hutches package, not the assembler)
doesn't translate a real4 push, as it's just a dword...

Svin, don't you mean that esp (and not ) should point to the double value?
As I see it, with a double pushed to the stack, esp would point to it while
would be the value? Or am I missing something here? Perhaps there's a way to do
an immediate double push with some ORG trickery, but... dunno. Would look messy :).
Posted on 2002-05-16 06:20:45 by f0dder
f0dder, you are right.
I meant so after the push I can refer to it as
qword ptr
of course esp here is pointer
qword ptr - is value itself.
Posted on 2002-05-16 20:34:48 by The Svin

How do you push real8 ?
so esp would a pointer to it?
This macro is designed to counter the behavior of MASM when it comes to relocatable address after the macro. Unless you know what your doing, it is best to leave the macro alone. Warning: I don't use this macro, but it appears to work. You have to be very careful when using ORG in MASM to back over the assemblers work. Many tables are maintained by the assembler and you risk creating errors in those internal tables. ORG wasn't designed explicitly for this kind of thing.
iPUSHr8 MACRO val:REQ

REAL8 val
db 068h ; push
REAL8 val
org $-(8+1+8)
db 090h ; you can replace these with something useful?
db 090h ; you can replace these with something useful?
db 090h ; you can replace these with something useful?
db 068h ; push
org $+4+1+4
nop ; relocation of addresses here don't take place...
nop
nop
nop
ENDM
Here is the move macro, as long as we are on the topic:
iMOVr8   MACRO memloc:REQ, val:REQ

db 0C7h, 05h ;C7 05^
dd offset memloc ;C7 05 rr rr rr rr^
db 0,0,0,0,0,0 ;C7 05 rr rr rr rr 00 00 00 00 00 00^
dd offset memloc+4 ;C7 05 rr rr rr rr 00 00 00 00 00 00 rr rr rr r4^
org $-10 ;C7 05 rr rr rr rr^00 00 00 00 00 00 rr rr rr r4
REAL8 val ;C7 05 rr rr rr rr 01 23 45 67 89 AB CD EF^
org $-4 ;C7 05 rr rr rr rr 01 23 45 67^89 AB CD EF
db 0C7h, 05h ;C7 05 rr rr rr rr 01 23 45 67 C7 05^CD EF
REAL8 val ;C7 05 rr rr rr rr 01 23 45 67 C7 05 01 23 45 67 89 AB CD EF^
org $-8 ;C7 05 rr rr rr rr 01 23 45 67 C7 05^01 23 45 67 89 AB CD EF
dd offset memloc+4 ;C7 05 rr rr rr rr 01 23 45 67 C7 05 rr rr rr r4^89 AB CD EF
org $+4 ;C7 05 rr rr rr rr 01 23 45 67 C7 05 rr rr rr r4 89 AB CD EF^
ENDM
Posted on 2002-05-16 21:47:42 by bitRAKE