1,105,391 Community Members

need help to write code how to convert binary string to decimal

Member Avatar
Newbie Poster
7 posts since May 2011
Reputation Points: -1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]

I am a beginner in MIPS and we're given this assignment to write a programs in MIPS to convert binary string to decimal
i do some research and I found some helpful information:
TO convert a binary number to decimal
Let X be a binary number, n digits in length, composed of bits Xn-1 ... Xo
Let D be a decimal number
Let i be a counter

1. Let D = 0
2. Let i = 0
3. While i < n do:
If Xi == 1 (i.e if bit i in X is 1), then set D = (D + 2 to the power of i)
Set i = (i + 1)

I am trying to write these in assembly language and really need help, here are what i have started, need help to guide me to finish this :( its very hard for me

.align 2

	move $zero, $r3		# assume that $r3 = 0, represent a decimal num
	addi $r4, $r4, 0	# i = 0, store in $r4
	addi $r5, $r5, 8	#8 digits in length 

	li $v0, 4		#code for print string
	la $a0, prompt		#load address of prompt into $a0
	syscall			#print the prompt message
	li $v0, 8		#code for read strings
	la $a0, binary		#addr of buffer (binary)
	li $a1, 9		#size of buffer (1 byte)
	syscall			#


Loop:	slt $r1, $r4, $r5	#while i < n
        beg			# am stuck here



prompt: .asciiz "Insert Binary String: \n"
output: .asciiz "The decimal number is: "

I am stuck here:
If Xi == 1 (i.e if bit i in X is 1), then set D = (D + 2 to the power of i)
Set i = (i + 1)
How do i write the Xi( bit i in X) in mips and plus look at my code above please

This article has been dead for over three months: Start a new discussion instead
Start New Discussion
View similar articles that have also been tagged: