Multiply and Divide Example



A tedious way to multiply and divide positive integers:

 
	INCLUDE Irvine16.inc 
	
	.data 
	
Prompt  BYTE	"Enter a positive integer: ", 0 

	.code 
	
Main	PROC

	MOV	AX, @data
	MOV	DS, AX
Lp:	MOV	DX, OFFSET Prompt
	CALL	WriteString
	CALL	ReadInt
	CALL	CrLf
	MOV	SI, AX
	CALL	WriteString
	CALL	ReadInt
	CALL	CrLf
	MOV	DI, AX 
	CALL	MyMul	
	CALL	WriteDec
	CALL	CrLf
	MOV	AX, DX 
	CALL	WriteDec
	CALL	CrLf
	CALL	CrLf
	
	MOV	DX, OFFSET Prompt
	CALL	WriteString
	CALL	ReadInt 
	CALL	CrLf
	MOV	SI, AX
	CALL	WriteString
	CALL	ReadInt 
	CALL	CrLf
	PUSH    AX
	CALL	WriteString
	CALL	ReadInt 
	CALL	CrLf
	MOV	DX, AX
	POP	AX
	CALL	MyDiv	
	MOV	AX, DI 
	CALL	WriteDec
	CALL	CrLf
	MOV	AX, SI
	CALL	WriteDec
	CALL	CrLf
	CALL	CrLf
	
	JMP	Lp
	
	EXIT
	
Main	ENDP 

MyMul	PROC
; receive:
; SI, DI - operands
; return:
; DX, AX - product

        push DI
        push CX
        mov AX, 0
        mov DX, 0
        mov CX, 16
   L1:  shl AX, 1
        rcl DX, 1 
        shl DI, 1
        jnc L2
        add AX, SI
        jnc L2
        inc DX
   L2:  loop L1
        pop CX
        pop DI
        ret
MyMul   ENDP

MyDiv	PROC
; receive:
; DX, AX - dividend 
; SI - divisor (need SI > DX) 
; return:
; DI - quotient
; SI - remainder

	push DX
	push CX
	push AX
	mov DI, 0 
	mov CX, 16
   L1:  shl AX, 1
   	rcl DX, 1
	jc L2 
        cmp DX, SI 
   	jb L3
   L2:	sub DX, SI 
   	stc 
   	jmp L4 
   L3:  clc 
   L4:  rcl DI, 1
   	loop L1 
   	mov SI, DX
        pop AX
        pop CX 
        pop DX
        ret
MyDiv   ENDP

	END	Main