In a program I convert command line parameters to a 32 bit number with the following code.

if digit (bl) is a number: edx=edx*10+bl


remember to deallocate the space used by arg.v(1) with strfree as below...

// get first argument - 

arg.v(1);
mov (eax, curop);

mov (0, edx);
mov (0, ebx);

getn1a:
mov ([eax], bl);
if (bl in '0'..'9') then
and (15, bl);
lea (edx, [edx + edx *4]);
lea (edx, [edx + edx]);
add (ebx, edx);
inc (eax);
jmp getn1a;
endif;
mov (edx, firstnum);
strfree (curop);
Posted on 2003-06-03 04:01:51 by V Coder
Thank you for your feedback. If you don't mind, could you comment the code snippet. I am trying to refresh my memory and I'ts been a long while.

What would change in your code if I wanted to store the number as a 64 bit integer?

Is there an easier way, does HLA support Casting a string into an integer?
Posted on 2003-06-03 11:07:16 by C. Wardell

Thank you for your feedback. If you don't mind, could you comment the code snippet. I am trying to refresh my memory and I'ts been a long while.

What would change in your code if I wanted to store the number as a 64 bit integer?

Is there an easier way, does HLA support Casting a string into an integer?


Well, the HLA Standard Library provides a set of routines that will convert a string of digits into the corresponding integer value (and you get to choose the size from eight bits to 128 bits, on power of two boundaries). So you could just do something like this:



arg.v(1);
mov( eax, digitStr ); // digitStr is type <<string>>.
conv.strTou64( digitStr, 0 ); // Result comes back in EDX:EAX
mov( eax, (type dword uns64Result[0]));
mov( edx, (type dword uns64Result[4]));
strfree( digitStr );


For more details, check out:

http://webster.cs.ucr.edu/Page_hla/HLADoc/HTMLDoc/HLAStdlib.html

and search for conv.strTou64

Cheers,
Randy Hyde
P.S. It's generally a good idea to put a try..endtry block around the conversion (conv.strTou64) to handle the exception that might occur if you attempt to convert an illegal value to a number.
Posted on 2003-06-03 14:17:37 by rhyde
Thanks Randy,

I managed to get the code working and outputing the uns64 value correctly, but I then run into another problem of using the INC on this value. INC only works on 8,16,or 32b values correct? Is there a corresponding set of ops for the 64b values?

Thanks
Posted on 2003-06-03 15:05:21 by C. Wardell
Randy!!!

You would not believe how much stress I faced contemplating coding that conversion. Countless sleepless nights!

Well actually, I was not sleeping, neither was I trying to code it because I was too sleepy to attempt. I think I did it an evening (or morning, when my brain was awake-ish).

Anyway. Your way is better. Thanks much!

I was going to post to ask how to do it, but I read some stuff in the AoAHLA manual. Yeah I need to read Stdlib!!!
Posted on 2003-06-03 21:31:36 by V Coder
I hoped the scratch-basic code I wrote on the top would be self explanatory...

if digit (bl) is a number: edx=edx*10+bl


Remember you are reading the number from most sig digit to least sig digit...
If you come across a character that is not a digit, then whatever you have so far is it...

eg. 12345
first bl=
1 >>>>> edx=edx*10+1=1
2 >>>>> edx=1*10 +2 = 12
3 >>>>> edx=12*10 + 3 = 123
etc.

I use lea to multiply by 10.
First multiply by 5: edx*4 + edx
Then multiply by 2: edx + edx

eax is a pointer to the next digit in the string. If a number is read, edx is updated, and we loop to check to see if the next character is numeric as well.
If the character is not numeric, then edx is the number...

// get first argument - 

arg.v(1);
mov (eax, curop);

mov (0, edx);
mov (0, ebx);

getn1a:
mov ([eax], bl);
if (bl in '0'..'9') then // if in the ascii range $30 to $39
and (15, bl); // Convert ascii character $30 '0' to $39 '9' to binary. strip the upper nybble
lea (edx, [edx + edx *4]); // multiply by 10
lea (edx, [edx + edx]);
add (ebx, edx); // add new digit
inc (eax); // loop for next character
jmp getn1a;
endif;
mov (edx, firstnum);
strfree (curop);
Posted on 2003-06-03 21:42:03 by V Coder

Thanks Randy,

I managed to get the code working and outputing the uns64 value correctly, but I then run into another problem of using the INC on this value. INC only works on 8,16,or 32b values correct? Is there a corresponding set of ops for the 64b values?

Thanks

There is no single instruction that operates on 64-bit values like this.
However, the HLA Standard Library's MATH module does provide a series of routines that operate on 64-bit and 128-bit values.
See:
http://webster.cs.ucr.edu/Page_hla/HLADoc/HTMLDoc/HLAStdlib.html#pgfId-1047405

E.g.,

math.addq( 1, dest, dest ); // computes dest := dest + 1, 64-bits.

Note, however, that the in-line multi-precisions operations are almost always faster and smaller. The advantage of the standard library routines is that they go to great pains to return the flags the same way a 64-bit or 128-bit instruction would set them (which is one reason these functions operate more slowly than the straight-line code).
Of course, if all you need is a 64-bit INC, try this:



#macro inc64( dest ):noOvrFlw;
inc( (type dword dest) );
jnz noOvrFlw;
inc( (type dword dest[4]) );
noOvrFlw:
#endmacro
.
.
.
inc64( var64 );


For more information on multi-precision operations,
see
http://webster.cs.ucr.edu/Page_AoAWin/HTML/AdvancedArithmetic.html#998265
Cheers,
Randy Hyde
Posted on 2003-06-04 14:16:23 by rhyde