1 Replies - 948 Views - Last Post: 31 May 2018 - 10:14 PM

#1 SpencerC   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-May 18

Add/Subtract single digit using tasm 1.4

Posted 30 May 2018 - 10:15 AM

Hi.. Can someone please help me with my program?
Well, the program is supposed to ask 2 digits then display an option whether to add or subtract.
I was able to show the sum correctly as well as the difference when the first number is lower.
However if 1st input is higher i am getting an incorrect output for the difference.

.MODEL SMALL
.STACK 100h
.DATA 

SELECTION DB 13,10,10,'Options',10,10,'a.)ADDITION ',10,'b.)SUBTRACTION',10,10,'SELECT option: $'
FIRST_NUMBER DB 13,10,10,'ENTER FIRST NUMBER:$'
SECOND_NUMBER DB 13,10,'ENTER SECOND NUMBER:$'
msgEr DB 13,10,13,10,'INVALID! $'
ANSWER DB 13,10,10,'          THE SUM IS: $'
ANSWER2 DB 13,10,10,'         THE DIFFERENCE IS: $'

.CODE

start:

mov ax,@data
mov ds,ax

mov dx,OFFSET FIRST_NUMBER
mov ah,9
int 21h

mov ah,1
int 21h
sub al,30h
MOV BL,AL


cmp bl, '0'
jnz ms2
cmp bl, '1'
jnz ms2
cmp bl, '2'
jnz ms2
cmp bl, '3'
jnz ms2
cmp bl, '4'
jnz ms2
cmp bl, '5'
jnz ms2
cmp bl, '6'
jnz ms2
cmp bl, '7'
jz ms2
cmp bl, '8'
jz ms2
cmp bl, '9'
jz ms2
jne error

ms2:
AND BL,0Fh
mov dx,OFFSET SECOND_NUMBER
mov ah,9
int 21h
mov ah,1
int 21h
mov cl, al


cmp cl, '0'
jz optn
cmp cl, '1'
jz optn
cmp cl, '2'
jz optn
cmp cl, '3'
jz optn
cmp cl, '4'
jz optn
cmp cl, '5'
jz optn
cmp cl, '6'
jz optn
cmp cl, '7'
jz optn
cmp cl, '8'
jz optn
cmp cl, '9'
jz optn
jne error

optn:
mov dx,OFFSET SELECTION
mov ah,9
int 21h
mov ah,1
int 21h

cmp al,'a'
jz Addition
cmp al,'b'
jz compare
cmp al,'c'
jz Done

error:
lea dx,msgEr
mov ah,09h
int 21h 

Done:
mov ah,4ch 
int 21h

Addition:
mov ah,0
ADD CL,BL
AAA
OR AX,3030H
MOV Bx,AX

mov dx,OFFSET ANSWER
mov ah,9
int 21h

mov dl,CL
mov ah,2
int 21h 
jmp done

compare:
 cmp CL,BL
 jna normal
 ja reverse 
 
 
 reverse:
 mov ah,00h
 SUB CL,BL
 MOV AL,CL
 AAS
 OR AX,3030h
 MOV BX,AX

 mov dx,OFFSET ANSWER2
 mov ah,09h
 int 21h

 mov dl,"-"
 mov ah,02h
 int 21h
 mov dl,AL
 mov ah,02h
 mov dl,BL
 mov ah,02h
 int 21h
 jmp done
 
 normal:
 mov ah,0
 SUB BL,CL
 MOV AL,BL
 AAS
 OR AX,3030H
 MOV Bx,AX
 mov dx,OFFSET ANSWER2
 mov ah,9
 int 21h

 mov dl,Bl
 mov ah,2
 int 21h
 jmp done



 end start


Is This A Good Question/Topic? 0
  • +

Replies To: Add/Subtract single digit using tasm 1.4

#2 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,109
  • Joined: 03-April 12

Re: Add/Subtract single digit using tasm 1.4

Posted 31 May 2018 - 10:14 PM

It looks like you forgot to convert the second input to number (missing sub al,30h).
It's funny that the first number passes these: "cmp bl, '0'". I thought '0' would give the ASCII code for zero (0x30).

Also these make me wonder (the high byte):

Quote

110 OR AX,3030H

Adding 0x30 would have been clearer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1