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

MASM汇编代码编译报错A2074及A4023问题求助

Fixes for MASM Compilation and Calculation Errors

Compilation Error Fixes

1. Segment Initialization (Error A2074)

MASM requires explicit initialization of the Data Segment (DS) register to access variables in your DATA segment. Add these lines at the start of the MAIN procedure:

mov ax, DATA
mov ds, ax

2. Entry Point Naming (Warning A4023)

If assembling with the /COFF switch, rename MAIN to _MAIN (add a leading underscore). For DOS-targeted programs, avoid the warning entirely by assembling with the OMF object format using this command:

ml /c /omf LabW6.asm

Logical Calculation Fixes

Your original code had order-of-operations and variable mismatches. Below is the corrected code with accurate arithmetic implementations matching your comments:

; Declare data segment
DATA SEGMENT
    num1 DW 4
    num2 DW 5
    num3 DW 2
    num4 DW 1
    num5 DW -5
    num6 DW 2
    num7 DW 6
    num8 DW 3
    num9 DW 4
    num10 DW 16
    num11 DW 5
    num12 DW 3
    num13 DW 6
    num14 DW 1
    num15 DW 35
DATA ENDS

; Declare code segment
CODE SEGMENT
MAIN PROC
    ; Initialize data segment
    mov ax, DATA
    mov ds, ax

    ; Calculate 4 + 5 * 2 and store in num4 (Result: 14)
    mov ax, num2   ; ax = 5
    mul num3       ; ax = 5 * 2 = 10
    add ax, num1   ; ax = 10 + 4 = 14
    mov num4, ax

    ; Calculate 16 - 5 and store in num11 (Result: 11)
    ; Note: Original comment referenced 12-1, but code used num10(16) and num11(5)
    mov ax, num10  ; ax = 16
    sub ax, num11  ; ax = 16 - 5 = 11
    mov num11, ax

    ; Calculate -5 + 2 and store in num6 (Result: -3)
    mov ax, num5   ; ax = -5
    add ax, num6   ; ax = -5 + 2 = -3
    mov num6, ax

    ; Calculate (4 + 2) * 6 and store in num7 (Result: 36)
    mov ax, num1   ; ax = 4
    add ax, num3   ; ax = 4 + 2 = 6
    mov bx, num7   ; bx = 6
    mul bx         ; ax = 6 * 6 = 36
    mov num7, ax

    ; Calculate 16 / 5 and store in num11 (Result: 3, integer division)
    mov ax, num10  ; ax = 16
    cwd            ; Extend ax to dx:ax (dx = 0 for positive 16)
    mov bx, num11  ; bx = 5
    div bx         ; ax = 16 / 5 = 3, dx = 1 (remainder)
    mov num11, ax

    ; Calculate -(3 + 4) * (6 - 1) - 35 and store in num14 (Result: -70)
    mov ax, num12  ; ax = 3
    add ax, num9   ; ax = 3 + 4 = 7
    neg ax         ; ax = -7
    mov bx, num13  ; bx = 6
    sub bx, num14  ; bx = 6 - 1 = 5
    mul bx         ; ax = -7 * 5 = -35
    sub ax, num15  ; ax = -35 - 35 = -70
    mov num14, ax

    ; Calculate -3 + 4 * 6 - 1 and store in num6 (Result: 20)
    mov ax, num1   ; ax = 4
    mul num7       ; ax = 4 * 6 = 24
    add ax, -3     ; ax = 24 - 3 = 21
    sub ax, num14  ; ax = 21 - 1 = 20
    mov num6, ax

    ; Calculate 25 MOD 3 and store in num9 (Result: 1)
    mov ax, 25     ; ax = 25
    mov bx, 3      ; bx = 3
    cwd            ; Extend ax to dx:ax
    div bx         ; dx = 25 % 3 = 1 (remainder)
    mov num9, dx   ; Store remainder as MOD result

    ; Exit program
    mov ax, 4c00h
    int 21h

MAIN ENDP
CODE ENDS
END MAIN

Key Calculation Corrections:

  • 4 + 5*2: Fixed order to perform multiplication first, then addition.
  • -3 +4*6 -1: Replaced incorrect variable num5 (-5) with -3 and used num1*num7 (4*6) as intended.
  • 25 MOD3: Directly loaded 25, performed division, and stored the remainder from the dx register (correct for word division).
  • 16-5: Updated comment to match actual variable values used in code.

Assembly & Linking Command Example

For DOS-compatible executable output, use these commands:

ml /c /omf LabW6.asm
link16 LabW6.obj, LabW6.exe;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 10:10:59