Code Refactoring and Bug Fixing with Large Language Models in Low-Level System Development

    This article documents a hands-on test of using large language models (LLMs) to repair and refactor abandoned legacy code in UEFI/BIOS development—one of the most demanding areas of low-level systems programming. After one model failed to get a Tetris game running in a UEFI environment, another LLM was brought in to take over the seven-file codebase. Through two rounds of iteration—first getting the program to run, then refining it—the AI successfully diagnosed a wild-pointer issue caused by GOP QueryMode after SetMode, fixed an incorrect GOP GUID configuration, and brought the game to life. The article also summarizes the core strengths LLMs bring to low-level development, including cross-module code comprehension, pointer and memory debugging, and protocol knowledge retrieval, along with how cloud platforms can accelerate such workflows.

    Introduction

    AI-assisted programming is now commonplace at the application layer, such as web and mobile development. But its performance in low-level system development—UEFI/BIOS in particular—has rarely been explored in depth. Low-level development comes with tough challenges: cumbersome environment setup, strict hardware interface requirements, and unforgiving memory management. I recently ran a deep test of LLMs’ ability to develop and repair code in a low-level environment, and the results revealed surprising technical potential and practical value.

    Test Background and Task Definition

    In an earlier test, I asked two different LLMs to each build a low-level game in a UEFI environment. Model A performed impressively: in 2.5 hours it wrote a Breakout game that compiled on the first attempt and ran successfully the first time. Model B, attempting Tetris, struggled for hours, never managed to set up a working build environment, and left behind only a well-structured, modular codebase of seven files—a program that couldn’t run at all.

    Throwing away that half-finished, non-functional code seemed like a waste. So I decided to have Model A take over Model B’s mess and try to fix and run the Tetris game. The idea was to fight fire with fire—to test an AI’s ability to take over, understand, and refactor complex legacy code. As before, the test ran in two rounds: the first round focused on getting the code to run; the second on refinement and optimization.

    Round One: From Dead Code to a Running Program

    I copied Model B’s source code, untouched, into a fresh build environment and brought in Model A to take over.

    Step 1: Full Code Review

    The takeover instruction was direct: “Review this code and find potential problems.” Model A quickly shifted into analysis mode, examining each module in turn—the entry function, the main loop, game logic, the rendering module, and input handling. After a few minutes of deep traversal, it produced a detailed list of issues and stated that the program should run once they were fixed.

    Step 2: Debugging Crashes and Memory Anomalies

    Following the AI’s suggested fixes, the first run got the program to start, but it crashed immediately after printing “Starting Game Loop” on screen. I guided Model A to add more detailed debug output and monitor execution progress over the serial port.

    Analyzing the serial logs, Model A pinpointed an exception occurring after a GOP (Graphics Output Protocol) call. Digging further into the code logic, it identified the problem in the info pointer returned by QueryMode—a classic wild-pointer case. After SetMode switches the display mode, the memory the original pointer refers to becomes invalid. The AI’s fix was precise: re-query the framebuffer information after the SetMode operation. With that fix applied, the program stopped crashing.

    Step 3: Fixing the Failed Graphics Rendering

    Although the program no longer crashed, the expected graphical interface still didn’t appear. I fed the symptom back to Model A. After a second round of analysis of the run results, it focused on the GOP protocol’s GUID configuration. In common UEFI development frameworks, multiple GOP-related GUIDs are defined, and if the wrong one is configured, the system cannot properly obtain the graphics output protocol. Model A re-checked the GUID definitions, corrected the configuration, and the game finally rendered on screen.

    Deep Dive: The Technical Value of AI in Low-Level Development

    From this practice, several core strengths of LLMs in low-level system development stand out.

    First, strong cross-module logical comprehension. Even when facing code it did not generate, the LLM quickly untangled the call relationships between the entry function, the game main loop, and the rendering module—breaking the stereotype that AI can only handle isolated functions.

    Second, precise memory and pointer debugging. In a UEFI environment, without the memory protection mechanisms of a modern OS, a wild pointer often causes an outright system reboot or freeze. The LLM was able to infer from context how the SetMode operation affected memory validity, demonstrating a deep understanding of low-level runtime behavior.

    Third, knowledge retrieval and protocol adaptation. For a complex protocol like GOP, the AI could accurately distinguish the purposes of different GUIDs and quickly locate a configuration-level error when rendering failed—significantly reducing the time developers spend digging through low-level manuals.

    Supporting Development with Cloud Platforms

    In real-world enterprise development, building and debugging this kind of low-level code often requires stable infrastructure. With elastic compute instances and logging services from cloud platforms such as Baidu AI Cloud, developers can quickly spin up standardized build environments and use cloud-based monitoring and alerting to capture serial output and application status in real time. Combining a cloud environment with AI coding tools can dramatically shorten the write-compile-feedback loop and improve overall engineering efficiency.

    Conclusion

    The value of large language models in programming is extending from pure “code generation” toward “code understanding, review, and refactoring.” Faced with structurally complex, even subtly flawed legacy code, AI has shown the potential of a “senior engineer.” With clear feedback mechanisms and reasonable guidance, AI can genuinely become an indispensable assistant in low-level system development, offering fresh approaches to maintaining and refactoring complex systems.