I am having difficulty implementing Booth's Algorithm in mips and need some assistance urgently. FOr this assignment, I have to allow the user to enter two number representing the multiplier and the multiplicant. The the Booth's algorithm when implemented in mips should print the results in both binary and decima.

Recommended Answers

All 5 Replies

Consult WikiPedia. It shows Booth's Algorithm in minute detail, and gives an example.

Your code should have several subroutines:
- one to display a binary number
- one to do Booth's algorithm and return the product

To display a binary number, just peel bits off of one end and put '0' or '1' into a character string as appropriate. Then print the string.

To input and output a decimal number, just use syscall. Here's a sample to get you started.

str_please_enter_int:	.asciiz "Please enter an integer: "
str_you_entered_int:	.asciiz "You entered: "

sys_print_int:		.word 1
sys_print_string:	.word 4
sys_read_int:		.word 5
sys_exit:		.word 10

i:			.word

main:	# ask for an integer
	lw  $2,  print_string
	la  $a0, str_please_enter_int
	syscall

	# get integer --> i
	lw  $2,  sys_read_int
	syscall
	la  $t0, i
	sw  $2,  ($t0)

	# Tell the user what the integer was
	lw  $2,  sys_print_string
	la  $a0, str_you_entered_int
	syscall

	lw  $a0, i
	syscall

	# All done
	lw  $2,  sys_exit
	syscall

Hope this helps.

Thank you very much up to this point. This is what my code looks like. When I try to print I get no result.

.text
.globl main
main:

li $v0, 4 #Enter multiplicant
la $a0, msg1
syscall
li $v0, 5
syscall
move $a0, $v0

li $v0, 4 #Enter multiplier
la $a0, msg2
syscall
li $v0, 5
syscall
move $a1, $v0

li $v0, 0
li $t1, 0
li $t0, 12 # twelve bits
algorithm:
and $t2, $a0, 24 #24 bit mask
sll $t2, $t2, 1
or $t2, $t2, $t1
beq $t2, 2, check10
beq $t2, 1, check01
b shift

check10: sub $v0, $v0, $a1
b shift

check01: add $v0, $v0, $a1

shift:
and $t1, $a0, 16 #16 bit mask
and $t2, $v0, 8 #8 bit mas
sll $t2, $t2, 31
srl $a0, $a0, 1
or $a0, $a0, $t2
sra $v0, $v0, 1
sub $t0,$t0, 1
bnez $t0, algorithm

# Printing the 8 bits results ($v0,$a0)
li $v0, 4
la $a0, msg3
syscall
li $v0,1
move $v0, $a0
syscall

.data
msg1: .asciiz"\nEnter multiplicand"
msg2: .asciiz"\nEnter multiplier"
msg3: .asciiz"\nThe Eight bits result is"

I'll take a better look over all your code in a minute, but when you print you accidentally swapped your source and destination in the move command. move $v0, $a0 should be move $a0, {whatever register has the number to print} Hope this helps.

I was trying to print the values in both register $v0 and $a0 - the result of the multiplication.

I'm looking your code over and I'm going to stop now. You need to get out a piece of paper and write down what your various registers values mean.

For example, you ask for an integer, get it and put it in $a0. Two lines later you completely clobber it with the address of the next message to print --you've lost the value you asked for!

MIPS processors have a ton of registers. Pick something that isn't likely to be used elsewhere, like $s0..$s7. When you need a temporary value, use $t0..$7. Only use $a0..$a3 when you need to (say, to read or print an integer).

Once you have your list, go through your code and make sure each register only has the value it is supposed to.

Booth's Algorithm has three values that are used: A, S, and P. You could say
A = $s0
S = $s1
P = $s2
Thereafter, $s0 should only contain the value appropriate to A. Etc.

Hope this helps.

[EDIT] Response to your last post. You can only print one at a time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.