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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

new to assembly, need some help

Status
Not open for further replies.

jwilt11

Programmer
Jun 4, 2014
6
0
0
US
my goal is to add all the number in ary, but my problem seems to come on the command "add eax,[esi]" if anyone could give me some advice it would be greatly appreciated.


TITLE MASM Template (main.asm)

; Description:
;
; Revision date:

INCLUDE Irvine32.inc
.data
myMessage BYTE "MASM program example",0dh,0ah,0

ary dword 100, -50, 125, 94, 35, -92, 82, 634, 193, 99, -54, 1, -5, 200, 15
dword 81, 44, 143, 132, 52, -62, 38, 56, 42, -81, 55, 43, 121, 25, 0

.code
main PROC
call Clrscr

lea esi,ary ;
addloop: ;
add esi,4 ;
add eax, [esi] ;
cmp esi,0 ;
jne addloop ;

call DumpRegs

mov edx,offset myMessage
call WriteString

exit
main ENDP

END main
 
It runs successfully but acts like an infinite loop and won't print anything. When I run it on debug it shows an error on the line previously stated
 
Code:
lea esi,ary ;

You're loading an ADDRESS into esi, which will never be zero.

Code:
cmp esi,0 ;
jne addloop ;

The loop continues when esi <> 0, which it will never be.

I can only guess you are wanting to add numbers until the CONTENTS of esi is zero.

Since you can't compare against the contents of memory, you'll have to move it to a register before you do so.

Code:
 lea esi,ary
 addloop:
 add esi,4
 mov ecx, [esi]
 add eax, ecx
 cmp ecx,0
 jne addloop
 mov answer, eax

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
ah that makes sense, thank you for the help
 
I have a new problem in my sumloop or squareloop, it just does an infinite loop and won't print and debug won't show anything is wrong.

TITLE MASM Template (main.asm)

; Description:
;
; Revision date:

INCLUDE Irvine32.inc
.data
myMessage BYTE "MASM program example",0dh,0ah,0

ary dword 100, -50, 125, 94, 35, -92, 82, 634, 193, 99, -54, 1, -5, 200, 15
dword 81, 44, 143, 132, 52, -62, 38, 56, 42, -81, 55, 43, 121, 25, 0

.code
main PROC
call Clrscr

mov eax,0 ; *******************
mov ebx,0 ; *******************
mov ecx,0 ; ** set registers **
mov edx,0 ; ** to zero **
mov ebp,0 ; *******************

mov esi,offset ary ; set esi to START of the array

addloop: ; looping structure for computing the sum
inc ecx ; counter for # of values in array
mov edx,[esi] ; move the value in the array to edx
add eax, edx ; add number just read in to eax to keep the sum
add esi,4 ; to move through the array
cmp edx,0 ; compare the just read in value to sentinal value
jne addloop
dec ecx ; subtract one from counter to account for sentinal value

mov edx,eax ; make copy of sum
subloop: ; division by repeated subtraction
inc ebx ; increase by 1 for each loop for average
sub edx,ecx ; subtract # of values from sum
cmp edx,ecx ; compare sum to the number of values
jnl subloop

mov edx,0
mov eax,0
sumloop: ; looping structure for computing the sum
dec ecx ; decrease to compare to # of values
mov ebp,[esi] ; move the value in the array to ebp
sub ebp,ebx ; subtract the average from the value just read in
mov edi,ebp ; copy value after subtraction for counter
squareloop:
add edx,ebp ; repeated addition to find square
dec edi ; starts at value after subtracting average and decreases to 0 by 1
cmp edi,0 ; exits when the number has been squared
jne squareloop
add eax,edx ; sum of all values after being squared
add esi,4 ; to move through the array
cmp ecx,0 ; compare the total # of values to current # of values
jne sumloop

call DumpRegs

mov edx,offset myMessage
call WriteString

exit
main ENDP

END main
 
a
Code:
TITLE MASM Template						(main.asm)

; Description:
; 
; Revision date:

INCLUDE Irvine32.inc
.data
myMessage BYTE "MASM program example",0dh,0ah,0
    
    ary	  dword	    100, -50, 125, 94, 35, -92, 82, 634, 193, 99, -54, 1, -5, 200, 15
		  dword	    81, 44, 143, 132, 52, -62, 38, 56, 42, -81, 55, 43, 121, 25, 0

.code
main PROC
	call Clrscr 
	   
	   mov	 eax,0				    ;   *******************
	   mov	 ebx,0				    ;   *******************
	   mov	 ecx,0				    ;   ** set registers **
	   mov	 edx,0				    ;   **    to zero	**
	   mov	 ebp,0				    ;   *******************

	   mov esi,offset ary			    ;   set esi to START of the array

addloop:							    ;   looping structure for computing the sum
	   inc	 ecx					    ;   counter for # of values in array
	   mov	 edx,[esi]			    ;   move the value in the array to edx
	   add	 eax, edx				    ;   add number just read in to eax to keep the sum
	   add	 esi,4				    ;   to move through the array
	   cmp	 edx,0				    ;   compare the just read in value to sentinal value
	   jne	 addloop
	   dec	 ecx					    ;   subtract one from counter to account for sentinal value

	   mov	 edx,eax				    ;   make copy of sum
subloop:							    ;   division by repeated subtraction
	   inc	 ebx					    ;   increase by 1 for each loop for average
	   sub	 edx,ecx				    ;   subtract # of values from sum
	   cmp	 edx,ecx				    ;   compare sum to the number of values
	   jnl	 subloop

	   mov	 edx,0
	   mov	 eax,0
sumloop:							    ;   looping structure for computing the sum
	   dec	 ecx					    ;   decrease to compare to # of values
	   mov	 ebp,[esi]			    ;   move the value in the array to ebp
	   sub	 ebp,ebx				    ;   subtract the average from the value just read in
	   mov	 edi,ebp				    ;   copy value after subtraction for counter
squareloop:						    
	   add	 edx,ebp				    ;   repeated addition to find square
	   dec	 edi					    ;   starts at value after subtracting average and decreases to 0 by 1
	   cmp	 edi,0				    ;   exits when the number has been squared
	   jne	 squareloop			    
	   add	 eax,edx				    ;   sum of all values after being squared
	   add	 esi,4				    ;   to move through the array
	   cmp	 ecx,0				    ;   compare the total # of values to current # of values
	   jne	 sumloop

	call	  DumpRegs

	mov	 edx,offset myMessage
	call WriteString

	exit
main ENDP

END main
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top