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

时间:2014-07-10 23:01:50

标签: c assembly translation mips

我希望将以下代码翻译成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;
}

2 个答案:

答案 0 :(得分:1)

使用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

答案 1 :(得分:0)

我的gcc可以将它转换为以下程序集。

    .file   1 "C2MIPS.c"

 # -G value = 8, Cpu = 3000, ISA = 1
 # GNU C version cygnus-2.7.2-970404 (mips-mips-ecoff) compiled by GNU C version cygnus-2.7.2-970404.
 # options passed:  -msoft-float
 # options enabled:  -fpeephole -ffunction-cse -fkeep-static-consts
 # -fpcc-struct-return -fcommon -fverbose-asm -fgnu-linker -msoft-float
 # -meb -mcpu=3000

gcc2_compiled.:
__gnu_compiled_c:
    .rdata
    .align  2
$LC0:
    .ascii  "Enter an integer\n\000"
    .sdata
    .align  2
$LC1:
    .ascii  "%d\000"
    .rdata
    .align  2
$LC2:
    .ascii  "Sum of digits of entered number = %d\n\000"
    .text
    .align  2
    .globl  main
    .ent    main
main:
    .frame  $fp,40,$31      # vars= 16, regs= 2/0, args= 16, extra= 0
    .mask   0xc0000000,-4
    .fmask  0x00000000,0
    subu    $sp,$sp,40
    sw  $31,36($sp)
    sw  $fp,32($sp)
    move    $fp,$sp
    jal __main
    sw  $0,20($fp)
    la  $4,$LC0
    jal printf
    la  $4,$LC1
    addu    $5,$fp,16
    jal scanf
$L2:
    lw  $2,16($fp)
    bne $2,$0,$L4
    j   $L3
$L4:
    lw  $2,16($fp)
    li  $3,1717960704           # 0x66660000
    ori $3,$3,0x6667
    mult    $2,$3
    mfhi    $6
    sra $3,$6,2
    sra $4,$2,31
    subu    $3,$3,$4
    move    $5,$3
    sll $4,$5,2
    addu    $4,$4,$3
    sll $3,$4,1
    subu    $2,$2,$3
    sw  $2,24($fp)
    lw  $2,20($fp)
    lw  $3,24($fp)
    addu    $2,$2,$3
    sw  $2,20($fp)
    lw  $2,16($fp)
    li  $3,1717960704           # 0x66660000
    ori $3,$3,0x6667
    mult    $2,$3
    mfhi    $6
    sra $3,$6,2
    sra $4,$2,31
    subu    $2,$3,$4
    sw  $2,16($fp)
    j   $L2
$L3:
    la  $4,$LC2
    lw  $5,20($fp)
    jal printf
    move    $2,$0
    j   $L1
$L1:
    move    $sp,$fp         # sp not trusted here
    lw  $31,36($sp)
    lw  $fp,32($sp)
    addu    $sp,$sp,40
    j   $31
    .end    main