You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用nasm将目录下所有.asm文件转换为.bin文件?

Hey there! Let's walk through how to batch convert all .asm files in a directory (and subdirectories, if needed) to .bin files using NASM. I'll cover both the approaches you asked for—placing .bins right next to their source .asm files, and setting up a mirrored directory structure for all outputs.

Option 1: Generate .bin Files Alongside .asm Files

This is the simpler approach: each .asm file gets a matching .bin file in the same folder.

For Linux/macOS (Bash Script)

Create a shell script (e.g., compile_asm.sh) with this code:

#!/bin/bash

# Loop through all .asm files in the current directory and subdirectories
find . -type f -name "*.asm" | while read -r asm_file; do
    # Replace the .asm extension with .bin for the output file
    bin_file="${asm_file%.asm}.bin"
    # Compile with NASM: -f bin specifies flat binary format, -o sets output path
    nasm -f bin "$asm_file" -o "$bin_file"
    echo "Compiled $asm_file -> $bin_file"
done

Make it executable with chmod +x compile_asm.sh, then run it. It'll recursively process all .asm files in your directory tree.

For Windows (Batch Script)

Create a batch file (e.g., compile_asm.bat) with this code:

@echo off
setlocal enabledelayedexpansion

REM Loop through all .asm files in current directory and subdirectories
for /r %%f in (*.asm) do (
    REM %%~nf gets the filename without extension; build the .bin path
    set "bin_file=%%~dpnf.bin"
    REM Compile with NASM
    nasm -f bin "%%f" -o "!bin_file!"
    echo Compiled %%f -> !bin_file!
)
endlocal
pause

Double-click the batch file to run it. It'll handle spaces in filenames and process subdirectories too.

Option 2: Create a Mirrored Directory for .bin Files

If you want to keep your source files clean and store all .bins in a separate, mirrored directory structure:

For Linux/macOS (Bash Script)

Create a script like compile_asm_mirrored.sh:

#!/bin/bash

# Define your output root directory (change this to whatever you want)
OUTPUT_DIR="bin_output"

# Create the output root if it doesn't exist
mkdir -p "$OUTPUT_DIR"

# Find all .asm files recursively
find . -type f -name "*.asm" | while read -r asm_file; do
    # Get the relative path of the .asm file (without the leading ./)
    relative_path="${asm_file#./}"
    # Replace .asm extension with .bin and prepend the output directory
    bin_file="$OUTPUT_DIR/${relative_path%.asm}.bin"
    # Create the parent directory for the .bin file (so we don't get "no such file or directory" errors)
    mkdir -p "$(dirname "$bin_file")"
    # Compile the file
    nasm -f bin "$asm_file" -o "$bin_file"
    echo "Compiled $asm_file -> $bin_file"
done

Run it the same way as the first script. It'll create a bin_output folder that mirrors your source directory structure, with all .bin files in their corresponding places.

For Windows (Batch Script)

Create compile_asm_mirrored.bat:

@echo off
setlocal enabledelayedexpansion

REM Define your output root directory
set OUTPUT_DIR=bin_output

REM Create output root if it doesn't exist
if not exist "%OUTPUT_DIR%" mkdir "%OUTPUT_DIR%"

REM Recursively process all .asm files
for /r %%f in (*.asm) do (
    REM Get the full path of the .asm file without extension
    set "asm_full=%%~dpnf"
    REM Get the relative path from the current directory
    set "relative=!asm_full:%cd%=!"
    REM Build the output .bin path
    set "bin_file=%OUTPUT_DIR%!relative!.bin"
    REM Create the parent directory for the .bin file
    mkdir "!bin_file!\.." 2>nul
    REM Compile with NASM
    nasm -f bin "%%f" -o "!bin_file!"
    echo Compiled %%f -> !bin_file!
)
endlocal
pause

This will create a bin_output folder with the same subdirectory structure as your source, containing all the compiled .bin files.

Quick Notes

  • Make sure NASM is added to your system's PATH environment variable. If not, replace nasm with the full path to your NASM executable (e.g., C:\nasm\nasm.exe on Windows).
  • If you don't need to process subdirectories, remove the find . -type f part (Linux) or /r flag (Windows) from the loops.
  • You can add extra NASM flags if needed (e.g., -g for debug info, though flat binary .bin files don't typically use this).

内容的提问来源于stack exchange,提问作者user13605748

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 21:17:31