Hello,

I'm having a problem trying to trying to store a local variable in my asm program. I push 3 parameters: nbrElts (number of elements in the array), nbrArray (the address of the array), and result1 (the average). I'm trying to divide by the number of elements that I push into the local variable space [ebp-4], but it locks up at that instruction and I can't figure out why. Any help would be greatly appreciated!

Main program:

.386
.MODEL FLAT

INCLUDE io.h

cr        EQU    0dh   ; carriage return character
LF        EQU    0ah   ; linefeed character
maxNbrs EQU	5	; size of array


ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD		; you need to declare your external routine
EXTERN      Avg:near32					; use this syntax to avoid mistakes


.STACK  4096           			 			 ; reserve 4096-byte stack



.DATA                   			 		; reserve storage for data
directions	BYTE	cr,Lf,Lf,"You may enter up to 5 numbers.",cr,Lf,Lf
		BYTE	"This program will find the average.",cr,Lf,Lf,0
prompt		BYTE	cr,Lf,"Number to insert into array? ",0
number		DWORD	?
found		BYTE	cr,Lf,Lf,"The average is: ",0
result1		DWORD	?
remainder	DWORD	?,cr,Lf,Lf,0
nbrArray	DWORD	maxNbrs	DUP (?)
nbrElts		DWORD	?






.CODE                               		  		 ; start of main program code
_start:

        output	directions	; display directions
	mov 	nbrElts,0	; number of elements = 0
	mov	result1,0	; average starts at 0
	lea	ebx,nbrArray	; get address of nbrArray

looper:
	output	prompt		; prompt for number
	input	number,4	; get number
	atod	number		; convert to integer
	mov	[ebx],eax	; store number in array
	inc	nbrElts		; add 1 to nbrElts
	add	ebx,4		; get address of next item of array
	inc	ecx		; increase the ecx by 1
	cmp	ecx,5		; is the ecx equal to 5?
	je	endlooper		; if ecx = 5, exit loop
	jmp	looper		; otherwise, repeat
endlooper:	   
	
	push	nbrElts		; push number of elements onto stack
	lea	eax,nbrArray	; push address of array onto stack
	push	eax
	push	result1		; push the address of average onto stack

	call   	Avg    		; procedure call

	dtoa	result1,eax	;   convert integer to ascii
	dtoa	remainder,edx	;  convert remainder to ascii

	output	found		; output the average

           

INVOKE ExitProcess, 0      		 			; exit with return code 0

PUBLIC _start                          		 		; make entry point public

            END                        		 		; end of source code

The external call program:

.386
.MODEL FLAT

PUBLIC	Avg

.CODE

Avg  PROC   NEAR32
		  
	        push	ebp	   	; store ebp 
		mov	ebp,esp       	; freeze stack pointer
		sub	esp,4		; create space for one local dword
		push   	ecx             ; store ecx		
		push   	ebx	  	; store ebx 
		  		
		pushf			; store flags
		
		mov	ebx,[ebp+12]	; move array into ebx
		mov	ecx,[ebp+16]	; NbrElts in array into count
		mov	[ebp-4],ecx	; store number of elements 
		mov	eax,[ebp+8]	; move result1 address into eax

		
forCount:
		add	eax,[ebx] 	;   load array integer
		add	ebx,4		; get address for next item in array
		loop	forCount          ; loop until count is 0
done:

		
		
		div	DWORD PTR [ebp-4]	; divide eax by nbrelts

		popf			; restore flags
     		pop    	ecx	       	; restore ecx 
		pop    	ebx             ; restore ebx  		

  
		pop    	ebp 		; restore ebp    
        	ret           		; return
    




Avg  ENDP

PUBLIC AVG

	END

So simple...was missing mov esp,ebp to restore the stack pointer after subtracting 4 from it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.