Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sum Array values help 1

Status
Not open for further replies.

HotCode

Programmer
Sep 29, 2011
4
Hello, I got this simple program worked up and I just dont think it is doing what is is suppose to do. Now admittedly I am just starting in assembly language and I am still a bit lost.
This is in MASM and I want to sum every number in the array that is less than 50 (sample varable) I think I got it working but then its giving a rather large number, but that might be the index, I might be adding up the index by accident I think I am using the number in the array but not quite sure. Well thanks for any help here is the code

heads up
call WriteInt
call Crlf
call WriteSting
are from a lib from this guys book
Irvine

Code:
TITLE MASM AM5						(AM5.asm)

; Description:
; 
; Revision date:

INCLUDE Irvine32.inc
.data
myMessage BYTE "Assignment 5",0dh,0ah,0
myMess2	BYTE "The Sum is: " , 0

array DWORD 10, 60, 20, 33, 72, 89, 45, 65, 72, 18
sample DWORD 50
ArraySize = ($ - Array) / TYPE array
sum DWORD 0


.code
main PROC
	call Clrscr  
	
	mov	 edx,offset myMessage
	call WriteString
	
	mov ecx,0

	.WHILE ecx < ArraySize			;while( index < ArraySize)
		mov eax,array[ecx]			;put whats in the array into a register for easy typeing
			
			.IF eax <= sample		;is what in the eax/array <= sample
				add sum,eax			;if so then add it to sum
			.ENDIF	
		inc ecx						;index++
	.ENDW

	mov	 edx,offset myMess2
	call WriteString				;display sum message
	mov edi, sum					;display sum
	call WriteInt
	call	Crlf
	
	exit
main ENDP

END main
 
1. You are incrementing the array index by one BYTE, but the array is in DWORDS, which are 4 BYTES. So the array you are iterating through is:

10, 00, 00 00, 60, 00, 00, 00, 20, 00, 00;

2. Your array size is going to be off because you have an intervening DWORD from then end of array and where you calculate the size of the array. So instead of arraysize = sizeof array, arraysize = sizeof array + 1. (Not that it will make any difference in you calculation results, but nipping bad habits now will prevent bugs down the road.)

Code:
.data

myMessage BYTE "Assignment 5",0dh,0ah,0my
Mess2     BYTE "The Sum is: " , 0
array     DWORD 10, 60, 20, 33, 72, 89, 45, 65, 72, 18
ArraySize = ($ - Array) / TYPE array 
sample    DWORD 50
sum       DWORD 0

.code
main PROC
    call Clrscr
     mov edx, offset myMessage
    call WriteString    
     mov ecx, arraysize
     xor ebx, ebx              ;quicker way to zero out a register
     mov eax, sum              ;register math is quicker than memory math
    .WHILE  ebx < ecx
        mov edx, array[ebx*4]  ;scale index to 4 bytes, size of dword
        .IF eax <= sample      ;is what in the eax/array <= sample
            add eax,edx        ;register math is faster than memory access
        .ENDIF
        inc ebx                ;index++    
    .ENDW
    push eax;                  ;store sum
    mov edx,offset myMess2
    call WriteString           ;display sum message
    pop edi                    ;restore sum to edi
    call WriteInt
    call Crlf   
    exit
main ENDP
END main

 
Thank you for the advice I was still thinking in Java, that just slipped my mind about running though the addresses not doing like java, at least I was not off by 1 but 4.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top