BANDI HEMANTH
BANDI HEMANTH

Reputation: 21

Reversing integer values of an array using assembly language

I am trying to reverse integer values of an array using assembly language. Please check the below program and correct me.

;Reverse array of 32-bit integers

INCLUDE Irvine32.inc

.DATA

 intarray DWORD 600h,700h,300h,400h,500h,200h,100h

 .CODE

main PROC

mov esi, offset intarray
mov ecx, lengthof intarray

lea edi,[esi+(lengthof intarray*sizeof intarray)-sizeof intarray]
L1:
mov eax,[esi]              ;eax = value at start
mov ebx,[edi]
;xchg eax,ebx   ;ebx = value at end
mov [edi],eax              ;Store value from start at end
mov [esi],ebx              ;Store value from end at start
add esi,sizeof intarray                  ;esi = address of next item at start
sub edi,sizeof intarray                  ;edi = address of next item at end
cmp esi,edi                ;Have we reached the middle?
loop L1   

call DumpRegs
call WriteInt

ret 

;call DumpMem

exit

main ENDP

END main

Upvotes: 0

Views: 881

Answers (1)

Sep Roland
Sep Roland

Reputation: 39191

The loop instruction does not what you expect!

cmp esi,edi                ;Have we reached the middle?
loop L1

When you did the compare of these pointers you got certain flags from the CPU to allow you to interpret the result of the comparison. By using the loop instruction you neglected this!
What you need is a conditional jump that goes to the top of the loop as long as the ESI pointer is smaller than the EDI pointer. Since addresses are unsigned we use the condition below here.

cmp  esi, edi                ;Have we reached the middle?
jb   L1 

Upvotes: 2

Related Questions