I'm using Watcom C++ and I'm trying to speed up my programs by using inline assembler. I have a very simple task: I have two float-variables, and I want to copy a value like this:
var1=var2;
From one book I have I got an impresson that it should be done this way in assembler:
fld var2
fst var1
But then I get a runtime error "domain error in sqrt". It's a bit strange, since it has nothing to do with squareroots, but that's what happens. Is my code missing something essential?
Comments
In assembly, your problem should be solved by this code:
PUSH EAX
MOV EAX,var1
MOV var2,EAX
POP EAX
It should be enough for Integer 32bit variables. to use 16 bit vars, use AX instead of EAX, and to use 8 bit variables, use AL or AH. But I recommend you dont change the PUSHs and POPs.
davimedrade@hotmail.com
Davi Medrade
Brazil.
:
: In assembly, your problem should be solved by this code:
:
: PUSH EAX
: MOV EAX,var1
: MOV var2,EAX
: POP EAX
:
: It should be enough for Integer 32bit variables. to use 16 bit vars, use AX instead of EAX, and to use 8 bit variables, use AL or AH. But I recommend you dont change the PUSHs and POPs.
:
: davimedrade@hotmail.com
: Davi Medrade
: Brazil.
:
[blue]Davi,
The question was about the [b][italic]float[/italic][/b] values, not integer. The [b][italic]float[/italic][/b] usually takes more than 32 bit.[/blue]
64-bit:
PUSH ESI
PUSH EDI
LEA DI,Var2
LEA SI,Var1
CLD
MOVSW ;Moves 16-bits
MOVSW ;Moves more 16-bits
POP EDI
POP ESI
Note: ES must have the segment of the destination variable.
Note: DS must have the segment of the source variable.
You can add more MOVSW instructions above, in order to move the complete variable. The formula is:
numMOVSWs=(Numbits/16)
You must have numMOVSWs instrunctions MOVSW.
Davi Medrade
Master Programmer of Agnetron Software Ltda.
davimedrade@hotmail.com
So Paulo, Brazil
If it's 64-bit, should it not move two doublewords rather than two words?!
[code]
push ds
push es
push ax
push si
push di
mov ax,seg Var1
mov si,offset Var1
mov ds,ax
mov ax,seg Var2
mov di,offset Var2
mov es,ax
cld
movsd
movsd
pop di
pop si
pop ax
pop es
pop ds
ret
[/code]
numMOVSWs=(Numbits/16)
2 != 64 / 16
numMOVSDs=(Numbits/32)
2 = 64 / 32
Davi Medrade
Master Programmer of Agnetron Software Ltda.
davimedrade@hotmail.com
So Paulo, Brazil
[code]
;64-bit:
PUSH ESI
PUSH EDI
LEA DI,Var2
LEA SI,Var1
CLD
MOVSW ;Moves 16-bits
MOVSW ;Moves more 16-bits
POP EDI
POP ESI
[/code]
Two MOVSWs copy only 32-bits, not 64.
: [code]
: ;64-bit:
:
: PUSH ESI
: PUSH EDI
: LEA DI,Var2
: LEA SI,Var1
: CLD
: MOVSW ;Moves 16-bits
: MOVSW ;Moves more 16-bits
: POP EDI
: POP ESI
: [/code]
: Two MOVSWs copy only 32-bits, not 64.
:
Yes, but I said that the number of MOVSWs is calculated according to my formula. Using 64 in my formula will result in 4. That is the number of MOVSWs you will need to use to move 64 bits. (16*4=64)
Davi Medrade
Master Programmer of Agnetron Software Ltda.
davimedrade@hotmail.com
So Paulo, Brazil