From: breese@imada.ou.dk (Bjoern Reese)
Newsgroups: comp.sys.amiga.programmer
Subject: Re: Binary->Ascii Program
Date: 27 Sep 92 14:34:59 GMT
Organization: Dept. of Math. & Computer Science, Odense University, Denmark

ins141f@aurora.cc.monash.edu.au (Adam Bradley) writes:

>	You know the htype function in CSM or zshell ?  (Hex type)
>It prints out the hex values of the listing of the program.  Neat.
[...]
>	I want to use the value that I read in (two hex values, 0a or whatever)
>to be the basis for an offset in a look up table.  Relativly simple procedure.
>below is the outline of what I have done.
>
>field - stores the file I have read in.
>values - keeps the byte values of that which I want to print out !
>curraddr - keeps (somewhat messily !) the value that I want to print out
>           (Im sure that there is a way to do this using a base register etc
>   	    but I dont know how to do it :)

Try:
	moveq	#0,d5
	lea	values(pc),a4	;better, shorter and faster than `move.l
				;#values,a4' and it does exactly the same.
	move.b	(a3)+,d5
	lea	(a4,d5.l),a4	;a4 = a4 + d5


>a4 - will eventually keep the address of #values+(valueof)field 
>
>
>lookatit:	move.l	#field,a3
>loop:		add.b	#1,count
>		move.l  #values,a4
>		move.b	(a3)+,curraddr		; 00...FF in curraddr
>		add.b	curraddr,d5		; add Base (a4) to offset 
>		move.l 	d5,a2			; (curraddr) 
>		add.l   a2,a4
>						; (Yucky, yes I know.)

If you're going for speed use registers only (not memory) for variables.

>print:		move.l	clihandle,d1
>		move.l	a4,d2		
>		move.l	#2,d3
>		move.l	dosbase,a6
>		jsr	write(a6)

Uups, there goes the speed argument. Write() in dos.library isn't exactly fast.

>
>
>                      .......		    .......
>                      ....... Stuff removed .......
>                      .......    	    .......
>
>
>field: 	blk.b  (enough space for the program),0
>
>values:	dc.b '00','01','02','03','04','05','06','07','08','09','0a','0b'
>	dc.b 'f1','f2','f3','f4','f5','f6','f7','f8','f9','fa','fb','fc'
>	.......
>	.......
>	.......
>
>	dc.b 'fd','fe','ff',0
>

Large heap of data. Look below if you want to avoid it.

>
>curraddr:	dc.l	0
>filehd: 	dc.l	0
>
>		; Other usual file handling and dos stuff here...
>end

You can do a binary to ASCII convertion this way:

	lea	values(pc),a4

	move.b	(a4)+,d5
	move.b	d5,d6
	andi.b	#$0f,d5		;low nibble
	cmpi.b	#9,d5
	bls.s	.001
	addq.b	#7,d5		;Adjust for 'A'-'F'
.001	addi.b	#$30,d5		;Convert to ascii
				;d5 now contains the ASCII value for the low
				;nibble. Store it somewhere.
	lsr.b	#4,d6
	andi.b	#$0f,d6		;high nibble
	cmpi.b	#9,d6
	bls.s	.002
	addq.b	#7,d6		;Adjust for 'A'-'F'
.002	addi.b	#$30,d6		;Convert to ascii


The following subroutine converts a longword to an ASCII string using the same
method as above:

;------------------------------------------------------------------
;---	Convert D0.L into an ASCII string (pointed to by A0.L)
;------------------------------------------------------------------

ConvertHex:
	moveq	#8-1,d2		;Eight numbers in a longword
.loop	rol.l	#4,d0		;Shift nibble
	move.l	d0,d1
	andi.b	#$0f,d1		;Mask nibble
	cmpi.b	#9,d1
	bls.s	.001
	addq.b	#7,d1		;Adjust for 'A'-'F'
.001	addi.b	#$30,d1		;Convert to ascii
	move.b	d1,(a0)+	;Store ascii
	dbra	d2,.loop
	rts	


>	Is there any need to do this in assembly, could I do it in C and
>do it easier, does anyone know how CSH does the htype function.

The only reason to do assembly language is speed (and of course size, but that
argument fades as memory grows and grows), and as you use Write() the speed
superiority goes down the drain. I think you could have made it just as easy
in C, or even easier if you're used to C.

--

Bjoern Reese                   |     Email: breese@imada.ou.dk
Odense University, Denmark     |     Voice: +45 65 932 182 (private)

     "Keinem Gedanken Glauben schenken, der nicht im Freien geboren ist."
                       - Friedrich Nietzsche, Ecce Homo
Newsgroups: comp.sys.amiga.programmer