Back to home page

DOS ain't dead

Forum index page

Log in | Register

Back to the forum
Board view  Mix view

18 Bytes !!! Anyone can beat this? HEX16 for 8086 (Developers)

posted by DOS386(R), 13.04.2009, 10:20

Task: convert the value in DX (or some other register) into a 4-Bytes string representing the number in HEX using 8086 compatible instructions (no TSR this time, sorry :crying:)

Solution by King Udo (31 Bytes):

output_hex:

; On Entry:
;      dx = 2 byte hex value
; On Exit:
;      None

        mov      cx,4
output_hex10:
        mov      al,dh
        push     cx
        mov      cl,4
        shr      al,cl
        pop      cx
        and      al,0fh
        cmp      al,09h         ; greater 0-9?
        jg       output_hex20
        add      al,30h
        jmp      output_hex30
output_hex20:
        add      al,37h
output_hex30:
        ; call onecharal
        push     cx
        mov      cl,4
        shl      dx,cl
        pop      cx
        loop     output_hex10


My solution (18 Bytes):

use16

; HEX16 output conversion, 8086 compatible

; Input in DX or AX | Trashes AX CX DX

;  9 instructions 18 Bytes vary bare input in DX
; 10 instructions 19 Bytes very bare input in AX
; 11 instructions 22 Bytes including call, input in AX
; 18 instructions 29 Bytes including call, full register preservation and RET

        push   ax
        push   cx
        push   dx

        xchg   dx, ax       ; DX <- AX | Later peek the 4-bit groups from DX
        mov    cx, 1028     ; "MOVNTQ CL, 4" + "MOVNTQ CH, 4"
@@:     mov    al, dh       ; AL <- DH | Move all 8 bits, only upper 4 useful
        shl    dx, cl       ; 8086 compatible
        shr    al, cl       ; 8086 compatible
        cmp    al, 10       ; Now must be and is AL<=15 | Decimal "10" !!!
        sbb    al, $69      ; Securely Bugged Backup
        das                 ; Digital Attack System | ASCII result in AL
        call   onecharal    ; Input in AL | Must preserve CX and DX, not AX
        dec    ch           ; & No LOO'p on CH :-(
        jnz    short @b     ; &

        pope   dx
        pope   cx
        pope   ax
        ret
        ;----

---
This is a LOGITECH mouse driver, but some software expect here
the following string:*** This is Copyright 1983 Microsoft ***

 

Complete thread:

Back to the forum
Board view  Mix view
15115 Postings in 1359 Threads, 249 registered users, 25 users online (0 registered, 25 guests)
DOS ain't dead | Admin contact
RSS Feed
powered by my little forum