Nintendo DS Assembly Inerpreter

Discussion in 'NDS - Emulation and Homebrew' started by GEMISIS, Mar 28, 2013.

Mar 28, 2013
  1. GEMISIS
    OP

    Newcomer GEMISIS Advanced Member

    Joined:
    Jul 31, 2008
    Messages:
    53
    Country:
    United States
    Hey everyone,
    So, I just finished a simple Assembly Interpreter for the Nintendo DS/DSi. Got the idea from a professor who made one in php for a class of mine, so I made one in C :)
    I've put it online for anyone who wants to download it, as well as the source code for people to look at. It can be used to learn some basic bare bones assembly, and is pretty useful for that. Plus you can see all the memory values and register values on the top screen as it runs ^^

    Controls:
    Right: Progress to the next command.
    B: Run through the program without pausing (except for key input).
    Start: Force the program to end.Once the program has ended, press start to exit.

    Currently, it only loads test.asm from the root directory (or NitroFS, or if neither of those work, a built in one), but I plan to add a file browser to it later on. Overall, nothing too huge right now, just something for fun. :)

    Please note there are issues with NO$GBA due to the keyboard being used in this app.

    Source Code: https://github.com/gemisis/ASM-Interpreter
    App Download: https://github.com/gemisis/ASM-Interpreter/raw/master/ASM-Interpreter-DS/ASM-Interpreter-DS.nds

    Screenshots:
    [​IMG][​IMG][​IMG]


    The list of commands is as follows:

    add reg1, reg2 - Adds 2 registers and stores the results in AC. Example: add r1, r2
    sub reg1, reg2 - Subtracts reg2 from reg1 and stores the results in AC. Example: sub r1, r2
    mul reg1, reg2 - Multiplies reg1 and reg2 and stores the results in AC. Example: mul r1, r2
    div reg1, reg2 - Divides reg1 by reg2 and stores the results in AC. Example: div r1, r2
    mod reg1, reg2 - Performs the modulus function on reg1 with reg2 and stores the results in AC. Example: mod r1, r2
    store mem1, reg1 - Stores reg1 in mem1. Example: store m1, r1
    load reg1, mem1 - Loads reg1 with mem1. Example: load r1, m1
    copy reg1, reg2 - Copies reg1 to reg2. Example: copy r1, r2
    jmp label - Jumps to the chosen label. (label needs quotes around it) Example: jmp "labeltitle"
    set reg1, val - Sets reg1 with the value val. Example: set r1, 42
    jne reg1, label - Jumps to the label if reg1 does not equal AC. (label needs quotes around it) Example: jne r1, "labeltitle"
    jeq reg1, label - Jumps to the label if reg1 equals AC. (label needs quotes around it) Example: jeq r1, "labeltitle"
    cmp reg1, reg2 - Compares reg1 and reg2 and stores the results in AC. (0 if equal, -1 if reg1 < reg2, and 1 if reg2 > reg1) Example: cmp r1, r2
    print reg - Prints the register as an integer value. Example: print r1
    printc reg - Prints the register as a character value Example: print r1
    getc reg - Gets a character from the keyboard and stores it in the register. Example: getc r1
    mov reg1, reg2 - Moves register 2's value to register 1. Example: mov r1, r2
    mov reg1, #N - Moves value N to register 1. Example: mov r1, #0xff
    labels are defined with a colon at the end (no spaces between). Example: labeltitle:
    A # sign can be prefixed to all number values.


    -GEMISIS
     
  2. FAST6191

    Reporter FAST6191 Techromancer

    pip
    Joined:
    Nov 21, 2005
    Messages:
    21,738
    Country:
    United Kingdom
    Once I build a charger/fix an existing one for my DS I will have to give it a proper go. As it stands I tried sending http://imrannazar.com/The-Smallest-NDS-File through it but I sense I probably messed up something somewhere or it did not like part of the syntax there (for my part it was encoded in ANSI, windows new line).
     
    Rydian likes this.
  3. Coto

    Member Coto GBAtemp Addict

    Joined:
    Jun 4, 2010
    Messages:
    2,278
    Country:
    Chile
    very nice. Just took a read at main.c

    the assembly interpreter you've built can actually run the target ASM arquitecture(the program was built for) opcodes? or it stores everything on registers / stack and does basic jmp, ldi, add, sub, ldi, etc asm fixed functions?

    goood job I say, i'm learning assembly (between PPC, ARM and AVX extensions) so this could be useful.

    I'm on the phase of translating opcodes (from gb z80)
     
  4. GEMISIS
    OP

    Newcomer GEMISIS Advanced Member

    Joined:
    Jul 31, 2008
    Messages:
    53
    Country:
    United States
    It stores the program in an array of commands (structures that contain a command and its arguments), and then runs through each command. The actual language it's using is very bare-bones.

    The list of commands is as follows:

    add reg1, reg2 - Adds 2 registers and stores the results in AC. Example: add r1, r2
    sub reg1, reg2 - Subtracts reg2 from reg1 and stores the results in AC. Example: sub r1, r2
    mul reg1, reg2 - Multiplies reg1 and reg2 and stores the results in AC. Example: mul r1, r2
    div reg1, reg2 - Divides reg1 by reg2 and stores the results in AC. Example: div r1, r2
    mod reg1, reg2 - Performs the modulus function on reg1 with reg2 and stores the results in AC. Example: mod r1, r2
    store mem1, reg1 - Stores reg1 in mem1. Example: store m1, r1
    load reg1, mem1 - Loads reg1 with mem1. Example: load r1, m1
    copy reg1, reg2 - Copies reg1 to reg2. Example: copy r1, r2
    jmp label - Jumps to the chosen label. (label needs quotes around it) Example: jmp "labeltitle"
    set reg1, val - Sets reg1 with the value val. Example: set r1, 42
    jne reg1, label - Jumps to the label if reg1 does not equal AC. (label needs quotes around it) Example: jne r1, "labeltitle"
    jeq reg1, label - Jumps to the label if reg1 equals AC. (label needs quotes around it) Example: jeq r1, "labeltitle"
    cmp reg1, reg2 - Compares reg1 and reg2 and stores the results in AC. (0 if equal, -1 if reg1 < reg2, and 1 if reg2 > reg1) Example: cmp r1, r2
    print reg - Prints the register as an integer value. Example: print r1
    printc reg - Prints the register as a character value Example: print r1
    getc reg - Gets a character from the keyboard and stores it in the register. Example: getc r1
    labels are defined with a colon at the end (no spaces between). Example: labeltitle:

    I'll add this to the first post as well XD

    -GEMISIS
     
  5. FAST6191

    Reporter FAST6191 Techromancer

    pip
    Joined:
    Nov 21, 2005
    Messages:
    21,738
    Country:
    United Kingdom
    Given the DS technically does not have a div isn't that kind of cheating (also perhaps some clarification as to how it handles remainders)? Though I suppose splitting mov into copy and set rather than go for immediate and register values (an odd choice for a learning tool but I can respect that decision) is probably the bigger talking point.
     
  6. GEMISIS
    OP

    Newcomer GEMISIS Advanced Member

    Joined:
    Jul 31, 2008
    Messages:
    53
    Country:
    United States
    Again, this is meant to be more of a general assembly tool (Not specifically for the DS). Currently adding in mov and such though so that people can use either one. Will update the github once I'm done. :)

    Edit: Got around to adding mov (Haven't had much free time since posting this). Going to work on adding more tomorrow and over the weekend!

    As far as this goes, would you guys be in favor of a system that allows for the basics from multiple assembly languages (IE: x86 and ARM) on one unified test system? I'm thinking of doing this and see how it goes, so let me know what you guys think!
     
  7. FAST6191

    Reporter FAST6191 Techromancer

    pip
    Joined:
    Nov 21, 2005
    Messages:
    21,738
    Country:
    United Kingdom
    What could be somewhat interesting is the y86 - a kind of simplified version of the X86 processor and usually used as a teaching tool for assembly ( http://www.plantation-productions.com/Webster/ being a good example of its use).

    The trouble is I am not sure what goes- I never really went in for high level languages (not a path I encourage others to follow) and most of the trouble with assembly, after the initial fight past getting IO sorted, is dealing with the quirks like nested registers in X86 and the GBA/ARM ARM's THUMB mode, the absence of certain functions like divide (which is kicked to a coprocessor or BIOS function a lot of the time), to a slightly lesser extent lacking a full boolean logic complement, having to have memory in a separate instruction in the case of ARM if you learned X86 to start and in modern X86 trying to think your way around branch prediction and pipelining all on top of the other quirks like DMA restrictions/protocol and maybe X86 security (rings and such like).
     
  8. BassAceGold

    Member BassAceGold Testicles

    Joined:
    Aug 14, 2006
    Messages:
    494
    Country:
    Canada
    Looks neat! Might I suggest loading files through argv for now? That way you don't have to code up a file browser immediately and people can test their own stuff.
     
  9. GEMISIS
    OP

    Newcomer GEMISIS Advanced Member

    Joined:
    Jul 31, 2008
    Messages:
    53
    Country:
    United States
    Been a busy past few days, but finally managed to get all of the changes up to Github. Newest compiled binary allows for argv file loading (didn't think about that so thanks Bass!), and also allows for you to type in a file name at the beginning of the program. Also added support for the getC method to use input from a second set of arguments passed to the file (which can also be given through a 2nd argv variable after the file name, or also typed in after giving a file name). you can try out the argv stuff with the default test file, as it uses the getc method. Have a few more things I'm gonna add to the next release (a cursor while typing sounds really useful, as well as more commands you can call :) ), so keep your eyes open for it!
     
  10. GEMISIS
    OP

    Newcomer GEMISIS Advanced Member

    Joined:
    Jul 31, 2008
    Messages:
    53
    Country:
    United States
    For those interested, I have submitted this to NeoCompo, and updated the app to allow you to run more than one file per use. Contemplating adding a built in editor for files too, but we'll see how time permits on that as I have other projects I'm working on too!

    Link: http://www.neoflash.com/forum/index.php/topic,7641.0.html
     
  11. Coto

    Member Coto GBAtemp Addict

    Joined:
    Jun 4, 2010
    Messages:
    2,278
    Country:
    Chile
    today as soon as I arrive home, i'll test some PPC assembly on wii through linux.
    --
    Perhaps a good idea would be to support ARM directly, given a limited memory area. Whereas opcodes would throw on screen CPU output
     
  12. GEMISIS
    OP

    Newcomer GEMISIS Advanced Member

    Joined:
    Jul 31, 2008
    Messages:
    53
    Country:
    United States
    Again, this is more so newcomers can learn the very basics of assembly. It's more of a simplified assembly language than a full fledged one. If you're trying to learn how assembly languages work, or just want to write some simple samples, this is a pretty good one to use, but for more complex things, not so much.
     
  13. Gryphon93

    Member Gryphon93 GBAtemp Regular

    Joined:
    Nov 30, 2008
    Messages:
    145
    Country:
    Sweden
    A nice and interesting project. I'll try it out soon and I'm looking forward to it. It would be nice if one could load a NDS file and run through the binaries. Though you would have to add support for overlays in that case. Nice work, GEMISIS!
     
  14. GEMISIS
    OP

    Newcomer GEMISIS Advanced Member

    Joined:
    Jul 31, 2008
    Messages:
    53
    Country:
    United States
    Hope you enjoy it! I just went through and fixed some bugs with the keyboard and some other small things (Wohoo for the shift key finally! lol) and also added a text editor! You can now create files on the system (No editing files as of yet though).
     
  15. GEMISIS
    OP

    Newcomer GEMISIS Advanced Member

    Joined:
    Jul 31, 2008
    Messages:
    53
    Country:
    United States
    Just wanted to let everyone know that some more bug fixes and features have been added! Here's the current list for today:
    -Fixed some more bugs (specifically with scrolling text)
    -Added the ability to scrolling through text input (Press up to scroll up and down to scroll down)
    -Added a file browser for selecting items! Press A to select a file/folder and B to cancel!
     

Share This Page