unit hexfiles;


interface

{uses crt; }

type
	bfile =	file of byte;
	cfile =	file of char;
	hexdu = set of 'A'..'F';
	hexdl = set of 'a'..'f';
	hexd  = set of '0'..'9' ;
	hexv  = set of  0..15;


function hexdigit(byte1:byte):char; {Prints single hex digit }

function hexb(byte1:byte):string;   {Prints hex byte }

function hexw(work:word): string;   {Prints hex word }

function hexl(work:longint): string; {Prints hex equivalent of longint }

function strb(work:string) : byte;

function strw(work:string) : word;

function strl(work:string) : longint;

function get_byte(var input_file:bfile) : byte; {Gets byte from file }

function get_shortint(var input_file:bfile) : shortint; {gets short int }

function get_integer(var input_file:bfile) : integer;

function get_word(var input_file:bfile) : word;

function get_longint(var input_file:bfile): longint;

function mult (shift_count : word) : word;
{
Procedure reed;
 }

implementation



{ Print HEX comands here, Hexw for words, Hexb for bytes, Hexl for longints.}

function hexdigit(byte1:byte):char;
begin
	 if byte1 < 17 then
		  if byte1 < 10
			  then hexdigit := chr(ord('0') + byte1)
			  else hexdigit := chr(ord('A') + (byte1 - 10))
	 else hexdigit := chr(ord('Z')) {Returns a Z if there is an error}
end;


function hexb(byte1:byte):string;
begin;
hexb := hexdigit(byte1 div 16)+hexdigit(byte1 mod 16);
end;


function hexw(work:word): string;
begin;
hexw := hexb(hi(work)) + hexb(lo(work));
end;


function hexl(work:longint) : string;

var
	lo_word,hi_word : word;
	templongint		: longint;
	temppointer		: pointer;
	temp_ofs,temp_seg	: word;
begin;
templongint := work;
temppointer := addr(templongint);
temp_seg := seg(templongint);
temp_ofs := ofs(templongint);
	asm;
	push ax;  { use addr and seg functions to }
	push bx;  { get the segment and address of }
	push ds;  { the templongint address. }
	mov bx,temp_seg;
	mov ds,bx;
	mov bx,temp_ofs;
	mov	ax,[bx];
	mov lo_word,ax;
	mov ax,[bx+2];
	mov hi_word,ax;
	pop  ds;
	pop  bx;
	pop  ax;
	end;
hexl := hexb(hi(hi_word)) + hexb(lo(hi_word)) +
		hexb(hi(lo_word)) + hexb(lo(lo_word));
end;


function get_byte(var input_file:bfile) : byte;
var
	tempbyte			: byte;
begin;
read(input_file,tempbyte); {this is where all the basic I/O takes place }
get_byte := tempbyte;
end;


function get_shortint(var input_file:bfile) : shortint;
var
	tempbyte			: byte;
	tempshortint		: shortint;
begin;
read(input_file,tempbyte);
	asm
	push ax;
	mov  al,tempbyte;     {this routine uses assembly to move the binary}
	mov  tempshortint,al; {value from tempbyte to tempshortint providing}
	pop  ax               {conversion without messing with the signs directly}
	end;
get_shortint := tempshortint;
end;


function get_word(var input_file:bfile) : word;
var
	tempbyte1,tempbyte2	: byte;
	tempword			: word;
begin;
tempbyte1 := get_byte(input_file); {simple multiplication here }
tempbyte2 := get_byte(input_file);
tempword := (tempbyte2*256) + tempbyte1;
get_word := tempword;
end;


function get_integer(var input_file:bfile) : integer;
var
	tempbyte			: byte;
	tempshortint		: shortint;
	tempinteger			: word;
begin;
tempbyte	:= get_byte(input_file);  {get low unsigned byte first }
tempshortint:= get_shortint(input_file); {Now get signed byte }
tempinteger := (tempshortint*256) + tempbyte; {Multiply signed byte then }
get_integer := tempinteger;                   {add the unsigned one }
end;


function get_longint(var input_file:bfile): longint;
var
	tempword			: word;
	tempinteger			: integer;
	templongint			: longint;
begin;
tempword	:= get_word(input_file);  {unsigned word that comes first }
tempinteger := get_integer(input_file);{then the signed word (integer) }
templongint := (tempinteger*65536) + tempword;{Now multiply the signed }
get_longint := templongint; {By sixteen bits(2^16=65,535) and add unsigned. }
end;


function mult(shift_count : word) : word;
begin
	mult := 1;
	case shift_count of
		0	: mult := 512;
		1	: mult := 2;
		2	: mult := 4;
		3	: mult := 8;
		4	: mult := 16;
		5	: mult := 32;
		6	: mult := 64;
		7	: mult := 128;
		8	: mult := 256;
		9	: mult := 512;
		10	: mult := 1024;
		else writeln('Shift parse error in MULT ',shift_count);
		end;
end;

function strb(work:string) : byte;
var
	byte1,byte2		: byte;
	char1,char2		: char;
	tmp1,tmp2		: byte;
begin;
char1 := upcase(work[1]);
char2 := upcase(work[2]);


	asm;
	push ax;
	mov al,char1;
	mov byte1,al;
	mov al,char2;
	mov byte2,al;
	pop ax
	end;


if byte1 < 71 then
	if byte1 < $040	then tmp1 := byte1-ord('0')
		else tmp1 := byte1-ord('A')+10
	else	writeln('Parse error in strb byte1 ');
if byte2 < 71 then
	if byte2 < $040	then tmp2 := byte2-ord('0')
		else tmp2 := byte2-ord('A')+10
	 else	writeln('Parse error in strb byte2 ');
strb := (tmp1*16) + tmp2;
end;




function strw(work:string) : word;
var
	str1,str2				: string;
	str,strb1,strb2			: word;
begin;
str1 := work[1] + work[2];
str2 := work[3] + work[4];
strb1 := strb(str1);
strb2 := strb(str2);
str := ((strb1 * 256) + strb2);
strw := str;
end;


function strl(work:string) : longint;
begin
write(work,'  ', length(work),'  ');
strl := 4;
end;

{

Procedure reed;
var bite : char;
begin
bite := readkey;
while (bite <> chr($0d)) and (bite <> chr($1b)) do bite := readkey;
end;
  }




END.
