Common MASM code: saving 8 or 16 bits of the register depending on the TYPE of character

I am trying to write an array reversal function that works correctly whether a static array BYTEor WORDs saves .

This is what I have so far, where I assumed that the array has 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 instruction movzx/movsxto move a smaller value from an array to a 32-bit register.

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

+4
source share
1 answer

myArray dl, dx edx . - :

    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
+4

All Articles