如何将数组从C转换为mips

时间:2013-09-23 17:13:25

标签: c mips

我有代码C:

for (i=0; i<98; i++) {
    C[i] = A[i+1] - B[i+2] * A[1];
}

阵列A,B,C的第一个地址是A000h,B000h,C000h

我转换为mips

 addi $a0,$zero,A000h
 addi $a1,$zero,B000h
 addi $a2,$zero,C000h
 li $t0,1
 li $t1,98

loop:

 addi $a0,0

 addi $t2,$a0,4
 addi $a1,8
 addi $a2,0
 lw   $t3,0($a0)
 lw   $t4,4($t2)
 lw   $t5,8($a1)
 mult $t5,$t5,$t3
 sub  $t6,$t4,$t5
 sw   $t6,0($a2)
 addi $t0,1
 bne  $t0,$t1,loop

检查它,谢谢

1 个答案:

答案 0 :(得分:0)

您可以将C编译为MIPS程序集,如果有帮助,请查看输出。以下C:

int main ()
{
  int i;
  int A[99], B[99], C[99];
  for (i=0; i<98; i++) {
    C[i] = A[i+1] - B[i+2] * A[1];
  }
}

使用我的编译器编译此MIPS代码

    .file   1 "loop.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:
    .text
    .align  2
    .globl  main
    .ent    main
main:
    .frame  $fp,1232,$31        # vars= 1208, regs= 2/0, args= 16, extra= 0
    .mask   0xc0000000,-4
    .fmask  0x00000000,0
    subu    $sp,$sp,1232
    sw  $31,1228($sp)
    sw  $fp,1224($sp)
    move    $fp,$sp
    jal __main
    sw  $0,16($fp)
$L2:
    lw  $2,16($fp)
    slt $3,$2,98
    bne $3,$0,$L5
    j   $L3
$L5:
    lw  $2,16($fp)
    move    $3,$2
    sll $2,$3,2
    addu    $4,$fp,16
    addu    $3,$2,$4
    addu    $2,$3,808
    addu    $3,$fp,28
    lw  $4,16($fp)
    move    $5,$4
    sll $4,$5,2
    addu    $3,$3,$4
    addu    $4,$fp,432
    lw  $5,16($fp)
    move    $6,$5
    sll $5,$6,2
    addu    $4,$4,$5
    lw  $5,0($4)
    lw  $4,28($fp)
    mult    $5,$4
    lw  $3,0($3)
    mflo    $7
    subu    $4,$3,$7
    sw  $4,0($2)
$L4:
    lw  $2,16($fp)
    addu    $3,$2,1
    sw  $3,16($fp)
    j   $L2
$L3:
$L1:
    move    $sp,$fp         # sp not trusted here
    lw  $31,1228($sp)
    lw  $fp,1224($sp)
    addu    $sp,$sp,1232
    j   $31
    .end    main