What's new

How to convert CHIP8 assembly coode to the C++ language

HAT39

New member
hello everybody,

I'd like to convert CHIP8 assembly coode to the C++ language but i want to know from where i start to do it

is there who do it before or it is difficult
 

zaba_3

New member
So basically you are trying to make an CHIP8 emulator? :p
I don't want to be rude but if you don't know where to start or have no idea how to do it,you propably aren't capable of doing it.
 
OP
H

HAT39

New member
thank you for your reply

i'm not trying to make a CHIP8 emulator after you make a debuger of CHIP8 you will have the source code in assembly what i want to do is how translate the assembly code to c++
 

Exophase

Emulator Developer
thank you for your reply

i'm not trying to make a CHIP8 emulator after you make a debuger of CHIP8 you will have the source code in assembly what i want to do is how translate the assembly code to c++

Are you asking how to write a program to do it automatically or how to do it by hand?
 
OP
H

HAT39

New member
hello Exophase

yes i want to write a program to do it automatically for example when i dissasemble a game i will have it's assembly code i want to translate it c++ for example
 

Exophase

Emulator Developer
hello Exophase

yes i want to write a program to do it automatically for example when i dissasemble a game i will have it's assembly code i want to translate it c++ for example

If you just want a one to one conversion you can change each instruction to something similar for C++ or some other different semantic representation than the "standard" ASM mnemonics for chip-8. Since you just want it to annotate disassembly it doesn't have to work as code per-se, that's a much harder (and probably generally unsolvable) problem. It also doesn't have to really fit C++ code exactly for every instruction. But unless chip-8 assembly is confusing to you for some reason this isn't going to add anything.

You'd have to decode the instruction just like you do in the emulation, then print a string using the instruction parameters... honestly this is such a simple concept that it's hard to really know how much more to explain.

But what you're probably asking for is a way to convert several instructions into higher level C++ representations. This is called "decompilation", although in this case it'd be applied to code that was never high level to begin with. This is a very open ended problem that is difficult to even do small amounts of. If you try to do this you'll probably be in way over your head.. and if you don't realize why this is too difficult a task to work on right now then you're probably even worse off..
 

Toasty

Sony battery
Yeah, this is deceptively complex. Pretty easy to do poorly, but hard to impossible to do correctly. C++ is a high-level language that is meant to be compiled; it's not machine instructions that are meant to be directly executed. Things like self-modifying code would be pretty impossible to translate consistently. (Though I don't know that any of the typical CHIP-8 ROMs use self-modifying code?) A disassembler is the way to go if you want to make a CHIP-8 program more readable.
 
OP
H

HAT39

New member
thank you very much Exophase for your explanation
yes i want to convert several instructions into higher level language representations(decompilation)

Toasty
i don't want to compile c++ language , we have the the chip8 opcodes what i want to do is convert its instructions to any high-level language
 
Last edited:

smcd

Active member
I would think 'translating' it into assembly and then making it into something like a COM (DOS yay!) type conversion would be somewhat attainable. But as I never finished my emulator (stupid graphics, mumble mumble) take that thought with a grain of salt the size of texas :)
 

Toasty

Sony battery
i don't want to compile c++ language , we have the the chip8 opcodes what i want to do is convert its instructions to any high-level language
I didn't mean to imply otherwise. My point was that C++ code is evaluated in an earlier context than opcodes, so there are some things a CHIP-8 program can do that can't be cleanly expressed in C++.
 

Exophase

Emulator Developer
I recommend reading this thesis:

http://itee.uq.edu.au/~cristina/dcc/decompilation_thesis.ps.gz

But there's no way you're ready to attempt something like this.

It'll be much harder to get good quality decompilation from chip-8 programs than the types of programs most decompilers target because they make several assumptions about the code that you can't. For instance, that a standard ABI is used between functions defining what registers are used to pass and return values and what are allowed to be live after the function returns. They can even go so far as to recognize particular idioms from specific compilers.

Chip-8 programs are small. Try converting an entire one by hand first. If you can't do that then there's no way you'll be able to write algorithms to do it.
 
Last edited:

Toasty

Sony battery
A disassembler basically just translates the binary opcodes from a ROM into human-readable assembly code.
 

Top