Polynomial Functions for a Linear Feedback Shift Register
# Problem 1 code
# Function to compute the next state of an LFSR
.data
title: .asciiz "Problem 1.\n"
message: .asciiz "\nLFSR output state for the input "
state1: .asciiz "0x00000001: "
state2: .asciiz "0xdeadbeef: "
state3: .asciiz "0x400439a0: "
state4: .asciiz "0x00000000: "
.code
.globl main
# main function
main:
la $a0,title
syscall $print_string
li $a0,0x00000001 # testing start state 0x00000001
jal LFSR
la $a0,message
syscall $print_string
la $a0,state1
syscall $print_string
move $a0,$v0 # move output lfsr state to a0 for printing
syscall $print_int
li $a0,0xdeadbeef # testing start state 0xdeadbeef
jal LFSR
la $a0,message
syscall $print_string
la $a0,state2
syscall $print_string
move $a0,$v0 # move output lfsr state to a0 for printing
syscall $print_int
li $a0,0x400439a0 # testing start state 0x400439a0
jal LFSR
la $a0,message
syscall $print_string
la $a0,state3
syscall $print_string
move $a0,$v0 # move output lfsr state to a0 for printing
syscall $print_int
li $a0,0x00000000 # testing start state 0x00000000
jal LFSR
la $a0,message
syscall $print_string
la $a0,state4
syscall $print_string
move $a0,$v0 # move output lfsr state to a0 for printing
syscall $print_int
syscall $exit
# LFSR function with the taps:
# x^24+x^23+x^21+x^20+1
# the input is taken from $a0
# the output is in $v0
LFSR:
li $t0,0x00D80000 # mask to use for the taps
andi $t1,$a0,1 # test LSB
srl $v0,$a0,1 # lfsr >>1
beqz $t1,skip
xor $v0,$v0,$t0 # xor tap mask
skip:
jr $ra # return to the caller
# Problem 2 code
# Program to compute the number of states of an LFSR
.data
title: .asciiz "Problem 2.\n"
result: .asciiz "\nNumber of states: "
.code
.globl main
# main function
main:
la $a0,title
syscall $print_string
li $s0,0 # count=0
li $s1,0x00000001 # initial=0x00000001
move $a0,$s1 # state=initial
do: # do {
jal LFSR
move $a0,$v0 # state=LFSR(state)
addi $s0,$s0,1 # count ++
bne $a0,$s1,do # } while (state!=initial)
la $a0,result
syscall $print_string
move $a0,$s0 # move resulting count to a0 for printing
syscall $print_int
syscall $exit
# LFSR function with the taps:
# x^24+x^23+x^21+x^20+1
# the input is taken from $a0
# the output is in $v0
LFSR:
li $t0,0x00D80000 # mask to use for the taps
andi $t1,$a0,1 # test LSB
srl $v0,$a0,1 # lfsr >>1
beqz $t1,skip
xor $v0,$v0,$t0 # xor tap mask
skip:
jr $ra # return to the caller
# Problem 2 code
# Program that allows the user to guess a random number 7 times
.data
title: .asciiz "Problem 3.\n"
prompt1: .asciiz "\nPlease enter a name: "
prompt2: .asciiz "\nPlease enter your guess for the random number: "
win1: .asciiz "Congratulations "
win2: .asciiz ", you guessed the number correctly!\n"
toohigh: .asciiz "Your guess was too high "
toolow: .asciiz "Your guess was too low "
ending: .asciiz "!\n"
fail1: .asciiz "\nYou have used all seven guesses "
fail2: .asciiz "!\nThe random number is: "
name: .space 100
.code
.globl main
# main function
main:
la $a0,title
syscall $print_string
la $a0,prompt1 # 1. ask for a name
syscall $print_string
la $a0,name # point to place to read string
li $a1,100 # max number of chars to read
syscall $read_string # read name from user
syscall $random # 2. call random and set as the initial state
move $s1,$v0
# 3. generate a number between 0 and 127
li $s0,7 # count=7 for repeating 7 times
1: move $a0,$s1 # set initial state
jal LFSR # generate a random number
move $s1,$v0 # use the output as input for next cycle
addi $s0,$s0,-1 # decrement count
bnez $s0,1b # repeat while it's not zero
andi $s1,$s1,127 # restrict to 0-127
#4. repeat for 7 guesses
li $s0,7 # count=7 for trying 7 times
2:
la $a0,prompt2 # ask for a guess
syscall $print_string
syscall $read_int
beq $v0,$s1,win # if the user guessed the number, he wins
bgt $v0,$s1,high # see if it was higher than the random number
low:
la $a0,toolow # it was too low
syscall $print_string
b 3f
high:
la $a0,toohigh # it was too high
syscall $print_string
3:
la $a0,name # print user's name
syscall $print_string
la $a0,ending # print ending !
syscall $print_string
addi $s0,$s0,-1 # decrement count
bnez $s0,2b # repeat while it's not zero
# 5. tell user 7 guesses were used and print number
la $a0,fail1 # print all guesses have been used
syscall $print_string
la $a0,name # print user's name
syscall $print_string
la $a0,fail2 # print the random number message
syscall $print_string
move $a0,$s1 # print the random number
syscall $print_int
b end
win:
la $a0,win1 # 4a. user wins, congratulate him
syscall $print_string
la $a0,name
syscall $print_string
la $a0,win2
syscall $print_string
end:
syscall $exit
# LFSR function with the taps:
# x^24+x^23+x^21+x^20+1
# the input is taken from $a0
# the output is in $v0
LFSR:
li $t0,0x00D80000 # mask to use for the taps
andi $t1,$a0,1 # test LSB
srl $v0,$a0,1 # lfsr >>1
beqz $t1,skip
xor $v0,$v0,$t0 # xor tap mask
skip:
jr $ra # return to the caller