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!! |
Anyone?
|
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. |
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 |
Quote:
Also:
|
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 --- rod. |
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 07:03 AM. |