[NXC] Implementing Pointers

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

[NXC] Implementing Pointers

Post by muntoo »

If one* were to implement pointers, how would they do it?

Miscellaneous questions:
  • What assembly instruction set is NBC closest to? (x86? "Intel syntax"? I'm a beginner to this stuff. :))
  • How would the RXE byte code look like for the "pointers code"?
  • Where would the changes have to be made in:
    • The firmware (c_cmd.c?)
    • The compiler (implementing it in NBC, then the syntax tree/etc stuff for the NXC compiler)
    • The RXE executable file format
  • NEW: Which [free!!] compiler shall I use? :)
*No one in particular.
Last edited by muntoo on 20 Jul 2011, 21:42, edited 1 time in total.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: [NXC] Implementing Pointers

Post by spillerrec »

iirc, every variable has an ID. I do not know how the firmware actually handles it but here is how I think it works: To read/write to the variable the firmware will need to look that ID up and return the corresponding variable and type.
A pointer variable type should be added, but instead of having a position in memory it should contain an ID to a variable. It would need its own special type and store the ID as an unsigned int. When the firmware looks up the variable, it should notice the pointer type and instead of returning the result it should look it up again but with the new ID. (A recursive process, with the danger of looping.)

Every opcode which receives the pointer will then act on the variable it points to and never the ID it actually contains. (IIRC you will not be able to directly access members of a pointed structure, you would have to move it into a temporary structure of same type.)
The mov-opcode will also not affect the ID, it will affect the pointed variable. A 'set-address' opcode will do that instead, first parameter is the pointer and second parameter is the address. A 'get-address' opcode will then return the ID of the second parameter into the first.
A bit of a warning though, an ID of 0 is valid, a NULL pointer would actually be an entirely different value... (Can't remember it though, I think it was something in the lines of 6xxxx.)
(Edit: I changed this part significantly since my earlier idea wasn't very C like...)

(Dynamically created variables could be stored in those as long they are given an ID, but that is an entirely different matter.)

Well, this is my idea of how a simple pointer implementation would work on the standard firmware.
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
timpattinson
Posts: 224
Joined: 30 Oct 2010, 04:10
Location: 127.0.0.1
Contact:

Re: [NXC] Implementing Pointers

Post by timpattinson »

muntoo wrote:
  • NEW: Which [free!!] compiler shall I use? :)
For what?
The FW, or stealing their code?
I know you can create an ARM project in Code::Blocks, with gcc
screen.jpg
Maybe if would work better with IAR embedded Workbench (free 128kb limited version, for the nxt), which the Lego devs used
Commit to Lego Mindstorms StackExchange Q&A http://area51.stackexchange.com/proposals/4105
Minboards IRC Channel #mindboards on Freenode
My blog: http://timpattinson.wordpress.com/
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 2 guests