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

8086汇编Mastermind游戏代码报错:重复声明问题求助

解决emu8086中Mastermind汇编代码的重复声明错误

你的代码出现重复声明错误的核心原因是数据段中的变量code与代码段的段名code重名。在8086汇编语法中,变量名、段名、标号等标识符不能重复,emu8086会将二者视为同一标识符,从而触发冲突报错。

解决步骤

  1. 修改代码段的段名,避免与数据段的code变量名称冲突,比如将code segment改为code_seg segment;
  2. 同步修改段结束指令,将code ends改为code_seg ends;
  3. 后续需补充实现代码中未定义的子过程(如rand、getchar等),否则编译时会出现未定义标识符的错误。

修改后的代码

; Mastermind game in assembly language for the 8086

; Declare data segment
data segment
    code db 4 dup (0) ; Random 4-digit code
    guess db 4 dup (0) ; Player's current guess
    feedback db 10 dup (0) ; Feedback for each attempt
    attempts db 0 ; Number of attempts
    digits db '0123456789', 0 ; Valid digits
data ends

; 修改代码段名称,避免和data段的code变量重名
code_seg segment
start:
    ; Initialize the code
    call generate_code

    ; Loop for 10 attempts
    mov bx, 10
    repeat:
        ; Get the player's guess
        call get_guess

        ; Check the guess against the code
        call check_guess

        ; Give feedback to the player
        call give_feedback

        ; Check if the player won
        call check_win

        ; Decrement the number of attempts
        dec bx
        jnz repeat

    ; The player lost
    call print_lose
    jmp exit

; Generates a random 4-digit code
generate_code:
    ; Initialize the random number generator
    mov ax, 0
    mov cx, 0x1234
    mov dx, 0x5678
    call rand

    ; Generate 4 random digits
    mov cx, 4
    generate_digit:
        ; Get a random digit
        mov ax, 10
        call rand
        mov al, dl

        ; Store the digit in the code
        mov [code + cx - 1], al

        ; Decrement the counter
        dec cx
        jnz generate_digit

    ; Return
    ret

; Gets the player's guess
get_guess:
    ; Prompt the player
    call print_prompt
    call read_guess

    ; Validate the guess
    call validate_guess

    ; Return
    ret

; Reads the player's guess from the input
read_guess:
    ; Read 4 characters
    mov cx, 4
    read_character:
        ; Read a character
        call getchar

        ; Store the character in the guess
        mov [guess + cx - 1], al

        ; Decrement the counter
        dec cx
        jnz read_character

    ; Return
    ret

; Validates the player's guess
validate_guess:
    ; Check that each character is a valid digit
    mov cx, 4
    validate_character:
        ; Get the current character
        mov al, [guess + cx - 1]

        ; Check if the character is a valid digit
        mov bx, digits
        call strchr
        cmp ax, -1
        jz invalid

        ; Decrement the counter
        dec cx
        jnz validate_character

    ; Return
    ret
code_seg ends

额外注意事项

你代码中调用的rand、print_prompt、getchar、strchr、check_guess等子过程均未实现,后续需要补充这些功能的汇编代码才能让程序正常运行:

  • rand可通过DOS中断21h的2Ch功能获取系统时间,以此初始化随机数种子;
  • getchar可调用DOS中断21h的01h功能读取键盘输入;
  • strchr需要自行实现字符串查找逻辑,遍历digits数组判断输入字符是否为有效数字。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 16:55:22