Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 10th, 2012, 11:50 AM   #1
Farel
Desperate Student
 
Farel's Avatar
 
Join Date: Dec 2012
Posts: 36
Rep Power: 0 Farel is on a distinguished road
Getting numbers from ascii

Processor Architecture: Assembly
Architecture: IA-32(CISC) - Intel 32 bit
Syntax: AT&T
Environment: Unix

I have a program, it's meant to turn an ASCII written number into an integer written one and put it in one of the registers.
s1 - This is the ASCII text containing the number, we were told to not check if it's really numbers, we assume it has to be one
tab_bytes - It's the size of s1

[ICODE=assembly]
mov $0,%EAX
mov $0,%EBX
mov $0,%ECX
mov $10,%EDX
while:
cmp $tab_bytes,%ECX
jge endwhile
mul %EDX
mov s1(%ECX),%BL
sub $48,%BL
add %EBX,%EAX
inc %ECX

jmp while
endwhile:[/icode]

It goes through s1, each symbol and subtracts 48 from them cause zero has an ascii code of 48, one has one of 49 etc.

I tried 2 versions. The first one looks just like this. The second one pushed the "mul %EDX" between the inc and jmp below. There is a huge difference, but none works! With the first, EAX is left equal to the last digit, and with the second one it's just zero.

I'm very confused why it does not work at all. I would be very grateful if anyone could help me here!
Farel is offline   Reply With Quote
Old Dec 10th, 2012, 12:09 PM   #2
sixstringartist
Programming Guru

 
Join Date: Jun 2005
Posts: 1,333
Rep Power: 14 sixstringartist will become famous soon enough
Re: Getting numbers from ascii

Its not clear to me what the assignment is. You take an ASCII character as input and have to output what?

Please provide example output for the following input:

'a'
'A'
'9'

Also, can you write the solution to this problem in C? Its often easier to translate from C to asm than it is to design it in asm first.
sixstringartist is offline   Reply With Quote
Old Dec 10th, 2012, 12:41 PM   #3
Farel
Desperate Student
 
Farel's Avatar
 
Join Date: Dec 2012
Posts: 36
Rep Power: 0 Farel is on a distinguished road
Re: Getting numbers from ascii

Allrighty, here's the whole program in c and as I've noticed, it works perfectly. And as much as it's text, it's about NUMBERS, so that's the trick. I want to also underline how tab_bytes needs to be decreased in size, as in Linux, there's none of that problem with zero as the last character.

c Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. char s1[]="2413";
  4. int main(int argc, char *argv[])
  5. {
  6. int tab_bytes=sizeof(s1)/sizeof(s1[0]);
  7. tab_bytes--;
  8. int a=0;
  9. int b=0;
  10. int c;
  11. int d=10;
  12. for (c=0; c<tab_bytes; c++)
  13. { a=a*d;
  14. b=s1[c];
  15. b=b-48;
  16. a=a+b;
  17. }
  18. printf("%d \n",a);
  19. system("pause");
  20. return 0;
  21. }
Farel is offline   Reply With Quote
Old Dec 10th, 2012, 12:59 PM   #4
sixstringartist
Programming Guru

 
Join Date: Jun 2005
Posts: 1,333
Rep Power: 14 sixstringartist will become famous soon enough
Re: Getting numbers from ascii

I dont understand why you are multiplying by 10 or why you are aggregating the result. What is a supposed to be here?
sixstringartist is offline   Reply With Quote
Old Dec 10th, 2012, 2:16 PM   #5
Farel
Desperate Student
 
Farel's Avatar
 
Join Date: Dec 2012
Posts: 36
Rep Power: 0 Farel is on a distinguished road
Re: Getting numbers from ascii

I'm multiplying it by 10 because... well..
I read a 2, it's added to EAX, but I look for the next character, it's 4. So first thing to do is to multiply it by 10, so we get 20, and checking the next one: 20+4=24, and then 240+1=241, and finally 2410+3=2413. We get the number not represented by characters...

As for aggregating the result: I am, as one call tell, a newbie, I've never went this far in C, so when I type something, I wish to see the results to check if it works properly.
Farel is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Identify creditcard numbers shorvath Sed and Awk 1 Feb 2nd, 2011 1:32 PM
Read multiple binary files to ascii Zar Other Programming Languages 0 Nov 7th, 2009 8:40 AM
Positive and Negative numbers - won't compile "if" or "else" statements 3dserge C++ 13 Feb 26th, 2009 12:41 PM
ASCII Adjust after Multiplication rick barclay Assembly 1 Apr 25th, 2005 10:42 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:08 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC