You are on page 1of 4

.

data # Data section for variables and prompt strings


result: .space 4 # Variable to store the result (4 bytes for word)
prompt1: .asciiz "Enter the first 16-bit binary number: "
prompt2: .asciiz "Enter the second 16-bit binary number: "
prompt3: .asciiz "Enter the selector (3-bit binary): "
input1: .space 17 # Data section for input buffers
input2: .space 17
selector: .space 4 # Variable to store the selector input (4 bytes for
word)

.text # Code section for instructions


.globl main # Main function entry point

main:
# Display prompt for the first input
li $v0, 4 # Load system call code for printing a string
la $a0, prompt1 # Load the address of the prompt1 string
syscall # Print the prompt

# Read the first input as a string


li $v0, 8 # Load system call code for reading a string
la $a0, input1 # Load the address where the input will be stored
li $a1, 17 # Set the maximum number of characters to read (16
digits + null terminator)
syscall # Read the input

# Convert the first input from binary to integer (16 bits)


li $t0, 0 # Clear the register $t0 to store the integer value
li $t1, 1 # Set the initial value of the bit position to 1
loop1:
lbu $t2, input1($t1) # Load a byte from the input string
beq $t2, $zero, done1 # If the byte is null terminator, exit the loop
sub $t2, $t2, 48 # Convert ASCII character to integer (binary 0 or
1)
sll $t0, $t0, 1 # Shift the current value left by 1 bit
or $t0, $t0, $t2 # Bitwise OR to set the rightmost bit
addi $t1, $t1, 1 # Move to the next character
j loop1
done1:

# Display prompt for the second input


li $v0, 4 # Load system call code for printing a string
la $a0, prompt2 # Load the address of the prompt2 string
syscall # Print the prompt

# Read the second input as a string


li $v0, 8 # Load system call code for reading a string
la $a0, input2 # Load the address where the input will be stored
li $a1, 17 # Set the maximum number of characters to read (16
digits + null terminator)
syscall # Read the input

# Convert the second input from binary to integer (16 bits)


li $t3, 0 # Clear the register $t3 to store the integer value
li $t4, 1 # Set the initial value of the bit position to 1
loop2:
lbu $t5, input2($t4) # Load a byte from the input string
beq $t5, $zero, done2 # If the byte is null terminator, exit the loop
sub $t5, $t5, 48 # Convert ASCII character to integer (binary 0 or
1)
sll $t3, $t3, 1 # Shift the current value left by 1 bit
or $t3, $t3, $t5 # Bitwise OR to set the rightmost bit
addi $t4, $t4, 1 # Move to the next character
j loop2
done2:

# Display prompt for the selector


li $v0, 4 # Load system call code for printing a string
la $a0, prompt3 # Load the address of the prompt3 string
syscall # Print the prompt

# Read the selector as a string


li $v0, 8 # Load system call code for reading a string
la $a0, selector # Load the address where the selector will be
stored
li $a1, 4 # Set the maximum number of characters to read (3
bits + null terminator)
syscall # Read the selector

# Convert the selector from binary to integer (3 bits)


li $t7, 0 # Clear the register $t7 to store the integer value
(selector)
li $t8, 2 # Set the initial value of the bit position to 2
(since 2^2 is the leftmost bit in 3-bit binary)
loop3:
lbu $t9, selector($t8) # Load a byte from the selector string
beq $t9, $zero, done3 # If the byte is null terminator, exit the loop
sub $t9, $t9, 48 # Convert ASCII character to integer (binary 0 or
1)
mul $t9, $t9, $t8 # Multiply the bit value (0 or 1) by the
appropriate power of 2
add $t7, $t7, $t9 # Add the result to $t7
sub $t8, $t8, 1 # Move to the next character (shift right)
j loop3
done3:

# Check the value of the selector (000 in binary)


li $t9, 0 # Load the value 0 into $t9 (selector in decimal
form)
beq $t7, $t9, perform_or # If selector is 000, go to the perform_or section

# Check the value of the selector (001 in binary)


li $t9, 1 # Load the value 1 into $t9 (selector in decimal
form)
beq $t7, $t9, perform_and # If selector is 001, go to the perform_and section

# Check the value of the selector (010 in binary)


li $t9, 2 # Load the value 2 into $t9 (selector in decimal
form)
beq $t7, $t9, perform_add # If selector is 010, go to the perform_add section

# Check the value of the selector (011 in binary)


li $t9, 3 # Load the value 3 into $t9 (selector in decimal
form)
beq $t7, $t9, perform_subb # If selector is 011, go to the perform_sub section

# Check the value of the selector (100 in binary)


li $t9, 4 # Load the value 4 into $t9 (selector in decimal
form)
beq $t7, $t9, perform_sll # If selector is 100, go to the perform_sll section

# Check the value of the selector (101 in binary)


li $t9, 5 # Load the value 5 into $t9 (selector in decimal
form)
beq $t7, $t9, perform_srl # If selector is 101, go to the perform_srl section

# Check the value of the selector (111 in binary)


li $t9, 7 # Load the value 7 into $t9 (selector in decimal
form)
beq $t7, $t9, perform_sltt # If selector is 111, go to the perform_slt section

# If selector is none of the expected values, exit the program


li $v0, 10 # Load system call code for program exit
syscall # Terminate the program

perform_or:
# Perform the binary OR operation
or $t6, $t0, $t3 # Perform OR operation between the two inputs,
result stored in $t6
j print_result # Jump to the print_result section

perform_and:
# Perform the binary AND operation
and $t6, $t0, $t3 # Perform AND operation between the two inputs,
result stored in $t6
j print_result # Jump to the print_result section

perform_add:
# Perform the addition
add $t6, $t0, $t3 # Add the two inputs, result stored in $t6
j print_result # Jump to the print_result section

perform_subb:
# Perform the subtraction
sub $t6, $t0, $t3 # Subtract the second input from the first input,
result stored in $t6
#bgez $t6, slt_not_negative
#li $t6, 0
j print_result # Jump to the print_result section

perform_sll:
# Perform the logical left shift (4 times)
sll $t6, $t0, 4 # Left shift the first input 4 times, result stored
in $t6
j print_result # Jump to the print_result section

perform_srl:
# Perform the logical right shift (4 times)
srl $t6, $t0, 4 # Right shift the first input 4 times, result
stored in $t6
j print_result # Jump to the print_result section

perform_sltt:
# Perform the Set Less Than (SLT) operation
sub $t6, $t0, $t3 # Set $t6 to 1 if $t0 is less than $t3; otherwise,
set to 0
bgez $t6, slt_not_negative # If $t6 is positive or zero, go to
slt_not_negative
li $t6, 0 # If $t6 is negative, set it to 0 (output 0)
j print_result # Jump to the print_result section

slt_not_negative:
li $t6, 1 # If $t6 is positive or zero, set it to 1 (output
1)
j print_result # Jump to the print_result section

print_result:
# Display the result
li $v0, 1 # Load system call code for printing an integer
move $a0, $t6 # Move the result to register $a0
syscall # Print the result

# Exit the program


li $v0, 10 # Load system call code for program exit
syscall # Terminate the program

You might also like