Skip to content

Migrate NASM code to GAS #414

@pbalduino

Description

@pbalduino

Goal

Migrate the remaining NASM assembly files (.s extension) to GAS (GNU Assembler) format (.S extension), enabling full integration with the GCC toolchain and C preprocessor.

Context

Currently, MeniOS uses both NASM and GAS for assembly code:

  • NASM files (.s): Legacy stand-alone stubs with no C preprocessor usage
  • GAS files (.S): Modern assembly that integrates with GCC, using CPP and sharing headers/macros with C code

We lean on GAS for the .S sources because they depend on the C preprocessor and share headers/macros with the C toolchain; letting GCC drive GAS keeps those files in the same compilation flow as the rest of the kernel and avoids re-implementing the macro plumbing in NASM.

The few stand-alone .s files that NASM still handles (referenced in Makefile:733-735) are legacy stubs with no CPP usage, so it's been simpler to leave them there until we either convert them to .S or drop NASM entirely.

Current NASM Files

Three files remain in NASM format:

  1. src/kernel/lgdt.s - Load GDT (Global Descriptor Table)
  2. src/kernel/lidt.s - Load IDT (Interrupt Descriptor Table)
  3. src/kernel/pit.s - Programmable Interval Timer

These are compiled explicitly in the Makefile:

$(NASM) -f elf64 ./src/kernel/lgdt.s
$(NASM) -f elf64 ./src/kernel/pit.s
$(NASM) -f elf64 ./src/kernel/lidt.s

Benefits of Migration

  1. Unified toolchain: Use GCC/GAS exclusively, eliminating NASM dependency
  2. C preprocessor integration: Access to #include, #define, and shared kernel macros
  3. Consistent syntax: All assembly in GAS AT&T syntax
  4. Simplified build: Single compilation flow through GCC
  5. Better maintainability: Assembly code follows same conventions as other .S files

Implementation Plan

1. Convert lgdt.s to lgdt.S

  • Translate NASM Intel syntax → GAS AT&T syntax
  • Update instruction syntax (e.g., lgdt [rdi]lgdt (%rdi))
  • Add .intel_syntax noprefix directive if needed to minimize changes
  • Update Makefile to compile with GCC

2. Convert lidt.s to lidt.S

  • Similar translation to lgdt.s
  • Verify IDT loading still works correctly

3. Convert pit.s to pit.S

  • Translate PIT setup code
  • Ensure timer interrupts still fire correctly

4. Update Build System

  • Remove NASM-specific rules from Makefile (lines 733-735)
  • Remove ARCH_NASM_OBJECTS variable (line 141)
  • Update KERNEL_OBJ_FILES to use standard compilation
  • Remove NASM from BUILD_REQUIRED_TOOLS check (line 726)

5. Update Documentation

  • Remove NASM from toolchain requirements
  • Document GAS as the standard assembler

Testing & Validation

  • Kernel boots successfully
  • GDT loads correctly (check segmentation)
  • IDT loads correctly (test interrupts)
  • PIT timer fires at correct frequency
  • All existing tests pass

Related Issues

Notes

  • GAS supports both AT&T syntax (default) and Intel syntax via .intel_syntax directive
  • Most existing .S files use AT&T syntax for consistency with GCC conventions
  • Consider whether to use AT&T or Intel syntax for new .S files (recommend AT&T for consistency)

Definition of Done

  • All three .s files converted to .S format
  • NASM removed from build dependencies
  • Makefile cleaned up (NASM rules removed)
  • Kernel builds and boots successfully
  • All interrupts and timers working correctly
  • Documentation updated

Priority

HIGH - Critical for GCC/self-hosting milestone

Justification

This issue is essential for the Road to GCC milestone:

  1. Unified Toolchain: Eliminates NASM dependency, standardizing on GCC/GAS
  2. Build System Simplification: Enables Refactor monolithic Makefile into modular domain-focused structure #352 (Makefile refactoring)
  3. Self-Hosting Requirement: GCC compilation requires consistent toolchain
  4. Maintainability: All assembly follows same conventions

Impact

Estimated Effort: 2-3 weeks part-time


Related Issues:

Metadata

Metadata

Assignees

Labels

buildBuild system and toolchainkernelKernel-level implementation

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions