Linux And C In The Browser

There was a time when trying to learn to write low-level driver or kernel code was hard. You really needed two machines: one to work with, and one to screw up over and over again until you got it right. These days you can just spin up a virtual machine and roll it back every time you totally screw up. Much easier! We don’t think it is all that practical, but [nsommer] has an interesting post about loading up a C compiler and compiling Linux for a virtual machine. What’s different? Oh, the virtual machine is in your browser.

The v86 CPU emulator runs in the browser and looks like a Pentium III computer with the usual hardware. You might think it is slow and it certainly isn’t going to be fast as a rocket, but it does translate machine code into WebAssembly, so performance isn’t as bad as you might think.

The post goes into detail about how to build and create a simple machine web page that hosts v86. Once you cross-compile the kernel you can boot the machine up virtually. The other interesting part is the addition of tcc which is a pretty capable C compiler and much smaller and faster than the very traditional gcc.

The tcc build is tricky because the normal build process compiles the compiler and then uses the same compiler to build the default libraries. When cross-compiling, this doesn’t work well because the library you want for the host compile is different from the library you want to target for the second pass. You’ll see how to work around that in the post. The post continues to show how to do remote debugging and even gets QEMU into the mix. Debugging inside v86 doesn’t seem to work so far. There are more posts on this topic promised.

Honestly, this is one of those things like teaching a chicken to play checkers. It can be done, there’s little practical value, but it is still something to see. On the other hand, if you spend the weekend working through this, your next Linux porting project ought to seem easy by comparison.

Amazing what you can pull off with WebAssembly. If you need a quick introduction, check this one out from [Ben James].

45 thoughts on “Linux And C In The Browser

  1. Oh, HELL no. This is like Linux From Scratch, only the target machine is an interpreter. So you get all the convenience of building a complete operating system from source packages, PLUS all of the performance of running it all in Javascript! Yes, Javascript, since that’s what the CPU emulator is written in.

    It’s not like teaching a chicken to play checkers; it’s like teaching a chicken to build an injection molding machine to make checkers, THEN teaching it to play checkers.

        1. Well a lot of rural small towns, a thing like that would get you ostracized… they’d be all “There goes that Jim.. did you see what he did at the parade? Acting like he’s smarter than everyone else by knowing how to play checkers!”

      1. Look into mingw (MINimal Gnu for Windows) and MSYS2. MSYS2 is a Unix-like environment for Windows, and mingw is the GCC and its associated tools, including vi and make. But a warning here – not all of your C programs will compile, because underneath, it’s using the MS C runtime library bult into Windows, where proper GNU Linux uses glibc, and there are a number of differences. Tjere is also a similar project, Cygwin, which has what is supposed to be a more compatible runtime library,

        Also, if you want to write applications that use the graphical user interface, and can be built for Windows, Linux, MacOS, and other platforms, you can try FLTK, which in Windows can use either the mingw compiler or the free version of MS Visual Studio to do the actual builds, and on the Mac, can use the free Xcode tools from Apple. One source tree, all platforms.

        1. Will do. :)

          Goal is to get embedded controller Intel MCS BASIC-52 working on BOTH x86 and ARM processors. In 64-bit mode. So BASIC-52 has access BEST FP math instructions.

          Both BASIC-52 and enbedded controller fig Forth share common dictionary construction functions. So why recode? Let’s try to build BASIC-52 dictionary construction using fig Forth code?

          But we need to show how interface works. Prologs and epilogs need modification from c interfaces to 64-bit BASIC-52 pro/epilogs.

        2. I could be wrong about this, but I think that Linux Subsystem for Windows doesn’t create Windows executables. That is, executables created there can only run in Linux Subsystem for Windows. Both MSYS/MinGW and MS Visual Studio provide executables that don’t require any Linux on the system they’re being run on.

          1. Weong. Mingw is gcc confugired to compile gor win32/64 platforms. It can use windows dlls and use in general. The ,ain differences is it uses some modifies wersions of dlls where win dll become unnecessarly complex ie mfc amd dotnet dlls are hawing unnecessarily calls from each others functions and sometime function calls refer to function inside patched dll or some weirdly named dlls , mingw fixes that sometimes with redirectinf calls to something clear. Bug all in general its standard windows executables that are in general linked to (needs ) mingw specific libragiea and gcc has options to re redirect that back to windows version specidic dlls .

    1. Well.. if it runs on javascript…thats great because javascript is the most debug compatible platworm on earth it seems. So that means anything on that wirtual pc is supports debugging/byte tracking of anything inside ;)

    2. Well it’s WebAssembly, so it’s not completely interpreted like JS (which is JIT now days anyway). A developer compiles a supported language (C, Rust,…) to byte code (basically assembly but not for a specific CPU), and then the browser ‘interprets’ it into the CPUs instructions, similar to Java and C#.

      I’ve heard that WebAssembly also only has a ~20% difference compared to native performance, so it’s not as slow as you’re thinking.

      1. I think 80% of native code speed is pretty wishful thinking. I don’t know of any bytecode that runs near that fast. Still, even if JIT Javascript (after compiling) is at least 2/3 of native code speed, I guess I’d have to consider that not a complete non-starter.

      2. Then if we have time… we can use open source browsers like chrome …and use its opensource js-webassembly interpreter (by adding tracking code inside ) for debugging as last choice if standard methods doesnt work. Which probably means we can see anything cpu does from gods eyes… which probaly will be boring in general,still with lots of interesting stuff ;)

      3. You tell me something I do not know. I am cautioned.

        Intel MCS BASIC-52 is an embedded controller Basic.

        Embedded controller technology works for software requirements satisfaction.

        NSA required that no software, other than the app, be left on the microcontroller.

        And in event of software failure a visual and audio warning must be reported. :)

        Us Sandia labs embedded controller software engineers had to meet NSA software requirements.

        Boeing hardware engineers’ software standards document guided us.

  2. I’m not even sure what value there is in teaching a person to play checkers.

    Or even in playing checkers period.

    Actually at least 90 percent of this stuff we do is playing checkers.

    So let’s just drop this “practical value” rubbish and move on.

    1. You teach children to play checkers to get them started on planning ahead.
      I bet even Bobby Fisher played checkers first.
      Developing strategic thinking has great practical value.

      Adults still playing checkers? Everybody has limits, they are just dumb. Not their fault. World needs ditch diggers too.

      1. Oh, I seriously doubt THAT (the Bobby Fisher thing). Checkers is the BASIC of the board game world – once you’ve played it, it ruins you for any proper game, and there is no way to un-learn it.

        1. Bobby Fisher was 4 once.

          I bet every grandmaster, ever, played checkers first.
          I bet every 13 dan played gobang first.
          I bet every world series of poker winner played ‘go fish’ first.

  3. You know though. This x86 in a browser thing could have more interesting uses than running linux. ARM in a browser would be much much better than the doggone x86. I person could get unix edition 7 running in the browser with something like this for example.

  4. Want to play a game?

    I had enough of Linux converting XP to Puppy to run it.
    Love to have some of my olsd hardware useable.
    Not going to do it

    Please take my money and soul, Microsoft and Goggle..

    1. Because they are human, and to be human is to do stupid, stupid things. The better question would be, WHY, for the love of all that is sensible, did they put them in the Unicode table?

  5. Can any c/c++ programmer show us how to call “Hello World” writen in 64-bit NASM with machine code held within a c DATA or DIM array? Print Wello World to the terminal or new window. Return to c program which returns to A linux.

      1. Gathering as many good ideas as possibe. Goal to try get Intel MCS BASIC-52 working in a c array. Use code from fig Forth to implement … whith little work. Try to get embedded controller software tachnology working inexpensiive hardware. Sandia labs retiree project. And Sandia Labs approved BASIC and FORTH book writer. :)

  6. the claimed difference is jvm is designed to run java
    web assembly was initially designed to run c and rust
    with static types and manual memory management by default
    (it originally only supported i32 i64 f32 and f64 default)
    though I have heard of slowness for one reason or another on some occasions
    lack of native vector math. the fact that all memory accesses are bounds checked. lack of threading support. etc.
    but at least a few of these problems were addressed and one is unavoidable

    but another problem is that web assembly is susceptible to native exploits that your native cpu protects you against
    so I don’t blame you for being doubtful

    1. Comments and google seachces have changed my mind about MASM approach to bringing up Intel MCS BASIC- 52 on BOTH X86 and ARM processors.

      Try to write Forth/Basic 52 primative modules in c 64-bit c/c++, see if c modules work. :)

      Recompile c modules on Raspberry Pi 4B and see if they still work? :) After binaries. How binaries constructed unimportant.

      If so, tnen export the 64-bit macnine code modules to a file … to be imported/linked into BASIC-52 embedded controler LANGUAGE SYSTEM by changing pro/eplogs.

      Then link modules togehter to bring BASIC-52 to life. Using forth, of course. :)

      1 x86
      2 Rasperry Pi 4B.
      3 :) Android A11 Samsung cell phone.

      A11 displays screen on ROKU following Utube wireless instructions, we found.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.