############################### # SOLUTION FOR PROBLEM 1. ## ############################### # $t0 = location of the string # $t1 = hold scanned character # $s2 = holds the ascii value for a space(32) # $t3 = used for address computation # $s5 = used for starting address of TABLE2 for a word statistics # $s4 = used for starting address of TABLE4 for sentence statistics # $s0 = holds the word length cnt # $s1 = holds the address for TABLE .data string: .asciiz "IN A DISTANT GALAXY EONS BEFORE THE CREATION OF THE MYTHICAL PLANET KNOWN \n" return: .asciiz "\n" newline: .asciiz "\n" word_msg: .asciiz " number of occurences of words containing \n" eq_msg: .ascii " = " .align 4 TABLE2: .space 104 # for temporary use .align 4 TABLE4: .space 104 # for occurences cnt .text .globl main main: la $t0, string # stores the address for string in $t0 la $s5, TABLE2 la $s4, TABLE4 addi $s2, $0, 32 # stores the ascii value for space in $t2 LOOP: lbu $t1, 0($t0) # load the next charater beq $t1, $0, DONE # END of STRING nop beq $t1,$s2,RESETWLC # space nop addi $s0,$s0,1 #inclement word length cnt. ########## Goal: Record the word Statistics at the TABLE2 ############ ### Step1: Capitalize the Character ####### ### Step2: Record Statistics at TABLE2#### ######################################################################## slti $t2, $t1, 97 # < 'a' // not a lower case. bne $t2, $0, CAPITAL_DONE slti $t2, $t1, 123 # 'z' + 1 // 97-122 lower case. beq $t2, $0, CAPITAL_DONE # $t2 =1 means , character is lower case. addi $t1, $t1, -32 CAPITAL_DONE: addi $t1, $t1, -65 # Set $t1 to table index. ( A->0, B->1, etc.) sltiu $t2, $t1, 26 # If $t1 is >= 26 then it's not a letter. beq $t2, $0, NEXT # Note that comparison above is unsigned. nop sll $t1, $t1, 2 # Scale index. add $t5, $s5, $t1 # Add index on to address of first element add $t9, $s4, $t1 lw $t6, 0($t5) addi $t6, $t6, 1 sw $t6, 0($t5) NEXT: addi $t0,$t0,1 #point next character. j LOOP nop RESETWLC: beq $s0,$0,PreviousSF nop ### Goal : Update TABLE 4 for occurences of Words Character contain. ### TABLE2 has Character statistics of a word. ### Using that statistics, Update TABLE 4. #### If contents of TABLE2's cell is not zero, #####Inclement by one the contents of #### TABLE4's matching cell # address computation addi $t6,$0,25 # $t6 = for scanning TABLE2 add $t5,$0,$0 add $t9,$0,$0 REPEAT26: add $t8,$s5,$t9 # Starting Address of TABLE2 lw $t7,0($t8) beq $t7,$zero,NO_INC nop sw $0,0($t8) add $t8,$s4,$t9 # Starting Address of TABLE4 lw $t7,0($t8) # inclement and store back to TABLE4 addi $t7,$t7,1 # sw $t7,0($t8) # NO_INC: addi $t5,$t5,1 add $t9,$0,$t5 sll $t9,$t9,2 bne $t6,$t5,REPEAT26 nop NOCLEAR: nop PreviousSF: addi $t0,$t0,1 #point next character. j LOOP nop DONE: # print and Exit.. beq $s0,$0,PRINT nop PRINT: PW_LOOP_MAIN: la $a0,word_msg li $v0,4 syscall li $t1,0 li $t0,25 PW_LOOP: move $a0,$t1 addi $a0,$a0,65 li $v0,11 syscall la $a0,eq_msg li $v0,4 syscall add $t3,$0,$t1 # sll $t3,$t3,2 # multiply 4 to get byte address. add $t3,$t3,$s4 # get momory address. lw $a0,0($t3) # get Character information. li $v0,1 syscall la $a0,newline li $v0,4 syscall addi $t1,$t1,1 beq $t1,$t0,EXIT nop j PW_LOOP nop EXIT: addi $v0, $0, 10 syscall # Exits program jr $ra # nop ############################################## ## THIS IS OUTPUT OF THE ABOVE PROGRAM ###### ############################################## #number of occurences of words containing # A = 6 # B = 1 # C = 2 # D = 1 # E = 6 # F = 2 # G = 1 # H = 3 # I = 4 # J = 0 # K = 1 # L = 3 # M = 1 # N = 6 # O = 5 # P = 1 # Q = 0 # R = 2 # S = 2 # T = 6 # U = 0 # W = 1 # X = 1 # Y = 2 #Problem 2: 2-1) Write MIPS programs for the following c programs , .. # REGISTER USAGE ..SUM , $s0, i, $t0, 1)...for(i = 1; i<5 ;++1) { sum = sum + i; } 2.1.1.1) assume Delayed branch without delay slot 2.1.1.2) assume Delayed branch with delay slot 2)...for(i = 1; i<5;i = i+2) { sum = sum + i; sum = sum + i + 1; } 2.1.2.1) assume Delayed branch without delay slot 2.1.2.2) assume Delayed branch with delay slot 2-2)Count the total numbers of instructions(running instruction count) to compute sum for the 4 programs (2.1.1.1, 2.1.1.2, 2.1.2.1 and 2.1.2.2). ###################################### ### Delayed Branch ....Assume the instruction immeadately #### following Branch instruction is always excuted #### usuary put "NOP" instruction after a branch instruction #### Delay Slot ........Instead of putting "NOP" ##### putting some instruction which is useful 2.1.1.1) li $t0,1 li $t1,5 LOOP: beq $t0,$t1,EXIT_LOOP nop add $s0,$s0,$t0 addi $t0,$t0,1 j LOOP nop EXIT_LOOP: 2.1.1.2) ## Rearrange and get rid of "NOP" li $t0,1 li $t1,5 LOOP: beq $t0,$t1,EXIT_LOOP nop add $s0,$s0,$t0 j LOOP addi $t0,$t0,1 EXIT_LOOP: 2.1.2.1) li $t0,1 li $t1,5 LOOP: beq $t0,$t1,EXIT_LOOP nop add $s0,$s0,$t0 addi $t0,$t0,1 add $s0,$s0,$t0 addi $t0,$t0,1 j LOOP nop EXIT_LOOP: 2.1.2.2)## Rearrange and get rid of "NOP" li $t0,1 li $t1,5 LOOP: beq $t0,$t1,EXIT_LOOP nop add $s0,$s0,$t0 addi $t0,$t0,1 add $s0,$s0,$t0 j LOOP addi $t0,$t0,1 EXIT_LOOP: 2.2) 2.1.1.1 28inst.(inside loop 4times) 2.1.1.2 24inst.(inside loop 4times) 2.1.2.1 20inst.(inside loop twice) 2.1.2.2 18inst.(inside loop twice)