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
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”
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.
I wasn’t interested at all until I read your comment. 😜
It’s like fukcing a chicken, while playing checkers.
At the head of a small town parade, where everybody knows you.
Duh. What would be the point, otherwise?
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!”
How can I work gcc in windows, please send tips. Thank You
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.
If you want a more modern approach, check out Linux Subsystem for Windows.
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.
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.
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 .
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.
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 ;)
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.
And then you graduated kindergarden
Wait. If a kindergarten is able to teach Python to a chicken…
For something simpler, just try user-mode Linux. (ARCH=um)
Have a look at JOR1K open risc-v in browser, runs Linux/C, has Web access.
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.
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.
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.
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.
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.
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..
Fabrice Bellard did it first, with JSLinux: https://bellard.org/jslinux/tech.html
I thought for sure that’s what this was going to be
If you think that is cool take a look at this.
It has boxed wine and a bunch of ther things all running in the browser.
Drag and drop an EXE for 32 bit windows 2000 or xp in to the browser and watch it run
This is awesome, I found that guys github, its amazing the work he has done
Why people uses emojis everywhere? It is like the whole population is in 5th grade.
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?
And also, why didn’t they add a letter ‘i’ with heart-shaped dot, so we can REALLY return to 5th grade?
Oh. You can’t imagine my horror, when I thought for a moment that there was actually a heart-shaped one.
Because human history is circular… we’re just re-inventing Egyptian Hieroglyphs.
Since Facebook the internet is eternal september…in grade school.
now… could we compile docker for the browser?
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.
Thanks for suggestion.
g++ -S (or was it -s ) cppcode.cpp
Will generate gcc compatible working assembly file for anything working on c , except closed source linked libraries.
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. :)
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
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. :)
2 Rasperry Pi 4B.
3 :) Android A11 Samsung cell phone.
A11 displays screen on ROKU following Utube wireless instructions, we found.
Please be kind and respectful to help make the comments section excellent. (Comment Policy)