DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

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

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 4th September 2008
enpey enpey is offline
Port Guard
 
Join Date: May 2008
Location: Newcastle, Australia
Posts: 33
Thanked 2 Times in 2 Posts
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...

Code:
	.data
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

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

	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
a20:
	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
a30:	
        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

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

	li	$v0, 4
	la	$a0, msg2
	syscall

	jr $ra
I have used some
Code:
	li	$v0, 1
	add	$a0, $t4, $zero
	syscall
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
  #2   (View Single Post)  
Old 4th September 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

i suppose you are trying to do:
input: 0xabcde00f (2882396175)
result: 0xf007b3d5 (-267930667) ?

try:
Code:
.data
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

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

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

	move	$t2, $v0	# input from user in $t2
	lw	$t0, wurd	# $t0 = 00000000 00000000 00000000 00000001
				#  this is the mask
	lw	$t5, wurd2	# $t1 = 00000000 00000000 00000000 00000010
				#  this is the variable maintaining shift amount
	add	$s0, $zero, $zero # $s0 = 0000...0000
	
a10:	move	$t1, $t5
	and	$t4, $t2, $t0	# masking to isolate the bit we want to move
				#  our isolated bit is in $t4
a20:
	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
a30:	
        or	$s0, $s0, $t4	# add flipped bit to output
	sll	$t0, $t0, 1
	sll	$t5, $t5, 2	# starts one further left, and needs to go
				#  one less to the left

	beq	$t5, $zero, a40 # now need to shift bits right
	b	a10
	
a40:	li	$t5, 0x01	# reset shift counter
a50:	move	$t1, $t5
	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	$t5, $t5, 2	# same as above
	beq	$t5, $zero, a80	# finished flipping bits
	b	a50		# loop again

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

	li	$v0, 4
	la	$a0, msg2
	syscall

	jr $ra
(btw, my knowledge of mips is limited to the wiki i read half an hour ago so i could be off the mark )

Last edited by ephemera; 4th September 2008 at 06:52 PM.
Reply With Quote
  #3   (View Single Post)  
Old 4th September 2008
enpey enpey is offline
Port Guard
 
Join Date: May 2008
Location: Newcastle, Australia
Posts: 33
Thanked 2 Times in 2 Posts
Default

Haha yes thanks ephemera, that is exactly what I was trying to do.

I thought I had put the copy of $t1 to move the bit, but I must have taken it out.

Anyhow, thanks again ephemera
Reply With Quote
Reply

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 02:38 PM.


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