The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Number1awa on January 24, 2011, 10:50:31 PM

Title: Numbers are adding strangely.
Post by: Number1awa on January 24, 2011, 10:50:31 PM
Using MASM assembler


I just have one problem what i am supposed to do is implement

arrayW[0] = arrayB[0] + arrayD[0]; 
arrayW[1] = arrayB[1] + arrayD[1];
arrayW[2] = arrayB[2] + arrayD[2]

in assembly

INCLUDE irvine32.inc

.data

arrayB BYTE 170,193,57
arrayW WORD 3 DUP(?)
arrayD DWORD 517,1045,2000
.code
main PROC
call ClrScr
call crlf

mov ecx, LENGTHOF arrayW
mov edi, DWORD PTR OFFSET arrayD ;addr of first element in arrayD
mov esi, DWORD PTR OFFSET arrayB ;addr of first element in arrayB
mov edx, DWORD PTR OFFSET arrayW ;addr of first element in arrayW
L1:
mov eax, 0 ; reset accumulator
mov ax, [edi] ; mov [edi] to 16-bit
add al, [esi] ; add [esi] to lower half of ax
mov [edx], ax ; mov sum to [edx]



call crlf
call crlf
add esi, TYPE arrayB
add edi, TYPE arrayD
add edx, TYPE arrayW
call WriteInt


call crlf   
loop L1


call crlf
exit

main ENDP
END main


Everything is fine until the last numbers add itself....the output that i get is 687 1238 1801

Now I think that i know that 2000 + 57 does not equal 1801

all of my register should be fitting the values put into them....
I dont know what the problem is with it....

Thanks :)
Title: Re: Numbers are adding strangely.
Post by: FORTRANS on January 24, 2011, 11:03:13 PM
Hi,

   You should note that 1801 is 256 away from 2057.  A
carry is generated by the add and you need to propagate
the carry into AH.  So you need something like the following.

Regards,

Steve


        mov     ax, [edi]
        add     al, [esi]
        ADC     AH,0
        mov     [edx], ax
Title: Re: Numbers are adding strangely.
Post by: dedndave on January 24, 2011, 11:08:04 PM
DWORD PTR not appropriate here:
mov edi, DWORD PTR OFFSET arrayD ;addr of first element in arrayD
mov esi, DWORD PTR OFFSET arrayB ;addr of first element in arrayB
mov edx, DWORD PTR OFFSET arrayW ;addr of first element in arrayW

EDI holds the pointer into the dword array
mov ax, [edi]
so - that is sized wrong


i would start with a byte MOV into EAX, because you can use MOVZX
there is no ADDZX   :P

movzx eax,byte ptr [esi]
add   eax,[edi]
mov   [edx],ax
Title: Re: Numbers are adding strangely.
Post by: Number1awa on January 24, 2011, 11:42:36 PM
Thanks guys.....I figured something carried....but did not know how to get it....forgot about ADC

also....duh....of course the DWORD PTR did not need using....that was a bad mistake :)

Thank You
Title: Re: Numbers are adding strangely.
Post by: dedndave on January 24, 2011, 11:52:40 PM
that was the point of using MOVZX
if you do it that way, MOV EAX,0 and ADC AH,0 are not needed

as for the DWORD PTR's - they won't hurt anything - just not needed - save some typing   :bg