python - Bubble Sort Function on MIPS returning "Invalid language element:-..." error for all negative numbers
问题描述
I have been trying to write a faithful translation for the following python code into 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
I have written the following code in 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
When I assemble the code, I am getting the error "Invalid language element:-..." for all negative numbers in the code (e.g. addi $sp, $sp, −20).
Can you recommend any solutions to this?
解决方案
推荐阅读
- python - 如何在迭代时成功弹出/删除字典项
- css - ReactJS+MaterialUI v1 自动嵌套列表填充
- python - 我们如何在 python 中使用压缩进行日志旋转?
- excel - 我可以使用索引匹配对字符串中的多个文本进行通配符吗
- mongodb - 对数组 mongodb 的索引进行排序
- java - 在服务器上上传 5 个相机图像时,图像在 android 中变得模糊
- json - org.json.JSONException: No value for opening_hours ,如何处理此类错误
- web-services - Talend Studio 运行时
- css - CSS/SCSS 位置:绝对工具提示 - 防止离开屏幕
- java - JSP/Servlet 中的隐藏表单