I don't understand the goal of multiplying by 10 at all.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?
A person said firstly in this topic that I was right about thathi R,
34"-> 3:'0010' 4:'0010' , this is an incorrect way of expressing the value.
34decimal =00100010 binary, weighting is 0,0,32,0,0,0,2,0
Derived from Bit weighting as 128,64,32,16,8,4,2,1
E
https://stackoverflow.com/questions/27336052/converting-decimal-to-binary-in-assembler
What is the difference? I didn't understand?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
So the ASCII decimal number need to be expressed by this form firstly? Or we need to do another thing before that?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
You need to do another thing firstly.So the ASCII decimal number need to be expressed by this form firstly? Or we need to do another thing before that?
Oh okay i understand this step now.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!
I think it's another way to express a decimal number no?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?
You don’t convert each character to ASCII!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?
<Deleted>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
Thanks for your answerThese 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.
So you ask me to do an algorithm firstly and after writing the code?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.
No, I am not asking you.So you ask me to do an algorithm firstly and after writing the code?
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?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.