# demo.s -- This program demonstrates the shift and logical operations # included in the MIPS instruction set. # # purpose of registers # $s0 -- which operation # $s1 -- operand 1 # $s2 -- operand 2 # $s3 -- result # $s4 -- number that bitprint() shows in binary .data operand1_str: .asciiz "Please enter an integer operand: " operation_str: .ascii "Which bitwise /shift operation would you like " .ascii "to perform? Here's the list:\n" .ascii "1 - sll - shift left 7 - or - bitwise OR\n" .ascii "2 - srl - shift right logical 8 - xor - exclusive XOR\n" .ascii "3 - sra - shift right arith. 9 - nor - not OR\n" .ascii "4 - rol - rotate left 10 - not - bitwise NOT\n" .ascii "5 - ror - rotate right\n" .ascii "6 - and - bitwise AND\n" .asciiz "Which operation would you like? (1-10) " operand2_str: .asciiz "Please enter the second operand: " operand1_out: .asciiz "Operand 1 is " operand2_out: .asciiz "Operand 2 is " result_out: .asciiz "The result is " blank: .asciiz " " newline: .asciiz "\n" .text main: li $v0, 4 # prompt for first operand la $a0, operand1_str syscall li $v0, 5 # get first operand syscall move $s1, $v0 li $v0, 4 # prompt for the particular operation la $a0, operation_str syscall li $v0, 5 # get operation number (1-10) syscall move $s0, $v0 beq $s0, 10, skip_operand2 # the NOT doesn't have a 2nd operand li $v0, 4 # prompt for 2nd operand la $a0, operand2_str syscall li $v0, 5 # get 2nd operand syscall move $s2, $v0 skip_operand2: bne $s0, 1, try_srl sllv $s3, $s1, $s2 j print_result try_srl: bne $s0, 2, try_sra srlv $s3, $s1, $s2 j print_result try_sra: bne $s0, 3, try_rol srav $s3, $s1, $s2 j print_result try_rol: bne $s0, 4, try_ror rol $s3, $s1, $s2 j print_result try_ror: bne $s0, 5, try_and ror $s3, $s1, $s2 j print_result try_and: bne $s0, 6, try_or and $s3, $s1, $s2 j print_result try_or: bne $s0, 7, try_xor or $s3, $s1, $s2 j print_result try_xor: bne $s0, 8, try_nor xor $s3, $s1, $s2 j print_result try_nor: bne $s0, 9, try_not nor $s3, $s1, $s2 j print_result try_not: not $s3, $s1 # no error checking of input: we assume # this is only possibility left print_result: # In the case of the shift operations, we want to print the 2nd # operand as a decimal integer so that it would be more meaningful # to the user. For logical operations, the 2nd operand will be # printed in binary so the user can compare all the bit patterns. li $v0, 4 # introduce operand 1 la $a0, operand1_out syscall move $s4, $s1 jal bitprint # ready to show binary rep'n of $s1-->$s4 beq $s0, 10, result_only # NOT has no 2nd operand li $v0, 4 # introduce operand 2 la $a0, operand2_out syscall bge $s0, 6, not_shift # for non-shift operation, print as binary li $v0, 1 # print operand 2 as decimal move $a0, $s2 syscall li $v0, 4 # print newline la $a0, newline syscall j result_only not_shift: move $s4, $s2 jal bitprint # ready to show binary rep'n of $s2-->$s4 result_only: li $v0, 4 # introduce result la $a0, result_out syscall move $s4, $s3 jal bitprint # call bitprint() with $s3-->$s4 li $v0, 10 # done! syscall #-------------------------------------------------------------------------- # procedure bitprint - print the 32 bits of the number $s4, and put a space # after every 8 bits for readability # # $s5 = mask # $s6 = loop counter # $s7 = bitwise result bitprint: li $t1, 1 sll $s5, $t1, 31 # put 1 in most significant bit (leftmost bit) li $s6, 0 loop: bge $s6, 32, endloop # done with loop? and $s7, $s5, $s4 # is most significant bit = 0? beqz $s7, put_zero li $v0, 1 # print bit '1' li $a0, 1 syscall j after_put_zero put_zero: li $v0, 1 # print bit '0' li $a0, 0 syscall after_put_zero: addi $s6, $s6, 1 # increment loop counter sll $s4, $s4, 1 # shift number left by 1 bit position rem $t1, $s6, 8 # compute the mod by 8 to see if we need space bnez $t1, loop li $v0, 4 # print space la $a0, blank syscall j loop endloop: li $v0, 4 # print newline la $a0, newline syscall jr $ra # return