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 :)
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
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
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
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