Функция Bubble Sort на MIPS возвращает ошибку «Недопустимый элемент языка: -...» для всех отрицательных чисел - PullRequest
1 голос
/ 11 апреля 2020

Я пытался написать точный перевод для следующего кода python в MIPS.

Python:

def bubble sort( the list ) : n = len(the list)  for a in range(n−1):  for i in range(n−1):
    item= thelist[i]
    item to right = the list[i+1]
     if item > item to right:
       the list[i] = item to right
       the list[i+1] = item

Я написал следующий код в MIPS:

    .data

    .globl bubble_sort

    .text       
    bubble_sort:
        # save $fp and $ra , and update $fp
        addi $sp , $sp , -8         # make space for $fp and $ra
        sw $fp, 0($sp)          # store $fp on stack
        sw $ra, 4($sp)          # store $ra on stack
        addi $fp, $sp, 0        # copy $sp to $fp

    # allocate 5 local variables (n) 
    addi $sp, $sp, −20      # 5 local is 4 bytes (n, a, i, item, item_to_right)

    # setup n=length of array
    lw $t0, 8($fp)          # load a list which is a pointer to the heap memory
    lw $t1 , ($v0)          # load length
    sw $t1, −4($fp)         # n = length of a list

    # setup a=0
    addi $t2, $0, $0
    sw $t2, −8($fp)         # a = 0

    # setup i=0
    addi $t3, $0, $0
    sw $t3, -12($fp)        #i=0

loop1: 
    # increment a
    addi $t2, $t2, 1
    sw $t2, −8($fp)         # a+1

    #check condition (for a in range len(the list)-1)
    lw $t0 , -8($fp)        # load a
    lw $t1, −4($fp)         # n = length of a list
    addi $t1, $t1, -1       #$t1=n-1
    slt $t2, $t0,$t1
    beq $t2,0, deallocate

loop2:
    # increment i
    addi $t3, $t3, 1
    sw $t3, -12($fp)        #i+1

    #check condition (for i in range len(the list)-1)
    lw $t3 , -12($fp)       # load i
    lw $t1, −4($fp)         # n = length of a list
    addi $t1, $t1, -1       #$t1=n-1
    slt $t2, $t3,$t1
    beq $t2,0, loop1

    # item = the_list[i] 
    # get the_list[i] in $t4
    lw $t0, −12($fp)    # $t0 = i
    sll $t1, $t0, 2     # $t1 = 4∗i
    lw $t2, 8($fp)      # $t2 = &(start of the list)
    add $t3, $t1, $t2   # $t3 = &(start of the list) + 4∗i
    lw $t4, 4($t3)      # $t4 = the list[i]
    lw $t0 , −16($fp)   # $t0 = item
    sw $t4, −16($fp)    # item = the_list[i]

        #item_to_right = the list[i+1]
        lw $t0, −12($fp)    # $t0 = i
        addi $t0, $t0, 1
    sll $t1, $t0, 2     # $t1 = 4∗i
    lw $t2, 8($fp)      # $t2 = &(start of the list)
    add $t3, $t1, $t2   # $t3 = &(start of the list) + 4∗i
    lw $t4, 4($t3)      # $t4 = the list[i+1]
    lw $t0 , −20($fp)   # $t0 = item_to_right
    sw $t5, −20($fp)    # item_to_right = the_list[i+1]


        #if item > item_to_right: 
        slt $t6,$t5,$t4
        beq $t6, $0, loop2

        #the list[i] = item to right
    #the list[i+1] = item
        sw $t4, 4($v0)
        sw $t3 , 8($v0)

        #increment address
        lw $t0, 8($fp)          # load a list which is a pointer to the heap memory
    addi $t0, $t0, 4        # incrementing address by 4 bytes

    j loop2

    deallocate: 
        addi $sp, $sp, 20       # deallocate 5 local variables
        # restore $fp and $ra
        lw $fp , 0($sp)         # restore $fp
        lw $ra , 4($sp)         # restore $ra
        addi $sp, $sp, 8        # remove space on stack for $fp and $ra

        jr $ra  

    exit:    # Exit the program
            addi $v0, $0, 10
        syscall

Когда я собираю код, я получаю сообщение об ошибке «Недопустимый элемент языка: -...» для всех отрицательных чисел в коде (например, addi $ sp, $ sp, −20).

Можете ли вы порекомендовать какие-либо решения для этого?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...