Generic MASM code: storing 8 or 16 bits of a register depending on TYPE of a symbol

徘徊边缘 提交于 2019-12-10 15:15:36

问题


I am trying to write an array-reversing function that works correctly whether the static array holds BYTEs or WORDs.

This is what I have so far where I assumed the array was of size 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

I can use the movzx/movsx instruction to move the smaller sized value from the array into a 32-bit register.

The problem is writing something that assembles to an 8bit store or 16bit store depending on TYPE.


回答1:


You can define a symbol conditionally on the type of myArray to be either the text dl, dx or edx and use that in place of the register. Something like:

    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


来源:https://stackoverflow.com/questions/37770559/generic-masm-code-storing-8-or-16-bits-of-a-register-depending-on-type-of-a-sym

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!