I need to sum 2 separate arrays. One array totals to 243, and I had no issue adding it and storing it in a value. But the second array totals 330 and and since it is a 16bit number I think I have to use dx register instead of dl. However now when I add the numbers the register is set to 0000204A which isn't 330 in decimal format. What am I doing wrong? How do I go about fixing this?
.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
Note that add dx, [esi]
adds a word not a byte. By specifying dx
you also increased the implicit size of the array item. There are various ways to fix it. One option is to zero extend into a 16 bit register and do the addition that way, e.g.:
movzx ax, [esi]
add dx, ax
You could also handle the carry manually, such as:
add dl, [esi]
adc dh, 0