Question: Problem: Write a Nios II program that converts an n-digit decimal integer into a binary Example B.12 number. The decimal number is given as n


Problem: Write a Nios II program that converts an n-digit decimal integer into a binary Example B.12 number. The decimal number is given as n ASCII-encoded characters, as would be the case if the number is entered by typing it on a keyboard Solution: Consider a four-digit decimal number, D-dzd2dido. The value of this number is ((d3 x 10 + d2) x 10 + di) x 10 + do. This representation of the number is the basis for the conversion technique used in the program in Figure B.17. Note that each ASCII encoded character is converted into a Binary Coded Decimal (BCD) digit before it is used in the computation r2 is a counter, initialize it with n movia r2, N ldw 2. (r2) movia r3, DECIMAL /* r3 points to the ASCII digits. / add r4, r0, r0 1 r4 will hold the binary number. *! /* Get the next ASCII digit. LOOP:ldbr5. (r3) andi r5, r5, OxOF Form the BCD digit add addir3. r3. 1 r4, r4, r5 Add to the intermediate result. / Decrement the counter Multiply by 10 /* Increment the digit pointer ge beq muli br r2, r2, 1 r2, rO, DONE r4, r4, 10 LOOP Loop back if not done DONE: movia r5, BINARY Store the result in stw r4, (r5) memory location BINARY
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
