
;HEXIDEC--Main program--G6_14
;  converts hex on keyboard to dec on screen

;EQU STATEMENTS GO HERE

display	equ	2h		;video output
key_in	equ	1h		;keyboard input
doscall	equ	21h		;DOS interrupt number

;**********************************************************
hexidec segment			;defint code segment
;----------------------------------------------------------
main	proc	far		;main part of program

	assume cs:hexidec

;Main part of program links subroutines together

start:				;starting execution address

;set up stack for return
	push	ds		;save old data segment
	sub	ax,ax		;put zero in AX	
	push	ax		;zero on stack
  
;MAIN PART OF PROGRAM GOES HERE

	call	hexibin		;keyboard to binary
	call	crlf		;print cr & lf
;
	call	binidec		;binary to decimal
	call	crlf		;print cr & lf
	jmp 	main		;get next input
;
	ret			;return to DOS

main	endp			;end of main part of program
;----------------------------------------------------------
hexibin	proc	near		;define subprocedure

;Subroutine to convert hex on keybd to binary
;  result is left in BX register

	mov	bx,0		;clear BX for number

;Get digit from keyboard,convert to binary
newchar:
	mov	ah,key_in	;keyboard input
	int	doscall		;call DOS
	sub	al,30h		;ASCII to binary
	jl	exit		;jump if < 0
	cmp	al,10d		;is it > 9d ?
	jl	add_to		;yes,so it's digit
;not digit (0 to 9),may be letter (A to F)
	sub	al,27h		;convert ASCII to binary
	cmp	al,0ah		;is it <0a hex?
	jl	exit		;yes,not letter
	cmp	al,10h		;is it >0f hex?
	jge	exit		;yes,not letter
;is hex digit,add to number in BX
add_to:
	mov	cl,4		;set shift count
	shl	bx,cl		;rotate BX 4 bits
	mov	ah,0		;zero out AH
	add	bx,ax		;add digit to number
	jmp	newchar		;get next digit
exit:
	ret			;return from hexibin

hexibin endp			;end subprocedure
;----------------------------------------------------------
binidec	proc	near
;Subroutine to convert binary number in BX 
;  to decimal on console screen

	mov	cx,10000d	;divide by 10000
	call	dec_div
	mov	cx,1000d	;divide by 1000
	call	dec_div
	mov	cx,100d		;divide by 100
	call	dec_div
	mov	cx,10d		;divide	by 10
	call	dec_div
	mov	cx,1d		;divide by 1
	call	dec_div
	ret			;return from binidec

;----------------------------------------------------
dec_div	proc	near
;Subroutine to divide number in BX by number in CX
;print quotient on screen
;(numberator in DX+AX,denom in CX)

	mov	ax,bx		;number low half
	mov	dx,0		;zero out high half
	div	cx		;divide by CX
	mov	bx,dx		;remainder into BX
	mov	dl,al		;quotient into DL
;print the contents of DL on screen
	add	dl,30h		;convert to ASCII
	mov	ah,display	;display function
	int	doscall		;call DOS
	ret			;return from dec_div

dec_div	endp
;-------------------------------------------------------------
binidec	endp
;-------------------------------------------------------------
crlf	proc	near
;print carriage return and linefeed
	mov	dl,0ah		;linefeed
	mov	ah,display	;display function
	int	doscall		;call DOS
;
	mov	dl,0dh		;carriage return
	mov	ah,display	;display function
	int	doscall		;call DOS
	ret			;return from crlf

crlf	endp
;-------------------------------------------------------
hexidec ends			;end of code segment
;**********************************************************

	end	start		;end assembly
