Skip to main content
gamedev.net
🔒 Locked

Strange looking assembly code

Started by Atm97fin 14 replies 800+ views
Original Post
Atm97fin
Atm97fin
Should this code work?
.data
FixedScale   dd 65536.0
pFixednumber dd    ?
.code
;...
;stuff
;...

;strange code:
                        st(0)
fmul  [FixedScale]   ; some number
fistp [pFixednumber]


 
Now the idea of this code is to translate some number to fixedpoint. The strange thing is that fmul direction, which(for what I know) should multiply the st(0) with the number adressed by Fixedscale. But here the fixedscale is not an address, but a dword variable.
EasyGL - easy to use graphics library.
juuso
juuso
Doesn''t [FixedScale] actually mean its address?
Just like & in C.
Puzzler183
Puzzler183
quote:
Original post by juuso
Doesn't [FixedScale] actually mean its address?
Just like & in C.


No, it means the actual data I think...

EDIT: BTW, it looks like something to do fixed point (like to scale it into a short).

[edited by - Puzzler183 on November 29, 2003 8:37:24 AM]
Jolle
Jolle
Sure FixedScale is an address. And [FixedScale] is the data at the address.
Atm97fin
Atm97fin
Exactly. So that code shouldn''t work, unless the assembler automagically create memory place for that 65536.0 in real format and put the FixedScale variable point into it.

I took this code from a professional programmers assembly example, could he have put that error there on purpose, so that the code wouldn''t work just by copy/pasteing it.
EasyGL - easy to use graphics library.
Jolle
Jolle
But the line "FixedScale dd 65536.0" should be read like this: FixedScale is a label, and will have the address to the data at the place it was written. dd says that here we have a dword of data, which we then say should be 65536.0. So, FixedScale points to the data containing 65536.0, and [FixedScale] would be 65536.0.

"FixedScale equ 65536.0" would set FixedScale to 65536.0. It is not the same thing as dd.

So, the answer is yes, that code should work.

[edited by - Jolle on November 29, 2003 11:10:05 AM]
Puzzler183
Puzzler183
No, I think the problem is that you don't quite get it. The code works just fine, you just don't understand the concept of memory or soemthing...

EDIT: BTW, I posted while Jolle was posting...

[edited by - Puzzler183 on November 29, 2003 11:10:53 AM]
Charles B
Charles B
It''s the same as C :
(1)

static const float Scale=65536.0f;

void main(void)
{
float x=3.07f;
float y=x*Scale;
printf("y=%f\n",y);
}

So I can''t see any problem. In asm the stored variables (.data dd, ...) are labelled and this represents the object address, not the object itself. You are accustommed to C or C++. Take a look at the asm code generated by (1), you will see that x=3.07f also requires a memory access, because the float constants can not be part of the asm instructions as it is the case for integers.

Anyway it''s not the fastest way to convert float to fixed point. Adding a 32bits or 64 bits"magic" number to "denormalize" the floating point is better ... if you don''t use the integer right after you stored it.
; x in st(0)
fadd qword ptr[Magic]
fstp qword ptr[TempQWord]
; ...
mov eax, dword ptr[TempQWord]

You can code it in C :
static double temp, magic;

temp = x+magic;
// ...
int i = ((int*)&temp)[ENDIANNESS];

For a PC ENDIANNESS= 0, a PPC =1.

Well search for the good old pentopt.txt on the web. However the fmul fild becomes more and more relevant on modern CPUs and in some contexts.


"Coding math tricks in asm is more fun than Java"
Jan Wassenberg
Jan Wassenberg
AP is somewhat confused.
1. [FixedScale] is perfectly valid, and refers to the contents of the memory labelled FixedScale. MASM (rightfully referred to as quirks mode by TASM), a subset of which is made available by __asm, also allows FixedScale for the same purpose, and requires offset FixedScale for the address. This idiocy is the reason people sometimes use lea when loading the address, which is slower. Anyway, IA-32 doesn''t generally support memory-indirect addressing (exceptions: jmp, call).
2. You are quite right in pointing out that 65536.0 is not normalized. However, I have yet to see an assembler that requires anything other than decimal notation, and doesn''t convert it to IEEE format.
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3
Jolle
Jolle
AP is right if we are talking about a 6809 processor and the like, but I don''t think that''s the case... I pretty sure I''m right if we are talking about normal x86, using for example NASM.
Atm97fin
Atm97fin
Then what''s the difference between fmul and fistp instructions?
EasyGL - easy to use graphics library.
Jolle
Jolle
fmul multiplies st0 with what you specify (here [FixedScale], i.e. 65536.0)
fistp stores st0 as an integer at the place specified (here at the place pFixedNumber), then pops floating point buffer... or whatever it should be called.
Atm97fin
Atm97fin
So, what''s the difference between these commands:


.data
NumberOne dd 65536.0
FixedNumber dd ?

.code
;1.
fmul [NumberOne]
fistp [FixedNumber]
;2.
fmul NumberOne
fistp FixedNumber

EasyGL - easy to use graphics library.
Puzzler183
Puzzler183
The second one doesn''t work? You really need to not do floating point now, and build a basic understand of assembly first.
Jan Wassenberg
Jan Wassenberg
They are equivalent in MASM (see above). This stupid quirk has led to much confusion *sigh*
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.