0

What this program should do

I am taking two numbers as input from the user and want to check if these two numbers are not equal to zero. Then the program should just add them, but if the first variable is greater than the second, then it should be subtracted from the second.

In the other case it shouldn't perform any operation and prompt the user the possible error.

Here is my code adding both two numbers.

The errors I get are:

  • When I enter two zeros, it just executes all instructions step by step.
  • And subtraction is not performing.
DOSSEG
.MODEL SMALL
.STACK 100H
.DATA
VAR1 DB ?
VAR2 DB ?
MSG1 DB  'ADDITION IS : $'
MSG2 DB  'ADDITION IS NOT POSSIBLE : $'
MSG3 DB  'SUBTRACTION IS : $'
MSG4 DB  'SUBTRACTION IS NOT POSSIBLE : $'
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS ,AX

MOV AH ,1
INT 21H
MOV VAR1 ,AL
MOV AH, 1
INT 21H
MOV VAR2 ,AL
CMP VAR2,0
JNE J1
JE J3

J1:
CMP VAR1,0
JNE J2
JE J4
J2:
MOV DX ,OFFSET MSG1
MOV AH,9
INT 21H

MOV DL,VAR2
ADD  DL,VAR1
SUB DL,48
MOV AH,2
INT 21H

MOV  DL,VAR1

JMP L5
JE L8
L5:
MOV DX ,OFFSET MSG3
MOV AH,9
INT 21H
MOV BL,VAR2
SUB DL,BL
ADD DL,48
MOV AH,2
INT 21H
JMP J9
L8:
MOV DX ,OFFSET MSG4
MOV AH,9
INT 21H
JMP J9
J3:
MOV DX ,OFFSET MSG2
MOV AH,9
INT 21H
JMP J9

J4:
MOV DX ,OFFSET MSG2
MOV AH,9
INT 21H
JMP J9

J9:
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
zx485
  • 28,498
  • 28
  • 50
  • 59
  • In the case of subtraction, should the greater number be subtracted from the the smaller one, or the other way round? This part was unclear to me. – zx485 Mar 13 '20 at 19:55
  • 1
    Looks like you compared against integer `0` instead of the ASCII code for the digit `'0'` (aka 48 which you did subtract later after adding two ASCII codes). [Why won't the program print the sum of the array?](https://stackoverflow.com/a/53252729). Use your debugger to look at register values! What do you mean "subtraction is not performing."? Do you mean you get a wrong result, or no output, or what? Not a [mcve] – Peter Cordes Mar 13 '20 at 19:55
  • Yes greater no should be subtracted from smaller @zx485 – Brown Bear Mar 13 '20 at 20:12
  • No output in case of var2-var1 @Peter Cordes – Brown Bear Mar 13 '20 at 20:13
  • can you able to fix this code ? @zx485 – Brown Bear Mar 13 '20 at 20:38
  • Use your debugger to look at register values. In the L5: block, it looks like DL will still be the low byte of the address you put in DX, so subtracting `var2` and adding `'0'` might lead to a non-printable ASCII code that looks like whitespace when you output it to the screen. – Peter Cordes Mar 13 '20 at 21:08

0 Answers0