##############################################
# Program Name: Test read/write bit string
# Programmer: Koren
# Date: 10/14/2015
#############################################
.data # Data declaration section
Prompt1: .asciiz "Set1: "
Prompt2: .asciiz "Set2: "
Buffer: .space 80
Comma: .asciiz ","
NewLine: .asciiz "\n"
Union: .asciiz "Union of sets: "
Inters: .asciiz "Intersection of sets: "
Result: .asciiz "The set is: "
Bye: .asciiz "Have a nice day."
.globl main
.text
main:
li $v0, 4 # System call code for Print
la $a0, Prompt1 # Load adddress of prompt into $a0
syscall
la $t0, readSetHex # Use readSet to read the set from standard input
jalr $s6, $t0
move $s1, $s3 # Store a copy of the result in $s1
li $v0, 4 # System call code for Print String
la $a0, Result # Load address of msg into $a0
syscall # Print the string
la $t0, printSet # Use printSet to print the first set
jalr $s6, $t0
li $v0, 4 # System call code for Print
la $a0, Prompt2 # Load adddress of prompt into $a0
syscall
la $t0, readSetHex # Use readSet to read the set from standard input
jalr $s6, $t0
move $s2, $s3 # Store a copy of the result in $s2
li $v0, 4 # System call code for Print String
la $a0, Result # Load address of msg into $a0
syscall # Print the string
la $t0, printSet # Use printSet to print the second set
jalr $s6, $t0
li $v0, 4 # System call code for Print String
la $a0, Union # Load address of msg into $a0
syscall # Print the string
or $s3, $s1, $s2 # Compute the union
la $t0, printSet # Use printSet to print the union
jalr $s6, $t0
li $v0, 4 # System call code for Print String
la $a0, Inters # Load address of msg into $a0
syscall # Print the string
and $s3, $s1, $s2 # Compute the intersection
la $t0, printSet # Use printSet to print the union
jalr $s6, $t0
li $v0, 4 # System call code for Print String
la $a0, Bye # Load address of msg into $a0
syscall
li $v0, 10 # Terminate program and
syscall # Return control to the system
##############################################################
readSet: # Read from standard input, the result will be in $s3, use $t3-$t7
li $v0, 8
la $a0, Buffer
la $a1, 80
syscall
move $s3, $a0
li $t7, 0x0 # Will contain the bit string
li $t6, 0x30 # ASCII value for '0'
li $t5, 0x31 # ASCII value for '1'
li $t4, 0x0 # End-of-string character
rloop : lb $t3, ($s3)
beq $t3, $t4, endread
beq $t3, $t5, one
beq $t3, $t6, zero
b next
one : sll $t7, $t7, 1
ori $t7, $t7, 0x1
b next
zero : sll $t7, $t7, 1
next : addi $s3, $s3, 1
b rloop
endread : move $s3, $t7
jr $s6
##############################################################
readSetHex: # Read from standard input, the result will be in $s3, use $t3-$t7
li $v0, 8
la $a0, Buffer
la $a1, 80
syscall
move $s3, $a0
li $t7, 0x0 # Will contain the bit string
li $t4, 0x0 # End-of-string character
hloop : lb $t3, ($s3)
beq $t3, $t4, endreadh # Null char, end of string!
li $t5, 0x30 # Remove 48 to the char, which is ASCII of '0'
sub $t6, $t3, $t5 # Result in $t6
bltz $t6, nexth # ASCII < 48 -> do nothing
li $t5, 0x0a # Remove 10 more
sub $t2, $t6, $t5 # Result in $t2
bgez $t2, notdigit # It it's 58 or more, it's not a 0-9 digit
digit : sll $t7, $t7, 4
or $t7, $t7, $t6 # Add the digit
b nexth # Next char!
notdigit : li $t5, 0x41 # Remove 65 to the char, which is ASCII of 'A'
sub $t6, $t3, $t5 # Result in $t6
bltz $t6, nexth # ASCII < 65 -> do nothing
li $t5, 0x06 # Remove 6 more
sub $t2, $t6, $t5 # Result in $t2
bgez $t2, notupper # It it's 71 or more, it's not a A-F digit
upper : sll $t7, $t7, 4
or $t7, $t7, $t6 # Add the A-F digit
addi $t7, 0x0a
b nexth # Next char!
notupper : li $t5, 0x61 # Remove 97 to the char, which is ASCII of 'a'
sub $t6, $t3, $t5 # Result in $t6
bltz $t6, nexth # ASCII < 97 -> do nothing
li $t5, 0x06 # Remove 6 more
sub $t2, $t6, $t5 # Result in $t2
bgez $t2, nexth # It it's 103 or more, it's not a a-f digit
lower : sll $t7, $t7, 4
or $t7, $t7, $t6 # Add the a-f digit
addi $t7, 0x0a
nexth : addi $s3, $s3, 1
b hloop
endreadh : move $s3, $t7
jr $s6
##############################################################
printSet: # Assume the set to print is a word in $s3, will modify $t2-$t7
move $t6, $s3 # Move the set into $t6
li $t2, 0x1 # A one to test the results
li $t3, 0x0 # A zero to test the results
li $t4, 0x1 # The number of the set
li $t7, 0x20 # Max number of sets
loop: and $t5, $t6, $t2 # Test the lowest bit
beq $t5, $t3, check # Result is non-zero only if the bit is set
li $v0, 1 # System call code for integer print
move $a0, $t4 # Set integer to be printed
syscall # print the integer
li $v0, 4 # System call code for Print String
la $a0, Comma # Load address of msg into $a0
syscall # Print the string
check: beq $t7, $t4, back # check whether it was the last set
srl $t6, $t6, 1 # shift the bit one place right to test next bit
addi $t4, $t4, 1 # add 1 to the set number
b loop
back: li $v0, 4 # System call code for Print String
la $a0, NewLine # Load address of msg into $a0
syscall # Print the string
jr $s6
##############################################################