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...