problem to sum negative numbers in array

Started by umen, August 20, 2005, 04:08:44 PM

Previous topic - Next topic

umen

Hello all
i have this code that counts and prints the content of array and it is working great
but when it gets to negative numbers , i dont know how to deal with them . can someone please how
can i sum the array that contains negative numbers ?
here is the code (with positive numbers :

---------------------------------------------------------------------------------------
DSEG SEGMENT
; variables:
RESULT DW ?
INTARRY DW 1,10,128,255,7,150,200,250,21,10
MESS DB 'THE TOTAL IS ','$'


DSEG ENDS
SSEG SEGMENT STACK
DW  100H DUP(?)
SSEG ENDS
CSEG SEGMENT
ASSUME DS:DSEG,SS:SSEG,CS:CSEG
; number of elements:
BEGIN:
MOV [RESULT], 0000H ; CLEAR RESULT
MOV CX, 10    ; COUNT OF ELEMENTS
MOV BX, OFFSET INTARRY ; POINT TO INTARRY
ADD     BX,256
MOV SI, 0000H ; FIRST ELEMENT
XOR AX, AX ; CLEAR TOTAL
LP1: ADD AX, [BX + SI] ; ADD VALUE TO TOTAL
INC SI ; NEXT ELEMENT
INC SI
DEC CX
JNE LP1
MOV [RESULT], AX ; STORE TOTAL

MOV DX, OFFSET MESS ; PRINT MESSAGE
ADD     DX,256
MOV AH, 9H
INT 21H
MOV AX,[RESULT]
CALL PRINT_AX_2

; wait for any key press:
mov ah, 0
int 16h

PRINT_AX      PROC    NEAR
      PUSH    AX
      ADD     DL,30H
      CMP     DL,'9'
      JBE     OK
      ADD     DL,7
OK:   MOV     AH,2
      INT     21H
      POP     AX
      RET
PRINT_AX        ENDP
;***********************************************************
print_ax_1      proc near
         
      MOV     CL,4
      MOV     DL,AL
      SHR     DL,CL
      CALL    PRINT_AX
      MOV     DL,AL
     
      AND     DL,0FH
      CALL    PRINT_AX
      MOV     DL,AH
      SHR     DL,CL
      CALL    PRINT_AX
      MOV     DL,AH
 
      AND     DL,0FH
      CALL    PRINT_AX
      ret
print_ax_1      endp

;***************************************************************
print_ax_2     proc near
      MOV     CL,4
      MOV     DL,AH
      SHR     DL,CL
      CALL    PRINT_AX
      MOV     DL,AH
      AND     DL,0FH
      CALL    PRINT_AX
      MOV     DL,AL
      SHR     DL,CL
      CALL    PRINT_AX
      MOV     DL,AL
      AND     DL,0FH
      CALL    PRINT_AX
      ret
print_ax_2      endp
CSEG ENDS
END BEGIN

--------------------------------------------------------------------------------------------------
but the result is strange when it comes dealing with array that contains negative numbers
say array like that :
INTARRY   DW   1,10,128,-255,7,150,200,-250,-21,-10

thanks for the helpers

I added the "code" "/code" tags in square brackets so you code was easier to read. Just click on the "mofify" button to see what the code tas look like.

MichaelW

The ADD instruction can handle negative integers as well as positive integers. The problem is in the code that displays the result. Basically, you need to check the sign of the result and display a "-" in front of the value if the sign is negative. For signed integers the most significant bit functions as a sign bit. A value of 0 for the sign bit indicates a positive integer and a value of 1 indicates a negative integer.
eschew obfuscation

QvasiModo

Quote from: MichaelW on August 22, 2005, 12:20:11 AM
The ADD instruction can handle negative integers as well as positive integers. The problem is in the code that displays the result. Basically, you need to check the sign of the result and display a "-" in front of the value if the sign is negative. For signed integers the most significant bit functions as a sign bit. A value of 0 for the sign bit indicates a positive integer and a value of 1 indicates a negative integer.

In pseudo code:

if number < 0:
    print "-"
    number = -number
end if
print number

MichaelW

Quote from: QvasiModo on August 22, 2005, 11:21:11 PM
In pseudo code:

if number < 0:
    print "-"
    number = -number
end if
print number

Duh, I left out an essential operation, the negation of the value. Thanks QvasiModo.
eschew obfuscation