flat assembler
Message board for the users of flat assembler.

Index > DOS > problem to sum negative numbers in array (masm)

Author
Thread Post new topic Reply to topic
umen



Joined: 14 Aug 2005
Posts: 16
umen
Hello all (this is genral masm problem)
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
Post 20 Aug 2005, 16:11
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1605
Location: Ukraine
shoorick
hi! have no time for your qustion currently.
1. try to use loop or jcxz instead of jne after dec cx
2. why do you still use masm: home work? ready handbook? (just interesting Smile )
regards !
Post 20 Aug 2005, 19:09
View user's profile Send private message Visit poster's website Reply with quote
umen



Joined: 14 Aug 2005
Posts: 16
umen
Hello
the reason is simple ( I think ) Im learning masm because I there is lots of internet links that can help me.
but im only in the beginning and I trying hard to learn.
Post 21 Aug 2005, 06:00
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1605
Location: Ukraine
shoorick
understand Wink so, with subj: any success?
Post 30 Aug 2005, 07:57
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.