Floating point (SSE)
The XMM registers
x86-64 floats live in sixteen 128-bit XMM registers. SSE2 is baseline on every x86-64 CPU, so scalar double arithmetic uses the SD (scalar double) forms: movsd, addsd, subsd, mulsd, divsd:
movsd xmm0, [pi] addsd xmm0, xmm1 mulsd xmm0, [two]
SSE arithmetic
Each instruction takes two operands like the integer ones: destination first, source second, both either XMM registers or one memory operand. Comparisons set the integer flags, so COMISD plus a conditional jump branches on a float comparison:
comisd xmm0, xmm1 ja xmm0_greater jb xmm0_less
Printing a double
To print a double, pass it in XMM0 to printf with a %f format string, set AL to 1 (one vector register used), and link against libc. Raw sys_write has no float formatting: convert to text yourself, or use printf. This complete program adds 3.5 and 2.0 and prints the result:
default rel extern printf section .data pi dq 3.5 two dq 2.0 fmt db "%.1f", 10, 0 section .text global main main: push rbp movsd xmm0, [pi] addsd xmm0, [two] mov rdi, fmt mov eax, 1 ; one vector register call printf wrt ..plt pop rbp xor eax, eax ret
nasm -f elf64 -o float.o float.asm
gcc -no-pie -o float float.o
./float
echo $?
5.5
0
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How many XMM registers does x86-64 have?
Answer
16
The sixteen XMM registers are xmm0 through xmm15, each 128 bits wide.
-
Which instruction adds two scalar doubles?
Answer
addsd
SD means scalar double; PS is packed single and PD packed double.
-
What does the SD suffix mean?
Answer
Scalar double
The instruction works on the low double of the register and leaves the other bits.
-
Which register passes a double argument to printf?
Answer
XMM0
Floating-point arguments go in XMM0 through XMM7.
-
What must AL hold when calling a variadic function with one double?
Answer
1
AL counts the vector registers that carry arguments; one double means 1.
-
Why cannot raw sys_write print a double?
Answer
sys_write has no floating-point formatting
write prints bytes, so the program must convert the number to text itself or use printf.
-
What does this program print?
default rel extern printf section .data a dq 1.5 b dq 2.25 fmt db "%.2f", 10, 0 section .text global main main: push rbp movsd xmm0, [a] addsd xmm0, [b] mov rdi, fmt mov eax, 1 call printf wrt ..plt pop rbp xor eax, eax retAnswer
3.75
1.5 plus 2.25 is 3.75, and the format prints two decimals.
-
Which instruction compares two scalar doubles?
Answer
comisd
COMISD compares the low doubles and sets the integer flags for a conditional jump.
-
What does
movsd xmm0, [pi]load?Answer
One double into the low half of xmm0
The scalar load reads eight bytes and leaves the rest of the register unchanged.
-
Which instruction multiplies a packed pair of doubles?
Answer
mulpd
PD is packed double: two doubles in one XMM register.
The short snippets on this page are fragments: they show the SSE forms. The complete program was assembled with NASM 3.01 and linked with gcc 15.2.0, and it was run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev