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

GUI Turbo Assembler报错:Unexpected end of file encountered求助

汇编代码编译报错:Unexpected end of file encountered 解决方法

我刚接触汇编领域,仍在学习阶段,缺乏使用GUI Turbo Assembler的相关知识。我的汇编代码运行时出现“Unexpected end of file encountered”错误,恳请各位帮忙解决该问题,谢谢!

报错的原始代码

.MODEL SMALL 
.stack 100h

sys_exit equ 1
sys_read equ 3
sys_write equ 4
stdin equ 0
stdout equ 1

.data
.code
start:
    msg1 db "enter a number: ", len1 equ $- msg1
    
    msg2 db "enter a number: ",  len2 equ $- msg2
    
    msg3 db "the sum is ", len3 equ $- msg3
    
        num1 resb 2
        num2 resb 2
        res resb 1
    
.bss
        
        
        
        section .text
        global_start
        
        
        
        mov eax,sys_write
        mov ebx,stdout
        mov ecx,msg1
        mov edx,len1
        int 21h
        
        mov eax,sys_read
        mov ebx,stdin
        mov ecx,num1
        mov edx,2
        int 21h
        
        mov eax,sys_write
        mov ebx stdout
        mov ecx,msg2
        mov edx,len2
        int 21h
       
        mov eax,sys_read
        mov ebx,stdin
        mov ecx,num2
        mov edx,2
        int 21h
        
        mov eax,sys_write
        moc ebx,stdout
        mov ecx,msg3
        mov edx,len3
        int 21h
        
        mov eax,[num1]
        sub eax,'0'
        
        mov ebx, [num2]
        sub ebx,'0'
        
        add eax,ebx
        add eax,'0'
        
        mov [res],eax
        
        mov eax,sys_write
        mov ebx,stdout
        mov ecx,res
        mov edx,1
        int 21h
        
        exit:
        
        mov eax,sys_exit
        xor ebx,ebx
        int 21h

问题分析与修复要点

你的代码核心问题是混合了16位DOS汇编和32位Linux汇编的语法,而GUI Turbo Assembler仅支持16位DOS环境,同时存在多处语法错误,导致汇编器解析失败。具体问题和修复方式如下:

  • 语法混用:删除Linux汇编的section .text和global _start,仅保留DOS汇编的.data、.code、.bss段定义
  • 寄存器错误:替换32位寄存器(eax/ebx等)为16位寄存器(ax/bx等),适配DOS 16位环境
  • 拼写错误:修正mov ebx stdout(缺少逗号)、moc ebx,stdout(指令拼写错误)这类语法问题
  • 段位置错误:将变量num1/num2/res移到.bss段,.bss段需放在.data之后、.code之前
  • 缺少结束标记:16位DOS汇编必须在末尾添加end start,指定程序入口并结束汇编,这是解决"Unexpected end of file"的关键

修复后的可运行代码

.MODEL SMALL 
.STACK 100H

; DOS中断功能号定义
sys_exit equ 4C00h  ; DOS退出功能,返回码0
sys_read equ 01h    ; 单字符读取功能
sys_write equ 09h   ; 字符串输出功能

.DATA
msg1 db "enter a number: $"
msg2 db "enter a number: $"
msg3 db "the sum is $"

.BSS
num1 resb 2
num2 resb 2
res resb 1

.CODE
START:
    ; 初始化数据段寄存器
    mov ax, @DATA
    mov ds, ax

    ; 输出提示信息msg1
    mov ah, sys_write
    lea dx, msg1
    int 21h

    ; 读取第一个数字
    mov ah, sys_read
    int 21h
    mov [num1], al

    ; 输出换行,优化输入体验
    mov ah, 02h
    mov dl, 0Dh
    int 21h
    mov dl, 0Ah
    int 21h

    ; 输出提示信息msg2
    mov ah, sys_write
    lea dx, msg2
    int 21h

    ; 读取第二个数字
    mov ah, sys_read
    int 21h
    mov [num2], al

    ; 输出换行
    mov ah, 02h
    mov dl, 0Dh
    int 21h
    mov dl, 0Ah
    int 21h

    ; 输出结果提示msg3
    mov ah, sys_write
    lea dx, msg3
    int 21h

    ; 计算两个数字的和
    mov al, [num1]
    sub al, '0'
    mov bl, [num2]
    sub bl, '0'
    add al, bl
    add al, '0'
    mov [res], al

    ; 输出计算结果
    mov ah, 02h
    mov dl, [res]
    int 21h

    ; 退出程序
    mov ah, sys_exit
    int 21h

END START  ; 指定程序入口并结束汇编

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 18:02:43