-1

I am writing a MIPS code for quicksort, and as a part of it, I need to write a main function that will first take an integer input from the user, N, which is the number of elements the user wishes to enter, and then it will ask the user for the respective numbers they want to enter as input. I wrote a skeleton code for the same in C, and the part that does this is as follows:-

int main()
{
    int N;
    scanf("%d\n", &N);      // will be the first line, will tell us the number of inputs we will get

    int i=0, A[N];

    int n = N;

// Loop to enter the values to be sorted into an array we have made A[]. the values are entered as a1, a2.... and so on.
    while(n!=0)
    {
        scanf("%d\n", &A[i]);
        i++;
        n--;
    }
}

I have also written a MIPS code for this part, which is as follows

#PROGRAM : QuickSort

.data

prompt : .asciiz "Number of integers : "
         .align 4
arrayA : .space 40000

.text

main:

    la $a0, prompt
    li $v0, 4
    syscall          # print the prompt asking the user for array length input

    li $v0, 5        # $v0 holds the value of N(no of elements to be given as input, as given by the user)
    syscall

    addi $s0, $v0, zero    # move the value stored in $v0(which holds the number of elements in the array) to the register $s0

    li $t0, 0        # code to initialise variable i(index of array), and set it's value as 0

    la $s1, arrayA   # base address of the array A is loaded onto the register $s1

    move $t1, $s0    # the value of N(which is stored in $s0) is also stored in the register $t1 now

    # code to read the number of registers to be input by the user

L1:

    beq $t1, $zero, outL1   # branch to the outL1 if the value of $t1(which holds value of n(=N)) is equal to 0

    li $v0, 5
    syscall               # input value of the element taken

    sw $v0, arrayA($t0)   # assign the value input by the user to the respective element in the array in memory

    addi $t0, $t0, 4      # add 4(no of bytes used up) to the index of the array

    addi $t1, $t1, -1     # n = n-1 (n is in $t1 and is equal to the number of elements the user want to input)

    j L1                  # go to the start of the loop L1

outL1:                    # exited the first while loop for entering the values into the array A

Ideally, I would have liked to dynamically assign memory to an array of size N, however, I am not quite sure how to do that so I defined an array, arrayA, of Nmax * 4 size(we have been given the value of Nmax), and I am just getting the number of integers the user wants to input, and looping the procedure to ask for inputs that N number of times to only fill up the first N elements of arrayA. I am a bit unsure, however, where the address of arrayA is stored. Also, not quite sure if the process using which I am trying to enter the user input elements as elements of the array is correct. Additionally, now I have to pass this array into another function, so do I need to load its base address onto the argument register in the caller function, or will simply accessing arrayA in the callee function work too?

user202004
  • 151
  • 8

1 Answers1

0

I am a bit unsure, however, where the address of arrayA is stored.

It's a constant that can be loaded into the register of your choice. For example, la $t0, arrayA loads the address of arrayA into $t0.

Also, not quite sure if the process using which I am trying to enter the user input elements as elements of the array is correct.

As far as I can tell there's no way for the user to tell the program that they're finished entering data, or to handle errors. What happens if the user just hits the enter key without inputting a number?

Additionally, now I have to pass this array into another function, so do I need to load its base address onto the argument register in the caller function, or will simply accessing arrayA in the callee function work too?

Either will work.

puppydrum64
  • 1,598
  • 2
  • 15