# Simple Calculator

• (2 Pages)
• 1
• 2

## 28 Replies - 22482 Views - Last Post: 12 March 2013 - 08:43 PM

### #1 Fredex

Reputation: 1
• Posts: 86
• Joined: 16-January 12

# Simple Calculator

Posted 01 March 2013 - 04:23 AM

I am just a newbie to this language. I am making a simple calculator using TASM. I already get the function of multiplication but my problem is the addition, subtraction and division.

Here is my code in multiplication:
```.model small
.stack 10h
.data
number db (?)
digit db 0,0,'\$'

.code

mov ax,@data
mov ds,ax

mov ax,3
int 10h

mov ah,1
int 21h

mov bl,al
sub bl,30h

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov ah,1
int 21h

mov number[0],al
sub number[0],30h

mov ch,0
mov cl,number[0]

mov al,bl
mov bl,0

balik:

loop balik

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov bh,0

mov cx,bx
digit_0:

cmp bl,0Ah
jl digit_1

sub bl,0Ah
inc digit[0]

loop digit_0

digit_1:
mov digit[1],bl

mov ah,2
mov dl,digit[0]
int 21h

mov ah,2
mov dl,digit[1]
int 21h

mov ah,4ch
int 21h

end

```

That is for multiplication only. Can you gave me an idea on how should I convert my formula in multiplication in order that my calculator provide answer for addition, subtraction and multiplication..thanks in advance your attention is much appreciated.

Is This A Good Question/Topic? 0

## Replies To: Simple Calculator

### #2 Fredex

Reputation: 1
• Posts: 86
• Joined: 16-January 12

## Re: Simple Calculator

Posted 01 March 2013 - 08:23 PM

Anyone has idea in my problem??

### #3 GunnerInc

• "Hurry up and wait"

Reputation: 899
• Posts: 2,336
• Joined: 28-March 11

## Re: Simple Calculator

Posted 01 March 2013 - 11:10 PM

Create separate functions for addition, subtraction, multiplication and division. Add a menu so the user can choose what operation to use.

### #4 Fredex

Reputation: 1
• Posts: 86
• Joined: 16-January 12

## Re: Simple Calculator

Posted 02 March 2013 - 07:40 PM

GunnerInc, on 02 March 2013 - 06:10 AM, said:

Create separate functions for addition, subtraction, multiplication and division. Add a menu so the user can choose what operation to use.

thanks.. first i make separate codes for every operation, but i cant get the logic of addition,subtraction and division..i just get the multiplication through exploring..any advise how multiplication differs to the 3 other operation..thanks again.

### #5 GunnerInc

• "Hurry up and wait"

Reputation: 899
• Posts: 2,336
• Joined: 28-March 11

## Re: Simple Calculator

Posted 02 March 2013 - 08:08 PM

First, why are you multiplying by addition?

There are mnumonics that do those operations:
SUB
DIV
MUL

### #6 Fredex

Reputation: 1
• Posts: 86
• Joined: 16-January 12

## Re: Simple Calculator

Posted 02 March 2013 - 09:19 PM

GunnerInc, on 03 March 2013 - 03:08 AM, said:

First, why are you multiplying by addition?

There are mnumonics that do those operations:
SUB
DIV
MUL

thank you so much for this...i really dont know that theres mul and div function..

how to use mul and div function??can you site a sample?thanks.

### #7 Fredex

Reputation: 1
• Posts: 86
• Joined: 16-January 12

## Re: Simple Calculator

Posted 03 March 2013 - 01:43 AM

Master Gunner I got the addition..

this is my code:
```
.model small
.stack 10h
.data
prom db "Enter the 1st no.:\$"
promp db "Enter the 2nd no.:\$"
num db 2 dup(?),'\$'
stop_num db,10,13,'\$'
.code
mov ax,@data
mov ds,ax

mov ah,9
lea dx,prom
int 21h

mov ah,1
int 21h

mov num[0],al

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov ah,9
lea dx,promp
int 21h

mov ah,1
int 21h

mov num[1],al

mov bl,0

sub bl,30h

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov ah,9
lea dx,result
int 21h

mov dl,13
int 21h

mov ah,2
mov dl,bl
int 21h

mov ah,4ch
int 21h

end

```

can you please give the subtraction formula of this

```	add bl,num[0]

sub bl,30h

```

thanks..

### #8 GunnerInc

• "Hurry up and wait"

Reputation: 899
• Posts: 2,336
• Joined: 28-March 11

## Re: Simple Calculator

Posted 03 March 2013 - 09:32 PM

You cannot learn unless you know what you are doing and why. Do you know why you are subtracting 48 from bl?

Lets say the first number entered is "5", the ASCII code for the character "5" (NOT THE NUMBER 5) is 53. The second number entered is "4", the ASCII code for the character "4" is 52.

So:
5 = 53
+4 = 52
-------
9 = 105 the character that the ASCII code represents is "i", in order to print the number 9, you subtract 48 from 105, which gives you 57 which is the ASCII code for "9". This will ONLY work for single digits, to convert multi digits to ASCII is a bit more involved.

Let's do subtraction:
5 = 53
-4 = 52
-------
1 = 1 < - this is the NUMBER 1 not the character 1! Add 48 to the number 1 and it will give you the ASCII code for "1" - 49.

You really need to have an ASCII table next to you while you are learning and you will see the spacing between 1 and "1" is 48.

```    mov bl,num[0]

sub bl,30h
```

How would you convert to subtraction?

### #9 Fredex

Reputation: 1
• Posts: 86
• Joined: 16-January 12

## Re: Simple Calculator

Posted 04 March 2013 - 02:30 AM

GunnerInc, on 04 March 2013 - 04:32 AM, said:

You cannot learn unless you know what you are doing and why. Do you know why you are subtracting 48 from bl?

Lets say the first number entered is "5", the ASCII code for the character "5" (NOT THE NUMBER 5) is 53. The second number entered is "4", the ASCII code for the character "4" is 52.

So:
5 = 53
+4 = 52
-------
9 = 105 the character that the ASCII code represents is "i", in order to print the number 9, you subtract 48 from 105, which gives you 57 which is the ASCII code for "9". This will ONLY work for single digits, to convert multi digits to ASCII is a bit more involved.

Let's do subtraction:
5 = 53
-4 = 52
-------
1 = 1 < - this is the NUMBER 1 not the character 1! Add 48 to the number 1 and it will give you the ASCII code for "1" - 49.

You really need to have an ASCII table next to you while you are learning and you will see the spacing between 1 and "1" is 48.

```    mov bl,num[0]

sub bl,30h
```

How would you convert to subtraction?

Thanks sir..

I just reverse my addition to get subtraction.

This is my code:
```.model small
.stack 10h
.data
prom db "Enter the 1st no.:\$"
promp db "Enter the 2nd no.:\$"
num db 2 dup(?),'\$'
stop_num db,10,13,'\$'
.code
mov ax,@data
mov ds,ax

mov ah,9
lea dx,prom
int 21h

mov ah,1
int 21h

mov num[0],al

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov ah,9
lea dx,promp
int 21h

mov ah,1
int 21h

mov num[1],al

mov bl,0

sub bl,num[1]

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov ah,9
lea dx,result
int 21h

mov dl,13
int 21h

mov ah,2
mov dl,bl
int 21h

mov ah,4ch
int 21h

end

```

is it capable to loop addition to get multiplication as well as division from subtraction ??

where should I store the value of muliplier and multiplicand??

### #10 Fredex

Reputation: 1
• Posts: 86
• Joined: 16-January 12

## Re: Simple Calculator

Posted 04 March 2013 - 07:10 PM

Master Gunner, I need your advice regarding mul mnemonics...I have no idea of using it..

Can you give a sample operation with the formula that can get the product like what you've done in addition.

```	mov num[1],al

mov bl,0

sub bl,num[1]

```

you said using this add mnemonics on getting product of two number is not good enough..
```.model small
.stack 10h
.data
number db (?)
digit db 0,0,'\$'

.code

mov ax,@data
mov ds,ax

mov ax,3
int 10h

mov ah,1
int 21h

mov bl,al
sub bl,30h

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov ah,1
int 21h

mov number[0],al
sub number[0],30h

mov ch,0
mov cl,number[0]

mov al,bl
mov bl,0

balik:

loop balik

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov bh,0

mov cx,bx
digit_0:

cmp bl,0Ah
jl digit_1

sub bl,0Ah
inc digit[0]

loop digit_0

digit_1:
mov digit[1],bl

mov ah,2
mov dl,digit[0]
int 21h

mov ah,2
mov dl,digit[1]
int 21h

mov ah,4ch
int 21h

end

```

### #11 GunnerInc

• "Hurry up and wait"

Reputation: 899
• Posts: 2,336
• Joined: 28-March 11

## Re: Simple Calculator

Posted 04 March 2013 - 10:24 PM

For multi digit numbers, you have to loop through the string and add/subtract 48 appropriately.

The INTEL and AMD programmer manual have all the opcode you will need and their usage http://www.dreaminco...etting-started/

For samples, just search the internet! There are TONS of samples. The first few hits for: "masm mul sample" are good.

### #12 Fredex

Reputation: 1
• Posts: 86
• Joined: 16-January 12

## Re: Simple Calculator

Posted 05 March 2013 - 12:48 AM

GunnerInc, on 05 March 2013 - 05:24 AM, said:

For multi digit numbers, you have to loop through the string and add/subtract 48 appropriately.

The INTEL and AMD programmer manual have all the opcode you will need and their usage http://www.dreaminco...etting-started/

For samples, just search the internet! There are TONS of samples. The first few hits for: "masm mul sample" are good.

I will as again a question.

whats wrong with the looping of this multiplication.?

```Title arithmetic
.model small
.stack 10h
.data
prom db "Enter the 1st no.:\$"
promp db "Enter the 2nd no.:\$"
num db 2 dup(?),'\$'
stop_num db,10,13,'\$'
.code
mov ax,@data
mov ds,ax

mov ah,9
lea dx,prom
int 21h

mov ah,1
int 21h

mov num[0],al
;mov bl,al

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov ah,9
lea dx,promp
int 21h

mov ah,1
int 21h

mov num[1],al
;	mov bl,al
[b]	mov bx,ax

mov cx,ax
boom:
loop boom

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov ah,9
lea dx,result
int 21h

mov dl,13
int 21h

mov ah,2
mov dl,bl
int 21h

mov ah,4ch
int 21h

end

```

thanks again..

### #13 Fredex

Reputation: 1
• Posts: 86
• Joined: 16-January 12

## Re: Simple Calculator

Posted 05 March 2013 - 01:24 AM

I mean...TASM is old and outdated..

### #14 Fredex

Reputation: 1
• Posts: 86
• Joined: 16-January 12

## Re: Simple Calculator

Posted 05 March 2013 - 07:28 PM

how to solve this???

```.model small
.stack 10h
.data
operations db "A-Addition S-Subtraction M-Multiplication D-Division\$"
choice db "Enter Operation:\$"
prom db "Enter the 1st no.:\$"
promp db "Enter the 2nd no.:\$"
result db "The answer is \$"
choice1 db (?)
number db (?)
digit db 0,0,'\$'
num db 2 dup(?),'\$'
stop_num db,10,13,'\$'

.code

mov ax,@data
mov ds,ax

start:

mov ah,9
lea dx,operations
int 21h

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov ah,9
lea dx, choice
int 21h

mov ah,1
int 21h

mov choice1,al

cmp choice1,'A'

cmp choice1,'S'
je subtraction

cmp choice1,'M'
je multiplication

cmp choice1,'D'
je division

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov ah,9
lea dx,prom
int 21h

mov ah,1
int 21h

mov num[0],al

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov ah,9
lea dx,promp
int 21h

mov ah,1
int 21h

mov num[1],al

mov bl,0

sub bl,30h

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov ah,9
lea dx,result
int 21h

mov ah,2
mov dl,bl
int 21h

jmp exit

subtraction:

mov ah,9
lea dx,prom
int 21h

mov ah,1
int 21h

mov num[0],al

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov ah,9
lea dx,promp
int 21h

mov ah,1
int 21h

mov num[1],al

mov bl,0

sub bl,num[1]

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov ah,9
lea dx,result
int 21h

mov ah,2
mov dl,bl
int 21h

jmp exit

multiplication:
mov ah,9
lea dx,prom
int 21h

mov ah,1
mov bl,al
sub bl,30h

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov ah,9
lea dx,promp
int 21h

mov ah,1
int 21h

mov number[0],al
sub number[0],30h

mov ch,0
mov cl,number[0]

mov al,bl
mov bl,0

balik:

loop balik

mov ah,2
mov dl,10
int 21h

mov dl,13
int 21h

mov ah,9
lea dx,result
int 21h

mov bh,0

mov cx,bx
digit_0:

cmp bl,0Ah
jl digit_1

sub bl,0Ah
inc digit[0]

loop digit_0

digit_1:
mov digit[1],bl

mov ah,2
mov dl,digit[0]
int 21h

mov ah,2
mov dl,digit[1]
int 21h

int 21h

jmp exit

division:

exit:

mov ah,4ch
int 21h

end

```

how to solve this error mesage????

D:\>tasm final

Assembling file: final.ASM
**Error** final.ASM(50) Relative jump out of range by 002Ch bytes
**Error** final.ASM(53) Relative jump out of range by 00A4h bytes
Error messages: 2
Warning messages: None
Passes: 1
Remaining memory: 441k

thanks...

This post has been edited by Fredex: 05 March 2013 - 07:29 PM

### #15 GunnerInc

• "Hurry up and wait"

Reputation: 899
• Posts: 2,336
• Joined: 28-March 11

## Re: Simple Calculator

Posted 05 March 2013 - 08:22 PM

In my post up there, post #11, the link I gave you contains links to the INTEL and AMD docs, look at them! They contain all opcodes/mneumonics and tell how to use them. Try it! It describes MUL and DIV and how to use them.

I haven't used 16 bit code in over 20 years... I really don't like looking at code without comments, make my life harder.

I can Assemble no problems with MASM and NASM. In 16 bit code, you can only jump to labels a certain amount of bytes away. You will need to jump to a dummy label, then jump to your multiplication label, OR put each operation in a procedure and call the procedure.