|
<--- Turn the page
   
(contents page)
   
Turn the page ---> Number ConversionHex to Dec to Binary to Octal |
|
For one of the features for this month I thought I would include some code to a small number conversion utility. All it does is allow the user to enter a 16bit number of any of the four mentioned formats and then it prints a value for each format. That is it. Here's how it works. On the command line, you type a number, say 1234h, and you want the number conversion of decimal. NUMS 1234h Then Nums (the name of this utility) returns: 1234h 4660d 0001001000110100b 11064o All you do is denote which format you are giving NUMS by a simple h, d, b, or o at the end of the number. If NUMS doesn't find an applicable format or the number is larger than 16 bits, then it returns nothing. .model tiny
.186
.external prthex, prtdec, prtbin, prtoct
.code
mov si,0080h ; points to command line
lodsb ;
or al,al ; if no command line, exit
jnz short CmmdOK
ret
CmmdOK: inc si ; skip space
push si ; save position
gettype: lodsb ; get descriptor type
cmp al,13 ; find end of number
jne short gettype ;
mov al,[si-2] ; al now = descriptor type
pop si ; restore starting pos
call upprit ; upper case it
cmp al,'B' ; is it bin
je short IsBin ;
cmp al,'H' ; is it hex
je short IsHex ;
cmp al,'O' ; is it oct
je short IsOct ;
cmp al,'D' ; is it dec
je short IsDec ;
jmp short Done ; if not B,H,O, or D then quit
IsBin: call DoBin
jc short Done
jmp short Prtit
IsHex: call DoHex
jc short Done
jmp short Prtit
IsOct: call DoOct
jc short Done
jmp short Prtit
IsDec: call DoDec
jc short Done
Prtit: call prtthem
Done: ret
DoDec proc near uses bx cx dx si
mov cx,06 ; no more than 5 digits
xor bx,bx ; bx = 0 = starting
xor dx,dx
DecL: lodsb
call upprit
cmp al,'D'
je short DecD
cmp al,'0'
jb short DecDc
cmp al,'9'
ja short DecDc
sub al,'0'
push ax
mov ax,bx
mov bx,10
mul bx
mov bx,ax
pop ax
jc short DecDc ; catches everything over 65540d
xor ah,ah
add bx,ax
loop DecL
jmp short DecDc
DecD: clc
mov ax,bx
ret
DecDc: stc
ret
DoDec endp
DoHex proc near uses bx cx dx si
mov cx,05 ; no more than 5 digits
xor bx,bx ; bx = 0 = starting
HexL: lodsb
call upprit
cmp al,'H'
je short HexD
cmp al,'0'
jb short HexDc
cmp al,'9'
jbe short IsHexn
cmp al,'A'
jb short HexDc
cmp al,'F'
ja short HexDc
sub al,7
IsHexn: sub al,'0'
shl bx,04
or bl,al
loop HexL
jmp short HexDc
HexD: clc
mov ax,bx
ret
HexDc: stc
ret
DoHex endp
DoBin proc near uses bx cx dx si
mov cx,17 ; no more than 16 digits
xor bx,bx ; bx = 0 = starting
BinL: lodsb
call upprit
cmp al,'B'
je short BinD
cmp al,'0'
jb short BinDc
cmp al,'1'
ja short BinDc
sub al,'0'
shl bx,1
or bl,al
loop BinL
jmp short BinDc
BinD: clc
mov ax,bx
ret
BinDc: stc
ret
DoBin endp
DoOct proc near uses bx cx dx si
mov cx,07 ; no more than 06 digits
xor bx,bx ; bx = 0 = starting
OctL: lodsb
call upprit
cmp al,'O'
je short OctD
cmp al,'0'
jb short OctDc
cmp al,'7'
ja short OctDc
sub al,'0'
shl bx,3
jc short OctDc
or bl,al
loop OctL
jmp short OctDc
OctD: clc
mov ax,bx
ret
OctDc: stc
ret
DoOct endp
upprit proc near
cmp al,'a'
jb short uppritd
cmp al,'z'
ja short uppritd
sub al,32
uppritd: ret
upprit endp
prtthem proc near
push ax ; save for hex
push ax ; and for dec
push ax ; and for bin
push ax ; and for oct
call prthex
mov ah,02h
mov dl,'h'
int 21h
mov dl,' '
int 21h
int 21h
call prtdec
mov dl,'d'
int 21h
mov dl,' '
int 21h
int 21h
call prtbin
mov dl,'b'
int 21h
mov dl,' '
int 21h
int 21h
call prtoct
mov dl,'o'
int 21h
mov dl,13
int 21h
mov dl,10
int 21h
ret
prtthem endp
.end
|
<--- Turn the page
   
(contents page)
   
Turn the page ---> Page 4