MARS MIPS和结构的节点 [英] MARS MIPS and struct nodes

查看:309
本文介绍了MARS MIPS和结构的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

typedef struct node {    
    int data;    
    struct node *next;    
} nodeL; 

假设我要翻译成MIPS汇编语言上面的声明,我怎么办呢?除了分配内存(使用系统调用9),这是在.text段完成后,怎么样的。数据段
此外,关于什么的定位

推荐答案

甚至提code之前,你需要澄清是否要创建将静态数据(数据段),局部数据结构(栈)或动态分配的数据(堆)。有不同的方式来分配每个

Before even mentioning code, you need to clarify whether the struct you want to create will be static data (data segment), local data (stack), or dynamically allocated data (heap). There are different ways to allocate each.

不过,在讨论之前,你需要做的第一件事是确定结构实例的布局。最起码它可以是:

But before discussing that, the very first thing you need to do is determine the layout of the struct instance. At the very least it could be:

------------------
| data - 32-bits |
------------------
| next - 32-bits |
------------------

要静态地创建一个实例,它只是:

To create an instance statically, it's simply:

    .data
    .align 2
anInstance:     .word   0,0

和堆:

    .text
Allocator.newNode:
    li $a0, 8           #allocate 8 bytes
    li $v0, 9
    syscall             #returns word-aligned ptr
    jr $ra

如果放置在栈上,简单地分配给它8个字节。

If placing on the stack, simply allocate 8 bytes for it.

一个更清洁的方法是使用一个基于原型的方法。

A cleaner way is to use a prototype-based method.

您对象布局变为:

------------------
| size - 32-bits |
------------------
| atr 1 - 32-bits|
------------------
| atr 2 - 32-bits|
------------------
.
.
.
------------------
| atr n - 32-bits|
------------------

对于每个结构,创建该分配程序将用于创建实例的原型。

For each struct, you create a prototype which the allocation routine will use to create the instance.

    .data
ListProto:      .word   8,0     #size, head ptr
NodeProto:      .word   12,0,0  #size, data, next ptr    

    .text
main:
    la $a0, ListProto
    jal Allocator.newObject     #create new list instance

    la $a0, NodeProto
    jal Allocator.newObject     #create new node instance

Allocator.newObject:
    lw $a0, 0($a0)      #a0 = object size
    li $v0, 9
    syscall
    jr $ra

无论你想实例实际上保持大小字段是由你。这种做法,你可以简单地添加的原型,就是这样。

Whether you want the instance to actually keep a size field is up to you. With that approach you can simply add prototypes and that's it.

这篇关于MARS MIPS和结构的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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