0

; Hello, I'm new to assembly language x8086, the problem is how can I add two negative numbers (decimal) and display them in HEX. I'm stuck at the point the code does not detect the negative numbers and I don't know how to display it in HEX. can someone help me? here my code

TITLE PROB12
PAGE 60,132
.MODEL SMALL
.STACK 64H
.DATA
NUM1 DB 8,?,8 DUP (?)
NUM2 DB 8,?,8 DUP (?)
PROMPT1 DB CR,LF,'Enter the first number','$'
PROMPT2 DB CR,LF,'Enter the second number','$'
PROMPT3 DB CR,LF,'The total sum is '
SUM DB 7 DUP (?),'$'
CR EQU 0DH
LF EQU 0AH
.CODE
MAIN: MOV AX,@DATA
MOV DS,AX
CALL CLEAR ;clear screen
MOV AH,09
MOV DX,OFFSET PROMPT1
INT 21H ;display first prompt
MOV AH,0AH
MOV DX,OFFSET NUM1
INT 21H ;get first number
MOV AH,09
MOV DX,OFFSET PROMPT2
INT 21H ;display second prompt
MOV AH,0AH
MOV DX,OFFSET NUM2
INT 21H ;get second number
MOV SI,OFFSET NUM1 + 8 ;point to LSD of number 1
MOV DI,OFFSET NUM2 + 8 ;point to LSD of number 2
MOV BX,OFFSET SUM + 6 ;point to LSD of sum
MOV CX,7 ;add 7 bytes
CLC ;clear carry
ADD_LP: MOV AL,[SI] ;get byte from number 1
ADC AL,[DI] ;add byte from number 2
PUSHF ;save any carry
AAA ;ASCII adjust
OR AL,30H ;make it ASCII
POPF ;restore flags
MOV [BX],AL ;store sum (in BCD)
DEC SI ;decrement pointers
DEC DI ;to point to next byte
DEC BX
LOOP ADD_LP ;loop through 7 bytes
MOV AH,09
MOV DX,OFFSET PROMPT3
INT 21H ;display result
MOV AH,4CH
INT 21H ;go back to DOS
;--------------------------------------------------
CLEAR PROC
MOV AH,06 ;clear screen function
MOV AL,00 ;page 0
MOV BH,07 ;normal attribute
MOV CX,0 ;entire screen
MOV DX,184FH
INT 10H
RET
CLEAR ENDP
END MAIN
Sep Roland
  • 33,889
  • 7
  • 43
  • 76

1 Answers1

0

I'm stuck at the point the code does not detect the negative numbers and I don't know how to display it in HEX

It makes no sense asking about the above if the code that you have written has more fundamental issues.

Your code uses the DOS.BufferedInput function 0Ah, and hopes to find the least significant digit of the inputted numerical characters at offset 8 of the input structure. This approach can only work if the user at the keyboard always provides precisely 7 digits. That's not very user-friendly.

The loop that later adds two such numbers is not propagating the carry like it should! Currently you are preserving the carry from the adc operation, but that's pointless since it will always be clear. You need to propagate throughout the loop the carry that you obtain from the aaa instruction.

  MOV  CX,7    ;add 7 bytes
  CLC
ADD_LP:
  MOV  AL,[SI] ;get byte from number 1
  ADC  AL,[DI] ;add byte from number 2
  AAA          ;ASCII adjust

  PUSHF        ;save any carry
  OR   AL,30h  ;make it ASCII
  POPF         ;restore flags

  MOV  [BX],AL ;store sum (in BCD)
  DEC  SI      ;decrement pointers
  DEC  DI      ;to point to next byte
  DEC  BX
  LOOP ADD_LP  ;loop through 7 bytes

If you were going to detect a negative number then you would have to inspect the very first inputted characters cmp byte ptr [NUM1 + 2], '-' and cmp byte ptr [NUM2 + 2], '-'. If both are indeed present then the current addition is fine as long as you iterate over the remaining 6 digits (7-1) and that you write a "-" character at SUM.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76