LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-25-2009, 02:22 AM   #1
matt123
LQ Newbie
 
Registered: Apr 2009
Posts: 11

Rep: Reputation: 0
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!!

Last edited by matt123; 04-25-2009 at 01:54 PM.
 
Old 04-25-2009, 01:54 PM   #2
matt123
LQ Newbie
 
Registered: Apr 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Anyone?
 
Old 04-25-2009, 02:56 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
Old 04-26-2009, 02:01 AM   #4
matt123
LQ Newbie
 
Registered: Apr 2009
Posts: 11

Original Poster
Rep: Reputation: 0
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
 
Old 04-26-2009, 04:54 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,396

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814
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!?
 
Old 04-26-2009, 06:53 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.

Last edited by theNbomr; 04-26-2009 at 06:54 PM.
 
Old 04-27-2009, 01:15 AM   #7
matt123
LQ Newbie
 
Registered: Apr 2009
Posts: 11

Original Poster
Rep: Reputation: 0
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting extended ascii (,) in bash script Hko Programming 4 12-29-2012 03:42 AM
How to converting the c++ code to assembly Nejad Programming 17 12-29-2008 12:50 PM
Converting float to ascii in C C_to_be Programming 2 10-29-2007 06:41 PM
Converting ASCII to Binary? Darx Linux - Software 0 04-08-2004 10:43 AM


All times are GMT -5. The time now is 07:35 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration