Author Topic: Convert decimal to base 5?  (Read 10431 times)

anta40

  • Member
  • ***
  • Posts: 311
Re: Convert decimal to base 5?
« Reply #15 on: May 21, 2013, 06:36:42 PM »
«div  ecx»  divide  edx:eax by ecx 
so do this

xor   edx, edx
div    ecx
-----------------
and do this also
push ecx
print ....  ; <---destroys ecx
pop   ecx

it is safe if we use  ebx or esi or edi

Hmm I think I stil don't get it right...

Code: [Select]
L2:
    xor edx, edx
    div ecx
    push ecx
    push edx
    print str$(edx)
    pop edx
    pop ecx
    jmp L1

 :redface:

dedndave

  • Member
  • *****
  • Posts: 8825
  • Still using Abacus 2.0
    • DednDave
Re: Convert decimal to base 5?
« Reply #16 on: May 21, 2013, 07:37:01 PM »
these functions may (and usually do) destroy EAX, ECX, EDX
they preserve EBX, EBP, ESI, and EDI
Code: [Select]
L2:
    xor edx, edx
    div ecx
    push eax
    push ecx
    push edx
    print str$(edx)
    pop edx
    pop ecx
    pop eax
    jmp L1

RuiLoureiro

  • Member
  • ****
  • Posts: 819
Re: Convert decimal to base 5?
« Reply #17 on: May 22, 2013, 12:13:27 AM »
anta40,
This is your code

Quote
include \masm32\include\masm32rt.inc
.686
.code
start:
    ;xor edx,edx
    mov eax, 123   ; number to convert
    ;mov ecx, 5      ; base
    mov ebx, 5           ; base ebx=5  ebx is safe
L1:
    cmp eax, 0      ; if number is still > 0?
    ;jg L2         ; yes. keep dividing   
    jl L3         ; done

L2:
       xor edx, edx
        div ebx         ; number = number eax / base ebx

        push eax    ; preserve the quotient EAX
        ;
             print str$(edx)           ; show the remainder edx
        ;
        pop eax     ; preserve the quotient EAX
        jmp L1

L3:
    exit
end start

dedndave

  • Member
  • *****
  • Posts: 8825
  • Still using Abacus 2.0
    • DednDave
Re: Convert decimal to base 5?
« Reply #18 on: May 22, 2013, 12:31:04 AM »
what you are probably missing is to convert the binary remainder to ASCII before storing it

Code: [Select]
Base5   PROC USES EDI dwValue:DWORD,lpBuffer:LPSTR

;the buffer pointed to by lpBuffer should be at least 15 bytes long
;a pointer to the first base 5 ASCII digit is returned in EAX

        mov     edi,lpBuffer
        mov     ecx,5
        add     edi,14
        mov     eax,dwValue
        mov     [edi],ch

Base50: xor     edx,edx
        dec     edi
        div     ecx
        or      dl,30h        ;make it an ASCII char
        or      eax,eax
        mov     [edi],dl
        jnz     Base50

        xchg    eax,edi
        ret

Base5   ENDP

of course, if you use the str$ macro, it does that for you
if you want to see the dividend, quotient, and remainder at each step...
Code: [Select]
Base50: push    ecx
        push    eax
        print   str$(eax),32
        pop     eax
        pop     ecx

        xor     edx,edx
        dec     edi
        div     ecx

        push    ecx
        push    eax
        push    edx
        push    edx
        print   str$(eax),32
        pop     edx
        print   str$(edx),13,10
        pop     edx
        pop     eax
        pop     ecx

        or      dl,30h        ;make it an ASCII char
        or      eax,eax
        mov     [edi],dl
        jnz     Base50

RuiLoureiro

  • Member
  • ****
  • Posts: 819
Re: Convert decimal to base 5?
« Reply #19 on: May 22, 2013, 01:15:11 AM »
Dave,
 
      This is a work to you
     
      For base x digits a,b,c,d,e we have
     
(eq. 5) abcde (base x ) = a x^4 + b x^3 + c x^2 + d x + e

     =>

(eq. 6) abcde (base x ) = x(x(x(x(a) + b) + c) +d) + e

     Whats your general magic rule to get the digits p,q,r,s,t,u,v... of base y.

dedndave

  • Member
  • *****
  • Posts: 8825
  • Still using Abacus 2.0
    • DednDave
Re: Convert decimal to base 5?
« Reply #20 on: May 22, 2013, 01:51:06 AM »
that equation looks worse than it is - lol
it's really simple, once you get the hang of it

i wrote this document a few years ago, when i was working on the bignum to ascii routines
hope it helps   :t

dedndave

  • Member
  • *****
  • Posts: 8825
  • Still using Abacus 2.0
    • DednDave
Re: Convert decimal to base 5?
« Reply #21 on: May 22, 2013, 02:08:54 AM »
i might add.....

ling long kai fang is most efficient when working with very large values
for smaller values, like single dwords, the "divide-and-conquer" method can be more efficient
well - just don't use the DIV instruction - lol
for an example of what i mean, have a look at Base5b posted above
it uses divide-and-conquer, but uses mutliply-to-divide and a larger base (25)
if we really wanted it to be fast, we might divide by 625 and use a larger look-up table   :P