如何将三个地址代码转换为MIPS汇编语言? [英] How to convert a three address code to MIPS Assembly language?

查看:656
本文介绍了如何将三个地址代码转换为MIPS汇编语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个项目,在这个项目中,我必须创建一个转换程序,该转换程序将为C代码生成MIPS汇编代码.正在使用的编程语言是C ++,直到生成三个地址代码为止,我一直都在做,并且对如何继续进行感到非常困惑.

I am doing a project in which I have to create a translator that would generate a MIPS assembly code for a C code. The programming language that am using is C++ and I have done till generation of three address code and am really confused about how to proceed further.

推荐答案

如上所述,它是直接翻译.真的没有什么要澄清的.例如,使用以下三个地址的代码:

As already stated, it's a direct translation. There's really nothing to clarify. As an example, take the following three-address code:

      i := 0                  ; assignment
L1:   if i >= 10 goto L2      ; conditional jump
      t0 := i*i
      t1 := &b                ; address-of operation
      t2 := t1 + i            ; t2 holds the address of b[i]
      *t2 := t0               ; store through pointer
      i := i + 1
      goto L1
L2:

MIPS的翻译是:

        li $t0, 0             #allocator assigned i to t0
L1:     bge $t0, 10, L2    
        mult $t1, $t0, $t0  
        la $t2, b             
        add $t3, $t2, $t0   
        sw $t1, ($t3)       
        addi $t0, $t0, 1
        j L1
L2:

如果您有幸拥有这样的三个地址,则几乎无需执行任何操作.找到相应的操作码以配合该指令.寄存器分配已经完成.如果三地址代码实际上是一串字符串,我会考虑编写一个小型解析器(使用生成器),而不是尝试从字符串中提取信息.

If your lucky enough to have three-address like that, you barely have to do anything. Find the corresponding opcode to go with the instruction. The register allocation has already been done. If the three-address code is literally a bunch a strings, I'd consider writing a small parser (using a generator) instead of trying to extract information from the strings.

这篇关于如何将三个地址代码转换为MIPS汇编语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆