ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Click Here to receive this Complete Guide absolutely free.

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 # ###############

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.

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!

######## 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!?

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.

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

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.