hi guys, i am trying to add the numbers in array but got a few problems in it that i couldn't solve it due to the new programmer in assembly language. here i have posted my code can you help me out please
INCLUDE Irvine32.inc
.data
ary DWORD 5, 8, 6, 3, 6, 5, 2, 7, 4, 7, 9, 10, 12, 3, 4, 6, 7, 8, 9 11, 2, 13, 5, 5, 10, 8
link DWORD 27
.code
main PROC
call Clrscr
mov EAX,0
mov ESI,OFFSET ary
mov ECX,link
loop:
add EAX,[esi]
add ESI,4
dec ECX
cmp ECX,0
jne loop
call writeDec
exit
main ENDP
END main
it should work - well, almost - you are missing a comma in the array
although - it will add the value 27, as well
there are only 26 values in the array :bg
a few pointers:
1) you don't need to define a dword with the count - use LENGTHOF
2) it takes only 2 bytes to XOR EAX,EAX - it takes 5 bytes to MOV EAX,0 (SUB EAX,EAX works also)
3) when you DEC ECX, you do not need to CMP ECX,0 - the ZERO FLAG will be set by DEC
INCLUDE Irvine32.inc
.data
ary DWORD 5, 8, 6, 3, 6, 5, 2, 7, 4, 7, 9, 10, 12, 3, 4, 6, 7, 8, 9, 11, 2, 13, 5, 5, 10, 8
.code
main PROC
call Clrscr
xor EAX,EAX
mov ESI,OFFSET ary
mov ECX,LENGTHOF ary
loop0:
add EAX,[esi]
add ESI,4
dec ECX
jne loop0
call WriteDec
exit
main ENDP
END main
EDIT - one more thing
writeDec should be WriteDec
ok - couple more - lol
loop is a reserved word - name it loop0 or something
in fact, you could use the LOOP instruction :P
i had to add some include lines to make it assemble...
the paths may be different on your installation
INCLUDE Irvine32.inc
INCLUDELIB Irvine32.lib
INCLUDELIB \masm32\lib\kernel32.lib
INCLUDELIB \masm32\lib\user32.lib
.data
ary DWORD 5, 8, 6, 3, 6, 5, 2, 7, 4, 7, 9, 10, 12, 3, 4, 6, 7, 8, 9, 11, 2, 13, 5, 5, 10, 8
.code
main PROC
call Clrscr
xor EAX,EAX
mov ESI,OFFSET ary
mov ECX,LENGTHOF ary
loop0:
add EAX,[esi]
add ESI,4
dec ECX
jne loop0
call WriteDec
exit
main ENDP
END main
result:
175
thank you really appreciated :U
I have one more question if I had divide the total sum by total number of numbers it show only the whole numbers. how can i display the decimal numbers too.
if we use 32-bit registers, DIV divides a 64-bit value by a 32-bit value
the 64-bit dividend (or numerator) is in EDX:EAX
the divisor (denominator) is a 32-bit value, and can be in register or memory
the results are in EAX (quotient) and EDX (modulus or remainder)
the fraction may be expressed as the remainder/divisor
when your dividend is 32 bits or less, you must zero the EDX register prior to division
if you attempt to divide by 0 or perform a division that would yield a quotient larger than 32 bits (overflow), an exception is generated
in this case, the divisor is not in memory - it is a constant (LENGTHOF ary)
we can put that in register to divide
you already have the value in EAX, so.....
xor edx,edx
mov ecx,LENGTHOF ary
div ecx
;EAX = quotient
;EDX = remainder
;ECX = divisor
if you want to round the result to the nearest whole integer...
shl edx,1
cmp edx,ecx
cmc
adc eax,0
will work so long as the remainder is less than 32 bits in length
the carry flag is cleared if (2 x remainder) is above or equal to the divisor
then, the carry flag is inverted and added to the result
if the remainder may be 32 bits...
shl edx,1
jc @F
cmp edx,ecx
cmc
@@: adc eax,0
notice that the maximum remainder is (divisor - 1)