Problem: Write a Nios II program that converts an n-digit decimal integer into a
ID: 2247273 • 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 movia ldw movia add r2, N r2, (r2) r3, DECIMAL r4, r0, r0 with s/ /* r3 points to the ASCII digits. /** r4 will hold the binary number. */ /* Get the next ASCII digit. /* Form the BCD digit. / Add to the intermediate result. */ /* Increment the digit pointer. /* Decrement the counter. LOOP: dbr5. (r3) andi add addi subi be muli br movia stw r5, r5, OxOF r4, r4, r5 r3. r3. 1 r2, r2. 1 r2, r0, DONE r4, r4, 10 LOOP r5, BINARY/* Store the result in r4. (r5) /* Multiply by 10 /* Loop back if not done. DONE: memory location BINARY. *Explanation / Answer
hi
rom pythonds.basic.stack import Stack
def divideBy2(decNumber):
remstack = Stack()
while decNumber > 0:
rem = decNumber % 2
remstack.push(rem)
decNumber = decNumber // 2
binString = ""
while not remstack.isEmpty():
binString = binString + str(remstack.pop())
return binString
print(divideBy2(42))
thanks
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.