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

改写为16位后MASM x86汇编代码报A2031错误的修复咨询

16位MASM汇编数组索引错误修复问题

对应以下C++代码的MASM x86汇编代码:

void main()
{
unsigned int i, B[10], A[10]={3,2,3,1,7,5,0,8,9,2}; 
for (i=0; i<10; i++){
B[i] = A[i] + 2 * (2*i + ( (3*i+1) / 5) );
}

这段代码使用32位寄存器和变量时可正常运行,但改为16位后出现错误。

汇编代码如下:

.386  ; Specify instruction set
.model flat, stdcall  ; Flat memory model, std. calling convention
.stack 4096 ; Reserve stack space
ExitProcess PROTO, dwExitCode: WORD  ; Exit process prototype

.data
    A WORD  3,2,3,1,7,5,0,8,9,2  
    B WORD  10 DUP (?)                    
                            
    var1 WORD 0
    quotient WORD 0

 ; A[i] + 2 * (2*i + ( (3*i+1) / 5) )
.code      
main PROC
    mov si, 0                   

loop_start:

    ;(3*i+1)
    mov ax, 3                    
    mul si                       
    inc ax                    
    
    ; / 5)
    mov dx,0   
    mov cx,5
    div cx    
    mov quotient,ax

    ; 2*i
    mov ax,2                           
    mul si     

    ; (2*i + ( (3*i+1) / 5) )
    mov bx, quotient
    add ax,bx
   
   ; 2 * (2*i + ( (3*i+1) / 5) )
   mov var1,2
   mul var1
   mov var1, ax
  
    ; load array A
    lea bx,A
    mov cx,4

    ;A[i] + 2 * (2*i + ( (3*i+1) / 5) )
    ;imul dx,si 
    ;mov bx, dx
    mov ax,[bx + TYPE A * si]
    add bx,var1
   
   ;load array B and store
    lea ax,B
    mov  [ax + dx], bx

    ; i<10; i++
    inc si                    
    cmp si, 10                 
    jl loop_start  

INVOKE ExitProcess, 0 ; call exit function
  
main ENDP ; exit main procedure
END main  ; stop assembling

我遇到的错误是error A2031: Must be index or base register,报错位置为读取数组索引的mov ax,[bx + TYPE A * si]行。我知道bx是基址寄存器,也曾尝试在[]外将bx与si相加,但仍无法解决问题!

更新:是否有办法在不使用32位索引、寄存器和变量的情况下修复这段代码?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 14:45:17