使用汇编语言反转数组的整数值
Reversing integer values of an array using assembly language
我正在尝试使用汇编语言反转数组的整数值。请检查下面的程序并纠正我。
;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
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
我正在尝试使用汇编语言反转数组的整数值。请检查下面的程序并纠正我。
;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
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