Advertisement
Guest User

Untitled

a guest
Jan 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.        .data
  2.         inputstring:    .space 1001     #define MAX_CHARS 1001
  3.                                        #char input_sentence[MAX_CHARS];
  4.         outputstring:   .space 51       ##define MAX_WORD_LENGTH 51
  5.                                        #char word1[MAX_WORD_LENGTH];
  6.         outputstring1:  .space 51       #char word[MAX_WORD_LENGTH];
  7.         inputstring1:   .space 1001     #char word_array[MAX_CHARS];
  8.         uniquewords:    .space 501      ##define MAX_UNIQUE_STRINGS 501
  9.                                        #int word_count[MAX_UNIQUE_STRINGS];
  10.        
  11.         inp:            .asciiz "\ninput: "
  12.         newl:           .asciiz "\n"
  13.         nullch:         .asciiz "\0"
  14.  
  15.         .text
  16.  
  17. #global vars:
  18. #$s0 - input string
  19. #$s1 - char_index/word
  20. #$s2 - word_count
  21. #$s3 - word1
  22. #$s4 - uniques_char_counter
  23. #$s5 - word_array
  24. #$s6 - next_unique_counter
  25. #$s7 - is_eos
  26.  
  27. main:
  28.         add $s7, $0, $0                 #is_eos = 0;
  29.         la $s3, outputstring            #char word1[MAX_WORD_LENGTH]; set $s3 to outputstring address
  30.         la $s5, inputstring1            #char word_array[MAX_CHARS]; set $s4 to inputstring1 address
  31.         la $s2, uniquewords             #int word_count[MAX_UNIQUE_STRINGS]; set $s2 to uniquewords address
  32.         add $s4, $0, $0                 #uniques_char_counter = 0; set %s4 to 0
  33.         add $s1, $0, $0                 #int char_index = 0; set $s1 to 0
  34.         add $s6, $0, $0                 #next_unique_counter = 0; set $s6 to 0
  35.         jal read_input
  36. search:
  37.         jal process_input               #start searching for words
  38.  
  39.        
  40. read_input:                             #read_input(input_sentence);
  41.         li $v0, 4
  42.         la $a0, inp                    
  43.         syscall                         #print inp
  44.         li $v0, 8
  45.         li $a1, 1001
  46.         la $a0, inputstring
  47.         syscall                         #save input into $a0
  48.         move $s0, $a0                   #move inputstring to $s0
  49.         jr $ra
  50.  
  51. output:
  52.         add $t6, $0, $0                 #int x = 0;
  53.         add $t5, $0, $0                 #int counter = 0;
  54.         add $t0, $s6, $0                #num_unique_words = next_unique_counter;
  55.         addi $t1, $0, -1                #max_frequency = -1;
  56.         add $t2, $0, $0                 #num_words_with_max_frequency = 0;
  57.         la $s1, outputstring1           #char word[MAX_WORD_LENGTH];
  58.         la $s5, inputstring1            #char word_array[MAX_CHARS]
  59.         la $s2, uniquewords             #int word_count[MAX_UNIQUE_STRINGS]
  60.         beq $t0, $0, printoutput        #if num_uunique_words == 0, branch to printoutput
  61.         outputloop:
  62.                 lb $t9, ($s2)
  63.                 beq $t9, $t1, else2             #if(word_count[x] == max_frequency){
  64.                 blt $t9, $t1, donothing2        #if(word_count[x] < max_frequency){
  65.                         lb $t1, ($s2)                   #max_frequency = word_count[x]
  66.                         addi $t2, $0, 1                 #num_words_with_max_frequency = 1;
  67.                        
  68.                         add $t7, $0, $0                 #int y = 0;
  69.                         la $s1, outputstring1           #reset the address of $s1 to outputstring1
  70.                         resetword:                      #clears our word
  71.                                 lb $t9, nullch          # word[y] = '\0';
  72.                                 sb $t9, ($s1)
  73.                                 addi $t7, $t7, 1        #y++;
  74.                                 addi $s1, $s1, 1        #move $s1 to next address
  75.                                 bge $t7, 50, resetword  #while(y < MAX_WORD_LENGTH){
  76.                         la $s1, outputstring1     #reset the address of $s1 to outputstring1
  77.                         add $t7, $0, $0           #y = 0;
  78.                         la $s5, inputstring1      #reset the address of $s5 to inputstring1
  79.                         writeword:                #while(y < uniques_char_counter);writes the xth (value of $t6) unique string into word ($s1)
  80.                                 lb $t4, ($s5)
  81.                                 bne $t4, '\0', donothing1       # if(word_array[y] == '\0'){
  82.                                         addi $t5, $t5, 1        #counter++;
  83.                                         addi $s5, $s5, 1        #y++
  84.                                         addi $t7, $t7, 1
  85.                         donothing1:
  86.                                 bne $t5, $t6, donothing3        #if(counter == x){
  87.                                 la $s1, outputstring1
  88.                                         loopwrite:              #while(word_array[y] != '\0')
  89.                                                 lb $t9, ($s5)                   #word[z] = word_array[y];
  90.                                                 sb $t9, ($s1)                   #saves value of the address $s5 to the address of $1
  91.                                                 addi $s5, $s5, 1                #y++;
  92.                                                 addi $t7, $t7, 1                
  93.                                                 addi $s1, $s1, 1                #z++;
  94.                                                 lb $t8, ($s5)
  95.                                                 bne $t8, '\0', loopwrite        #while(word_array[y] != '\0'); proceeds to write next character if not at end of the word
  96.                                         addi $t5, $t5,1                         #counter++;
  97.                         donothing3:
  98.                                 addi $t7, $t7, 1        #y++
  99.                                 addi $s5, $s5, 1        #moves word and word_array to next address
  100.                                 addi $s1, $s1, 1
  101.                         blt $t7, $s4, writeword         #while(y < uniques_char_counter); continues looping until we reach the word position we're looking for
  102.                         j donothing2
  103.                 else2:                                  #else if(word_count[x] == max_frequency)
  104.                         lb $t8, ($s2)                           #set $t8 to current value of $s2
  105.                         bne $t8, $t1, donothing2
  106.                         addi $t2, $t2, 1                        #num_words_with_max_frequency++; increases ($s2) == $t1
  107.                 donothing2:    
  108.                         add $t5, $0, $0                         #counter = 0;
  109.                         addi $t6, $t6, 1                        #x++;
  110.                         addi $s2, $s2, 1
  111.         bne $t0, $t6, outputloop                        #while(x < num_unique_words){
  112. printoutput:
  113.         la $s1, outputstring1
  114.         addi $sp, $sp, -4               #store address of word on stack
  115.         sw $s1, ($sp)                  
  116.         addi $sp, $sp, -4               #store num_words_with_max_frequency on stack
  117.         sw $t2, ($sp)
  118.         addi $sp, $sp, -4               #store max_frequency on stack
  119.         sw $t1, ($sp)
  120.         addi $sp, $sp, -4               #save num_unique_words on stack
  121.         sw $t0, ($sp)
  122.        
  123.         lw $a0, ($sp)                   #print outputs
  124.         addi $sp, $sp, 4                #loads num_unique_words from stack and prints it out
  125.         li $v0, 1
  126.         syscall
  127.         jal printnewline
  128.        
  129.         lw $a0, ($sp)
  130.         addi $sp, $sp, 4                #loads max_frequency from stack and prints it out
  131.         li $v0, 1
  132.         syscall
  133.         jal printnewline
  134.        
  135.         lw $a0, ($sp)
  136.         addi $sp, $sp, 4                #loads num_words_with_max_frequency from stack and prints it out
  137.         li $v0, 1
  138.         syscall
  139.         jal printnewline
  140.        
  141.         lw $t0, ($sp)
  142.         la $a0, ($t0)
  143.         addi $sp, $sp, 4                #loads address of word from stack and prints the word
  144.         li $v0, 4
  145.         syscall
  146.        
  147.         j clear_addresses
  148.  
  149. clear_addresses:                                        #clears out all addresses allocated in outputstring, inputstring, uniquewords
  150.         add $t0, $0, $0                                 #int x = 0;
  151.         la $t1, outputstring                            
  152.         word1_clear:                                    #while(x<MAX_WORD_LENGTH){
  153.                 lb $t9, nullch  
  154.                 sb $t9, ($t1)                           #word1[x] = '\0';
  155.                 addi $t0, $t0, 1                        #x++;
  156.                 addi $t1, $t1, 1
  157.                 bne $t0, 50, word1_clear
  158.         add $t0, $0, $0                                 #x = 0;
  159.         la $t1, inputstring1
  160.         word_array_clear:                               #while(x<MAX_CHARS){
  161.                 lb $t9, nullch  
  162.                 sb $t9, ($t1)                           #word_array[x] = '\0';
  163.                 addi $t0, $t0, 1                        #x++;
  164.                 addi $t1, $t1, 1
  165.                 bne $t0, 1000, word_array_clear
  166.         add $t0, $0, $0                                 #x = 0;
  167.         la $t1, uniquewords
  168.         word_count_clear:                               # while(x<MAX_UNIQUE_STRINGS){
  169.                 lb $t9, nullch  
  170.                 sb $t9, ($t1)                           #word_count[x] = 0;
  171.                 addi $t0, $t0, 1                        #x++;
  172.                 addi $t1, $t1, 1
  173.                 bne $t0, 500, word_count_clear
  174.         add $t0, $0, $0                                 #x = 0;
  175.         la $t1, outputstring1
  176.         resetword1:                                     #while(x<MAX_WORD_LENGTH){
  177.                 lb $t9, nullch          
  178.                 sb $t9, ($t1)                           #word[x] = 0;
  179.                 addi $t0, $t0, 1                        #x++;
  180.                 addi $t1, $t1, 1
  181.                 bne $t0, 50, resetword1
  182.         j main
  183.        
  184. printnewline:                           #prints new line
  185.         la $a0, newl
  186.         li $v0, 4
  187.         syscall                
  188.         jr $ra
  189.                
  190. process_input:                  
  191.         addi $sp, $sp, -4
  192.         sw $ra, 0($sp)                  #store return address on stack
  193.         add $t3, $0, $0                 #int input_index = 0;
  194.         lb  $t4, nullch                 #char cur_char = '\0'; set $t3 to nullch
  195. returnuniq:
  196.         add $t2, $0, $0                 #int word_found = 0; set $t2 to 0
  197.         add $t1, $0, $0                 #int is_delim_ch = 0; set $t1 to 0
  198.         add $s1, $0, $0                 #set char_index to 0
  199.         la $s3, outputstring            #set index of $s3 back to start
  200.         while_pi:                       #while( end_of_sentence == 0) {
  201.                         bne $s7, $0, output             #branch to output if eos != 0
  202.                         lb $t3, ($s0)                   #cur_char = inp[input_index]; take byte at $s0 and save into $t3
  203.                         addi $sp, $sp, -1              
  204.                         sb $t3, 0($sp)                  #put the byte on the stack
  205.                         jal is_delimiting_char          #is_delim_ch = is_delimiting_char(cur_char); checks if it's delimiting char
  206.                 returndel:                              #if ( is_delim_ch == 1 ); return label for is_deliminatng_char
  207.                         beq $s1, $0, skip               #if char_index == 0, skip word found increment
  208.                         addi $t2, $t2, 1                #increment word_found ($t2)
  209.                         j skip  
  210.                 else1:  sb $t3, ($s3)                   #word1[char_index] = cur_char; store char in $t3 address of $s3
  211.                         addi $s1, $s1, 1                #char_index++; increment char_index
  212.                         addi $s3, $s3, 1                #increment address $s3 - has the value of outputstring address
  213.                 skip:  
  214.                         addi $s0, $s0, 1                #increment input string
  215.                         bne $t2, $0, unique_start       #if(word_found == 1); branch to unique_start if word_found != 0
  216.         j while_pi                                      #jump to start of loop
  217.  
  218. unique_start:
  219.         lb $t9, nullch                         #word1[char_index] = '\0'; set current index of word1 to nullch
  220.         sb $t9, ($s3)
  221.         addi $s1, $s1, 1                       #char_index++; increment char_index
  222.         j process_unique                       #process_unique(char_index);
  223.        
  224. process_unique:
  225.         add $t5, $0, $0                 #int unique_location_index = 0
  226.         add $t6, $0, $0                 #int counter = 0;
  227.         la $s3, outputstring            #set index of $s3 back to start
  228.         la $s5, inputstring1            #set index of $s5 back to start
  229.         la $s2, uniquewords             #set index of $s2 back to start
  230.         beq $s4, $0, add_unique         #if(uniques_char_counter == 0) branch to add_unique
  231.         compare:                                        #while(counter < uniques_char_counter){
  232.                 lb $t8, ($s3)
  233.                 lb $t9, ($s5)
  234.                 bne $t8,$t9, notsamechar                # if(word_array[counter] == word1[counter1]){
  235.                         bne $t9, '\0', donothing        # if(word_array[counter] == '\0'){
  236.                                 add $s2, $s2, $t5       #word_count[unique_location_index]++;
  237.                                 lb $t7, ($s2)
  238.                                 addi $t7, $t7, 1
  239.                                 sb $t7, ($s2)
  240.                                 j returnuniq            #return 0;
  241.                 donothing:
  242.                         addi $s3, $s3, 1                #counter++;
  243.                         addi $t6, $t6, 1
  244.                         addi $s5, $s5, 1                #counter1++;
  245.                         j compare
  246.         notsamechar:                                    #else{
  247.                 la $s3, outputstring                    #counter1 = 0;
  248.                 addi $t5, $t5, 1                        #unique_location_index++;
  249.                 lb $t9, ($s5)
  250.                 bne $t9, '\0', nextword
  251.         NWret:
  252.                 addi $s5, $s5, 1                        #counter++;
  253.                 addi $t6, $t6, 1                        #increment $t6 by 1
  254.                 bne $s4, $t6, compare                   #check if while is still satisfied
  255.                 j add_unique                            #add_unique(index);
  256.                
  257. nextword:                                               #while(word_array[counter] != '\0'){
  258.         addi $s5, $s5, 1                                #counter++;
  259.         addi $t6, $t6, 1                                #nextword moves the word_array to the next word
  260.         lb $t8, ($s5)
  261.         beq $t8, '\0', NWret
  262.         j nextword
  263.        
  264. add_unique:
  265.         add $t6, $0, $0                 #int x = 0;
  266.         la $s5, inputstring1            #reset word_array to its start address
  267.         la $s3, outputstring            #reset word1 to its start address
  268.         la $s2, uniquewords             #reset word_count to its start address
  269.         add $s5, $s5, $s4
  270.         add_word:
  271.                 lb $t8, ($s3)           #word_array[uniques_char_counter] = word1[x]
  272.                 sb $t8, ($s5)
  273.                 addi $t6, $t6, 1        #x++;
  274.                 addi $s4, $s4, 1        #uniques_char_counter++;
  275.                 addi $s3, $s3, 1
  276.                 addi $s5, $s5, 1
  277.                 bne $t6, $s1, add_word  #while(x < index){
  278.         add $s2, $s2, $s6               #word_count[next_unique_counter] = 1;
  279.         addi $t8, $0, 1
  280.         sb $t8, ($s2)
  281.         addi $s6, $s6, 1                #next_unique_counter ++;
  282.         j returnuniq
  283.  
  284. is_delimiting_char:                     #checks if top stack value is delimiting char
  285.         lb $t5, 0($sp)                  #store byte passed by while_pi in $t5
  286.         addi $sp,$sp,1
  287.         beq $t5, ' ', returndel         #if ( ch == ' ') { return 1; }
  288.         beq $t5, '.', returndel         #else if ( ch == '.') { return 1; }
  289.         beq $t5, '!', returndel         #else if ( ch == '!') { return 1; }
  290.         beq $t5, '?', returndel         #else if ( ch == '?') { return 1; }
  291.         beq $t5, '_', returndel         #else if ( ch == '_') { return 1; }
  292.         beq $t5, '-', returndel         #else if ( ch == '-') { return 1; }
  293.         beq $t5, '(', returndel         #else if ( ch == '(') { return 1; }
  294.         beq $t5, ')', returndel         #else if ( ch == ')') { return 1; }
  295.         beq $t5, ',', returndel         #else if ( ch == ',') { return 1; }
  296.         beq $t5, '\0', returndel        #else if ( ch == '\0') { return 1; }
  297.         addi $sp, $sp, -4
  298.         sw $ra, 0($sp)                  #save $ra on stack
  299.         beq $t5, '\n', is_eos           #else if ( ch == '\n') { return 1; }
  300.                                        #if newl, go to is_eos
  301.         addi $sp, $sp, 4
  302.         j else1                         #if not delimiting, go to else1
  303.  
  304. is_eos:                                 #set end_of_string ($s2) to 1
  305.         lw $ra, 0($sp)
  306.         addi $sp, $sp, 4
  307.         addi $s7, $s7, 1
  308.         jr $ra                          #jump to return address, obtained from the stack
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement