通用 MASM 代码:根据符号的类型存储 8 位或 16 位寄存器



我正在尝试编写一个数组反转函数,无论静态数组保持 BYTE s 还是 WORD s,它都能正常工作。

这就是我到目前为止所拥有的,我假设数组的大小为 WORD

.data
    myArray WORD 1231h, 2342h, 3353h, 4564h, 5675h, 7, 9
.code
main proc
    mov eax, 0  ;index of the left side of the array
    mov esi, SIZEOF myArray
    sub esi, TYPE myArray   ;index of the right side of the array
    Switch:
        movsx edx, [myArray + eax]  ;puts the left element of the array in eax
        xchg dx,  [myArray + esi]   ;exchange edx with the right element of the array
        mov [myArray + eax], dx ;puts the right element into the left part of the array
        add eax, TYPE myArray   ;eax is currently pointing to leftPtr so we add the appropriate amount to 
                                ;it depending on the size of each element in myArray. This way it will point
                                ;to the next element in the array
        sub esi, TYPE myArray   ;same concept as above except we are subtracting the amount from the right pointer
        cmp esi, eax            ;compare the right pointer to the left pointer
        jnle Switch             ;Jump if the right pointer is !(<=) the left pointer
main endp
end main

我可以使用movzx/movsx指令将较小大小的值从数组移动到 32 位寄存器中。

问题是编写一些组装到8位存储或16位存储的东西,具体取决于TYPE

你可以根据myArray的类型有条件地定义一个符号,可以是文本dldxedx,并用它来代替寄存器。像这样:

    IF TYPE myArray EQ TYPE BYTE
@DX TEXTEQU <dl>
    ELSEIF TYPE myArray EQ TYPE WORD
@DX TEXTEQU <dx>
    ELSEIF TYPE myArray EQ TYPE DWORD
@DX TEXTEQU <edx>
    ENDIF
    mov @DX, [myArray + eax]   ;puts the left element of the array in DX
    xchg @DX, [myArray + esi]  ;exchange DX with the right element of the array
    mov [myArray + eax], @DX   ;puts the right element into the left part of the array

最新更新