View Single Post
  #2   (View Single Post)  
Old 4th September 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
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