Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem: Write a Nios II program that converts an n-digit decimal integer into a

ID: 2247383 • Letter: P

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

Explanation / Answer

A) ldb loads a byte in the memory , but ldw loads a whole word in the memory.

we are getting ASCII digit at the start of loop section which takes 1 byte of space to store.

thats why to read that byte we are using ldb.

B) Code finishes when beq instruction returns true. When it returns true that is r2 get equal to r0, DONE part will start to execute and the code will get finish after that.

C) Since 0 is denoted by 0000 in BCD format, each bit is evaluated once and hence the LOOP function is evaluated 4 times, once for each bit. Hence, the "br LOOP" line is evaluated thrice. Hence, 3 times.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote