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

test.asm文件打开语句是否正确?程序无交互无输出排查

问题分析与修复方案

核心问题

  • 程序实际执行的是第一个main()函数,它仅完成读取test.asm文件到缓冲区的操作,随后直接退出,完全没有调用负责汇编解析、编码和输出的main2()函数。
  • main2()是实际的汇编处理入口,但从未被执行,导致没有指令解析,也不会生成输出文件。

修复步骤

  1. 调整程序入口:删除冗余的main()函数,将main2()重命名为main(),让程序启动时执行汇编处理逻辑。
  2. 正确调用程序:编译后通过命令行参数指定输入输出,示例:
    # 生成二进制输出文件
    ./assembler -o output.bin -i test.asm
    # 打印十六进制输出到控制台
    ./assembler -t -i test.asm
    
  3. 修复内存泄漏:encodeInstruction()中malloc的instruction指针需添加free(instruction);释放。
  4. 完善文件操作:printFILE()中添加fclose(file);确保文件写入完成并释放资源。
  5. 修正位运算优先级:encodeInstruction()中0xFF & curInstruction >> 24需加括号,改为0xFF & (curInstruction >> 24),避免运算顺序错误。

修改后的关键代码示例

// 删除原main()函数,将main2()重命名为main()
int main(int argc, char *argv[])
{
    char outfile[1024], infile[1024];
    infile[0] = 0;
    char requestText = 0, requestBinary = 0;
    if (argc < 2)
    {
        printf("assemble {-o filename | -t} -i test.asm\n");
        return(1);
    }
    
    for (int i = 1; i < argc; i++)
    {
        if (strcmp(argv[i], "-t") == 0)
        {
            requestText = 1;
        }
        else if(strcmp(argv[i], "-o") == 0)
        {
            i++;
            requestBinary = 1;
            strcpy(outfile, argv[i]);
        } 
        else if(strcmp(argv[i], "-i") == 0)
        {
            i++;
            strcpy(infile, argv[i]);
        }
        
    }
    
    if (requestBinary == 0 &&  requestText == 0)
    {
        printf("Must specify at least one output\n");
        return(0);
    }
    
    if (strlen(infile) == 0)
    {
        printf("Must specify input file with -i option\n");
        return(1);
    }
    
    unsigned char data[1024], *storage, *code, *curCode, *curStorage;
    storage = curStorage = MEMORY_STORAGE = data;
    code = curCode = data + 512;
        
    for (int i = 0; i < 1024; i++)
    {
        data[i] = 0;
    }
    
    unsigned char *line = malloc(sizeof(unsigned char)*1024);
    FILE *input = fopen(infile, "r");
    if (input == 0)
    {
        printf("Could not open input file\n");
        return(2);
    }
    while (fgets(line, 1024, input) != NULL)
    {
        curCode += encodeInstruction(line, curCode, &curStorage);
    }
    free(line);
    
    if (requestText)
    {
        printHEX(data);
    }
    if (requestBinary)
    {
        printFILE(data, outfile);
    }   
    return(0);
}

// 修复内存泄漏与位运算优先级
int encodeInstruction(unsigned char *line, unsigned char *data, unsigned char **storage )
{
    int offset = 0;
    char *instruction = malloc(sizeof(unsigned char)*1024);
    unsigned int curInstruction;
    strcpy(instruction, line);
    instruction = strtok(instruction, "\t");
    char *param = strtok(NULL, "\t");
    
    if (strcmp(instruction, "add") == 0)
    {
        curInstruction = encodeRA(0x20, param, 0);
        offset = 4;
    }
    else if (strcmp(instruction, "sub") == 0)
    {
        curInstruction = encodeRA(0x22, param, 0);
        offset = 4;
    }
    // ... 其余指令处理逻辑保持不变 ...
    
    if (offset == 4)
    {
        data[0] = 0xFF & (curInstruction >> 24);
        data[1] =  0xFF & (curInstruction >> 16);
        data[2] =  0xFF & (curInstruction >> 8);
        data[3] =  0xFF & curInstruction;
    }
    
    free(instruction);
    return(offset);
}

// 修复文件未关闭问题
void printFILE(unsigned char *storage, char *outfile)
{
    FILE *file = fopen(outfile, "w");
    
    if (file == 0)
    {
        printf("Could not open output file\n");
        exit(2);
    }
    size_t data_out = fwrite(storage, 1, 1024, file); 
    fclose(file);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 01:37:57