flat assembler
Message board for the users of flat assembler.

Index > Main > decimal to unicode

Author
Thread Post new topic Reply to topic
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 21 Oct 2012, 22:21
Hi. I'm having trouble about swapping for example AL register. I'm getting random value in eax, for example 123h and I want to convert it to unicode string. I'm trying to do it by division, but as I guess, I must store all results by reverse order.. Any other ways to do that? I tried this example for 2 bytes and works fine, but if I'll get more than 2 byte result, it will just stuck. Help Sad
Code:
;decimal to unicode.
        xor ebx,ebx
@@:
        mov ecx,10 ;base 10 system.
        xor edx,edx
        div ecx
        ;is it done yet?
        test eax,eax
        jz @f
        add edx,'0' ;make it ascii.
        xchg dh,dl ;now unicode.
        mov bx, dx
        shl ebx,16 ;bitshift for plasing another values.
        jmp @b
@@:
        ret    
Post 21 Oct 2012, 22:21
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 21 Oct 2012, 23:18
Since the architecture is little-endian storing DX without any swapping should already provide the wide character you need, so search the forum for an IntToStr implementation and modify it to store DX instead of DL.
Post 21 Oct 2012, 23:18
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 21 Oct 2012, 23:33
You don't get me. I have e.g 123h decimal. When I'm converting to unicode, it must be written with big-endian.
if I get remainders first time 1, second time 2, and last time 3, it must be written as 3, 2 and 1.
Post 21 Oct 2012, 23:33
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1625
Location: Toronto, Canada
AsmGuru62 22 Oct 2012, 04:05
This is possible solution:
Code:
;
; EAX = value to convert into string
; EDI = address of the UNICODE string buffer
;
UInt32_to_UNICODE:
    pusha
    xor    ebx, ebx    ; EBX = 0 to count digits in result

    push    10        ; ECX = 10
    pop    ecx

.divide_by_10:
    xor    edx, edx
    div    ecx

    add    dl, '0'        ; Store next digit into stack
    push    edx
    inc    ebx

    test    eax, eax    ; Is there more digits?
    jnz    .divide_by_10    ; Yes. Go back for more.
    ;
    ; At this point stack has EBX digits stored.
    ; These must be put into buffer at EDI.
    ;
    mov    ecx, ebx

.save_result:
    pop    eax
    stosw
    loop    .save_result
    ;
    ; Terminate string with null
    ;
    mov    [edi], cx
    popa
    ret
    
Post 22 Oct 2012, 04:05
View user's profile Send private message Send e-mail Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 22 Oct 2012, 09:57
AsmGuru62
.......... HOW I FORGOT ABOUT STACK! I feel like a dumbass right now. Thank you, AsmGuru62!
Post 22 Oct 2012, 09:57
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1625
Location: Toronto, Canada
AsmGuru62 22 Oct 2012, 14:27
This code assumes that Direction Flag is cleared, because for Win32 it is always cleared in User code.
If you're coding for your own OS, then probably, you should add CLD at the beginning of that procedure.
Post 22 Oct 2012, 14:27
View user's profile Send private message Send e-mail Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 21 Nov 2012, 16:52
AsmGuru62 wrote:
If you're coding for your own OS, then probably, you should add CLD at the beginning of that procedure.
And save/restore it if calling/API conventions don't specify its state clearly. ;-)
Post 21 Nov 2012, 16:52
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.