; PROGRAM: DEC TO BINARY
; AND BINARY TO DECIMAL
read macro num
mov ah, 01h
int 21h
mov bh, al
int 21h
mov bl, al
sub bh, 30h
mov al, 0ah
mul bh
sub bl, 30h
add al, bl
mov num, al
endm
print macro s
mov ah, 09h
mov dx, offset s
int 21h
endm
data segment
msg_dec db 'Enter decimal: $'
msg_bin db 'Enter binary: $'
binis db 'Binary is: $'
decis db 'Decimal is: $'
nl db 0dh,0ah,'$'
menu db 'Select Operation:',0dh,0ah,
'1.Decimal to Binary',0dh,0ah,
'2.Binary to Decimal',0dh,0ah,
'3.Quit $'
choice db 'Choice: $'
deci db 00h
bin db 15 dup('$')
temp db 15 dup('$')
invOp db 'Invalid Option!$'
notbin db 'Not a binary number!$'
count db 0
data ends
code segment
assume cs:code, ds:data
start:
mov ax, data
mov ds, ax
print menu
ENTERCHOICE:
print nl
print choice
mov ah, 01h
int 21h
sub al, 30h
cmp al, 01h
je DECTOBIN
cmp al, 02h
je BINTODEC
cmp al, 03h
je FINISH
print nl
print invOp
jmp ENTERCHOICE
DECTOBIN:
print nl
print msg_dec
read deci
mov al, deci
mov si, offset temp ;store ascii of binary
mov dh, 00h
CONVBINARY:
mov ah, 00h
mov cl, 02h
div cl ;divide by 2
mov bl, ah
add bl, 30h
mov [si], BL
inc si
inc dh ;count for reversing
cmp al, 00h
je EXITCONV
jmp CONVBINARY
EXITCONV:
mov di, offset bin
dec si
REVERSE:
cmp dh, 00
jz ENDREVERSE
mov bh, [si]
mov [di], bh
inc di
dec si
dec dh
jmp REVERSE
ENDREVERSE:
mov [di], '$'
print nl
print binis
print bin
jmp ENTERCHOICE
BINTODEC:
print nl
print msg_bin
mov si, offset bin
mov ah, 01h
mov ch, 00h
READBIN:
int 21h
cmp al, 0dh
jz BINREAD
mov [si], al
inc si
inc ch ;ch number of bits
jmp READBIN
BINREAD:
dec si
mov cl, 00h ;power of 2
mov dx, 00h
FINDDEC:
cmp ch, 00h
je EXITDECIMAL
mov bl, 02h
mov count, cl
mov al, 01h
mov ah, 00h
FINDPOW2:
cmp cl, 00h
je POWERFOUND
mul bl
dec cl
jmp FINDPOW2
POWERFOUND:
mov cl, count
mov bl, [si]
sub bl, 30h
cmp bl, 02h
jge NOTBINARY
mul bl
add dx, ax
dec si
dec ch
inc cl
jmp FINDDEC
EXITDECIMAL:
mov ax, dx
mov si, offset deci
call hex2ascii
print nl
print decis
print deci
jmp ENTERCHOICE
NOTBINARY:
print notbin
jmp ENTERCHOICE
FINISH:
mov ah, 4ch
int 21h
hex2ascii proc near
mov cx, 0000h
REPDIV:
mov dx, 0000h
mov bx, 000ah
div bx
add dl, 30h
push dx
inc cx
cmp ax, 000ah
JGE REPDIV
add al, 30h
mov [si], al
inc si
SAVEASCI:
pop dx
mov [si], dl
inc si
loop SAVEASCI
STOP1:
mov [si], '$'
ret
hex2ascii endp
code ends
end start
**************************
OUTPUT
Z:\S5IT\masm>DEC_BIN
Select Operation:
1.Decimal to Binary
2.Binary to Decimal
3.Quit
Choice: 1
Enter decimal: 10
Binary is: 1010
Choice: 2
Enter binary: 1010
Decimal is: 10
Choice: 3
**************************
0 comments:
Post a Comment