Thread: Struct node - from C to MIPS assembly language.

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    69

    Struct node - from C to MIPS assembly language.

    Code:
    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?

    I am using the MARS simulator.

    PS: I hope this is not too off-topic, but I have found no other relevant forum.
    Last edited by Xpl0ReRChR; 04-20-2012 at 03:13 AM.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    It should be in "tech" at best.

    I don't know about the rest of that but system level allocators have a tendency to allocate memory with a certain granularity and page size.

    Soma

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    Well I'm getting the following: Runtime exception at 0x00400080: store address not aligned on word boundary 0x00000001.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    What you are interested in is padding.

    Soma

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    I have absolutely no idea what you mean.

    I should have mentioned I'm using MARS, which is a MIPS simulator.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Alignment, granularity, pages size, and padding are all different things relating to where variables need to be and where they wind up.

    When discussing system level allocators:

    "Alignment" is where variables need to be to work or at least for best results.
    "Granularity" is the size of the smallest reserved chunk of memory.
    "Page Size" is the size of the smallest committed chunk of memory.
    "Padding" is the fixed distance necessary to add between one variable and the next so that consecutive variables are where they need to be.

    You said "Alignment" and posted a structure; I kind of just assumed you were worried about needing to adjust the position of the structure which you almost certainly don't need to do because pages are almost certainly going to be correctly aligned for any native variable when the memory comes from a system level allocator.

    What you are really concerned about is padding. I don't know what you need to add to the structures offset in memory to get the pointer correctly aligned.

    I can give you an example. If the pointer needs to be aligned to 8 bytes and the `int' variable you are using is 2 bytes you need to add an additional 6 bytes.

    If the system doesn't give you reasonably aligned memory from a system level allocator you'll have to adjust the alignment of the structure as well.

    *shrug*

    Emulators don't necessarily follow the real thing exactly. I remember using an "ARM" emulator that never complained about things that clearly shouldn't be allowed.

    Soma

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Though I don't know MIPS assembly language I know ARM pretty well, and I'm confused by the question. So, .text usually contains instructions, and .data contains data. A C struct declaration is neither of these things. I guess you're asking how to allocate memory for it? What do you have so far?


    I scoured around for some documentation of how syscall 9 behaves on the MARS simulator, but doesn't seem to be very helpful. I don't think there is any guarantee of alignment of the returned address (though as has been said, it wouldn't be unreasonable to expect it to be quite well aligned e.g. on a page boundary).

    The MIPS ABI says that structures should be as aligned as their most aligned member. So this'll come down to the size of the pointer -- is it MIPS32 or MIPS64? Suppose it's a 64-bit pointer, then your struct needs to be 8 bye aligned and will look like:
    Code:
    Offset  
    0           int data;    
    4           4 bytes padding
    8           struct node *next;
    And will have a total size of 16 bytes.

    Assuming the address returned from the syscall isn't aligned, you can align it to 8 bytes like (I don't know MIPS asm at all so check this, I just looked at a reference briefly)
    Code:
     # address in $t0
      add $t0,$t0,7
      li $t1,7
      nor $t1,t1,t1
      and $t0,$t0,$t1
    No idea if that is remotely legal MIPS Given the address, add 7 to it, then AND it with NOT(7) (0xffffffff8) to clear the bottom 3 bits. Bottom 3 bits clear means 8 byte aligned. The addition makes sure you don't end up with an address below the one you allocated.

    So then you'll have an aligned address in $t0.
    Code:
    lw   $t1 ($t0)      # load word from address in $t0 and put in $t1, that's your "int data"
    ld    $t2 8($t0)    # Eww. I've never appreciated ARM syntax so much. Load doubleword from address ($t0 + 8) and put in t2, that's your next pointer
    And so on and so forth. Adjust appropriately if you're on a 32-bit system with 32-bit pointers.

    I don't think you need/want to do anything in the .data segment. I'm not sure -- MIPS may handle this differently, but on ARM if translating from C, you'd put your initialised global variables in there. So if you had:
    Code:
    nodeL initnode = {5, NULL};
    You'd put it in the .data section:
    Code:
        .data
    
        .align 3 
    initnode:
        .word 5
        .space 4 # or .align 3
        .double 0

    What are you trying to accomplish anyway? If you're trying to learn assembly language I'd definitely not recommend doing it by directly translating C. I'd say start from a problem (e.g. "read a string, invert the case of all the characters and print it") and try to solve it.
    If you have some C you want to run, get a compiler and a simulator that lets you load compiled binaries.
    If you want to understand the relation between C and MIP assembler, get a compiler and a disassembler (e.g. GNU tools have gcc and objdump) and have a play, look at the output, maybe copy some of it into MARS. Wouldn't help you with this as a call to malloc is just a call to malloc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assembly Language Help ;)
    By shrink_tubing in forum C++ Programming
    Replies: 24
    Last Post: 03-11-2011, 12:47 AM
  2. convert c programm to mips assembly
    By mariakat in forum Tech Board
    Replies: 8
    Last Post: 02-14-2011, 08:39 PM
  3. Converting C into MIPS Assembly
    By clag in forum C Programming
    Replies: 5
    Last Post: 02-13-2010, 07:48 PM
  4. Assembly language.
    By JOZZY& Wakko in forum Tech Board
    Replies: 0
    Last Post: 12-18-2009, 05:58 AM
  5. A question of the MIPS assembly program
    By ok_good in forum Tech Board
    Replies: 0
    Last Post: 05-03-2006, 10:13 PM

Tags for this Thread