Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

c - MARS MIPS and struct nodes

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

Assuming I want to translate the above declaration in MIPS assembly language, how am I supposed to do it? Apart from allocating memory (using syscall 9), which is done in the .text segment, what about the .data segment? Also, what about alignment?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

And on the heap:

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

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

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

Your object layout becomes:

------------------
| 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...