Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- inputstring: .space 1001 #define MAX_CHARS 1001
- #char input_sentence[MAX_CHARS];
- outputstring: .space 51 ##define MAX_WORD_LENGTH 51
- #char word1[MAX_WORD_LENGTH];
- outputstring1: .space 51 #char word[MAX_WORD_LENGTH];
- inputstring1: .space 1001 #char word_array[MAX_CHARS];
- uniquewords: .space 501 ##define MAX_UNIQUE_STRINGS 501
- #int word_count[MAX_UNIQUE_STRINGS];
- inp: .asciiz "\ninput: "
- newl: .asciiz "\n"
- nullch: .asciiz "\0"
- .text
- #global vars:
- #$s0 - input string
- #$s1 - char_index/word
- #$s2 - word_count
- #$s3 - word1
- #$s4 - uniques_char_counter
- #$s5 - word_array
- #$s6 - next_unique_counter
- #$s7 - is_eos
- main:
- add $s7, $0, $0 #is_eos = 0;
- la $s3, outputstring #char word1[MAX_WORD_LENGTH]; set $s3 to outputstring address
- la $s5, inputstring1 #char word_array[MAX_CHARS]; set $s4 to inputstring1 address
- la $s2, uniquewords #int word_count[MAX_UNIQUE_STRINGS]; set $s2 to uniquewords address
- add $s4, $0, $0 #uniques_char_counter = 0; set %s4 to 0
- add $s1, $0, $0 #int char_index = 0; set $s1 to 0
- add $s6, $0, $0 #next_unique_counter = 0; set $s6 to 0
- jal read_input
- search:
- jal process_input #start searching for words
- read_input: #read_input(input_sentence);
- li $v0, 4
- la $a0, inp
- syscall #print inp
- li $v0, 8
- li $a1, 1001
- la $a0, inputstring
- syscall #save input into $a0
- move $s0, $a0 #move inputstring to $s0
- jr $ra
- output:
- add $t6, $0, $0 #int x = 0;
- add $t5, $0, $0 #int counter = 0;
- add $t0, $s6, $0 #num_unique_words = next_unique_counter;
- addi $t1, $0, -1 #max_frequency = -1;
- add $t2, $0, $0 #num_words_with_max_frequency = 0;
- la $s1, outputstring1 #char word[MAX_WORD_LENGTH];
- la $s5, inputstring1 #char word_array[MAX_CHARS]
- la $s2, uniquewords #int word_count[MAX_UNIQUE_STRINGS]
- beq $t0, $0, printoutput #if num_uunique_words == 0, branch to printoutput
- outputloop:
- lb $t9, ($s2)
- beq $t9, $t1, else2 #if(word_count[x] == max_frequency){
- blt $t9, $t1, donothing2 #if(word_count[x] < max_frequency){
- lb $t1, ($s2) #max_frequency = word_count[x]
- addi $t2, $0, 1 #num_words_with_max_frequency = 1;
- add $t7, $0, $0 #int y = 0;
- la $s1, outputstring1 #reset the address of $s1 to outputstring1
- resetword: #clears our word
- lb $t9, nullch # word[y] = '\0';
- sb $t9, ($s1)
- addi $t7, $t7, 1 #y++;
- addi $s1, $s1, 1 #move $s1 to next address
- bge $t7, 50, resetword #while(y < MAX_WORD_LENGTH){
- la $s1, outputstring1 #reset the address of $s1 to outputstring1
- add $t7, $0, $0 #y = 0;
- la $s5, inputstring1 #reset the address of $s5 to inputstring1
- writeword: #while(y < uniques_char_counter);writes the xth (value of $t6) unique string into word ($s1)
- lb $t4, ($s5)
- bne $t4, '\0', donothing1 # if(word_array[y] == '\0'){
- addi $t5, $t5, 1 #counter++;
- addi $s5, $s5, 1 #y++
- addi $t7, $t7, 1
- donothing1:
- bne $t5, $t6, donothing3 #if(counter == x){
- la $s1, outputstring1
- loopwrite: #while(word_array[y] != '\0')
- lb $t9, ($s5) #word[z] = word_array[y];
- sb $t9, ($s1) #saves value of the address $s5 to the address of $1
- addi $s5, $s5, 1 #y++;
- addi $t7, $t7, 1
- addi $s1, $s1, 1 #z++;
- lb $t8, ($s5)
- bne $t8, '\0', loopwrite #while(word_array[y] != '\0'); proceeds to write next character if not at end of the word
- addi $t5, $t5,1 #counter++;
- donothing3:
- addi $t7, $t7, 1 #y++
- addi $s5, $s5, 1 #moves word and word_array to next address
- addi $s1, $s1, 1
- blt $t7, $s4, writeword #while(y < uniques_char_counter); continues looping until we reach the word position we're looking for
- j donothing2
- else2: #else if(word_count[x] == max_frequency)
- lb $t8, ($s2) #set $t8 to current value of $s2
- bne $t8, $t1, donothing2
- addi $t2, $t2, 1 #num_words_with_max_frequency++; increases ($s2) == $t1
- donothing2:
- add $t5, $0, $0 #counter = 0;
- addi $t6, $t6, 1 #x++;
- addi $s2, $s2, 1
- bne $t0, $t6, outputloop #while(x < num_unique_words){
- printoutput:
- la $s1, outputstring1
- addi $sp, $sp, -4 #store address of word on stack
- sw $s1, ($sp)
- addi $sp, $sp, -4 #store num_words_with_max_frequency on stack
- sw $t2, ($sp)
- addi $sp, $sp, -4 #store max_frequency on stack
- sw $t1, ($sp)
- addi $sp, $sp, -4 #save num_unique_words on stack
- sw $t0, ($sp)
- lw $a0, ($sp) #print outputs
- addi $sp, $sp, 4 #loads num_unique_words from stack and prints it out
- li $v0, 1
- syscall
- jal printnewline
- lw $a0, ($sp)
- addi $sp, $sp, 4 #loads max_frequency from stack and prints it out
- li $v0, 1
- syscall
- jal printnewline
- lw $a0, ($sp)
- addi $sp, $sp, 4 #loads num_words_with_max_frequency from stack and prints it out
- li $v0, 1
- syscall
- jal printnewline
- lw $t0, ($sp)
- la $a0, ($t0)
- addi $sp, $sp, 4 #loads address of word from stack and prints the word
- li $v0, 4
- syscall
- j clear_addresses
- clear_addresses: #clears out all addresses allocated in outputstring, inputstring, uniquewords
- add $t0, $0, $0 #int x = 0;
- la $t1, outputstring
- word1_clear: #while(x<MAX_WORD_LENGTH){
- lb $t9, nullch
- sb $t9, ($t1) #word1[x] = '\0';
- addi $t0, $t0, 1 #x++;
- addi $t1, $t1, 1
- bne $t0, 50, word1_clear
- add $t0, $0, $0 #x = 0;
- la $t1, inputstring1
- word_array_clear: #while(x<MAX_CHARS){
- lb $t9, nullch
- sb $t9, ($t1) #word_array[x] = '\0';
- addi $t0, $t0, 1 #x++;
- addi $t1, $t1, 1
- bne $t0, 1000, word_array_clear
- add $t0, $0, $0 #x = 0;
- la $t1, uniquewords
- word_count_clear: # while(x<MAX_UNIQUE_STRINGS){
- lb $t9, nullch
- sb $t9, ($t1) #word_count[x] = 0;
- addi $t0, $t0, 1 #x++;
- addi $t1, $t1, 1
- bne $t0, 500, word_count_clear
- add $t0, $0, $0 #x = 0;
- la $t1, outputstring1
- resetword1: #while(x<MAX_WORD_LENGTH){
- lb $t9, nullch
- sb $t9, ($t1) #word[x] = 0;
- addi $t0, $t0, 1 #x++;
- addi $t1, $t1, 1
- bne $t0, 50, resetword1
- j main
- printnewline: #prints new line
- la $a0, newl
- li $v0, 4
- syscall
- jr $ra
- process_input:
- addi $sp, $sp, -4
- sw $ra, 0($sp) #store return address on stack
- add $t3, $0, $0 #int input_index = 0;
- lb $t4, nullch #char cur_char = '\0'; set $t3 to nullch
- returnuniq:
- add $t2, $0, $0 #int word_found = 0; set $t2 to 0
- add $t1, $0, $0 #int is_delim_ch = 0; set $t1 to 0
- add $s1, $0, $0 #set char_index to 0
- la $s3, outputstring #set index of $s3 back to start
- while_pi: #while( end_of_sentence == 0) {
- bne $s7, $0, output #branch to output if eos != 0
- lb $t3, ($s0) #cur_char = inp[input_index]; take byte at $s0 and save into $t3
- addi $sp, $sp, -1
- sb $t3, 0($sp) #put the byte on the stack
- jal is_delimiting_char #is_delim_ch = is_delimiting_char(cur_char); checks if it's delimiting char
- returndel: #if ( is_delim_ch == 1 ); return label for is_deliminatng_char
- beq $s1, $0, skip #if char_index == 0, skip word found increment
- addi $t2, $t2, 1 #increment word_found ($t2)
- j skip
- else1: sb $t3, ($s3) #word1[char_index] = cur_char; store char in $t3 address of $s3
- addi $s1, $s1, 1 #char_index++; increment char_index
- addi $s3, $s3, 1 #increment address $s3 - has the value of outputstring address
- skip:
- addi $s0, $s0, 1 #increment input string
- bne $t2, $0, unique_start #if(word_found == 1); branch to unique_start if word_found != 0
- j while_pi #jump to start of loop
- unique_start:
- lb $t9, nullch #word1[char_index] = '\0'; set current index of word1 to nullch
- sb $t9, ($s3)
- addi $s1, $s1, 1 #char_index++; increment char_index
- j process_unique #process_unique(char_index);
- process_unique:
- add $t5, $0, $0 #int unique_location_index = 0
- add $t6, $0, $0 #int counter = 0;
- la $s3, outputstring #set index of $s3 back to start
- la $s5, inputstring1 #set index of $s5 back to start
- la $s2, uniquewords #set index of $s2 back to start
- beq $s4, $0, add_unique #if(uniques_char_counter == 0) branch to add_unique
- compare: #while(counter < uniques_char_counter){
- lb $t8, ($s3)
- lb $t9, ($s5)
- bne $t8,$t9, notsamechar # if(word_array[counter] == word1[counter1]){
- bne $t9, '\0', donothing # if(word_array[counter] == '\0'){
- add $s2, $s2, $t5 #word_count[unique_location_index]++;
- lb $t7, ($s2)
- addi $t7, $t7, 1
- sb $t7, ($s2)
- j returnuniq #return 0;
- donothing:
- addi $s3, $s3, 1 #counter++;
- addi $t6, $t6, 1
- addi $s5, $s5, 1 #counter1++;
- j compare
- notsamechar: #else{
- la $s3, outputstring #counter1 = 0;
- addi $t5, $t5, 1 #unique_location_index++;
- lb $t9, ($s5)
- bne $t9, '\0', nextword
- NWret:
- addi $s5, $s5, 1 #counter++;
- addi $t6, $t6, 1 #increment $t6 by 1
- bne $s4, $t6, compare #check if while is still satisfied
- j add_unique #add_unique(index);
- nextword: #while(word_array[counter] != '\0'){
- addi $s5, $s5, 1 #counter++;
- addi $t6, $t6, 1 #nextword moves the word_array to the next word
- lb $t8, ($s5)
- beq $t8, '\0', NWret
- j nextword
- add_unique:
- add $t6, $0, $0 #int x = 0;
- la $s5, inputstring1 #reset word_array to its start address
- la $s3, outputstring #reset word1 to its start address
- la $s2, uniquewords #reset word_count to its start address
- add $s5, $s5, $s4
- add_word:
- lb $t8, ($s3) #word_array[uniques_char_counter] = word1[x]
- sb $t8, ($s5)
- addi $t6, $t6, 1 #x++;
- addi $s4, $s4, 1 #uniques_char_counter++;
- addi $s3, $s3, 1
- addi $s5, $s5, 1
- bne $t6, $s1, add_word #while(x < index){
- add $s2, $s2, $s6 #word_count[next_unique_counter] = 1;
- addi $t8, $0, 1
- sb $t8, ($s2)
- addi $s6, $s6, 1 #next_unique_counter ++;
- j returnuniq
- is_delimiting_char: #checks if top stack value is delimiting char
- lb $t5, 0($sp) #store byte passed by while_pi in $t5
- addi $sp,$sp,1
- beq $t5, ' ', returndel #if ( ch == ' ') { return 1; }
- beq $t5, '.', returndel #else if ( ch == '.') { return 1; }
- beq $t5, '!', returndel #else if ( ch == '!') { return 1; }
- beq $t5, '?', returndel #else if ( ch == '?') { return 1; }
- beq $t5, '_', returndel #else if ( ch == '_') { return 1; }
- beq $t5, '-', returndel #else if ( ch == '-') { return 1; }
- beq $t5, '(', returndel #else if ( ch == '(') { return 1; }
- beq $t5, ')', returndel #else if ( ch == ')') { return 1; }
- beq $t5, ',', returndel #else if ( ch == ',') { return 1; }
- beq $t5, '\0', returndel #else if ( ch == '\0') { return 1; }
- addi $sp, $sp, -4
- sw $ra, 0($sp) #save $ra on stack
- beq $t5, '\n', is_eos #else if ( ch == '\n') { return 1; }
- #if newl, go to is_eos
- addi $sp, $sp, 4
- j else1 #if not delimiting, go to else1
- is_eos: #set end_of_string ($s2) to 1
- lw $ra, 0($sp)
- addi $sp, $sp, 4
- addi $s7, $s7, 1
- jr $ra #jump to return address, obtained from the stack
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement