- #1
twoski
- 181
- 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...