flat assembler
Message board for the users of flat assembler.

Index > Windows > How to convert Integer to a String?

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



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 28 Sep 2010, 13:23
Code:
stdcall fitoa,[g_value],g_buffer    

To understand, you need FASM and IA-32 manuals.
Start with first instruction, look it up, remember it.
Move to next instruction, repeat the process.
Take it apart bit-by-bit, we all learned this way...

EX: I never used or seen the fbstp instruction before.
I had no idea what it meant or did.
I opened the manual and searched it.
Now i understand its purpose.
It is now forever in my head...


Last edited by bitshifter on 28 Sep 2010, 13:54; edited 1 time in total
Post 28 Sep 2010, 13:23
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 28 Sep 2010, 13:48
to not puzzle newbie with invoke/stdcall difference here is an example (where it was cooked)

type a number in upper edit, then press OK Wink


Description:
Download
Filename: sw298.0.0.5.1.zip
Filesize: 4.25 KB
Downloaded: 374 Time(s)

Post 28 Sep 2010, 13:48
View user's profile Send private message Visit poster's website Reply with quote
iic2



Joined: 26 Jun 2008
Posts: 122
iic2 28 Sep 2010, 15:42
Quote:
simple right ?.. mate its hard for me I'm starter >.< and btw I need in 32bit mode very small example on the world

Overflowz, the only way to understand the simplicity of assembler is to sit down and explore a few samples given. I mean, stay down in that one procedure and change and remove stuff just to se what happen. Save those copies as back-ups with special names just in case you want to try something difference with it or even add more to it. I do that for weeks before moving on and it was worth it and still is. I wanted to understand the complete flow of the code while googling like hell all at the same time. Than you move on to the next adventure. When it all said and done, you will only need to master 5 - 10 main blocks of assembler code. With those in mind, you can rebuild many, many more customized for your program needs, quickly ... but only after you learn what it does.

It will be truly amazing all you will learn with assembler, that other programmers will "NEVER" learn just because of the language they were taught to use which they are also are taught not to use ASM. This is more geared as an promotion to keep you from exploring outside their tools. C++, JAVA, etc. I'm taking a C++ course now and it has really goofy syntax but it's teach you how to talk the programming sh*t which is needed on a job. I will never stop moonlight with ASM. With it, you will always be on top of any solution needed.
Post 28 Sep 2010, 15:42
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 28 Sep 2010, 17:45
iic2, you're 100% right! Smile I'm just guy who wants to learn things in 1 day and thats not possible. I'll see those examples and I'll try to learn what does what. Thank you for ur reply! I'm really glad Smile
Post 28 Sep 2010, 17:45
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 28 Sep 2010, 19:12
Hey, this method is the best for me but 1 question, is that possible to write with proc's and not with ":" things ? and also, when I tried add 16 number to ecx, instead of HEX numbers it's showing "0,1,2,3,4,5,6,7,8,9,:,;,<,=,>,?,10...". someone can add function that should replace ;,:,<.. things into A,B,C,D,E,F .. ? Razz Thank you!
Code:
dec2str:
        ; EAX = value 
        ; EDI = buffer 
        mov     ecx,16
    .stack_dec: 
        xor     edx,edx 
        div     ecx 
        add     edx,'0' 
        push    edx 
        test    eax,eax 
        jz      .purge_dec 
        call    .stack_dec 
    .purge_dec: 
        pop     dword[edi] 
        inc     edi 
        ret                  
Post 28 Sep 2010, 19:12
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 28 Sep 2010, 19:26
Overflowz,

Check for edx>9 and add 'A'-10 instead of '0'.

This method indeed is simple, but unnecessarily resource hungry (use iteration instead of recursion whenever possible).

Division by power of 2 can be done other way.
Post 28 Sep 2010, 19:26
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4354
Location: Now
edfed 28 Sep 2010, 19:32
yes, it is possible:
there was a contest in general forum about binary to hexa conversion, and the winner algorythm was this one:

Code:
locodelassembly:
;use32
        mov ah,al
        and al,0fh
        call @f
        xchg al,ah
        shr al,4
@@:
        cmp al,10
        cmc
        sbb dl,dl
        and dl,7
        add al,dl
        add al,'0'
        ret

    
Post 28 Sep 2010, 19:32
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 28 Sep 2010, 20:00
edfed, the first place actually belongs to MHajduk with a 25 bytes entry which additionally does not violate the original rules (i.e. no branches nor stack is used while in my code are used both). bitRAKE also posted 25 bytes code, but since he was second in doing that then so is his position Wink
Post 28 Sep 2010, 20:00
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4354
Location: Now
edfed 28 Sep 2010, 20:10
but your too is 25 bytes, and it is the fastest.
that's why i say you are the winner.

Code:
bitrake:
xor     ah, ah
mov     bl, 16 
div     bl 
imul    ebx, eax, 7
shr     bx, 6  
and     bl, 1 
lea eax,[eax+8*ebx+'00'] ; 32 bit
sub eax,ebx
    


the code from bitrake there is the smallest and the fastest, under some condition, tosay, my pc see loco as the fastest, maybe tomorrow, it will be bitrake.


this one from mhajduk is just one of the slower, but one of the smaller too.
Code:
mhajduk:
ror al,4
aam 16
imul ebx, eax, 7
shr bx, 6 
and bl, 1 
lea eax,[eax+8*ebx+'00']
sub eax,ebx
                        
Post 28 Sep 2010, 20:10
View user's profile Send private message Visit poster's website Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6115
Location: Poland
MHajduk 28 Sep 2010, 20:35
edfed wrote:
this one from mhajduk is just one of the slower, but one of the smaller too.
Code:
mhajduk:
ror al,4
aam 16
imul ebx, eax, 7
shr bx, 6 
and bl, 1 
lea eax,[eax+8*ebx+'00']
sub eax,ebx
    
edfed, please be more careful if you try to quote somebody. I didn't write the code you've "quoted" above. Here you can see what in fact I wrote:
  • First solution:
    MHajduk wrote:
    28 bytes, ASCII output in AX
    Code:
    xor     ebx, ebx
    mov dl, 9
    
    mov        ah, al
    shr   ah, 4
    and    al, 0Fh
     
    cmp     dl, al
    adc   bl, bl
    
    cmp       dl, ah
    adc   bh, bh
    
    add       ax, 0x3030
    imul      ebx, ebx, 7 
    add     eax, ebx    
  • Second solution:
    MHajduk wrote:
    25 bytes, ASCII output in AX register (but with bytes in the reversed order):
    Code:
    xor   ah, ah
    mov   bl, 16
    div   bl
    
    imul  ebx, eax, 7 
    shr     bx, 6 
    and   bl, 1
     
    imul      ebx, ebx,7
    add       ax, 3030h
    add        eax, ebx    

BTW, the contest which Fanael started was about size not speed, hence your "opinions" are completely inadequate here.
Post 28 Sep 2010, 20:35
View user's profile Send private message Visit poster's website Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji 28 Sep 2010, 22:14

Just a quick note in French to tease edfed

aie edfed, y'a MHajduk qui t'as mis à la masse là ...
edfed, je te taquine (amicalement) !
Razz

_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 28 Sep 2010, 22:14
View user's profile Send private message Send e-mail Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 29 Sep 2010, 12:34
baldr, I need example mate Razz I dont know much assembly im totally noob.
Post 29 Sep 2010, 12:34
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 29 Sep 2010, 12:46
One must learn to stand before walking.
One must learn to walk before running.
So why then are you looking at running shoes already?
Post 29 Sep 2010, 12:46
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 29 Sep 2010, 13:29
True. I just dont understand much english isn't that hard to imagine ?.. Smile
Post 29 Sep 2010, 13:29
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 01 Oct 2010, 14:28
1 simple question, what does "div ecx" ? for example ecx has value 50, when we will call "div ecx" what will happened ? ty. and what the point of doing that ?
Post 01 Oct 2010, 14:28
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 01 Oct 2010, 14:57
Quote:

1 simple question, what does "div ecx" ?

Divides the unsigned number in EDX:EAX (both registers are combined to generate a 64-bit dividend), by the number in ECX and the quotient is stored in EAX and the reminder in EDX. Consider reading the manuals like http://flatassembler.net/docs.php?article=manual#2.1.4 , http://www.intel.com/products/processor/manuals/ (the instruction set manuals), and the perhaps easier to follow than Intel's manuals http://developer.amd.com/documentation/guides/Pages/default.aspx (volume 3). Additionally, Intel's and AMD's first volume are important for basic Assembly knowledge.
Post 01 Oct 2010, 14:57
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 14 Oct 2010, 19:18
I'm getting mad about this.. I dont understand the code.. just someone can tell me if this can be done with MOVSB or STOSB function ? thanks.
Post 14 Oct 2010, 19:18
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 14 Oct 2010, 20:33
Are you sure you really wanted to post this question here and not in the other one you were trying to concatenate two strings?

Anyway, if you mean the integer to string functionality then no, you can't use none of those instructions to magically convert integer to string.

Lets try a different approach first:
Code:
void toString(char *buff, unsigned int number, unsigned int base)
{
   char digit;

   digit = (number % base);
   digit += (digit > 9 ? 'A' - 10 : '0');
   number = number / base;

   if (number != 0){

      toString(buff, number, base);
      buff++; /* Move base pointer */
   }

   buff[0] = digit;
   buff[1] = '\0'; /* NUL char. It gets overwritten unless this is the least significant digit (we are going to accept this inneficiency for now) */
}    
Do you understand that? If you do then I'll provide you the Assembly equivalent.
Post 14 Oct 2010, 20:33
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 14 Oct 2010, 21:12
grrrr never mind.. I'll try to learn everything about assembly and I'll learn that later when I can understand things like that. Anyway thank you very much for replies.
Post 14 Oct 2010, 21:12
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20571
Location: In your JS exploiting you and your system
revolution 15 Oct 2010, 00:09
Overflowz wrote:
I'll try to learn everything about assembly ...
Since I first saw a computer I've been trying to do that also. I'm still not there yet and there seems to be a long way still to go.
Post 15 Oct 2010, 00:09
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.