使用汇编语言反转数组的整数值

时间:2017-03-04 02:56:47

标签: assembly x86 masm irvine32

我试图使用汇编语言来反转数组的整数值。请检查以下程序并纠正我。

;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

1 个答案:

答案 0 :(得分:1)

loop指令不符合您的期望!

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

当您对这些指针进行比较时,您会从CPU获得某些标志,以便您解释比较结果。通过使用loop指令,您忽略了这一点! 只要ESI指针小于EDI指针,您需要的是条件跳转到循环的顶部。由于地址是未签名的,我们在此处使用 条件。

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