home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of Shareware - Software Farm 2
/
wosw_2.zip
/
wosw_2
/
CPROG
/
CEXPRESS.ZIP
/
BITS.ASM
/
HEX2INT.ASM
< prev
next >
Wrap
Assembly Source File
|
1989-05-03
|
2KB
|
72 lines
;unsigned char _hex_to_int(hex_string);
; char *hex_string;
EXTRN _memory_model:byte
EXTRN _error_code:byte
_TEXT SEGMENT BYTE PUBLIC 'CODE'
ASSUME CS:_TEXT
PUBLIC _hex_to_int
_hex_to_int proc near
push bp ;
mov bp,sp ;set stack frame
cmp _memory_model,0 ;near or far?
jle begin ;jump if near
inc bp ;else add 2 to BP
inc bp ;
begin: cmp _memory_model,2 ;data near or far?
jb L0 ;jump if near
les di,[bp+4] ;
jmp short L1 ;
L0: mov di,[bp+4] ;
mov ax,ds ;ES = DS
mov es,ax ;
L1: sub ax,ax ;return 0 if error
mov dl,0 ;counts chars
mov _error_code,1 ;errorcode 1 = null string
cmp byte ptr es:[di],0 ;null?
je L6 ;quit if so
LY: cmp byte ptr es:[di],0 ;move ptr to end of string
je L2 ;
inc di ;
jmp short LY ;
L2: sub bx,bx ;
dec di ;move ptr back one
mov bl,es:[di] ;get a char
cmp bl,0 ;end of string?
je LX ;
cmp bl,0 ;end of string?
je L6 ;quit if so
inc _error_code ;errorcode 2 = char out of range
cmp bl,48 ;test range
jb L6 ;
cmp bl,57 ;
jna L4 ;
cmp bl,97 ;
jnb L3 ;
add bl,32 ;make lower case
L3: cmp bl,97 ;'a'
jb L6 ;
cmp bl,102 ;'f'
ja L6 ;
sub bl,87 ;make numeric
jmp short L5 ;
L4: sub bl,48 ;make numeric
L5: mov cl,dl ;
shl cl,1 ;multiply by four
shl cl,1 ;
shl bx,cl ;move mask to position
or ax,bx ;copy over the nibble
inc dl ;inc counter
cmp dl,4 ;
jne L2 ;loop
LX: mov _error_code,0 ;no error
L6: pop bp ;
cmp _memory_model,0 ;quit
jle quit ;
db 0CBh ;RET far
quit: ret ;RET near
_hex_to_int endp
_TEXT ENDS
END