我正在尝试找到此阵列的AVG:1742,1065,-67,-2988,-796,-1000,31,-67,-100,1180 我相信我的方法是将数字从POS更改为NEG。 我的编程需要计算上面的数组然后打印massege如果AVG是NEG或POS,它总是打印AVG是POS,即使它不是。 这是我目前的代码:
; 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
答案 0 :(得分:1)
如果在将所有输入添加到一起之前更改所有输入的符号,那么显然总是会得到相同的符号(禁止溢出),因此也会得到平均值。此外,计算的平均值将是错误的,因为您将平均幅度而不是实际值。
你改变标志的方法确实是错误的,所以不要这样做。只需按照在小学阶段学到的方法计算平均值:添加数字并除以计数。