将c \ c ++代码翻译成MIPS汇编程序

时间:2013-04-16 12:43:18

标签: assembly static mips

如何将此代码翻译成MIPS汇编程序

#include <stdio.h>

void func() {
    static int x = 0;
    printf("%d\n", x);
    x++;
}

int main() {
    func(); 
    func(); 
    func(); 
    return 0;
}

我不明白MIPS中的静态变量类比是什么

1 个答案:

答案 0 :(得分:1)

    .file   1 "myC.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:
    .sdata
    .align  2

x.2:
    .word   0
    .align  2
$LC0:
    .ascii  "%d\n\000"
    .text
    .align  2
    .globl  func
    .ent    func
func:
    .frame  $fp,24,$31      # vars= 0, regs= 2/0, args= 16, extra= 0
    .mask   0xc0000000,-4
    .fmask  0x00000000,0
    subu    $sp,$sp,24
    sw  $31,20($sp)
    sw  $fp,16($sp)
    move    $fp,$sp
    la  $4,$LC0
    lw  $5,x.2
    jal printf
    lw  $2,x.2
    addu    $3,$2,1
    sw  $3,x.2
$L1:
    move    $sp,$fp         # sp not trusted here
    lw  $31,20($sp)
    lw  $fp,16($sp)
    addu    $sp,$sp,24
    j   $31
    .end    func
    .align  2
    .globl  main
    .ent    main
main:
    .frame  $fp,24,$31      # vars= 0, regs= 2/0, args= 16, extra= 0
    .mask   0xc0000000,-4
    .fmask  0x00000000,0
    subu    $sp,$sp,24
    sw  $31,20($sp)
    sw  $fp,16($sp)
    move    $fp,$sp
    jal __main
    jal func
    jal func
    jal func
    move    $2,$0
    j   $L2
$L2:
    move    $sp,$fp         # sp not trusted here
    lw  $31,20($sp)
    lw  $fp,16($sp)
    addu    $sp,$sp,24
    j   $31
    .end    main

enter image description here

我执行此转换的首选工具链是MipsIT程序。