6

Is the only way to move a value into an xmm register by first moving the value into an integer register, dunno what they are called, and then into the xmm register e.g.

mov   [eax], (float)1000   ; store to memory
movss xmm1,[eax]           ; reload

or

mov        eax,  1000       ; move-immediate integer
cvtsi2ss   xmm1,eax         ; and convert

or is there another way? Is there a way to directly move a value into a xmm register, something along the lines of: movss xmm1,(float)1000?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
JazzEX
  • 119
  • 1
  • 2
  • 6

2 Answers2

7

There are no instructions to load an SSE register with an immediate. The common idiom is to load the value you need from a global constant:

const   dd 1000.0

...

        movss xmm0,[const]
fuz
  • 88,405
  • 25
  • 200
  • 352
  • 2
    You can of course do it with 2 instructions using `mov eax, imm32` / `movd xmm0, eax`, but yes, static constants are often best. Related: [What are the best instruction sequences to generate vector constants on the fly?](https://stackoverflow.com/questions/35085059/what-are-the-best-instruction-sequences-to-generate-vector-constants-on-the-fly). – Peter Cordes Dec 22 '17 at 19:35
4

It depends on the assembler.

UASM:

LOADSS xmm1,1000.0

ASMC, FASM, POASM, JWasm:

mov eax,1000.0
movd xmm1,eax

NASM:

mov eax,__?float32?__(1000.0)
movd xmm1,eax

MASM, YASM, Sol_Asm:

mov eax,447A0000h
movd xmm1,eax

You can also use a macro which creates constants in the data section. UASM already has a built-in macro FP4:

movss xmm1,FP4(1000.0)

If you use ASMC, POASM, JWasm or MASM, you can define this macro:

FP4 MACRO value
 LOCAL vname
 .const
 align 4
 vname REAL4 value
 .code
 EXITM <vname>
ENDM
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
palota
  • 465
  • 4
  • 8
  • 1
    The choice of whether to load a constant from memory or bounce an immediate through an integer register is independent of the assembler. It's weird that for MASM you show a macro that creates a constant in `.data`, while others use an immediate. Also, these are "assemblers", not "compilers". The difference is that it's up to you to make optimization choices like immediate vs. load from `.rdata`, not leaving it to a compiler to pick the best one. – Peter Cordes Apr 05 '21 at 00:25
  • `LOADSS` is presumably some kind of macro or pseudo-instruction provided by UASM. I can't find documentation on it; do you know what actual instructions it expands to? – Nate Eldredge Apr 05 '21 at 00:26
  • LOADSS expands to mov eax,1000.0; vmovd xmm1,eax – palota Apr 06 '21 at 08:13