.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.
🔒 Locked
Strange looking assembly code
Advertisement
Original Post
Should this code work?
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]
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.
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.
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]
"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]
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]
EDIT: BTW, I posted while Jolle was posting...
[edited by - Puzzler183 on November 29, 2003 11:10:53 AM]
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.
(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.
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.
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.
The second one doesn''t work? You really need to not do floating point now, and build a basic understand of assembly first.
They are equivalent in MASM (see above). This stupid quirk has led to much confusion *sigh*
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement