Project demonstrating NaN (Not a Number) generation and detection across different environments - from bare metal bootloaders to modern C programs with inline assembly.
This project explores how IEEE 754 NaN values are created and detected at different levels of the computing stack:
- Bare Metal Bootloaders - Generate NaN using x87 FPU instructions without an operating system
- C Programs - Generate and detect NaN using standard C library functions
- Inline Assembly - Generate NaN using SSE instructions and detect using UCOMISD
boot32.asm- Fixed version with improved display output- Clearer screen initialization
- Better byte-by-byte hex display
- All 8 bytes (16 hex digits) displayed correctly
-
nan.c- Standard C implementation:- Generates NaN using
0.0 / 0.0 - Detects NaN using
x != xcomparison - Uses
isnan()from<math.h> - Displays hex representation using type punning
- Generates NaN using
-
nan_asm.c- Inline assembly implementation:- Uses SSE instructions (
pxor,divsd) to generate NaN - Uses
ucomisdinstruction to detect NaN via parity flag - Demonstrates low-level NaN detection without library functions
- Uses SSE instructions (
test.s- GCC-generated assembly output (Intel syntax)- Shows how compiler generates NaN detection code
- Uses
ucomisd+jnp(jump if not parity) pattern
- NASM (Netwide Assembler)
- QEMU (for testing without real hardware)
# Ubuntu/Debian sudo apt install nasm qemu-system-x86 # Arch Linux sudo pacman -S nasm qemu-system-x86 # macOS brew install nasm qemu
- GCC (GNU Compiler Collection)
- Make
# Ubuntu/Debian sudo apt install build-essential # Arch Linux sudo pacman -S base-devel # macOS xcode-select --install
# Build everything
make
# Build only bootloaders
make bootloaders
# Build only C programs
make c_programs
# Clean all generated files
make clean# Run boot32.asm (fixed version)
make run-boot32You should see output like:
NaN = 0xFFF8000000000000
Press Ctrl+A then X to exit QEMU.
# Run nan.c
make run-nan
# Run nan_asm.c
make run-nan-asm
# Run all C programs
make testExpected output:
is not the same
x is NaN
NaN hex: 0x7ff8000000000000
A double-precision NaN has:
- Exponent: All 1s (0x7FF)
- Mantissa: Non-zero (0x8000000000000 for quiet NaN)
- Sign bit: Can be 0 or 1
Common quiet NaN values:
0x7FF8000000000000(positive quiet NaN, most common)0xFFF8000000000000(negative quiet NaN, x87 FPU default)
- Self-comparison:
x != xreturnstrueonly for NaN isnan()function: Standard C library functionucomisdinstruction: Sets parity flag (PF) when comparing with NaN- Bit pattern check: Examine exponent and mantissa directly
| Feature | x87 FPU (boot.asm) | SSE (nan_asm.c) | C Standard (nan.c) |
|---|---|---|---|
| NaN value | 0xFFF8... (negative) |
0x7FF8... (positive) |
0x7FF8... (positive) |
| Instructions | FLDZ, FDIV |
PXOR, DIVSD |
Compiler-dependent |
| Detection | Manual comparison | UCOMISD + PF |
isnan() macro |
- Educational: Understanding IEEE 754 floating-point at different abstraction levels
- Debugging: Testing FPU/SSE behavior in custom operating systems
- Bare Metal: Verifying floating-point hardware without OS dependencies
- Low-level Programming: Learning x86 assembly and FPU instructions
- Make sure you're using
qemu-system-x86_64 - Try adding
-serial stdioflag to see serial output - Verify boot signature (0xAA55) is present at offset 510
- Ensure GCC is installed:
gcc --version - Check for SSE2 support:
grep sse2 /proc/cpuinfo - Try removing
-msse2flag if your CPU doesn't support it
- Some compilers optimize away
0.0/0.0 - Use
-O0flag to disable optimizations - Or use the inline assembly version (
nan_asm.c)
This is educational code provided as-is for learning purposes.
Created for exploring NaN behavior across different computing environments.