LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Assembly language? converting ascii to decmial (https://www.linuxquestions.org/questions/programming-9/assembly-language-converting-ascii-to-decmial-721539/)

matt123 04-25-2009 02:22 AM

Assembly language? converting ascii to decmial
 
hello, iam tring to figure out assembly language. What i want to do is write a program purely in assembly that takes in a number in base 10 and converts it to hex octal and binary. Just know how to output to the screen and take in from the keyboard, but once i get the number in I am unable to work with them. i think because they are an ascii string as opposed to integers. trying to convert so i can manipulate them for the conversions to different bases. I may be way off, but am definatley lost and would appreciate any guidance!! Here is the code i have so far, but it does not do wnat i want it to:

.section .data
input:
.fill 256 #where i would like to store the converted number, if possible
prompt:
.asciz "Enter a decimal integer:"
prompt_end:
.equ promptlength, prompt_end-prompt

.section .bss
.lcomm number, 40 #number taken in from input, in ascii
.section .text
.globl _start
_start:
nop

######## display prompt string ####################
movl $4,%eax
movl $1,%ebx
movl $prompt,%ecx #string for output
movl $promptlength,%edx
int $0x80

######## get input #################################
movl $3, %eax
movl $0, %ebx
movl $number, %ecx
movl $40, %edx
int $0x80


######## convert from ascii to decmial ##############
# %edi holds current index
# %eax holds number
movl $0, %edi
movl number(,%edi,1),%eax
movl $3, %ecx
loop1:
sub $48, %eax
movl %eax, input
incl %edi
movl number(,%edi, 1), %eax
cmpl %edi, %ecx
jge loop1

########## Output converted number # ###############

movl $4, %eax
movl $1, %ebx
movl $input, %ecx
movl $4, %edx
int $0x80

movl $1,%eax
movl $0,%ebx
int $0x81
~


Thanks!!

matt123 04-25-2009 01:54 PM

Anyone?

theNbomr 04-25-2009 02:56 PM

Algorithmically, you need to take each ASCII-formated digit, and convert it to binary. This can be done with a simple lookup table, along with any necessary handling of invalid characters. Then, having converted the digit, you do the grade-school arithmetic of multiplying each digit (now using binary arithmetic) by its decade multiplier (or use lookups, again), and then add all of the decade-multiplied digits to get the resulting binary equivalent. This can be done in a loop, starting with either the least or most significant digit, and working iteratively through each entered digit.

--- rod.

matt123 04-26-2009 02:01 AM

I can somewhat understand your answer, but not sure how to write it in code. I also am not sure why it is better to convert to binary from asccii instead of decimal. I see that decimal is just char-48 , which is what i was trying to do in the posted code, although I did not account for the multiple of 10 for which i would need to add. If you could give me some guidance it would be much appreciated!

Thanks!!
Matt

ntubski 04-26-2009 04:54 PM

Quote:

######## convert from ascii to decmial ##############
This is the wrong way to think about it. You are actually converting from an ascii encoded decimal string to a native integer. A native integer is not decimal, hexadecimal, octal, or binary (well it's binary at the hardware level, but you're not designing hardware), what matters is that the arithmetic machine instructions (eg add, sub, mul, etc) work on it.

Also:
  • You should write $'0' instead of $48 if you really mean the ascii character
  • You put the input from user into a buffer called number, and then use a buffer called input for output!?

theNbomr 04-26-2009 06:53 PM

To expand upon what ntubski says: there are really just two representations of a number, especially an integer. There is the native format, with which you can perform arithmetic operations, and which we typically call 'binary'. Then there are the human readable formats, which are typically done using strings of ASCII characters, and which may be done in any radix. In computing, we tend to use the radices 2, 8, 10, and 16 most commonly. The particular representation is chosen according to the context, as we tend to choose the radix which makes it easiest to apply it to the problem.
The standard C functions atoi(), atol(), and atof(), as well as scanf() all perform the function you are trying to emulate.
To convert an ASCII decimal digit to its 'binary' value, simply subtract from it, the value "0", by which I mean the ASCII character that we use to display a zero. So...
"0" - "0" = 0
"1" - "0" = 1
"2" - "0" = 2
... you get the idea.
Code:

Integer = 0      # Initialize the answer
foreach AsciiDigit in NumberString    # Starting with least significant digit
    Byte = AsciiDigit - "0"
    Integer = (Integer * 10) + Byte
next AsciiDigit
#
# Integer contains binary value of NumberString
#

I haven't written x86 assembler in many years, so I won't embarrass myself by trying to write your code for you. Besides, this sounds like homework, and we don't do people's homework here.
--- rod.

matt123 04-27-2009 01:15 AM

figured out the conversion from acsii to numbers that you can work with mathematically. i will post it here:

######## convert from ascii to decimal ##############
# %edi holds current index
# %eax holds number
movl $0, %edi
movl $0, %ebx
loop1:
xor %eax, %eax #set %eax==0
movb number(,%edi,1), %al #get num for conv
cmp $0xA, %eax #check for nul
je done
sub $48, %al #sub for int
push %eax #save value
movl %ebx, %eax
movl $10, %ecx #multiplier
mul %ecx #multiply current value by 10
pop %ebx #
add %eax, %ebx
incl %edi
jmp loop1
done:
movl %ebx, input
movl %edi, declength


Thanks for the help!


All times are GMT -5. The time now is 02:47 PM.