flat assembler
Message board for the users of flat assembler.

Index > DOS > how to do binary to decimal?

Goto page Previous  1, 2
Author
Thread Post new topic Reply to topic
windwakr



Joined: 30 Jun 2004
Posts: 827
Location: Michigan, USA
windwakr
Neat, but confusing.

_________________
----> * <---- My star, won HERE
Post 27 Jul 2008, 02:46
View user's profile Send private message Reply with quote
Shahada



Joined: 25 Jul 2008
Posts: 77
Shahada
Thanks, but what I need is a bin to whatever base routine, like the one from Microsoft C library, not only bin2dec. This FAMOS guy neville said he has up to base 36 routine and I want to see it.
Post 27 Jul 2008, 12:47
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
Location: Michigan, USA
windwakr
Madis731 has a base 2 to 36 or anything in between program.
In this thread.

_________________
----> * <---- My star, won HERE
Post 27 Jul 2008, 15:48
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2141
Location: Estonia
Madis731
Hmm, interesting how compilers work Smile oh the joy of killing partial register stalls on the gcc one Razz

Intel C++ 10.1.022:
Code:
?chartobcd@@YAEE@Z      PROC NEAR
; parameter 1: ecx
$B1$1:                          ; Preds $B1$0
        mov       eax, 1717986919                               ;3.14
        movzx     ecx, cl                                       ;3.10
        imul      ecx                                           ;3.14
        mov       r8d, ecx                                      ;3.14
        sar       r8d, 31                                       ;3.14
        sar       edx, 2                                        ;3.14
        sub       edx, r8d                                      ;3.14
        lea       r9d, DWORD PTR [rdx+rdx*4]                    ;3.14
        add       r9d, r9d                                      ;3.14
        sub       ecx, r9d                                      ;3.14
        shl       edx, 4                                        ;3.21
        or        ecx, edx                                      ;3.31
        mov       eax, ecx                                      ;3.31
        ret                                                     ;3.31
        ALIGN     2
                                ; LOE
; mark_end;
?chartobcd@@YAEE@Z ENDP
    


Some comments:
-the magic number 1717986919 in hex is 66666667
-only one LEA and IMUL
-most of the instructions are 1 uop

Statistics too:
-It takes 8 core clock cycles at best to complete
-The instructions need 15 uops
Post 27 Jul 2008, 16:27
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Shahada



Joined: 25 Jul 2008
Posts: 77
Shahada
windwakr wrote:
Madis731 has a base 2 to 36 or anything in between program.

Thanks windwakr & Madis731, this is what I need.
Post 27 Jul 2008, 17:52
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
Location: Michigan, USA
windwakr
I just found some small routines for binary to bcd

#1: does binary to unpacked bcd up to 99
put number to convert in al
uses all of ax
Code:
aam
    

#2: does binary to packed bcd up to 99
number in al
uses all of ax
Code:
aam
aad 16
    

_________________
----> * <---- My star, won HERE
Post 28 Jul 2008, 21:07
View user's profile Send private message Reply with quote
neville



Joined: 13 Jul 2008
Posts: 507
Location: New Zealand
neville
Shahada wrote:
Thanks, but what I need is a bin to whatever base routine, like the one from Microsoft C library, not only bin2dec. This FAMOS guy neville said he has up to base 36 routine and I want to see it.


Here's the code. After windwankr's "rejection" Wink the other day I forgot about this thread and hadn't realised it was still active. Some changes for DOS (not tested!) BX probably needs to be 0007 on entry. Although NUMBASE has to be 32-bit of course you would normally only change the lowest byte of it.

Code:
;---------------------------------------------------------------------------
; DISPLAY 32-BIT VALUE IN ANY BASE 2-36+ 
;---------------------------------------------------------------------------
;enter with dx=row/col of LSD (RHS), bx=page/colour, 32-bit dword in EAX,
;required number base in dword CS:NUMBASE. Saves all regs!
;leading zeroes automatically suppressed! NB: prints right to left!
D32BITV:        PUSHAD
D32NEXT:        PUSH    EBX
                PUSH    EDX
                XOR     EDX,EDX         ;initialise remainder
                MOV     EBX,[CS:NUMBASE];required base (for divisor)
                DIV     EBX             ;DX=decimal next sig digit 0-9 binary
                MOV     ECX,EAX         ;save remaining value
                MOV     EAX,EDX         ;get decimal digit
                CALL    NYBL2HEX        ;convert to ascii
                POP     EDX             ;get cursor position
                POP     EBX             ;get colour
                CALL    DCHARP          ;display digit
                DEC     DL              ;shift cursor to left NB: LEFT!
                MOV     EAX,ECX         ;restore decimal value
                OR      EAX,EAX         ;=0?
                JZ      D32DONE         ; -y, done
                JMP     D32NEXT         ; -n, do next digit
D32DONE:        POPAD
                RET
;exit with EAX=0, EDX=MSD cursor position
;---------------------------------------------------------------------------
; DISPLAY SINGLE CHAR WITH POSITION OPTION (adapted for DOS from FAMOS)
;---------------------------------------------------------------------------
;enter DCHARP with reqd position (row/col) in dh/dl, char in al
DCHARP:
         PUSHA
               CALL    SETCUR
              MOV     AH,0EH
                INT     10H
               POPA
                RET
;---------------------------------------------------------------------------
; SET CURSOR POSITION (adapted for DOS from FAMOS)
;---------------------------------------------------------------------------
;enter SETCUR with reqd position (row/col) in dh/dl, preserves al
SETCUR:
                MOV     AH,2
                INT     10H
                RET
;---------------------------------------------------------------------------
; CONVERT NYBLLE TO EQUIVALENT HEX DIGIT
;---------------------------------------------------------------------------
;enter with valid nybble in al (bits 4-7 = 0)
NYBL2HEX:       ADD     AL,30H
                CMP     AL,3AH
                JB      NYBLDONE
                ADD     AL,07
NYBLDONE:       RET
;exit with ascii code of 1 hex digit in al, ah=0
;---------------------------------------------------------------------------
; DATA (accessed via CS:)
;---------------------------------------------------------------------------
NUMBASE: DD 00000010H                  ;base 16 = default    

_________________
FAMOS - the first memory operating system
Post 29 Jul 2008, 05:52
View user's profile Send private message Visit poster's website Reply with quote
neville



Joined: 13 Jul 2008
Posts: 507
Location: New Zealand
neville
Oops, typo above...

That should be "windwakr". No offence intended Wink
Post 29 Jul 2008, 05:54
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
Madis731 wrote:
Intel C++ 10.1.022:
Code:
        imul      ecx                                           ;3.14
        mov       r8d, ecx                                      ;3.14
        sar       r8d, 31                                       ;3.14
        sar       edx, 2                                        ;3.14
        sub       edx, r8d                                      ;3.14
        lea       r9d, DWORD PTR [rdx+rdx*4]                    ;3.14
        add       r9d, r9d                                      ;3.14
        sub       ecx, r9d                                      ;3.14
    


Some comments:


Seems to be 64-bit code Sad

_________________
Bug Nr.: 12345

Title: Hello World program compiles to 100 KB !!!

Status: Closed: NOT a Bug
Post 29 Jul 2008, 06:38
View user's profile Send private message Reply with quote
Shahada



Joined: 25 Jul 2008
Posts: 77
Shahada
Thanks neville, good example for me.
Post 29 Jul 2008, 06:58
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2141
Location: Estonia
Madis731
Sorry about 64-bit, but any assembly-programmer can change it to 32-bit on-the-fly ... I think. R** => E** and registers above r8 can be changed to memory references (maybe even stack).

This particular example is rather easy if your willing to sacrifice ebx, esi, edi, ebp. I would not touch stack neither ebp. Ebx is dangerous too Smile
so...
Code:
proc chartobcd ;ecx=in numchar, eax=out bcd
        mov       eax, 66666667h
        movzx     ecx, cl
        imul      ecx
        mov       esi, ecx
        sar       esi, 31
        sar       edx, 2
        sub       edx, esi
        lea       edi, [edx*5]
        add       edi, edi ; you can make these two lines
        sub       ecx, edi ; sub ecx,edi / sub ecx,edi
        shl       edx, 4
        or        ecx, edx
        mov       eax, ecx
        ret
endp
    


...you can, of course get rid of edi by changing it to esi (if you see the code, its free) and then you have one less register used. It is there for optimal performance so its your own choice.
Post 29 Jul 2008, 09:06
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< 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.

Powered by rwasa.