How would you convert ASCII characters to decimal numbers in assembly?

  • Thread starter Thread starter twoski
  • Start date Start date
  • Tags Tags
    Numbers
Click For Summary

Discussion Overview

The discussion revolves around converting ASCII characters representing decimal digits into decimal numbers using assembly language. It addresses the conversion of single ASCII characters, the process of converting strings of ASCII characters into whole integers, and considerations for fitting these numbers into a 32-bit word.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant explains that to convert an ASCII character representing a decimal digit, the hex value of the character is subtracted by 0x30 to obtain the decimal value.
  • Another participant questions whether part B is asking to store each digit in separate memory locations or if it is referring to storing digits as BCD (packed decimal).
  • A participant expresses confusion about converting a string to a number, suggesting that a loop is necessary for the process.
  • Another participant suggests that part C may be concerned with not storing intermediate results in an 8-bit character.
  • One participant speculates that part C could refer to overflow conditions if the string has more than 10 digits.
  • Another participant argues that having an array to store each integer representing 10's, 100's, etc., would be inefficient.
  • A participant proposes that the solution to part C could involve adding each 8-bit number together to fit into a 32-bit word.
  • One participant suggests that part B is asking what a string of ASCII digits represents and how to manipulate those digits to form a 32-bit binary number.

Areas of Agreement / Disagreement

Participants express differing interpretations of parts B and C, with no consensus on the exact requirements or implications of these parts. Multiple competing views remain regarding the best approach to the conversion process and the handling of data storage.

Contextual Notes

There is uncertainty regarding the specific requirements of parts B and C, particularly in terms of data storage and overflow considerations. The discussion reflects varying levels of understanding of the concepts involved.

twoski
Messages
177
Reaction score
2

Homework Statement



In assembly, data is usually input in the form of ASCII character strings and it is then converted to the appropriate form for processing.

a) Explain how you would do to convert an ASCII character representing a decimal digit (i.e. ‘0’, ‘1’, …, ‘9’) into an actual decimal value

b) Extend your answer in (a) by explaining how you would do to convert ASCII character strings to whole integer numbers (i.e. 10s, 100s, 100s, …).

c) Considering the fact that each ASCII character is stored on 8 bits, explain how you would do to fit your converted number in (c) into a 32-bit word.


The Attempt at a Solution



A. The hex values for each ASCII character representing a decimal number range from 0x30 to 0x39. We take this hex value and subtract 0x30 from it to achieve the proper decimal value. For example, the ascii value '1' is 0x31, so 0x31 – 0x30 = 0x01 = decimal value 1.

B. These are the steps you would take to convert a string to a number.

Start with a stored final result of 0.
Loop through the string starting at the most significant digit.
1. Multiply the stored result by 10.
2. Subtract 0x30 from the current ascii digit and add the result to the final result.
3. Move to the next digit in the string and start the process over at step 1.
4. Return with the final result if there are no more ascii digits.

C. I don't understand this question... If you're converting, say, "1234" into a number then it should fit easily into a 32 bit word...
 
Physics news on Phys.org
I think your logic is the answer for part c. Part b is confusing, is it asking you to store each digit (0 through 9) in separate memory locations, so you just end up with yet another string of bytes, but in the range 0 through 9 versus hex 30 through hex 39? For part b, you could store the digits as BCD (packed decimal), 4 bits per digit, but I doubt that is the intent of part b.
 
I was a little confused because the only way you can convert a string to a number (to my knowledge) is with the aid of a loop.

However, my solution to B does work... I just don't understand how it ties into C.
 
I think in (c) they want you to consider that you should not store intermediate results in an 8-bit character.
 
twoski said:
However, my solution to B does work... I just don't understand how it ties into C.
You solution is really a solution to C. It seems that B just wants you to convert a string of ASCII digits into a string of bytes that range from 0 to 9, although you'd need to define some value as a terminator for the string of bytes. Perhaps B is just an intermediate step that you're just supposed to think about without actually implementing it.
 
Would it be possible that C is referring to overflow conditions (ie. if you're converting a string that has over 10 digits then you are definitely going to overflow)?
 
twoski said:
Would it be possible that C is referring to overflow conditions (ie. if you're converting a string that has over 10 digits then you are definitely going to overflow)?
I'm not sure, but B implies separate integer numbers for 10's, 100's, ... .
 
That seems awfully inefficient... Having an array which stores each integer representing 10's, 100's, etc. would be rather wasteful wouldn't it?
 
twoski said:
That seems awfully inefficient... Having an array which stores each integer representing 10's, 100's, etc. would be rather wasteful wouldn't it?
Yes, it would be inefficient. That's not the point. This is a programming exercise where the student is to build up capabilities piece by piece. The intent is to aid the student's understanding.
 
  • #10
I just don't think it's right though... We haven't discussed arrays in class very much.

The only way i can see C making sense is if i store each individual converted number in 8 bits. In which case the answer to C would be to simply add each 8-bit number together in order for it to fit into a 32 bit word. I think.
 
  • #11
You already solved C. My impression of B is that it's just asking what a string of ascii digits represents, and what you should do with the digits, such as multipy the 10's digit by 10, multiply the 100's digit by 100, and then C asks how to create a loop that does this while converting a string into a 32 bit binary number. I don't think B is actually asking for a code fragment that stores the digits into an array.
 
  • #12
Ohhh, I see what you mean...
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
5K
Replies
17
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K