Prototyping ASM in a higher level language

Programming, for all ages and all languages.
moonchild
Member
Member
Posts: 73
Joined: Wed Apr 01, 2020 4:59 pm
Freenode IRC: moon-child

Re: Prototyping ASM in a higher level language

Post by moonchild »

eekee wrote: C has goto. :) It can't jump out of a function
Sure it can! longjmp is there, just waiting to be (ab)used!
User avatar
eekee
Member
Member
Posts: 827
Joined: Mon May 22, 2017 5:56 am
Freenode IRC: eekee
Location: Hyperspace
Contact:

Re: Prototyping ASM in a higher level language

Post by eekee »

moonchild wrote:
eekee wrote: C has goto. :) It can't jump out of a function
Sure it can! longjmp is there, just waiting to be (ab)used!
:lol: Yeah, "[C] does not prevent you doing stupid things because that would also prevent you doing clever things."

Isn't it curious how easy it is to forget C has goto, even when you know about longjmp?
Kaph — a modular OS intended to be easy and fun to administer and code for.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
User avatar
Wukl
Posts: 8
Joined: Tue Jul 05, 2011 5:17 am
Location: Delft, Netherlands
Contact:

Re: Prototyping ASM in a higher level language

Post by Wukl »

eekee wrote: Isn't it curious how easy it is to forget C has goto, even when you know about longjmp?
Reminds me of Longjmp - FOR SPEED!!!.

More on-topic: in the computer organization course that I TA, we encourage to initially "play compiler" to learn common control flow patterns. We even specifically ask them to implement a recursive function in Java first, also because this may be the first time they encounter recursion as a concept.

But after that they should just write assembly straight from the specification. If they want to implement more complex algorithms such as sorting, we'd rather have them implement it from pseudo-code rather than translating each statement and expression from C. The goal of the course is to get the students to think in assembly, not C or Java.

I'd say building an OS in assembly has the same kind of goal. If you'd rather build a working OS, use a "higher-level language" (C, maybe C++, Rust and so on, but not Lua). The best way to learn assembly is by just writing assembly. Preferably first in a well-supported environment, where you have a C library to fall back on and a debugger to single-step through your code. And it doesn't have to be perfect -- my sorting algorithm turned out to have a runtime complexity of O(n² log n), but it usually sorted fewer than 25 items so it was good enough.
JohnpaulTH
Posts: 20
Joined: Sat May 23, 2020 6:51 pm

Re: Prototyping ASM in a higher level language

Post by JohnpaulTH »

Wukl wrote: But after that they should just write assembly straight from the specification. If they want to implement more complex algorithms such as sorting, we'd rather have them implement it from pseudo-code rather than translating each statement and expression from C. The goal of the course is to get the students to think in assembly, not C or Java.
Gracious! I would not want to think in Java! Seriously though, I think there is a lot of wisdom in this. I am not writing an operating system because I want to make the next fully featured Operating system that supports all hardware and emulates all computers hitherto. I want something more like DOS, but with less functionality. A hobby OS where everything is exactly like I want it. I also want to gain understanding about how computers work, and I have gained a lot of that. It is all about control for me, and assembly gives me the most control.
User avatar
eekee
Member
Member
Posts: 827
Joined: Mon May 22, 2017 5:56 am
Freenode IRC: eekee
Location: Hyperspace
Contact:

Re: Prototyping ASM in a higher level language

Post by eekee »

@JohnpaulTH: Your goals aren't very different to mine. (I'm also planning a novel UI, but that could be a separate project.) I also wanted control and simplicity, but as I struggled with assembly, I realised it would help if I made and stuck to a standard for subroutine calls; a simple ABI. Knowing a bit about Forth, I know a Forth compiler can be made as if it were just one step up from a macroassembler. Preferring Forth's ultrasimple design over the peculiarities and relative limitations of Nasm's syntax and macro system, I switched to Forth. I don't think I've lost any worthwhile control, or rather I will have more than enough control when I write my Forth compiler. However, despite its simplicity, I'm finding writing the compiler both tough and boring, so I keep getting distracted by more fun things.
Kaph — a modular OS intended to be easy and fun to administer and code for.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
User avatar
Wukl
Posts: 8
Joined: Tue Jul 05, 2011 5:17 am
Location: Delft, Netherlands
Contact:

Re: Prototyping ASM in a higher level language

Post by Wukl »

JohnpaulTH wrote:Gracious! I would not want to think in Java!
Very! Especially considering that they will use Java for most of the CS program. :lol:
JohnpaulTH wrote:I am not writing an operating system because I want to make the next fully featured Operating system that supports all hardware and emulates all computers hitherto. I want something more like DOS, but with less functionality. A hobby OS where everything is exactly like I want it. I also want to gain understanding about how computers work, and I have gained a lot of that. It is all about control for me, and assembly gives me the most control.
Awesome, that's how I started as well. Have fun :)
User avatar
iansjack
Member
Member
Posts: 4604
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Prototyping ASM in a higher level language

Post by iansjack »

JohnpaulTH wrote:
Wukl wrote:It is all about control for me, and assembly gives me the most control.
That's really not true - at least not in the sense that you need to write the whole OS in assembler. The most control is obtained by using the appropriate language for the situation. So you write those (very few) parts of the system where low-level control is needed in assembler. The rest you write in a far more organised language, such as C, which will make the code clearer, easier to maintain, and almost certainly more efficient.

IMO, one of the biggest mistakes a beginner can make is to restrict themselves to assembler. An experienced assembler programmer, with extensive knowledge of algorithms, data structures, and how to implement them, may be able to write an entire OS in assembler. But mere mortals, like most of us, are setting ourselves up for failure in imposing this totally artificial limitation.
JohnpaulTH
Posts: 20
Joined: Sat May 23, 2020 6:51 pm

Re: Prototyping ASM in a higher level language

Post by JohnpaulTH »

iansjack wrote:That's really not true - at least not in the sense that you need to write the whole OS in assembler.
I don't need to write it all in assembler to get the most control, but given how simple and low level my OS is, there is hardly anything that I wouldn't lose control over to some degree. I am interested in higher-level languages for writing programs, but building a compiler targetting my operating system is a large project in itself. So I would be expending a lot of energy to create something I would use for about 25% of the OS, when I could gain mastery of assembly in the same or shorter amount of time.
User avatar
iansjack
Member
Member
Posts: 4604
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Prototyping ASM in a higher level language

Post by iansjack »

You don't need to write a C compiler that runs on your OS, any more than you need to write an assembler. (Not a trivial task, especially as you are limiting yourself to assembler.)

If you just want to learn assembly language then fair enough (although I would say that writing an OS is the very worst way of learning assembly language). But it's your project, and you know what you want to get out of it, so good luck with it. I just want to point out to other beginners that they should think very carefully before restricting themselves to assembly language. It seems obvious that this will lead to the most efficient OS, but in practice it almost certainly leads to a limited, buggy, inefficient result. (Unless you are an expert assembly language programmer.)
User avatar
eekee
Member
Member
Posts: 827
Joined: Mon May 22, 2017 5:56 am
Freenode IRC: eekee
Location: Hyperspace
Contact:

Re: Prototyping ASM in a higher level language

Post by eekee »

eekee wrote:Forth Forth Forth
And now I'm thinking of using a web server written entirely in assembly language. :mrgreen: It's rwasa. I'm not sure though. Its benchmark graphs look a bit suspicious, does "12b" mean 12 bytes? I suppose so, because one graph has "128kb". 12 bytes is too small even for HEAD requests, but rwasa's performance falls behind on the 128"kb" test. There is one thing I really don't like about it. I'm glad I spotted this in the documentation: "Due to the lack of verbosity with rwasa for configuration issues, often the easiest way to locate errors is with strace." I could link the associated library for amd64 assembly coders, but would anyone want a library under the GPLv3, not even LGPL? Anyway, it's here.

But anyway, on topic, I'm restarting my Forth compiler and this time, I'm prototyping it in Forth for conversion to assembly. I don't expect the complete Forth code to run, but I do want to test components. Looking at the last attempt, I remember the problem wasn't just boredom. I was uncertain of some design details, developing it under DOS with an 80x25 screen didn't help, and I was coding for real mode which is not what I really want; I'd have to do it all again. This time, I'm working on Win10, targetting amd64 and Win10 to get started.
Kaph — a modular OS intended to be easy and fun to administer and code for.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
User avatar
Thomas
Member
Member
Posts: 281
Joined: Thu Jun 04, 2009 11:12 pm

Re: Prototyping ASM in a higher level language

Post by Thomas »

Hi JohnpaulTH,
See:
https://holub.com/compiler/
to get an idea how you would do an assembler like emulation in a high level language ( C ). The intermediate code section of this book would be useful.
The book is a tome. Section 6.2 is what I would encourage you to read for your purposes. This is for C, but you could do the same in Lua with a bit of imagination.

--Thomas
Post Reply