在装配操作数组 [英] Manipulating arrays in assembly
问题描述
我有一个问题,我找不出:
I have a problem, that i cant figure out:
在汇编语言,编写接收指向整数数组和该数组的大小,并通过反转其元素而不复制阵列堆叠改变阵列的功能。使用专用指令并注册阵列( ESI
, EDI
工作; LODSB
, STOSB
, CLD
, STD
)。
In assembly language, write a function that receives a pointer to an array of integers and the size of this array, and changes the array by reversing its elements without copying the array to stack. Use the dedicated instructions and registers to work with arrays (esi
, edi
; lodsb
, stosb
, cld
, std
).
举例:1 2 3 4 5 - > 5 4 3 2 1
Example: 1 2 3 4 5 -> 5 4 3 2 1
任何人有什么建议吗?
推荐答案
冲销阵列 LODSB
和 STOSB
要求 CLD
和 STD
的每一个元素(因为指针之一需要增加和其他需要递减)或者也可以忘记 CLD
和 STD
,只是取消其他指针的不正确递增(或递减)通过减去2(或添加2)的两个元素吧。
Reversing an array with lodsb
and stosb
requires cld
and std
for every element (because one of the pointers needs to increment and the other needs to decrement), or alternatively, you can just forget cld
and std
and just cancel the incorrect increment (or decrement) of the other pointer by subtracting 2 (or adding 2) to it after each element.
总之,使用 LODSB
和 STOSB
在这种情况下让事情在我看来,不必要地复杂化。我会用这样的:
Anyway, using lodsb
and stosb
in this case makes things unnecessarily complicated in my opinion. I'd use something like this:
mov esi,start_address
mov edi,end_address
mov ecx,edi
sub ecx,esi
x1: test ecx,ecx
jz @ready
mov al,[esi]
xchg al,[edi]
mov [esi],al
inc esi
dec edi
dec ecx
jmp x1
@ready:
这篇关于在装配操作数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!