tags:

views:

77

answers:

3

hello, I have some question about assembly, is it possible to convert ASCII code of numbers (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) without 10 compares and branches like

if(input == 48) return 0;
...

(I'm talkin about code on assembly!) thanks in advance

+5  A: 

return input-'0';

This, of course, given that input is a digit. You can check it as follows, in C:

if(input >= '0' && input <= '9')....

Pavel Radzivilovsky
+1  A: 

Just subtract 48 (the ASCII value for the character 0).

SUB EAX, 48

The exact syntax depends on the specific processor and the assembler you are using.

Mark Byers
A: 

I'm not an assembly guy at all but this suggests you can use

sub     ebx, 48

to subtract 48 from your ascii code stored in the appropriate register. Of course this assumes intel assembly but I'd imagine others have similar functionality.

Steve Homer
Sure, I'd also assume other assemblers have substraction ;P
ninjalj
One would hope so lol.
Steve Homer
Hey, if you're lacking subtraction, you can always just multiply by -1, and then add. :P
Matt Ball