Register | Login
Forum Index > Requests and Feedback > strToInt function
Author Message
Pages: 1
Emil_halim
Ziron Beta Tester

avatar

(send private message)

Posts: 639
Topics: 104

Location:
Alex, Egypt
[742] strToInt function - posted: 2011-11-19 18:14:15

hi

i need strToInt function to calculate a string value.

http://www.freewebs.com/ogremagic/index.htm
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[743] - posted: 2011-11-19 18:49:04
hi.. lets see....

Code:
function strVal(char* str) {
  uses ebx esi edx ecx;
  
  esi = str;
  eax = xor;
  
  ecx = esi;
  while (char[ecx] <> 0) {
    ecx++;
  }
  sub ecx, esi
  
  if (ecx == 0) {
    return 0;
  }
  
  ebx = xor;
  edx = xor;

  if (char[esi] == "-") {
    edx--; 
    esi++;
    ecx--;
  }

  while (ecx <> 0) {
    bl = [esi];
    imul eax, 10
    sub bl, 48
    eax += ebx;
    
    esi++;
    ecx--;
  }
  
  ecx = eax;
  emit byte 0x23, 0xCA;  //temporary fix 
  shl ecx, 1
  sub eax, ecx
}


let me know if it works, usage should be:

Code:
ecx = strVal('123455');
ecx += 10;
print('ecx = ', ecx:int, '\r\n');


should print 123465 smile

if it works i will add it to he strings.zir file.

Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Emil_halim
Ziron Beta Tester

avatar

(send private message)

Posts: 639
Topics: 104

Location:
Alex, Egypt
[744] - posted: 2011-11-19 19:03:35

ok thanks i will try it,

i have found c code in the net , so i converted it to Ziron , what do you think
not tested yet.
Code:
function  strchr(char *s, int cha)
 {
         /* scan from left to right */
         uses esi
         esi = s
         while (char[esi] != nil) {
                 /* if we hit it, return it */
                 if (char[esi]==cha) {
                         return esi;
                 }
                 esi++;
         }
 
         /* if we were looking for the 0, return that */
         if (char[esi]==cha) {
                 return esi;
         }
 
         /* didn't find it */
         return nil;
}

function atoi(char* s)
{
         uses esi
         const digits[10] = '0123456789';  /* legal digits in order */
         DWord val=0;         /* value we're accumulating */
         int32 _neg=0;              /* set to true if we see a minus sign */

         /* skip whitespace */
         esi = s;
         while ( char[esi] == " ") {
                 esi++;
         }
    
         /* check for sign */
         if (char[esi] == "-") {
                 _neg=1;
                 esi++;
         }
        else if (char[esi] == "+") {
                 esi++;
        }
 
        /* process each digit */
        while (char[esi] != nil) {
                 char* where;
                 DWord digit;
                 
                 /* look for the digit in the list of digits */
                 where = strchr(@digits, char[esi]);
                 if (where==nil) {
                         /* not found; not a digit, so stop */
                         break;
                 }
 
                 /* get the index into the digit list, which is the value */
                 digit = (where - digits);
 
                 /* could (should?) check for overflow here */
 
                 /* shift the number over and add in the new digit */
                 val = val*10 + digit;
 
                 /* look at the next character */
                 esi++;
         }
         
         /* handle negative numbers */
         if (_neg) {
                 return -val;
         }
         
         /* done */
         return val;
}


http://www.freewebs.com/ogremagic/index.htm
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[745] - posted: 2011-11-19 19:14:25
I have improved the one i posted a little more

Code:
function strVal(char* str) {
  uses ebx esi ecx;
  
  esi = str;
  eax = xor;
  
  if (ecx == 0) {
    return 0;
  }
  
  ebx = xor;
  ecx = xor;

  if (char[esi] == "-") {
    ecx--; 
    esi++;
  }

  while (char[esi] <> 0) {
    bl = [esi];
    
    if (bl => "0" and bl <= "9") {
      imul eax, 10
      sub bl, 48
      eax += ebx;
    }
    esi++;
  }
  
  ebx = eax;
  emit byte 0x23, 0xD9;  //temporary fix 
  shl ebx, 1
  sub eax, ebx
}


this one will ignore any other characters...

123a5 will become 1235

Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Emil_halim
Ziron Beta Tester

avatar

(send private message)

Posts: 639
Topics: 104

Location:
Alex, Egypt
[746] - posted: 2011-11-19 19:16:01

ok , i have tested it and works very well.

BTW ,what is the opcode instruction of this code
Code:
emit byte 0x23, 0xCA;  //temporary fix 


http://www.freewebs.com/ogremagic/index.htm
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[747] - posted: 2011-11-19 19:17:26
Code:
and ebx, ecx


Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[749] - posted: 2011-11-19 19:20:13
note i've updated the beta release of Ziron too, i'm gonna now go out to a party smile but tomorrow maybe i will add full support for and instruction.

Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Emil_halim
Ziron Beta Tester

avatar

(send private message)

Posts: 639
Topics: 104

Location:
Alex, Egypt
[752] - posted: 2011-11-19 19:23:26

have a nice time in the party.
see you tomorrow

http://www.freewebs.com/ogremagic/index.htm
Pages: 1
create new reply


Quick reply:

Message:



Currently Active Users:
There are currently 13 user(s) online. 0 member(s) and 13 guest(s)
Most users ever online was 1046, January 28, 2022, 2:08 pm.


Statistics:
Threads: 225 | Posts: 1848 | Members: 51 | Active Members: 51
Welcome to our newest member, yecate
const Copyright = '2011-2024 © OverHertz Ltd. All rights reserved.';
Web development by OverHertz Ltd