Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1   (View Single Post)  
Old 4th September 2008
enpey enpey is offline
Port Guard
Join Date: May 2008
Location: Newcastle, Australia
Posts: 33
Default Flipping the bit sequence in MIPS

Hi all,

I am trying to flip the bit sequence of an integer, however I am having a bit of trouble. I am using the SPIM simulator on FreeBSD 7.0 (amd64).
I know I need to save the $s0 register and some of my comments are not the best, but this is what I have coded up so far...

msg1:	.ascii	"\nWelcome to the bit flipper!"
	.asciiz	"\nEnter an integer to start: "
msg2:	.asciiz	"\n\nFinished SPIM simulation\n"
wurd:	.word	0x01
wurd2:	.word	0x02

	li	$v0, 4		# syscall code for print_string
	la	$a0, msg1	# $a0 takes address of message to print

	li	$v0, 5		# syscall code for read_int
	syscall			# integer loaded into $v0

	move	$t2, $a0	# input from user in $t2
	lw	$t0, wurd	# $t0 = 00000000 00000000 00000000 00000001
				#  this is the mask
	lw	$t1, wurd2	# $t1 = 00000000 00000000 00000000 00000010
				#  this is the variable maintaining shift amount
	add	$s0, $zero, $zero # $s0 = 0000...0000
a10:	and	$t4, $t2, $t0	# masking to isolate the bit we want to move
				#  our isolated bit is in $t4
	sll	$t4, $t4, 1	# move the isolated bit left one
	sll	$t1, $t1, 1	# this is used to check shift amount
	beq   $t1, $zero, a30	# shifted left enough
	b	a20
        or	$s0, $s0, $t4	# add flipped bit to output
	sll	$t0, $t0, 1
	sll	$t1, $t1, 2	# starts one further left, and needs to go
				#  one less to the left

	beq	$t1, $zero, a40 # now need to shift bits right
	b	a10
a40:	li	$t1, 0x01	# reset shift counter
a50:	and	$t4, $t2, $t0	# masking to isolate the bit we want to move
				#  our isolated bit is in $t4
a60:	srl	$t4, $t4, 1	# move the isolated bit right one
	srl	$t1, $t1, 1	# shift amount counter
	beq	$t1, $zero, a70	# shifted right enough
	b	a60
a70:	or	$s0, $s0, $t4	# add flipped bit to output
	sll	$t0, $t0, 1
	sll	$t1, $t1, 2	# same as above
	beq	$t1, $zero, a80	# finished flipping bits
	b	a50		# loop again

	li	$v0, 1		# syscall code for print_int
	add	$a0, $s0, $zero	# $a0 is flipped bit sequence

	li	$v0, 4
	la	$a0, msg2

	jr $ra
I have used some
	li	$v0, 1
	add	$a0, $t4, $zero
blocks to see what is happening, and it seems not to loop at all through it. All the numbers I have input give me an output of "0".

Any help would be greatly appreciated as I am unsure of what is happening...
Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT. The time now is 08:34 PM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick