0

So I have a code where I have 2 labels in it, Press 1 for Addition and 2 for Subtraction problem is only one label will be read pls help me on solving this problem. This is my code:

    JMP START
msg1    dw     10, 13, 10, 13, "Choose Operations:",0Dh,0Ah,0Dh,0Ah,09h
        dw      "1- Addition[+]",0Dh,0Ah,09h
        dw      "2- Subtraction[-]",0Dh,0Ah,09h      
        dw      "3- Exit",0Dh,0Ah,09h
        dw      "Enter: " 
        dw      '$'   
START:
    mov ax, 3
    int 10h

    mov ah, 2
    mov dh, 10
    mov dl, 10
    int 10h

    lea     dx, msg1  
    mov     ah, 09h 
    int     21h     

getnum:        
    mov     ah, 1 
    int     21h        

    cmp     al, '1' 
    jl      START  
    cmp     al, '3'
    jg      START 

    cmp     al, "1"
    je      Addition
    cmp     al, "2"
    je     Subtraction
    cmp     al, "3"
    jmp     Quit
    cmp     al, "4" 
Quit: 
    mov   ah,4ch
    int   21h
Subtraction:
VAR1 DW 5, 2 DUP(?)
VAR2 DW 5, 2 DUP(?)

PROMPT DW 'ENTER FIRST NUMBER:', '$'
PROMPT1 DW 'ENTER SECOND NUMBER :', '$'
PROMPT2 DW 'RESULT OF FIRST AND SECOND NUMBER IS: ', '$'

LEA DX,PROMPT
MOV AH,09H
INT 21H

;======================

 MOV  AH, 00             
 INT  16h

 MOV VAR1,AL   

 MOV  DL, AL             
 MOV  AH, 2H             
 INT  21H 

;======================

MOV DL,0AH
MOV AH,02H
INT 21H

MOV DL,0DH
MOV AH,02H
INT 21H

LEA DX,PROMPT1
MOV AH,09H
INT 21H

;=================================

MOV  AH, 00             
INT  16h

MOV VAR2,AL   

MOV  DL, AL             
MOV  AH, 2H             
INT  21H 

;======================

MOV DL,0AH
MOV AH,02H
INT 21H

MOV DL,0DH
MOV AH,02H
INT 21H

;===============================
SUB VAR1,30H
MOV BH,VAR1
SUB VAR2,30H
MOV BL,VAR2
SUB BH,BL


LEA DX,PROMPT2
MOV AH,09H
INT 21H
;==================================

ADD BH,30H
MOV VAR2,BH
MOV  DL, BH                     
MOV  AH, 2H             
INT  21H 

Addition:  **~02 Jump > 128~**

mov ax, 3
int 10h
NUM1 DB ?
NUM2 DB ?
RESULT DB ?
M1 DB 10,13,"ENTER FIRST NUMBER TO ADD : $"
M2 DB 10,13,"ENTER SECOND NUMBER TO ADD : $"
M3 DB 10,13,"SUM: $"
M4 DB 10,13,"DIFFERENCE: $"

    mov ax, 3
    int 10h

    lea dx, M1
    mov ah, 9
    int 21h

    mov ah, 1
    int 21h    
    sub al, 30h
    mov NUM1, al
    lea dx, M2
    mov ah, 9
    int 21h

    MOV AH,1
    INT 21H   
    sub al, 30h
    mov NUM2, al
    add al, NUM1
    mov RESULT, al

    mov ah, 0
    aaa
    add ah, 30h
    add al, 30h

    mov bx, ax
    lea dx, M3
    mov ah, 9
    int 21h

    mov ah, 2
    mov dl, bh
    int 21h

    mov ah, 2
    mov dl, bl
    int 21h

    mov ah, 4ch
    int 21h    
ret
Michael Petch
  • 46,082
  • 8
  • 107
  • 198

1 Answers1

1

The conditional jumps on can only jump a distance of 127 bytes forward. The label Addition in your program is further away than this.

You solve the issue by re-arranging the tests so you can use a near jump that can jump the longer distance.

Because of the validation on the input

 cmp     al, "1"
 jb      START  
 cmp     al, "3"
 ja      START 

the only characters that you need to deal with are "1", "2" and "3".

First eliminate the "2", then eliminate the "3". What remains will be the "1".

 cmp     al, "2"
 je      Subtraction
 cmp     al, "3"
 je      Quit
 jmp     Addition 
Quit: 
 mov     ah, 4Ch
 int     21h
Subtraction:
 ...
Addition:
 ...

Your code has a serious problem in the fact that you've placed data where instructions are expected!

Subtraction:
VAR1 DW 5, 2 DUP(?)
VAR2 DW 5, 2 DUP(?)

PROMPT DW 'ENTER FIRST NUMBER:', '$'
PROMPT1 DW 'ENTER SECOND NUMBER :', '$'
PROMPT2 DW 'RESULT OF FIRST AND SECOND NUMBER IS: ', '$'

LEA DX,PROMPT
MOV AH,09H
INT 21H

You need to place the data below the instructions (much further down):

Subtraction:
 LEA DX,PROMPT
 MOV AH,09H
 INT 21H

 ...

 VAR1 DW 5, 2 DUP(?)
 VAR2 DW 5, 2 DUP(?)
 PROMPT DW 'ENTER FIRST NUMBER:', '$'
 PROMPT1 DW 'ENTER SECOND NUMBER :', '$'
 PROMPT2 DW 'RESULT OF FIRST AND SECOND NUMBER IS: ', '$'
Fifoernik
  • 9,779
  • 1
  • 21
  • 27
  • Why is data intermingled with code a serious problem? – Shift_Left Feb 14 '18 at 19:57
  • 1
    @Shift_Left: because it will decode and execute as instructions. It would be fine if it was in a separate section and simply *declared* in mixed order, but it's not here. – Peter Cordes Feb 14 '18 at 22:21