# HW 5 bitString solution # by Mark Fienup .data set1: .word 0 set2: .word 0 prompt1: .asciiz "Enter a string:" newline: .asciiz "\n" str_set_1: .asciiz "The first set is " str_set_2: .asciiz "\nThe second set is " str_u: .asciiz "\nThe union is " str_i: .asciiz "\nThe intersection is " str_d: .asciiz "\nThe difference is " str_c_1: .asciiz "\nThe set " str_c_T: .asciiz " contains the letter " str_c_F: .asciiz " DOES NOT contain the letter " str_a: .asciiz "'A' " str_q: .asciiz "'Q' " str_y: .asciiz "'Y' " str_z: .asciiz "'Z' " str1: .space 100 str2: .space 100 .text .globl main main: # $s0 is bitString 1 # $s1 is bitString 2 la $a0, prompt1 # print prompt 1 li $v0, 4 syscall la $a0, str1 # read string 1 li $a1, 100 li $v0, 8 syscall la $a0, newline # print newline character li $v0, 4 syscall la $a0, prompt1 # print prompt 2 li $v0, 4 syscall la $a0, str2 # read string 2 li $a1, 100 li $v0, 8 syscall la $a0, newline # print newline character li $v0, 4 syscall la $a0, str1 # convert str1 to a bitString jal bitString move $s0, $v0 la $a0, str2 #convert str2 to a bitString jal bitString move $s1, $v0 la $a0, str_set_1 # print set 1 li $v0, 4 syscall move $a0, $s0 jal print la $a0, newline # print newline character li $v0, 4 syscall la $a0, str_set_2 # print set 2 li $v0, 4 syscall move $a0, $s1 jal print la $a0, newline # print newline character li $v0, 4 syscall move $a0, $s0 # calculate and print the union move $a1, $s1 jal union move $t0, $v0 la $a0, str_u li $v0, 4 syscall move $a0, $t0 jal print la $a0, newline # print newline character li $v0, 4 syscall move $a0, $s0 # calculate and print the intersection move $a1, $s1 jal intersection move $t0, $v0 la $a0, str_i li $v0, 4 syscall move $a0, $t0 jal print la $a0, newline # print newline character li $v0, 4 syscall move $a0, $s0 # calculate and print the difference move $a1, $s1 jal difference move $t0, $v0 la $a0, str_d li $v0, 4 syscall move $a0, $t0 jal print la $a0, newline # print newline character li $v0, 4 syscall li $a0, 'A' # check if the letter 'A' is in the set move $a1, $s0 jal contains move $s4, $v0 la $a0, str_c_1 # print "The set " li $v0, 4 syscall move $a0, $s0 jal print if_7: beq $s4, $zero, else_7 la $a0, str_c_T # print "does contain " li $v0, 4 syscall j end_if_7 else_7: la $a0, str_c_F # print "DOES NOT contain " li $v0, 4 syscall end_if_7: la $a0, str_a # print "A" li $v0, 4 syscall la $a0, newline # print newline character li $v0, 4 syscall li $v0, 10 syscall end_main: ###################################################################### bitString: move $v0, $zero # $v0 is the bit string set # $v0 set is initially empty lb $t0, 0($a0) while: beqz $t0, end_while if: blt $t0, 65, else_if # if character >= 'A' and_1: bgt $t0, 90, else_if # and character <= 'Z' addi $t0, $t0, -65 # determine bit position li $t1, 1 # initial mask = 1 sll $t1, $t1, $t0 # mask for character or $v0, $v0, $t1 j end_if else_if: blt $t0, 97, end_if # if character >= 'a' and_2: bgt $t0, 122, end_if # and character <= 'z' addi $t0, $t0, -97 # determine bit position li $t1, 1 # initial mask = 1 sll $t1, $t1, $t0 # mask for character or $v0, $v0, $t1 end_if: addi $a0, $a0, 1 lb $t0, 0($a0) j while end_while: jr $ra ###################################################################### print: addi $sp, $sp, -100 # save space to build the string on stack addi $t0, $sp, 1 # $t0 points to next empty spot in string li $t1, 123 # load character '{' li $t2, 44 # load character ',' li $t6, 32 # load character ' ' (space) sb $t1, 0($t0) # move '{' to string sb $t6, 1($t0) # move 2 ' ' to string sb $t6, 2($t0) addi $t0, $t0, 3 li $t3, 0 # bit position to test li $t4, 65 # character at that bit position # (initially 'A') do: if_2: andi $t5, $a0, 1 # if test least significant bit = 1 then beqz $t5, end_if_2 sb $t4, 0($t0) # move character to the string sb $t2, 1($t0) # move ',' to string sb $t6, 2($t0) # move ' ' to string addi $t0, $t0, 3 end_if_2: addi $t3, $t3, 1 addi $t4, $t4, 1 srl $a0, $a0, 1 ble $t3, 25, do li $t1, 125 # overwrite last ',' with '}' sb $t1, -2($t0) sb $zero, -1($t0) # write NULL character at end addi $a0, $sp, 1 li $v0, 4 syscall jr $ra ###################################################################### union: or $v0, $a0, $a1 jr $ra ###################################################################### intersection: and $v0, $a0, $a1 jr $ra ###################################################################### difference: not $t0, $a1 and $v0, $a0, $t0 jr $ra ###################################################################### contains: li $v0, 0 move $t0, $a0 if_3: blt $t0, 65, else_if_3 # if character >= 'A' and_4: bgt $t0, 90, else_if_3 # and character <= 'Z' addi $t0, $t0, -65 # determine bit position li $t1, 1 # initial mask = 1 sll $t1, $t1, $t0 # mask for character and $v0, $a1, $t1 srl $v0, $v0, $t0 j end_if_3 else_if_3: blt $t0, 97, end_if_3 # if character >= 'a' and_3: bgt $t0, 122, end_if_3 # and character <= 'z' addi $t0, $t0, -97 # determine bit position li $t1, 1 # initial mask = 1 sll $t1, $t1, $t0 # mask for character and $v0, $a1, $t1 srl $v0, $v0, $t0 end_if_3: jr $ra ######################################################################