# Simple Calculator

• (2 Pages)
• 1
• 2

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

### #16 Fredex

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

## Re: Simple Calculator

Posted 05 March 2013 - 10:45 PM

GunnerInc, on 06 March 2013 - 03:22 AM, said:

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.

I cant relate to MASM & NASM...

### #17 Fredex

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

## Re: Simple Calculator

Posted 06 March 2013 - 12:18 AM

So far..This is what I've done. I cant get exact output of my project. Add and Subtract function is working..Multiplication and Division is my problem.

This is my code...

```.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 (?)
num db 2 dup(?),'\$'
stop_num db,10,13,'\$'
number db (?)
digit db 0,0,'\$'

.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

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

cmp choice1,'M'
je multiplication

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

cmp choice1,'D'
je division

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

```

Anyone can help me solving this...

### #18 Fredex

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

## Re: Simple Calculator

Posted 06 March 2013 - 05:09 AM

help_me...

### #19 GunnerInc

• "Hurry up and wait"

Reputation: 910
• Posts: 2,357
• Joined: 28-March 11

## Re: Simple Calculator

Posted 06 March 2013 - 05:31 AM

why have you not read the Intel manuals? you should have them if your programming in Assembly. mul and div are both there

### #20 Fredex

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

## Re: Simple Calculator

Posted 06 March 2013 - 02:37 PM

ok.ok

### #21 Fredex

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

## Re: Simple Calculator

Posted 07 March 2013 - 07:49 PM

GunnerInc, on 06 March 2013 - 12:31 PM, said:

why have you not read the Intel manuals? you should have them if your programming in Assembly. mul and div are both there

Difficult to understand.

### #22 Fredex

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

## Re: Simple Calculator

Posted 08 March 2013 - 08:32 PM

Anyone....help!Whats wrong with this code for multiplication?
```
.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],bl

mov bl,0

;	mov ax,bx
mov cx,ax
ulit:

loop ulit

;	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

```

what should I add to bl in the loop?is it bl itself???thanks for the help again.

### #23 GunnerInc

• "Hurry up and wait"

Reputation: 910
• Posts: 2,357
• Joined: 28-March 11

## Re: Simple Calculator

Posted 09 March 2013 - 10:22 PM

Quote

Difficult to understand.

What is difficult to understand???

```    mov     eax, 15
mov     ecx, 3
mul     ecx
; eax now contains 45
```

and you can use 32 bit registers in 16bit DOS code.

### #24 Fredex

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

## Re: Simple Calculator

Posted 10 March 2013 - 11:24 PM

GunnerInc, on 10 March 2013 - 05:22 AM, said:

Quote

Difficult to understand.

What is difficult to understand???

```    mov     eax, 15
mov     ecx, 3
mul     ecx
; eax now contains 45
```

and you can use 32 bit registers in 16bit DOS code.

ok..I will explore more.

### #25 Fredex

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

## Re: Simple Calculator

Posted 10 March 2013 - 11:31 PM

my problem now is division.

### #26 GunnerInc

• "Hurry up and wait"

Reputation: 910
• Posts: 2,357
• Joined: 28-March 11

## Re: Simple Calculator

Posted 11 March 2013 - 05:58 PM

Ok, so what is your problem with division? Did you look up the div mnemonic? What don't you understand?

### #27 Fredex

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

## Re: Simple Calculator

Posted 11 March 2013 - 09:09 PM

GunnerInc, on 12 March 2013 - 12:58 AM, said:

Ok, so what is your problem with division? Did you look up the div mnemonic? What don't you understand?

storing input to registers is my problem that's why I prefer to use sub in looping to get the answer of division.

this is what I've done.

```.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 bl,al

;	mov cl,bl

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 bl,al
mov al,0
mov al,cl

mov ch,bl

try:
sub al,bl

loop try

mov bl,al

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

```

what can you say??

### #28 GunnerInc

• "Hurry up and wait"

Reputation: 910
• Posts: 2,357
• Joined: 28-March 11

## Re: Simple Calculator

Posted 12 March 2013 - 02:57 PM

I have the printed manuals, but I cannot copy and past that, so I went to: http://download.inte...nual/325383.pdf

Looked up DIV:

Quote

Divides unsigned the value in the AX, DX:AX, EDX:EAX, or RDX:RAX registers (dividend) by the source operand
(divisor) and stores the result in the AX (AH:AL), DX:AX, EDX:EAX, or RDX:RAX registers. The source operand can
be a general-purpose register or a memory location. The action of this instruction depends on the operand size
(dividend/divisor). Division using 64-bit operand is available only in 64-bit mode.

Disregard AX, DIV uses DX:AX, EDX:EAX, or RDX:RAX, We are working with 32 bit numbers or smaller, so we need to zero out eDX.

Quote

storing input to registers is my problem

What does that mean??? Have you even tried to use DIV???

To divide 17/3:
```    xor     dx, dx
mov     ax, 17 ; dividend
mov     cx, 3 ; divisor
div     cx
```

Now, AX == 5 and DX == 2

You learn by trying.

### #29 Fredex

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

## Re: Simple Calculator

Posted 12 March 2013 - 08:43 PM

thanks master..Ive learn alot.