MIPS finding array intersection

Title:

Write a program to take out the number of unsigned words contained in both array A and array B and store them in memory, in which array a contains 20 numbers and array B contains 30 numbers. "No same!" is displayed if the same number cannot be found.

Train of thought:

It's quite simple to use high-level language to do this topic. Here, it's no longer displayed in high-level language. The idea is very simple. First, input 20 numbers of array a. Then, recycle 30 numbers of input array B, and compare them with those in array a one by one for each number input. If they are the same, put them into another array s, and start to input the next number. If 20 numbers are not the same after comparison, it is proved that the number is not in group A. Finally, when all 30 numbers have been input, check whether there are elements in array S. if so, print them. Otherwise, output "No same!".

Flow chart:

Code:

#Write a program to take out the number of unsigned words contained in array A and array B and store them in memory,
#There are 20 numbers in array A and 30 numbers in array B. "No same!" is displayed if the same number cannot be found.

.data 
    array:
        .align 2        #I didn't know before. debug for an hour
        .space 80      # Open up array A space 
    input_msg_A:
        .asciiz "Input 20 numbers of Arrar_A:\n" # Enter a digital prompt
    space_str:
        .asciiz " "       # Output space information
    input_msg_B:
        .asciiz "Input 30 numbers of Array_B:\n"
    sameNum:
        .align 2        #Two byte alignment
        .space 80       #Space of intersection
    notFound:
        .asciiz "No same!\n"
    Found:
        .asciiz "The last number sequence is:\n"

.text 
.globl main
main:
    la $t5, sameNum        #Place the address of the intersection in t5
    la $t6, array         # Put the array address in position t6
    move $t7, $zero       # For count control input
    addi $t8, $zero, 20  
    move $t3, $zero          #Use t3 to count the number of elements in intersection set

    la $a0, input_msg_A        #Output prompt information
    li $v0, 4
    syscall

    move $t0, $t6
input_A:
    li $v0, 5             # Input command 5 loaded to $v0 called
    syscall              # The number of system call inputs is put on v0

    sw $v0, 0($t0)
    addiu $t0, $t0, 4        #The value of t0 points to the next array element address

    addi $t7, $t7, 1       # Add one to $t7 of count
    bne $t7, $t8, input_A    # If not, loop the process

    la $a0, input_msg_B        #Output prompt information
    li $v0, 4
    syscall

    move $t0, $zero          #Count the number of inputs
    addi $t9, $zero, 30 
input_B:
    beq $t0, $t9, output    #Judge whether input is completed
    li $v0, 5
    syscall

    move $s4, $v0           #Save the input data in s4
    addi $t0, $t0, 1        #Count +1
    j Loop_1                #Compare the number entered with the number in array A



Loop_1:
    move $t7, $zero         #Start counting
    addi $t8, $zero, 20     
    move $t1, $t6           #Get array A first address
Loop_2:
    lw $t2, 0($t1)            #Put the elements of array A into t2
    beq $t2, $s4, AddNum    #If the same, add the sameNum array
    addiu $t1, $t1, 4       #Pointer +1
    addi $t7, $t7, 1        #Count +1
    bne $t7, $t8, Loop_2    #You haven't traversed the game yet, continue to traverse
    j input_B

AddNum:
    sw $s4, 0($t5)            #Save the number entered into memory
    addiu $t5, $t5, 4       #Address offset one bit
    addiu $t3, $t3, 1       #Number of elements in array + 1
    j input_B               #Enter next number

NoSame:                     #When intersection is empty, output
    la $a0, notFound
    li $v0, 4
    syscall
    j Exit

output: 
    beq $t3, $zero, NoSame

    la $a0, Found
    li $v0, 4
    syscall

    move $t7, $zero       # For count control input
    la $t0, sameNum
print_all:
    lw $a0, 0($t0)        # The number of addresses obtained is load ed to a0
    li $v0, 1              #output
    syscall
    la $a0, space_str
    li $v0, 4
    syscall

    addiu $t0, $t0, 4
    addi $t7,$t7,1  
    bne $t7,$t3,print_all  

Exit:
    li $v0 10            # Sign out 
    syscall

The experiment of the group planning has begun, and the practical training has also begun. I hope that the teacher can be merciful and let us spend this month in peace. Fortunately, this MIPS job is not difficult..

Posted on Tue, 31 Mar 2020 21:29:47 -0700 by elite_prodigy