🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.language.basm
Article #6

Better IntToStr? What do ya think

#6
From: Colin B Maharaj
Date: Tue, 19 Jun 2007 11:48
62 lines
2053 bytes
/*

Had some spare time and was playing around
a bit with some code optimization. I was
wondering if this is faster and just as
safe as IntToStr.

My little speed test says it is twice as fast
as itoa and 3 time as fast as IntToStr,
however itoa does hex and this does not!

Question is, do I need to be pushing any
registers on the stack to save and restore
on exit?

I think by wrapping it in a C function, I will
not have to worry about enter/exit ASM code (?)

*/


char *IntToStr2 (int i, char *s)
{
   int neg=0;
   if (i<0) { neg=1; i*= -1; s++; }
   asm
   {
      mov ebx, s
      push ebx
      mov eax, i
      mov ecx, 10
   IntToStrTop:
      cdq
      div  ecx
      add  dl, 48
      mov [ebx], dl
      inc ebx
      cmp ax, 0
      jnz short IntToStrTop
      mov [ebx], 0
      dec ebx
      pop eax
      push eax

   IntToStrTopOfstrrev:
      cmp ebx, eax
      jbe short IntToStrEnd
      mov cl, [ebx]
      mov ch, [eax]
      mov [ebx], ch
      mov [eax], cl
      inc eax
      dec ebx

   jmp IntToStrTopOfstrrev

   IntToStrEnd:
      pop eax
   }
   if (neg) { s--; *s='-'; }
}
Technical Details
Message-ID: <4677c279$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 107b11,967a3c560bdbf1af,start
X-Google-Attributes: gid107b11,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Tue, 19 Jun 2007 07:48:10 -0400
From: Colin B Maharaj <noreply@myhost.com>
User-Agent: Thunderbird 1.5.0.12 (Windows/20070509)
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.language.basm
Subject: Better IntToStr? What do ya think
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-Antivirus: avast! (VPS 000750-1, 06/18/2007), Outbound message
X-Antivirus-Status: Clean
NNTP-Posting-Host: 190.58.14.113
Message-ID: <4677c279$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1182253689 190.58.14.113 (19 Jun 2007 04:48:09 -0700)
Lines: 64
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail