Reputation: 1254
I have a problem, that i cant figure out:
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
).
Example: 1 2 3 4 5 -> 5 4 3 2 1
Anyone have any suggestions?
Upvotes: 0
Views: 2117
Reputation: 10570
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.
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:
Upvotes: 2