Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

NaN Testing Project

Project demonstrating NaN (Not a Number) generation and detection across different environments - from bare metal bootloaders to modern C programs with inline assembly.

Overview

This project explores how IEEE 754 NaN values are created and detected at different levels of the computing stack:

  1. Bare Metal Bootloaders - Generate NaN using x87 FPU instructions without an operating system
  2. C Programs - Generate and detect NaN using standard C library functions
  3. Inline Assembly - Generate NaN using SSE instructions and detect using UCOMISD

Files

Bootloaders (16-bit Real Mode)

  • 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

C Programs

  • nan.c - Standard C implementation:

    • Generates NaN using 0.0 / 0.0
    • Detects NaN using x != x comparison
    • Uses isnan() from <math.h>
    • Displays hex representation using type punning
  • nan_asm.c - Inline assembly implementation:

    • Uses SSE instructions (pxor, divsd) to generate NaN
    • Uses ucomisd instruction to detect NaN via parity flag
    • Demonstrates low-level NaN detection without library functions

Assembly Output

  • test.s - GCC-generated assembly output (Intel syntax)
    • Shows how compiler generates NaN detection code
    • Uses ucomisd + jnp (jump if not parity) pattern

Requirements

For Bootloaders

  • 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

For C Programs

  • GCC (GNU Compiler Collection)
  • Make
    # Ubuntu/Debian
    sudo apt install build-essential
    
    # Arch Linux
    sudo pacman -S base-devel
    
    # macOS
    xcode-select --install

Building

# Build everything
make

# Build only bootloaders
make bootloaders

# Build only C programs
make c_programs

# Clean all generated files
make clean

Running

Bootloaders in QEMU

# Run boot32.asm (fixed version)
make run-boot32

You should see output like:

NaN = 0xFFF8000000000000

Press Ctrl+A then X to exit QEMU.

C Programs

# Run nan.c
make run-nan

# Run nan_asm.c
make run-nan-asm

# Run all C programs
make test

Expected output:

is not the same
x is NaN
NaN hex: 0x7ff8000000000000

Technical Details

IEEE 754 NaN Representation

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)

NaN Detection Methods

  1. Self-comparison: x != x returns true only for NaN
  2. isnan() function: Standard C library function
  3. ucomisd instruction: Sets parity flag (PF) when comparing with NaN
  4. Bit pattern check: Examine exponent and mantissa directly

Differences Between Implementations

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

Use Cases

  • 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

Troubleshooting

Bootloader doesn't display anything in QEMU

  • Make sure you're using qemu-system-x86_64
  • Try adding -serial stdio flag to see serial output
  • Verify boot signature (0xAA55) is present at offset 510

C programs don't compile

  • Ensure GCC is installed: gcc --version
  • Check for SSE2 support: grep sse2 /proc/cpuinfo
  • Try removing -msse2 flag if your CPU doesn't support it

"NaN not detected" in C programs

  • Some compilers optimize away 0.0/0.0
  • Use -O0 flag to disable optimizations
  • Or use the inline assembly version (nan_asm.c)

References

License

This is educational code provided as-is for learning purposes.

Author

Created for exploring NaN behavior across different computing environments.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages