gen-asm-map - Assembly Source Map Generator¶
Overview¶
gen-asm-map is a tool that generates assembly source line to address mappings for ND-500 assembly files (.s). These mappings enable source-level debugging by tracking which assembly source lines correspond to which addresses in the compiled object code.
Problem Statement¶
The ND-500 toolchain creates source-level debugging maps:
- C source mappings: Generated from stabs debug info (when compiling with
-g) - Assembly mappings: Only generated when NO stabs are present
When C code is compiled with -g, the assembler converts stabs to C mappings but skips assembly line mappings. This means you can step through C source but not the generated assembly.
gen-asm-map solves this by generating assembly line mappings independently, which can then be merged with the C mappings.
Features¶
- ✅ Parses ND-500 assembly syntax (
.sfiles) - ✅ Tracks address changes (
.orgdirectives, instructions, data) - ✅ Handles TEXT, DATA, BSS segments
- ✅ Outputs standard map file format (compatible with nd500-ld)
- ✅ Simple text-based tool (no complex dependencies)
- ✅ Integrates seamlessly with existing build process
Installation¶
Requirements¶
- Python 3.6 or later (no additional packages needed)
Setup¶
# Tool is located in the nd500x repository
cd tools
# Make executable (if not already)
chmod +x gen-asm-map
# Test it
./gen-asm-map --help
Usage¶
Basic Usage¶
# Generate assembly map to stdout
gen-asm-map kernel.s
# Generate assembly map to file
gen-asm-map kernel.s kernel_asm.map
# Generate and merge with existing map
gen-asm-map kernel.s >> kernel.map
Integration with Build Process¶
Step-by-step integration:
# 1. Compile C to assembly with debug info
nd500-cc -g -S kernel.c -o kernel.s
# 2. Assemble (creates kernel.map with C entries only)
nd500-as -am kernel.s -o kernel.o
# 3. Generate assembly line mappings
gen-asm-map kernel.s > kernel_asm.map
# 4. Merge into kernel.map (BEFORE linking!)
cat kernel_asm.map >> kernel.map
# 5. Link (linker reads merged kernel.map)
nd500-ld -m -o kernel locore.o kernel.o
# Result: Final map has BOTH C and assembly entries!
Makefile Integration¶
Example Makefile rules:
# Variables
GEN_ASM_MAP = $(REPO_ROOT)/tools/gen-asm-map
# Rule to assemble with merged maps
%.o: %.s
# Assemble (creates .map with C entries from stabs)
$(AS) -am $< -o $@
# Generate assembly line mappings and merge
$(GEN_ASM_MAP) $< >> $*.map
@echo "✓ Assembled $< with merged C+assembly map"
Complete example (see examples/05-c-kernel/Makefile):
TOOLS_DIR = ../../tools
GEN_ASM_MAP = $(TOOLS_DIR)/gen-asm-map
kernel.o: kernel.s
@echo "Assembling kernel.s with map generation..."
$(AS) -am kernel.s -o kernel.o
@echo "✓ Generated kernel.map (C source entries)"
@echo "Generating assembly line mappings..."
$(GEN_ASM_MAP) kernel.s >> kernel.map
@echo "✓ Merged assembly entries into kernel.map"
Map File Format¶
Input (Assembly File)¶
; kernel.s
.text
.org 0
start:
nop ; Line 5
call _main, $0 ; Line 6
halt ; Line 7
_main:
mov $1, A1 ; Line 10
return ; Line 11
Output (Map File)¶
; Map file for kernel.s
; Format: filename:line (SEG) -> address (octal) # segment_name
; Generated by gen-asm-map
;
kernel.s:5 (T) -> 000000 # TEXT
kernel.s:6 (T) -> 000002 # TEXT
kernel.s:7 (T) -> 000004 # TEXT
kernel.s:10 (T) -> 000006 # TEXT
kernel.s:11 (T) -> 000010 # TEXT
Merged Map (After Integration)¶
; Map file for kernel.o
; Format: filename:line (SEG) -> address (octal)
;
kernel.c:100 (T) -> 000000 # C source line (from stabs)
kernel.c:101 (T) -> 000002
kernel.s:5 (T) -> 000000 # Assembly source line (from gen-asm-map)
kernel.s:6 (T) -> 000002
kernel.c:102 (T) -> 000006
kernel.s:10 (T) -> 000006
How It Works¶
Address Tracking¶
The tool maintains a current address counter and increments it based on:
-
.orgdirective: Sets address explicitly.org 0x100 ; Sets current address to 0x100 -
Instructions: Advance address (estimated 2 bytes minimum)
nop ; +2 bytes call _foo, $0 ; +2 bytes (actual may vary) -
Data directives: Advance by data size
.word 0x1234 ; +2 bytes .byte 42 ; +1 byte .long 0 ; +4 bytes -
Segment changes: Track TEXT/DATA/BSS
.text ; Segment = 'T' .data ; Segment = 'D' .bss ; Segment = 'B'
What's NOT Tracked¶
- Labels: Don't generate code, not mapped
- Comments: Ignored
- Assembler directives:
.globl,.extern, etc. - no code generated - Stabs directives:
.stabs,.stabn- handled by assembler
Limitations¶
-
Instruction length estimation: Default 2 bytes per instruction
- Actual ND-500 instructions vary (2-10 bytes)
- For perfect accuracy, would need full instruction decoder
- Good enough for debugging (maps to instruction start)
-
Macros: Not expanded (would need assembler's preprocessor)
-
Include files: Not processed (would need full preprocessor)
For most use cases, these limitations don't affect functionality since the goal is mapping source lines to approximate addresses for debugging.
Relocation Handling¶
CRITICAL: Addresses in the generated map file are relative to the object file (start at 0).
The linker automatically applies relocation when linking multiple object files:
Before linking (kernel.o):
kernel.s:10 -> 000000 (relative address)
After linking (final binary):
kernel.s:10 -> 000032 (relocated address, e.g., +0x32 offset)
This is handled automatically by nd500-ld - you don't need to do anything special!
Examples¶
Example 1: Single File Build¶
# Compile
nd500-cc -g -S program.c -o program.s
# Assemble
nd500-as -am program.s -o program.o
# Add assembly mappings
gen-asm-map program.s >> program.map
# Link
nd500-ld -m -o program program.o
# Result: program.map has both C and assembly entries
Example 2: Multi-File Build¶
# Build locore.o (bootstrap, no stabs)
nd500-as -am locore.s -o locore.o
gen-asm-map locore.s >> locore.map
# Build kernel.o (has stabs from -g)
nd500-cc -g -S kernel.c -o kernel.s
nd500-as -am kernel.s -o kernel.o
gen-asm-map kernel.s >> kernel.map
# Link (merges both maps with relocation)
nd500-ld -m -o kernel locore.o kernel.o
# Result: kernel.map has entries from:
# - locore.s (assembly)
# - kernel.c (C from stabs)
# - kernel.s (assembly from gen-asm-map)
Example 3: Automated Build (Makefile)¶
See complete working example: examples/05-c-kernel/Makefile
Key excerpt:
%.o: %.s
$(AS) -am $< -o $@
$(GEN_ASM_MAP) $< >> $*.map
Debugging¶
Verify Tool Output¶
# Generate map
gen-asm-map test.s > test.map
# Check format
cat test.map
# Expected output:
; Map file for test.s
; Format: filename:line (SEG) -> address (octal) # segment_name
; Generated by gen-asm-map
;
test.s:10 (T) -> 000000 # TEXT
test.s:11 (T) -> 000002 # TEXT
Verify Integration¶
# After building, check final map has both C and assembly entries
grep "\.c:" kernel.map | wc -l # Count C entries
grep "\.s:" kernel.map | wc -l # Count assembly entries
# Both should be non-zero
Common Issues¶
Issue: "No assembly entries in final map"
- Cause: Forgot to run
gen-asm-mapor forgot to merge - Fix: Add
gen-asm-map kernel.s >> kernel.mapafter assembly
Issue: "Wrong addresses in final map"
- Cause: Merged after linking (linker already wrote final map)
- Fix: Merge BEFORE linking, then run linker
Issue: "Duplicate entries in map"
- Cause: Running
gen-asm-map >>multiple times - Fix: Use
>first time, or clean before rebuild
Performance¶
- Speed: Processes ~10,000 lines/second (typical .s files are 1,000-10,000 lines)
- Memory: Minimal (stores only line→address mappings, ~100 bytes per entry)
- Impact on build time: < 0.1 seconds per file
Integration Checklist¶
When integrating into a new project:
- [ ] Copy
tools/gen-asm-mapto your project or reference from nd500x repo - [ ] Add Makefile rules to run
gen-asm-mapafter assembly - [ ] Ensure merge happens BEFORE linking
- [ ] Test with
grep "\.s:" final.mapto verify assembly entries - [ ] Update project documentation
- [ ] Add clean rule to remove generated map files
Troubleshooting¶
Tool doesn't run¶
# Check Python version
python3 --version # Should be 3.6+
# Check permissions
ls -l tools/gen-asm-map
# Should have 'x' permission
# Make executable if needed
chmod +x tools/gen-asm-map
Wrong output format¶
# Verify input is valid assembly
file kernel.s # Should be "ASCII text"
# Check for parsing errors
gen-asm-map kernel.s 2>&1 | head
Map entries missing¶
- Verify assembly file has actual instructions (not just directives)
- Check for
.orgdirectives that might skip addresses - Review segment changes (.text/.data/.bss)
See Also¶
- nd500-as: ND-500 assembler (generates map from stabs)
- nd500-ld: ND-500 linker (merges and relocates maps)
- nd500x debugger: Uses map files for source-level debugging
- examples/05-c-kernel: Complete working example
Contributing¶
To improve gen-asm-map:
- Better instruction length estimation: Add instruction decoder for accurate lengths
- Macro expansion: Process macros to map expanded lines
- Include file support: Follow
.includedirectives - Optimization: C implementation for faster processing
Submit improvements to the nd500x project repository.
License¶
Same as the ND500X project.
Contact¶
For issues or questions about gen-asm-map, see the ND500X project documentation.