flat assembler
Message board for the users of flat assembler.

Index > Main > Byte to hex, once again.

Goto page 1, 2, 3  Next
Author
Thread Post new topic Reply to topic
Fanael



Joined: 03 Jul 2009
Posts: 168
Fanael
Yet another little challenge for you all: write a code (smaller = better) that converts a byte in AL to hexadecimal.

Rules:
* The code has to compile fine with use32 and use64,
* No branches,
* The same goes for conditional moves,
* No memory accesses, except writing the result somewhere.
Post 15 Aug 2010, 19:13
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
to hexadecimal what? two ascii chars in AX? in memory? zero terminated? Or whatever goes?
Post 15 Aug 2010, 19:42
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Fanael



Joined: 03 Jul 2009
Posts: 168
Fanael
vid wrote:
Or whatever goes?
Yeah, as long as characters are encoded as ASCII.
Post 15 Aug 2010, 19:52
View user's profile Send private message Reply with quote
chaoscode



Joined: 21 Nov 2006
Posts: 64
chaoscode
Code:
mov ah,al
shr ah,4
and ax,0x0F0F
rol ax,8
mov ebx,msg
xlatb
rol ax,8
xlatb

msg db "0123456789ABCDEF"
    


edit: added code tags


Last edited by chaoscode on 15 Aug 2010, 22:28; edited 1 time in total
Post 15 Aug 2010, 20:25
View user's profile Send private message ICQ Number Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
chaoscode, you're violating the last rule.
Post 15 Aug 2010, 20:45
View user's profile Send private message Reply with quote
chaoscode



Joined: 21 Nov 2006
Posts: 64
chaoscode
Code:
mov     ah,al
shr     ah,4
and     ax,0x0F0F
add     ax,0x3030
cmp     al,0x39
jna     @f
add     al,7
@@:
cmp     ah,0x39
jna     @f
add     ah,7
@@.
    

EDIT. added quote tags


Last edited by chaoscode on 15 Aug 2010, 22:29; edited 1 time in total
Post 15 Aug 2010, 20:54
View user's profile Send private message ICQ Number Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Now you violated the 2nd rule Laughing
Post 15 Aug 2010, 21:06
View user's profile Send private message Reply with quote
chaoscode



Joined: 21 Nov 2006
Posts: 64
chaoscode
hmpf... it's quite difficult^^
Post 15 Aug 2010, 22:02
View user's profile Send private message ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
I presume external functions are not allowed either Wink

chaoscode: please use [code] tags around your code.
Post 15 Aug 2010, 22:14
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Fanael



Joined: 03 Jul 2009
Posts: 168
Fanael
vid wrote:
I presume external functions are not allowed either Wink

If you can call them without branching and touching memory, then they're of course allowed Wink

chaoscode wrote:
hmpf... it's quite difficult^^
Sure, it is difficult, but it's possible nevertheless Smile
Post 15 Aug 2010, 23:49
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
Based on Revolution's. Violates #1.

AL -> AX

Code:
        mov     ah,al
        shr     al,4
        cmp     al,10
        sbb     al,069h
        das
        xchg    ah,al
        and     al,$0f
        cmp     al,10
        sbb     al,069h
        das    

_________________
This is a block of text that can be added to posts you make.
Post 15 Aug 2010, 23:55
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
chaoscode



Joined: 21 Nov 2006
Posts: 64
chaoscode
Code:
use64
format elf64 executable
entry start

start:
mov     eax,0
@@:
cmp eax,0
mov ax,0xE5
call func
mov ax,0x9A
call func
ret

func:
mov    ah,al
shr    ah,4
and     ax,0x0F0F
mov        dx,ax
xor    bx,bx
add    dl,0xF6
rcl  bl,1
ror     bl,1
sar     bl,7
and     bl,0x07
add  al,bl

add        dh,0xF6
rcl  bh,1
ror     bh,1
sar     bh,7
and     bh,0x07
add  ah,bh
add    ax,0x3030
ret
    
Post 16 Aug 2010, 00:03
View user's profile Send private message ICQ Number Reply with quote
Alphonso



Joined: 16 Jan 2007
Posts: 294
Alphonso
Code:
         xor    ah,ah                  ; can we assume ah=0 ?
         mov    dx,ax                  ; could push but the stack is technically a memory op, isn't it?
         mov    bx,3030h               ; ascii offset
         mov    cx,110ah               ; ch=17, cl=10
         shr    al,4                   ; upper hex
         div    cl                     ; result = 0 for 0 to 9, 1 for A to F
         add    bh,ah                  ; add remainder to result BX, 0 to 9 = 0 to 9, A to F = 0 to 5
         mul    ch                     ; for 0 to 9 = 0, for A to F add +17 ascii offset
         add    bh,al                  ; add to result
         mov    ax,dx                  ; this could have been a pop if memory op allowed
         and    al,0fh                 ; lower hex
         div    cl                     ; as above
         add    bl,ah                  ;
         mul    ch                     ;
         add    bl,al                  ; BX = hex ascii of AL     


Not very small, 33 Bytes for 16 and 37 Bytes for 32/64.
Post 16 Aug 2010, 05:31
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
OK, big people is also getting in so I think it is OK to publish a spoiler Razz
Code:
ror ax, 4
and al, $0F
shr ah, 4
mov dh, 9

irps nibble, al ah
{
        cmp dh, nibble
        sbb dl, dl
        and dl, 'A' - '9' - 1
        add nibble, dl
}

add ax, '00'    
(But probably could be further optimized)

This one is 33 bytes in both 32- and 64-bit.

AH holds least significant nibble intentionally.
Post 16 Aug 2010, 05:58
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler
Code:
xor al,al
mov byte[buf], 0
mov byte[buf+1],0

buf rb 2
    

How's that? Laughing
Post 16 Aug 2010, 06:02
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Tyler wrote:
Code:
xor al,al
mov byte[buf], 0
mov byte[buf+1],0

buf rb 2
    

How's that? Laughing

That's wrong. It ought to be mov byte [...], '0'. But otherwise - good solution!
Post 16 Aug 2010, 09:04
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Fanael



Joined: 03 Jul 2009
Posts: 168
Fanael
Hehe, it could be optimized further:
Code:
xor al,al 
mov word [buf], '00' 

buf rb 2    
But it fails (i.e. returns the wrong result) if AL is not equal to 0 Razz

chaoscode: accepted, 49 bytes.
Alphonso: accepted.
LocoDelAssembly: accepted.
Post 16 Aug 2010, 09:36
View user's profile Send private message Reply with quote
Alphonso



Joined: 16 Jan 2007
Posts: 294
Alphonso
Well i suppose I could get rid of that horrible div.
Code:
         mov    ah,al
         shr    ah,4            ; use 'shr al,4' and
         and    al,0fh          ; 'and ah,0fh' to output Loco nibbles :p
         mov    ebx,eax
         xor    ecx,ecx
         mov    cl,7
         mul    ecx
         and    eax,4040h
         shr    eax,6
         mul    ecx
         add    ax,3030h
         add    ebx,eax         ; BX = hex ascii of AL or use 'add eax,ebx' for AX = hex ascii of AL.      
31 Bytes for 32/64.
Post 16 Aug 2010, 12:51
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
OK, if storing the value reversed is OK (Intel CPUs are little endian), then my entry could be this:
Code:
mov ah, al
and al, $0F
shr ah, 4
mov dh, 9

irps nibble, al ah
{
        cmp dh, nibble
        sbb dl, dl
        and dl, 'A' - '9' - 1
        add nibble, dl
}

add ax, '00'    
Same size as Alphonso's code above.
Post 16 Aug 2010, 15:16
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1288
Location: Paradise Falls
Picnic
28 bytes for 32/64. Routine must be called.
Code:
Byte2Hex:
            mov ah, al
            shr al, 4
            mov dh, 9
            call @F
            xchg al, ah
            and al, 0Fh
    @@:     cmp dh, al
            sbb dl, dl
            and dl, 7
            add al, dl
            add al, '0'
            ret
    
Post 16 Aug 2010, 20:46
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2, 3  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.