1

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

#include <stdio.h>

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

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

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

4

1 回答 1

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

在此处输入图像描述

我首选的用于进行这种转换的工具链是 MipsIT 程序。

于 2013-05-22T20:51:43.227 回答