Home > Archive > A86 Assembler > January 2005 > File I/O
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| amitkr 2005-01-10, 3:56 pm |
| Hi, I am a noobie so any help will be much appreciated...
I am trying to read a file and display the bytes read...
I am able to do so... but the output is in ASCII but I want the result to
be displayed as HEX values....
How to do this... please help
| |
| Jack Klein 2005-01-11, 3:55 am |
| On Mon, 10 Jan 2005 18:33:17 +0000 (UTC), "amitkr"
<spamtrap@crayne.org> wrote in comp.lang.asm.x86:
> Hi, I am a noobie so any help will be much appreciated...
>
> I am trying to read a file and display the bytes read...
>
> I am able to do so... but the output is in ASCII but I want the result to
> be displayed as HEX values....
>
>
> How to do this... please help
; character to hex
; called with the character to be converted in al
; returns high ASCII hex character in DH, low
; ASCII hex character in DL
byte_to_hex:
push ax
call nibble_to_hex
mov dl, al
pop ax
shr al, 1
shr al, 1
shr al, 1
shr al, 1
call nibble_to_hex
mov dh, al
ret
nibble_to_hex:
and al, 0fh
add al, 90h
daa
adc al, 40h
daa
ret
Works on every single x86 processor from the 8088/8086 on up. Fastest
on the 8088. From the 486 on up, a look-up table is faster than the
add/daa/adc/daa sequence.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~.../FAQ-acllc.html
| |
| amitkr 2005-01-11, 3:55 am |
| Hey why you use
shr al, 1
4 times instead of shr al,4
I belive both will give the same result... right
Please clarify
Thanx
| |
|
| "amitkr" <spamtrap@crayne.org> wrote in message
news:4fcda74c6701931189c3e0a36ef1a59e@lo
calhost.talkaboutprogramming.com...
> Hey why you use
>
> shr al, 1
>
> 4 times instead of shr al,4
So that it will run on the 8086 and 8088 which could only shift by 1 place.
> I belive both will give the same result... right
Yes. Using shr al, 4 will be 4 times faster, too.
-Matt
| |
| wolfgang kern 2005-01-11, 3:55 pm |
|
Jack Klein wrote:
[..Ascii2hex]
| ; character to hex
| ; called with the character to be converted in al
| ; returns high ASCII hex character in DH, low
| ; ASCII hex character in DL
|
| byte_to_hex:
| push ax
| call nibble_to_hex
| mov dl, al
| pop ax
| shr al, 1
| shr al, 1
| shr al, 1
| shr al, 1
| call nibble_to_hex
| mov dh, al
| ret
|
| nibble_to_hex:
| and al, 0fh
| add al, 90h
| daa
| adc al, 40h
| daa
| ret
|
| Works on every single x86 processor from the 8088/8086 on up. Fastest
| on the 8088. From the 486 on up, a look-up table is faster than the
| add/daa/adc/daa sequence.
Yes, lookup table solution is the fastest for larger than one byte values
or if already cached ahead, expect many clock-cycles penalty (~35 on K7)
for a 'new' data-fetch. DAA -method is shortest but very slow.
I found this as a good compromise in terms of size and speed:
(26 bytes, 10..12 clock-cycles w/o call/return, ie: as a macro)
[macro: ascii2hex ;al -> ax]
mov ah,al
shr al,4 ; 4 times '1' if <80186
cmp al,0ah ;and al,0fh is redundant after the shift
jc $+2 ;skip next
add al,07h
add al,30h
xchg al,ah ;ah holds high nibble character yet
and al,0fh
cmp al,0ah
jc $+2 ;skip next
add al,07h
add al,30h ;al holds low nibble character
[/macro]
__
wolfgang
| |
| spamtrap@crayne.org 2005-01-11, 3:55 pm |
| On Tue, 11 Jan 2005 06:59:47 +0000 (UTC), "amitkr"
<spamtrap@crayne.org> wrote:
>Hey why you use
>
>shr al, 1
>
>4 times instead of shr al,4
>
>I belive both will give the same result... right
Not on an 8088 or 8086
Later processors, yes.
>From the OP's message:
>Works on every single x86 processor from the 8088/8086 on up.
--
Arargh501 at [drop the 'http://www.' from ->] http://www.arargh.com
BCET Basic Compiler Page: http://www.arargh.com/basic/index.html
To reply by email, remove the garbage from the reply address.
| |
| Frank Kotler 2005-01-12, 3:55 am |
| wolfgang kern wrote:
> DAA -method is shortest but very slow.
; nibble masked off
cmp al, 0Ah
sbb al, 69h
das
shorter? possibly even slower?
Best,
Frank
| |
| wolfgang kern 2005-01-12, 8:55 pm |
|
Yes, correct Frank,
| > DAA -method is shortest but very slow.
| ; nibble masked off
| cmp al, 0Ah
| sbb al, 69h
| das
| shorter? possibly even slower?
your code (5 bytes,10 cycles) is shorter and faster than
the DAA-version, but still much slower than 'my' 3..4
cycles with 8 bytes.
All this anchient multifunctional ACCU-instructions
[aaa aad aam aas daa das] which need
[ 6 6 16 6 8 8 ] vectored clock-cycles on K7,
(not any better on Intel-CPU's)
can be considered as extremely slow and are
just usable if code-size is the main matter.
__
wolfgang
|
|
|
|
|