Simple Calculator

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 Fredex  Icon User is offline

  • D.I.C Head

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

Re: Simple Calculator

Posted 05 March 2013 - 10:45 PM

View PostGunnerInc, 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...
Was This Post Helpful? 0
  • +
  • -

#17 Fredex  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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'
	je addition
	
	cmp choice1,'S'
	je subtraction	
		
	mov ah,2
    mov dl,10
    int 21h

    mov dl,13
    int 21h	
		
addition:
	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
	
	add bl,num[0]
	add bl,num[1]
	
	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
	
	add bl,num[0]
	sub bl,num[1]
	
	add 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,'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:

    add bl,al

    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
    add digit[1],30h
    add digit[0],30h

    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... :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#18 Fredex  Icon User is offline

  • D.I.C Head

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

Re: Simple Calculator

Posted 06 March 2013 - 05:09 AM

:helpsmilie: :helpsmilie: :helpsmilie:

help_me...
Was This Post Helpful? 0
  • +
  • -

#19 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,287
  • 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
Was This Post Helpful? 0
  • +
  • -

#20 Fredex  Icon User is offline

  • D.I.C Head

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

Re: Simple Calculator

Posted 06 March 2013 - 02:37 PM

ok.ok :whatsthat:
Was This Post Helpful? 0
  • +
  • -

#21 Fredex  Icon User is offline

  • D.I.C Head

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

Re: Simple Calculator

Posted 07 March 2013 - 07:49 PM

View PostGunnerInc, 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.
Was This Post Helpful? 0
  • +
  • -

#22 Fredex  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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.:$"
	result db "The answer is$"
	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:
	add bl,num[0]
;	add bl,num[1]
	
	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.
Was This Post Helpful? 0
  • +
  • -

#23 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,287
  • 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.
Was This Post Helpful? 0
  • +
  • -

#24 Fredex  Icon User is offline

  • D.I.C Head

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

Re: Simple Calculator

Posted 10 March 2013 - 11:24 PM

View PostGunnerInc, 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.
Was This Post Helpful? 0
  • +
  • -

#25 Fredex  Icon User is offline

  • D.I.C Head

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

Re: Simple Calculator

Posted 10 March 2013 - 11:31 PM

my problem now is division.
Was This Post Helpful? 0
  • +
  • -

#26 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,287
  • 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?
Was This Post Helpful? 0
  • +
  • -

#27 Fredex  Icon User is offline

  • D.I.C Head

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

Re: Simple Calculator

Posted 11 March 2013 - 09:09 PM

View PostGunnerInc, 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.:$"
	result db "The answer is$"
;	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
	
;	add al,30h		
	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:
;	add bl,num[0]
	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??
Was This Post Helpful? 0
  • +
  • -

#28 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,287
  • 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.
Was This Post Helpful? 0
  • +
  • -

#29 Fredex  Icon User is offline

  • D.I.C Head

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

Re: Simple Calculator

Posted 12 March 2013 - 08:43 PM

thanks master..Ive learn alot.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2