gpt4 book ai didi

c - 将 C 代码转换为 MIPS 汇编代码

转载 作者:行者123 更新时间:2023-11-30 21:46:50 26 4
gpt4 key购买 nike

我希望将以下代码翻译为 MIPS 汇编语言。我该如何翻译它?或者这样做有什么问题吗?

#include <stdio.h>

int main()
{
int n, sum = 0, remainder;

printf("Enter an integer\n");
scanf("%d",&n);

while(n != 0)
{
remainder = n % 10;
sum = sum + remainder;
n = n / 10;
}

printf("Sum of digits of entered number = %d\n",sum);

return 0;
}

最佳答案

使用 gcc 将其翻译成这样的内容:

  4007a0 <main>:
4007a0: 3c040040 lui a0,0x40
int main() {
4007a4: 27bdffd8 addiu sp,sp,-40
4007a8: afbf0024 sw ra,36(sp)
int n, sum = 0, remainder;
printf("Enter an integer\n");
4007ac: 0c10018c jal 400630 <puts@plt>
4007b0: 248408b8 addiu a0,a0,2232
scanf("%d",&n);
4007b4: 3c040040 lui a0,0x40
4007b8: 248408cc addiu a0,a0,2252
4007bc: 0c10017c jal 4005f0 <scanf@plt>
4007c0: 27a50018 addiu a1,sp,24
while(n != 0)
4007c4: 8fa20018 lw v0,24(sp)
4007c8: 10400017 beqz v0,400828 <main+0x88>
4007cc: 3c036666 lui v1,0x6666
4007d0: 00002821 move a1,zero
{
remainder = n % 10;
4007d4: 24636667 addiu v1,v1,26215
4007d8: 00430018 mult v0,v1
4007dc: 000227c3 sra a0,v0,0x1f
4007e0: 00004810 mfhi t1
4007e4: 00093083 sra a2,t1,0x2
4007e8: 00c42023 subu a0,a2,a0
4007ec: 00043840 sll a3,a0,0x1
4007f0: 000430c0 sll a2,a0,0x3
4007f4: 00e63021 addu a2,a3,a2
4007f8: 00463023 subu a2,v0,a2
sum = sum + remainder;
4007fc: 00a62821 addu a1,a1,a2
400800: 1480fff5 bnez a0,4007d8 <main+0x38>
400804: 00801021 move v0,a0
400808: afa00018 sw zero,24(sp)
}
printf("Sum of digits of entered number = %d\n",sum);
40080c: 3c040040 lui a0,0x40
400810: 0c100188 jal 400620 <printf@plt>
400814: 248408d0 addiu a0,a0,2256
return 0;
}
400818: 8fbf0024 lw ra,36(sp)
40081c: 00001021 move v0,zero
400820: 03e00008 jr ra
400824: 27bd0028 addiu sp,sp,40

关于c - 将 C 代码转换为 MIPS 汇编代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24687687/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com