我正在学习装配,需要创建2个数组。对于第一个,我添加了2,然后打印值。第二个我减去2并打印。这些价值出来了。当我尝试交换值时,我得到略微偏离的数字值。我交换错了吗?
.586
.MODEL FLAT
INCLUDE io.h ; header file for input/output
.STACK 4096
.DATA
array1 DWORD 21H,22H,23H,24H,25H
array2 DWORD 31H,32H,33H,34H,35H
resultLbl1 BYTE "Array 1 values are",0
resultLbl2 BYTE "Array1 value1 is",0
resultLbl3 BYTE "Array1 value2 is",0
resultLbl4 BYTE "Array1 value3 is",0
resultLbl5 BYTE "Array1 value4 is",0
resultLbl6 BYTE "Array1 value5 is",0
string1 BYTE 40 DUP (?)
count DWORD 0
.CODE
_MainProc PROC
;add 2 to array1 elements
mov eax, array1
add eax, 2
dtoa string1, eax
output resultLbl1, string1
mov eax, array1+4
add eax, 2
dtoa string1, eax
output resultLbl1, string1
mov eax, array1+8
add eax, 2
dtoa string1, eax
output resultLbl1, string1
mov eax, array1+12
add eax, 2
dtoa string1, eax
output resultLbl1, string1
mov eax, array1+16
add eax, 2
dtoa string1, eax
output resultLbl1, string1
;subtract 2 from array2 elements
mov eax, array2
sub eax, 2
dtoa string1, eax
output resultLbl1, string1
mov eax, array2+4
sub eax, 2
dtoa string1, eax
output resultLbl1, string1
mov eax, array2+8
sub eax, 2
dtoa string1, eax
output resultLbl1, string1
mov eax, array2+12
sub eax, 2
dtoa string1, eax
output resultLbl1, string1
mov eax, array2+16
sub eax, 2
dtoa string1, eax
output resultLbl1, string1
;Swap array values in reverse
mov eax, array2+16
xchg array1, eax
mov eax, array2+12
xchg array1+4, eax
mov eax, array2+8
xchg array1+8, eax
mov eax, array2+4
xchg array1+12, eax
mov eax, array2+4
xchg array1+12, eax
mov eax, array2
xchg array1+16, eax
mov eax, array1
dtoa string1, eax
output resultLbl2, string1
mov eax, array1+4
dtoa string1, eax
output resultLbl3, string1
mov eax, 0 ; exit with return code 0
ret
_MainProc ENDP
END ; end of source code
答案 0 :(得分:1)
看起来您使用xchg
是错误的。首先,因为xchg
仅向eax
写入要被下一条指令杀死的值,第二条因为带有内存参数的xchg
具有(浪费的)隐式锁定前缀。
您应该使用明确的临时代替:
mov eax, array1
mov edx, array2+16
mov array2+16, eax
mov array1, edx
行
mov eax, array2+4
xchg array1+12, eax
也是重复的,这是可疑的。
答案 1 :(得分:0)
程序将array2值移动到array1中,但需要第二个mov来将array1值移动到array2中。正如gsg所指出的,使用xchg比使用mov需要更长的时间,所以我更新了这段代码片段以使用mov。
mov eax,array2+16
mov edx,array1
mov array1,eax
mov array2+16,edx
mov eax,array2+12
mov edx,array1+4
mov array1+4,eax
mov array2+12,edx
mov eax,array2+8
mov edx,array1+8
mov array1+8,eax
mov array2+8,edx
mov eax,array2+4
mov edx,array1+12
mov array1+12,eax
mov array2+4,edx
mov eax,array2
mov edx,array1+16
mov array1+16,eax
mov array2,edx