Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / Requests and Feedback

strToInt function

A community discussion post from the original code:zIRON forum.

Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

strToInt function

hi

i need strToInt function to calculate a string value.

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

Replies

Colin admin
Posts
928
Threads
49
Location
Bulgaria

hi.. lets see....

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:

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

should print 123465 :)

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

Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

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.

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

Colin admin
Posts
928
Threads
49
Location
Bulgaria

I have improved the one i posted a little more

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

Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

ok , i have tested it and works very well.

BTW ,what is the opcode instruction of this code

emit byte 0x23, 0xCA;  //temporary fix 

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

Colin admin
Posts
928
Threads
49
Location
Bulgaria
and ebx, ecx
Colin admin
Posts
928
Threads
49
Location
Bulgaria

note i've updated the beta release of Ziron too, i'm gonna now go out to a party :) but tomorrow maybe i will add full support for and instruction.

Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

have a nice time in the party.

see you tomorrow

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