############################### # SOLUTION FOR PROBLEM 1. ## ############################### # $t0 = location of the string # $t1 = hold scanned character # $t2 = holds the ascii value for a space(32) # $t3 = used for address computation # $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 as earth, vast civilizations have eveolved and ruling the galaxy is an interstellar Empire created from the ruins of an Old Republic that held sway for generations" return: .asciiz "\n" wordcntmsg: .asciiz "the word count of the length x \n" newline: .asciiz "\n" wordlength: .ascii " character word = " .align 4 TABLE: .space 100 .text .globl main main: la $t0, string # stores the address for string in $t0 la $s1, TABLE addi $t2, $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,$t2,RESETWLC # space nop addi $s0,$s0,1 #inclement word length cnt. addi $t0,$t0,1 #point next character. j LOOP nop RESETWLC: #inclement the word lenght cnt of that word length # from the memory location.($s1) # find memory location. beq $s0,$0,PreviousSF nop # address computation add $t3,$0,$s0 # addi $t3,$t3,-1 sll $t3,$t3,2 # multiply 4 to get byte address. add $t3,$t3,$s1 # get momory address. lw $t4,0($t3) # get previous word length cnt from the memory. addi $t4,$t4,1 sw $t4,0($t3) # inclement and store the cnt back. add $s0,$0,$0 #reset word length cnt PreviousSF: addi $t0,$t0,1 #point next character. j LOOP nop DONE: # print and Exit.. beq $s0,$0,PRINT nop # address computation add $t3,$0,$s0 # addi $t3,$t3,-1 sll $t3,$t3,2 # multiply 4 to get byte address. add $t3,$t3,$s1 # get momory address. lw $t4,0($t3) # get previous word length cnt from the memory. addi $t4,$t4,1 sw $t4,0($t3) # inclement and store the cnt back. PRINT: la $a0, wordcntmsg li $v0, 4 syscall li $t0,26 li $t1,1 P_LOOP: move $a0,$t1 li $v0,1 syscall la $a0,wordlength li $v0,4 syscall add $t3,$0,$t1 # addi $t3,$t3,-1 sll $t3,$t3,2 # multiply 4 to get byte address. add $t3,$t3,$s1 # get momory address. lw $a0,0($t3) # get previous word length cnt from the memory. li $v0,1 syscall la $a0,newline li $v0,4 syscall addi $t1,$t1,1 beq $t1,$t0,EXIT nop j P_LOOP nop EXIT: addi $v0, $0, 10 syscall # Exits program LB_SUB: # add $t4, $0, $a0 # srl $t4, $t4, 2 # sll $t4, $t4, 2 # sub $t5, $a0, $t4 # addi $t7, $0, 8 # mul $t6, $t5, $t7 # lw $v0, 0($t4) # addi $t8, $0, 24 # sub $t8, $t8, $t6 # sll $v0, $v0, $t8 # srl $v0, $v0, 24 # jr $ra # nop # ##################################### # SOLUTION FOR PROBLEM 2 ### ##################################### # 2-1a) li $t0,1 li $t1,5 LOOP: beq $t0,$t1,EXIT_LOOP nop add $s0,$s0,$t0 addi $t1,$t1,1 j LOOP nop EXIT_LOOP: # 2-1b) li $t0,1 li $t1,5 LOOP: beq $t0,$t1,EXIT_LOOP nop add $s0,$s0,$t0 addi $t1,$t1,1 add $s0,$s0,$t0 addi $t1,$t1,1 j LOOP nop EXIT_LOOP: # 2-1c) main: li $a0,4 jal recursive_sum nop RA: ############################ recursive_sum: addi $sp,$sp,-8 sw $ra,0($sp) sw $a0,4($sp) li $t0, 1 beq $a0,$t0,RET1 nop addi $a0,$a0,-1 jal recursive_sum nop RB: addi $a0,$a0,1 add $v0,$v0,$a0 j RET nop RET1: li $v0,1 RET: lw $ra,0($sp) lw $a0,4($sp) addi $sp,$sp,8 jr $ra nop 2-2 # Without Using Delay slot. ..a) middle (running count ..2+4*6 + 2 = 28) b) fastest (running count ..2+2*8 + 2 = 20) c) slowest (running count ..3*9 + 13 + 3*9 = 67) 2-3) # # 4 # RA # 3 # RB # 2 # RB # 1 # RB ################### 3) # To convert Little Endian format data to Big Endian format data # It looks like lbu $v0,0($a0) # BUT it is NOT. ###END of Solution.######