Click here to Skip to main content
15,065,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey Guys,
I'm trying to fint the AVG of this array : 1742,1065,-67,-2988,-796,-1000,31,-67,-100,1180
I belive my method to change the number from POS to NEG is worng.
my prog need's to calculate the above array then print massege if the AVG is NEG or POS, and it always print that the AVG is POS even though it's not.
here is my code so far:
; lab56.asm
;
    .MODEL SMALL
    .STACK 100h
    .DATA
AVG_NEG	 DB  'THE AVG IS NEG',13,10,'$'
AVG_POS	 DB  'THE AVG IS POS',13,10,'$'
INDEX    DB  'Numbers that are larger than the average are in indexes:',13,10,'$'
RES      DB  '                     ','$'
ARR	 	 DW  1742,1065,-67,-2988,-796,-1000,31,-67,-100,1180
Ten 	 DW  10
AVG      DW  0
temprint DB  '      ','$'
;Program start here:
	.CODE
	MOV AX,@DATA    ; DS can be written to only through a register
    MOV DS,AX       ; Set DS to point to data segment
	LEA SI, ARR
;
;   SUMUP
	MOV CX,10		;10 variables in array
Sum:
	MOV AX,[SI]
	CMP AX,0
	JG Pos_label
	XOR AX,0000000000000000b
	ADD AX,0000000000000001b
Pos_label:
	ADD AVG,AX
	ADD SI,2		;move to the next number
LOOP Sum
;	Divided by 10 to get the AVG
	CWD				; AX -> DX:AX
	IDIV Ten
	MOV AVG,AX
;	print
	
;	Check if NEG or POS
	CMP AVG,0
	JG Avg_label
	MOV AH,9       ; Set print option for int 21h
    MOV DX,OFFSET AVG_NEG		;  Set  DS:DX to point to AVG_NEG
    INT 21h
	JMP continue
Avg_label:
	MOV AH,9       ; Set print option for int 21h
    MOV DX,OFFSET AVG_POS		;  Set  DS:DX to point to AVG_POS
    INT 21h
continue:
;

;Program end's here:

     MOV AH,4Ch       ; Set terminate option for int 21h
     INT 21h       ; Return to DOS (terminate program)
     END 
Posted
Updated 22-Aug-13 8:26am
v3
Comments
Richard MacCutchan 23-Aug-13 4:03am
   
The average should take the sum of all the numbers, not the sum of all the absolute values. You can also use the NEG instruction to change the sign.

1 solution

ADD works regardless of the sign of numbers. Do the following:
- before the loop put zero into ax
- in the loop do the following: add the value of [si] to ax and then increase SI with 2
- the rest of your code looks fine

* using the avg memory location is unnecessary
* xor-ing something with zero does nothing with the original value (except setting some flags)
* what is that add ax,0001b ??????????????

EDIT:
BTW, what if the average is zero??? Is zero positive or negative in your case?
   
v2
Comments
idobry 23-Aug-13 3:11am
   
with XOR thing and the ADD AX,0001b i was trying to do:
for example number 10, in binary : 0001010 , so if i want to transfer to -10, first change evey 0->1 and 1->0 : 11110101 and the seconde step is to add 1 (0000001) so the finale result -10 : 1110110.

now i did as you said and changed my sumloop to this:
; SUMUP
MOV CX,10 ;10 variables in array
MOV AX,0
Sum:
MOV AX,[SI]
ADD SI,2 ;move to the next number
ADD AVG,AX
LOOP Sum

then i printed the sum result but i got 64541 and its like he dosnt read the neg numbers
pasztorpisti 23-Aug-13 9:41am
   
So basically you wanted to negate "manually" that is completely unnecessary because Add handles negative numbers well. The x86 instruction set uses 2's complement to represent negative numbers. With 2's complement the basic instructions: add and sub must be performed in the same way regardless of the sign. http://en.wikipedia.org/wiki/Two%27s_complement

So you don't need any neg. BTW, there is a neg instruction that does all of these in one step (neg ax)... "Xor something,0" does nothing (just changes some flags but the value remains the same), "xor something, 1111...11b" flips the bits where you specified 1s, with all ones it flips every bit, but if you want to flip every bit (and not only a few) then there is a NOT istruction for that (not ax).
I think its time to read this too: http://en.wikipedia.org/wiki/X86_instruction_listings (you need only the "Original 8086/8088 instructions" table)
pasztorpisti 23-Aug-13 9:42am
   
BTW, you are zeroing out ax at the beginning and then you are collecting the sum into avg (that is fortunately zeroed by default)... Total mess.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900