Number (decimal) in ascii -> binary

Thread Starter

Rlern

Joined Nov 25, 2021
20
Doing it in C gives you a direct map to doing it in assembler. You just have to understand what the C statement does and how to do the same thing in assembler.

Seems like the latter half of that process is where you’re stuck in converting to assembler.

The trick here is to understand a shortcut in assembler to multiply by 10. Go back to algebra. Multiplying by 10 is the same as adding two different factors. 10 can be the sum of what two numbers? There are many possible answers, but you have a hint in that you want to pick two numbers that are both easy to multiply with in assembler.

Think of how you multiply a number by 4 in assembler (and no, that isn’t necessarily one of the two numbers). Does the answer to this hypothetical question give you a hint as to the two numbers (which total to 10) that you should pick?
I don't understand the goal of multiplying by 10 at all.
If I have a string in ASCII which is a decimal number (ex : 34),I have to translate it in binary. So I will just do the conversion :
"34"-> 3:'0010' 4:'0010'
But the number in extended table ascii can only go to 255 (0 to 255) :https://www.ascii-code.com
So why 0..65535 if it's only 255 ? It means that if I have 34000 I will do :
34000 : 3:'0010' 4:'0010' 0:'0000' 0:'0000' 0:'0000'

There is no interest to multiply by 10 here, so why?
 

ericgibbs

Joined Jan 29, 2010
21,448
hi R,
He said you are right about this way.
ASCII "0" -> 30 -> '0011' '0000'
ASCII "5" -> 35 -> '0011' '0101'

Not this way.
34"-> 3:'0010' 4:'0010' , this is an incorrect way of expressing the value.

Do you need any more help with this problem.?
E
 

MrChips

Joined Oct 2, 2009
34,826
If you are going to convert a string to binary you will need to multiply by 10.
Why?

Example:
123 (decimal) is 1 x 100 + 2 x 10 + 3

In other words,
123 = 1 x 10 x 10 + 2 x 10 + 3
 

djsfantasi

Joined Apr 11, 2010
9,237
First, when you have a number, say 34, that is stored as ASCII, one byte contains the ASCII code for the character three (“3”). The next byte contains the code for the character “4”. It does not contain the value 34!

Specifically, the first byte contains the value 51 which is the ASCII code for “3”. The next byte contains the value 52.

Consider just the text “3”. You can get the value for “3” by subtracting 48.
51 - 48 = 3
The same rule applies for all the digits in ASCII code.

But what happens when there are more than one character in the ASCII text? You have to step through all the characters. And thus is where multiplying by 10 comes in.

See if you can figure out how!
 

Thread Starter

Rlern

Joined Nov 25, 2021
20
hi R,
He said you are right about this way.
ASCII "0" -> 30 -> '0011' '0000'
ASCII "5" -> 35 -> '0011' '0101'

Not this way.
34"-> 3:'0010' 4:'0010' , this is an incorrect way of expressing the value.

Do you need any more help with this problem.?
E
What is the difference? I didn't understand?
 

Thread Starter

Rlern

Joined Nov 25, 2021
20
If you are going to convert a string to binary you will need to multiply by 10.
Why?

Example:
123 (decimal) is 1 x 100 + 2 x 10 + 3

In other words,
123 = 1 x 10 x 10 + 2 x 10 + 3
So the ASCII decimal number need to be expressed by this form firstly? Or we need to do another thing before that?
 

djsfantasi

Joined Apr 11, 2010
9,237
So the ASCII decimal number need to be expressed by this form firstly? Or we need to do another thing before that?
You need to do another thing firstly.

I am not sure that you understand the difference between ASCII and decimal. In the form presented by MrChips, where did the 1 come from in 1x100?
 

Thread Starter

Rlern

Joined Nov 25, 2021
20
First, when you have a number, say 34, that is stored as ASCII, one byte contains the ASCII code for the character three (“3”). The next byte contains the code for the character “4”. It does not contain the value 34!

Specifically, the first byte contains the value 51 which is the ASCII code for “3”. The next byte contains the value 52.

Consider just the text “3”. You can get the value for “3” by subtracting 48.
51 - 48 = 3
The same rule applies for all the digits in ASCII code.

But what happens when there are more than one character in the ASCII text? You have to step through all the characters. And thus is where multiplying by 10 comes in.

See if you can figure out how!
Oh okay i understand this step now.
So If i understand correctly, the first is to convert each character in the variable into the ASCII code associated?
By your example : 34 stored as ASCII, one byte for "3" (so the ASCII code : 51), another one for "4" (so the ASCII code : 52)
After I think we need to add them, to obtain the final number 103 and convert it into binary (corresponding to the "16-bit unsigned number" in the code before)

But I still don't really understand the goal to multiply by 10?
 

Thread Starter

Rlern

Joined Nov 25, 2021
20
You need to do another thing firstly.

I am not sure that you understand the difference between ASCII and decimal. In the form presented by MrChips, where did the 1 come from in 1x100?
I think it's another way to express a decimal number no?
For example 42 (decimal) which can be expressed literally by 4*10+2
 

djsfantasi

Joined Apr 11, 2010
9,237
Oh okay i understand this step now.
So If i understand correctly, the first is to convert each character in the variable into the ASCII code associated?
By your example : 34 stored as ASCII, one byte for "3" (so the ASCII code : 51), another one for "4" (so the ASCII code : 52)
After I think we need to add them, to obtain the final number 103 and convert it into binary (corresponding to the "16-bit unsigned number" in the code before)

But I still don't really understand the goal to multiply by 10?
You don’t convert each character to ASCII!

It’s value is ASCII. Specifically, their values are based on the ASCII code.

As far as multiplying, you have a sequence of values set to 1, 2, and 3. How do you calculate the decimal value 123?
 

MrChips

Joined Oct 2, 2009
34,826
These concepts are fundamental to all computer programming. It is very important that you understand and master this at an early stage in your computer education.

When you see a value such as 123 printed on a sheet of paper or computer screen you are seeing three separate images/icons/flashcards:
[1] [2] [3]

I will call them flashcards. Each flashcard is just a picture with no numerical value.
Each "flashcard" is stored in computer memory as a stack of cards. Each card is given an arbitrary index number (predetermined by the ASCII convention).

The index number for the cards [0] to [9] are as follows:

index - card image
48 - [0]
49 - [1]
50 - [2]
51 - [3]
52 - [4]
53 - [5]
54 - [6]
55 - [7]
56 - [8]
57 - [9]

When you receive three cards [1] [2] [3], for example, you do not know what the image looks like.
All you are given is the card index, 49, 50, 51.

You subtract the index of card [0] = 48 in order to find the offset from card [0].

Hence for card [1], we get 49 - 48 = 1
Hence for card [2], we get 50 - 48 = 2
Hence for card [3], we get 51 - 48 = 3

If you receive three cards in succession, [1] [2] [3]

you evaluate the first card 49 - 48 = 1 and save this value = R.
When the next card arrives you evaluate this card = N and form a new value R = R x 10 + N
You keep on doing this until there are no more cards or there are no more cards with index from 48 to 57.

Ask questions if you do not follow this.

Edit: For a proper computer algorithm, initialize R = 0.
Hence the algorithm R = R x 10 + N works every time including the first card and subsequent cards.
 

Thread Starter

Rlern

Joined Nov 25, 2021
20
These concepts are fundamental to all computer programming. It is very important that you understand and master this at an early stage in your computer education.

When you see a value such as 123 printed on a sheet of paper or computer screen you are seeing three separate images/icons/flashcards:
[1] [2] [3]

I will call them flashcards. Each flashcard is just a picture with no numerical value.
Each "flashcard" is stored in computer memory as a stack of cards. Each card is given an arbitrary index number (predetermined by the ASCII convention).

The index number for the cards [0] to [9] are as follows:

index - card image
48 - [0]
49 - [1]
50 - [2]
51 - [3]
52 - [4]
53 - [5]
54 - [6]
55 - [7]
56 - [8]
57 - [9]

When you receive three cards [1] [2] [3], for example, you do not know what the image looks like.
All you are given is the card index, 49, 50, 51.

You subtract the index of card [0] = 48 in order to find the offset from card [0].

Hence for card [1], we get 49 - 48 = 1
Hence for card [2], we get 50 - 48 = 2
Hence for card [3], we get 51 - 48 = 3

If you receive three cards in succession, [1] [2] [3]

you evaluate the first card 49 - 48 = 1 and save this value = R.
When the next card arrives you evaluate this card = N and form a new value R = R x 10 + N
You keep on doing this until there are no more cards or there are no more cards with index from 48 to 57.

Ask questions if you do not follow this.

Edit: For a proper computer algorithm, initialize R = 0.
Hence the algorithm R = R x 10 + N works every time you get the first card and subsequent cards.
Thanks for your answer
I tried to understand it with my current code but there is something I don't understand.
The "main code" of my program with the MOV and the INC R0 are for these index? Or it's completely different?

Moreover, the R7 register in my code represent the first card, i'm not sure?
 

MrChips

Joined Oct 2, 2009
34,826
Don't focus on code for your solution.

Your algorithm must be written in plain language that you and anyone can understand.
It must not contain any computer jargon. It must be independent of computer language and computer architecture.

Once you have expressed you solution in plain language you can then translate it into any computer language.
Algorithm -> computer code ought to be direct with no confusion.

If the algorithm does to appear to work then your code will not work.
 

Thread Starter

Rlern

Joined Nov 25, 2021
20
Don't focus on code for your solution.

Your algorithm must be written in plain language that you and anyone can understand.
It must not contain any computer jargon. It must be independent of computer language and computer architecture.

Once you have expressed you solution in plain language you can then translate it into any computer language.
Algorithm -> computer code ought to be direct with no confusion.

If the algorithm does to appear to work then your code will not work.
So you ask me to do an algorithm firstly and after writing the code?
 

Thread Starter

Rlern

Joined Nov 25, 2021
20
No, I am not asking you.

I am telling you to write out the algorithm FIRST before writing code. This is the important part.

Writing code is the easy part.
But if i don't understand the code in the program of my professor how am i supposed to know how to write the algorithm?
 
Top