Arrays 通用MASM代码:根据符号类型存储寄存器的8位或16位
我正在尝试编写一个数组反转函数,该函数可以正确地工作,无论静态数组持有的是Arrays 通用MASM代码:根据符号类型存储寄存器的8位或16位,arrays,assembly,x86,masm,Arrays,Assembly,X86,Masm,我正在尝试编写一个数组反转函数,该函数可以正确地工作,无论静态数组持有的是BYTEs还是WORDs 到目前为止,我假设数组的大小为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 m
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
.data
myArray字1231h、2342h、3353h、4564h、5675h、7、9
.代码
主进程
mov-eax,0;数组左侧的索引
mov esi,myArray的大小
子esi,类型myArray;数组右侧的索引
开关:
movsx-edx,[myArray+eax];将数组的左元素放入eax中
xchg-dx,[myArray+esi];与阵列的右侧元素交换edx
mov[myArray+eax],dx;将右元素放入数组的左部分
添加eax,键入myArray;eax当前指向leftPtr,因此我们向
;它取决于myArray中每个元素的大小。这样它会指向你
;到数组中的下一个元素
子esi,类型myArray;与上面的概念相同,只是我们要从右指针中减去数量
cmp-esi,eax;比较右指针和左指针
jnle开关;如果正确的指针被选中,则跳转!(您可以根据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
如果类型为myArray EQ类型字节
@DX TEXTEQU
ELSEIF类型myArray EQ类型字
@DX TEXTEQU
ELSEIF类型myArray EQ类型DWORD
@DX TEXTEQU
恩迪夫
mov@DX,[myArray+eax];将数组的左元素置于DX中
xchg@DX,[myArray+esi];与数组的右侧元素交换DX
mov[myArray+eax],@DX;将右元素放入数组的左部分
不要将xchg
与内存操作数一起使用,除非您希望获得隐式锁定
前缀的效果(原子读修改写内存访问和完整内存屏障)。这比只使用两个tmp寄存器慢得多。另外,除非MASM为您添加了一个寄存器,否则不要忘记在函数末尾执行ret
。不过,我不知道这里主要问题的答案。您可能必须使用MASM宏在mov[mem]、dl
和mov[mem]之间进行选择,dx
。选择数组的类型
仅在它是静态的(而不是函数arg)时才起作用,对吗?好的,您不能告诉MASM您想要[esi]
表示特定的操作数大小。但我不使用MASM,只使用NASM/YASM和GNU作为。