0

Is it possible to compile assembly or x86/x64 code to JavaScript?

Or asm -> llvm-ir -> js?

UPD: So my final question is: I have asm listing (excerpt)

push %rbp
mov %rsp,%rbp
sub $0x10,%rsp
mov 0x5e41(%rip),%eax
lea 0x601a(%rip),%rdx
cltq
movzbl (%rdx,%rax,1),%eax
test %al,%al
jne loc_0040121b
movl $0x0,-0x4(%rbp)
jmp  loc_00401206

Can I run it in browser without modifications with help of emscriptten or any other tool?

5
  • 1
    What should be the result? Commented Oct 4, 2016 at 10:32
  • The fact is that asm is not compiled, it is assembled. If you want to emulate a machine in JS maybe it's easier to just use the binary output from a conventional assembler. Commented Oct 4, 2016 at 11:08
  • @Ped7g I want to run executable(small program) in browser without access to original source code Commented Oct 4, 2016 at 18:40
  • A quick search didn't lead to any x86_64 emulator, only 32b x86 found. For example this looks impressive bellard.org/jslinux (check technical notes, it's basically full 80486 emulation). Actually no wonder, as JS can't natively handle 64b numbers (only about 53b), so it would have to emulate register values as BigInteger -> probably additional 10x performance burden to the already slow emulation. You can maybe try some disassembler which is trying to reconstruct C code, but I think those died ~15-20 years back, when C compiler optimizers started produce too specialized machine code. Commented Oct 5, 2016 at 19:05
  • @Ped7g this one looks more interesting in my opinion copy.sh/v86 BTW: IDA Pro is still feeling good :) Commented Oct 5, 2016 at 20:03

1 Answer 1

1

I want to run executable(small program) in browser without access to original source code

So, you actually don't want to compile assembly to js, as you don't have the source. Which is good, as it doesn't make any sense anyway, asm is not like high-level languages having some variables and affecting platform trough some API, in asm basically any instruction can change state of platform a lot, and "variables" is the whole accessible memory.

A way how it makes sense:

You want to execute some machine code already prepared for some target platform (CPU + OS).

"In browser" (directly) it's not possible, browser doesn't process machine code of that CPU, neither does it provide the OS API/ABI/syscall. That executable needs the target OS, so it's instructions have the desired effect.

You can run emulator of that OS written in JS, and run the binary inside the emulator. Search for example for web-online dosbox (running old DOS games in browser, with reasonable speed and not-worst-accuracy, most of them actually being playable and looking as they should).

How to create emulator of your target platform is a bit too broad question and takes somewhat more effort, like decades in some cases. But you may be lucky, that there already exists emulator of your target OS, and has JS port and license allowing you to use it.

There's also second option: if it's really small program, it would be very likely simpler to rewrite it from scratch in JS and in OS/browser agnostic way.


edit: about machine code -> llvm ir:

Let's say simply "no". (although probably like 50% of machine code would be reasonably easy to translate, but the other half would require the translator to fully understand what the code does, which is fully NP problem (or not possible at all)).

Sign up to request clarification or add additional context in comments.

5 Comments

I think that running OS is overkill for such case, but I somehow forgot that calls to libc are platform dependent operations :)
@OwnageIsMagic: well.. yeah. And now consider the machine code even doesn't have to bother to call libc at all, and can access HW/OS directly.
I can grantee that program doesnt perform any interupts/syscals (explicit)
asm and machine code are 1:1 (for a given platform). Why you reference them like something very different?
@OwnageIsMagic: because when you have binary file, you have machine code. To get Assembly you have to first disassemble it. But if you assemble that back into machine code, you may get sometimes slightly different executable, in special case where some instruction has two+ possible opcodes (official/unofficial) and disassembler hides that. Also binary header fields from executable are usually not part of Assembly source, so it's another thing generated from scratch during compilation => another room for different bytes output. So relying on machine code only is safer option in emulation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.