索引如何在程序集 8086 中工作?

How to does indexing work in assembly 8086?

我在程序集 8086 中的索引有问题,我在程序集中有一个从 C 调用的过程,该过程采用这样定义的 long int 类型的指针数组 long int *arr_ptr[3], 代码是这样的(我认为我的 SI 是我的行指针, DI 是我的列指针):

MOV SI,[BP+6]   ;now si points to the first row
MOV DI,[SI]     ;now DI is pointing to the first row
                ; and at the first column column 
ADD SI,2        ;here i can move the SI pointer to the next row,
                ; but i want to do this in a loop so i thought
                ; ill define a variable cnt in my data and do this
MOV DI,[SI+cnt]

cnt 为 2 时的最后一行与 ADD SI,2 不同我该怎么办?我只想简单地遍历我的矩阵。

首先,您必须向下传递行数(假设它是可变的),或者使用 NULL ptr 终止行指针(我的偏好)。或者构建一个包含所有所需信息的结构——行数、数据地址——并将其 ptr 传递给您的处理例程。

假设您已经 NULL 终止行 ptrs,汇编代码可能如下所示:

    mov   si,[bp+6]
    jmp short rowloopentry
rowloop:
    << process row data pointed to by di >>
rowloopentry:
    mov   di,[si]                   ;get next row data ptr, and advance index
    add   si,2
    test  di,di                     ;process next row if not at end
    jnz   rowloop

或者,如果行计数作为参数传递,原型可能会变成 func(long int *arr_ptr[], unsigned rowcnt); 并且处理代码可能会循环如下:

    mov   cx,[bp+8]
    mov   si,[bp+6]
    test  cx,cx
    jz    zerorows
rowloop:
    mov   di,[si]                   ;get next row data ptr, and advance index
    add   si,2
    << process row data pointed to by di >>
    loop  rowloop                   ;process next row if not at end
zerorows:

以上忽略了针对特定处理器架构的优化。