将 C 语言转换为 MIPS 汇编语言
所以我已经从事这个工作有一段时间了,但我似乎无法弄清楚。我正在将 C 插入排序转换为 MIPS,以对字符串列表进行排序。这是重要的 C 代码(str_lt 比较两个字符串。String_lessthan):
void insertSort(char *a[], size_t length) {
int i, j;
for(i = 1; i < length; i++) {
char *value = a[i];
for (j = i-1; j >= 0 && str_lt(value, a[j]); j--) {
a[j+1] = a[j];
}
a[j+1] = value;
}
}
这是我的 MIPS 程序集的内容为了制作数组,我将每个字符串对齐 5 并将数组的前面作为 $a2 传递:
insertSort:
subu $sp, $sp, 32
sw $ra, 20($sp)
sw $fp, 16($sp)
li $t2, 0
addi $t2, $t2, 1 #t2 = i
move $t6, $a2 #t6 = value
loop1:
bge $t2, 16, endloop1
subi $t3, $t2, 1 #t3 = j
la $t7, -32($t6) #t7 = a[j]
loop2:
bltz $t3, endloop2
move $a0, $t6
move $a1, $t7
jal str_lt
beqz $v0, endloop2
lw $t0, 0($t7)
sw $t0, 32($t7)
subi $t3, $t3 1
subi $t7, $t7 32
b loop2
endloop2:
lw $t0, 0($t6)
sw $t0, 0($t7)
addi $t2, $t2, 1
addi $t6, $t6, 32
b loop1
endloop1:lw $ra, 20($sp)
lw $fp, 16($sp)
addiu $sp, $sp, 32
jr $ra
看起来就像我得到的 C 算法在数组中重新排列某些值时会覆盖它们一样,或者也许我只是翻译错了。
So I have been at this for awhile now, and I just can't seem to figure it out. I am translating a C insertion sort into MIPS to sort a list of strings. Here is the important C code (str_lt compares the two strings. String_lessthan):
void insertSort(char *a[], size_t length) {
int i, j;
for(i = 1; i < length; i++) {
char *value = a[i];
for (j = i-1; j >= 0 && str_lt(value, a[j]); j--) {
a[j+1] = a[j];
}
a[j+1] = value;
}
}
and here is what I have for my MIPS assembly To make the array, I aligned each string by 5 and passed the front of the array as $a2 :
insertSort:
subu $sp, $sp, 32
sw $ra, 20($sp)
sw $fp, 16($sp)
li $t2, 0
addi $t2, $t2, 1 #t2 = i
move $t6, $a2 #t6 = value
loop1:
bge $t2, 16, endloop1
subi $t3, $t2, 1 #t3 = j
la $t7, -32($t6) #t7 = a[j]
loop2:
bltz $t3, endloop2
move $a0, $t6
move $a1, $t7
jal str_lt
beqz $v0, endloop2
lw $t0, 0($t7)
sw $t0, 32($t7)
subi $t3, $t3 1
subi $t7, $t7 32
b loop2
endloop2:
lw $t0, 0($t6)
sw $t0, 0($t7)
addi $t2, $t2, 1
addi $t6, $t6, 32
b loop1
endloop1:lw $ra, 20($sp)
lw $fp, 16($sp)
addiu $sp, $sp, 32
jr $ra
It seems like the C algorithm I was given overwrites some values when it rearranges them in the array, or perhaps I just translated it wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 gcc 并使用选项 -S
文件中将在名为“main.s”的文件夹中创建一个新文件,在这个文件中会有汇编代码。
you can with gcc with the option -S
a new file will be create in your folder named: "main.s", in this file there will be the assembly code.