我需要对两个独立的数组求和。一个数组的总数是243,我没有问题添加它并将其存储在一个值中。但第二个数组总计330,因为它是一个16位的数字,我想我必须使用dx寄存器而不是dl。然而,现在当我添加数字时,寄存器被设置为0000204A,这不是十进制格式的330。我做错了什么?我该怎么解决这个问题?
.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD
.data
AR1 DB 25, 89, 49, 80
AR2 DB 30, 100, 50, 150
AR1_SUM DWORD 0 ; variable to store the sum of AR1 elements
AR2_SUM DWORD 0 ; variable to store the sum of AR2 elements
.code
_main PROC
mov esi, offset AR1 ; set ESI to point to the first element of AR1
mov ecx, 4 ; reset the loop counter
xor edx, edx ;
addloop1:
add dl, [esi] ; add the current byte to the sum
add esi, 1 ; move to the next byte in AR1
loop addloop1 ; repeat until all elements in AR1 have been added
mov AR1_SUM, edx
mov esi, offset AR2 ; set ESI to point to the first element of AR2
mov ecx, 4 ; reset the loop counter
xor edx, edx ; reset the sum register for AR2
addloop2:
add dx, [esi] ; add the current byte to the sum
add esi, 1 ; move to the next byte in AR2
loop addloop2 ; repeat until all elements in AR2 have been added
mov AR2_SUM, edx ; store the sum of AR2 in AR2_SUM
INVOKE ExitProcess, 0
_main ENDP
END _main
1条答案
按热度按时间b0zn9rqh1#
请注意,
add dx, [esi]
添加的是一个字,而不是一个字节。通过指定dx
,您也增加了数组项的隐式大小。有多种方法可以修复它。一种选择是零扩展到16位寄存器并以这种方式进行加法,例如:您也可以手动处理搬运,例如: