Author Topic: Converting assembly to C  (Read 23674 times)

0 Members and 5 Guests are viewing this topic.

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 11140
  • Country: fi
Re: Converting assembly to C
« Reply #125 on: April 12, 2025, 06:27:36 pm »
I agree here. Having 20+ years of experience fixing & modifying the Linux kernel myself I can say the Linux kernel is a prime example of extremely poorly written C code.

I don't really disagree, but "prime example" "extremely poor" sound like exaggeration. I mean, it has quite decent track record of working, and the track record of it being maintained for over 30 years and scaling to things never originally imagined, while almost every other alternative failed to do that, means it can't be totally hopeless.

I can accept saying it's not ideal, or that it could be better, but I'm sure there will be much better "prime examples" of "extremely poorly written C code". For starters, maybe pick something that is so buggy that it does not work, or that completely breaks with every update, or requires massive amounts of man-hours and money to maintain? Linux kernel is not issue-free, but it also doesn't tick any of those boxes. It almost always Just Works and most issues on linux systems are on userland side. Sometimes someone steps on a footgun that could have been avoided by better coding practices but clearly this haven't caused enough pain to drive any kind of large-scale shift into better coding style. In other words, clearly it's good enough; I'm sure it's horrible for purists and idealists.

And academics especially hate the linux kernel, since it reminds them every day that such "amateurish" coding style "with goto and all" can be so succesful and work so well, when their elegantly designed software science ideas fail. Heck, I remember from Uni that even the existence of C language was too much to some. But practice always wins over theory.
« Last Edit: April 12, 2025, 06:33:39 pm by Siwastaja »
 
The following users thanked this post: SiliconWizard

Online nctnico

  • Super Contributor
  • ***
  • Posts: 30117
  • Country: nl
    • NCT Developments
Re: Converting assembly to C
« Reply #126 on: April 12, 2025, 06:50:07 pm »
Basically your argument is 'it works, so it can't be bad'. But that is the wrong way to look at it. Linux kernel coding style is like Evel Knievel coding. Only very highly 'skilled' people can write and maintain code like that. Judging by how Linus tends to react to (cancel) people wanting to simplify things you can argue he only likes coding acrobats / stunt people to work on the kernel. The problem is that not everyone is a coding acrobat and stunts tend to go wrong every now and then. Evel Knievel broke many bones in his carreer as a stunt man...

If you lead a team of programmers to work on a commercial project, you will want to lay down some coding style ground rules (like MISRA for example) and probably some additional ones as well to make sure the code has a certain level of quality (as in maintainability and testability) so it is easy (cheap) to maintain and your team doesn't require super skilled people to get a decent amount of work done. You may think Radiolistener is full of BS right now, but once you gain more coding experience on larger projects, you'll see that the suggestions made by Radiolistener do make sense (especially when working in bigger teams on large code bases). And if this sounds like a 'lowest common denominator' approach then the message has come across; that is the case. KISS rules.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 
The following users thanked this post: cfbsoftware

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 11140
  • Country: fi
Re: Converting assembly to C
« Reply #127 on: April 12, 2025, 06:59:46 pm »
Yet somehow the linux kernel is one of the largest software projects known to human kind, with some of the largest numbers of contributors popping in and doing something. Claiming that it requires extraordinary skills to do so cannot be right.

You can say that it's done in a wrong way and it's utter shit, but then your definition for what is shit is irrelevant, because it produced one of the best software projects known to human kind (relatively to the sheer size of the project and its scope, and number of contributors).

What is "good" and "bad" is matter of taste, but I prefer useful definitions; yes, because Linux kernel works very well, is maintainable in real world, and people come and go and make the required modifications easier than in most other projects, it must mean that code cannot be very bad, for a useful definition of "bad". Yes, it's this simple. I believe in track records and practical results over the opinions of nctnico, sorry.
« Last Edit: April 12, 2025, 07:02:34 pm by Siwastaja »
 

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 5083
  • Country: gb
Re: Converting assembly to C
« Reply #128 on: April 12, 2025, 08:00:47 pm »
Basically your argument is 'it works, so it can't be bad'. But that is the wrong way to look at it. Linux kernel coding style is like Evel Knievel coding. Only very highly 'skilled' people can write and maintain code like that. Judging by how Linus tends to react to (cancel) people wanting to simplify things you can argue he only likes coding acrobats / stunt people to work on the kernel. The problem is that not everyone is a coding acrobat and stunts tend to go wrong every now and then. Evel Knievel broke many bones in his carreer as a stunt man...

If you lead a team of programmers to work on a commercial project, you will want to lay down some coding style ground rules (like MISRA for example) and probably some additional ones as well to make sure the code has a certain level of quality (as in maintainability and testability) so it is easy (cheap) to maintain and your team doesn't require super skilled people to get a decent amount of work done. You may think Radiolistener is full of BS right now, but once you gain more coding experience on larger projects, you'll see that the suggestions made by Radiolistener do make sense (especially when working in bigger teams on large code bases). And if this sounds like a 'lowest common denominator' approach then the message has come across; that is the case. KISS rules.


I not only think exactly like you, Nctnico, but I also "measured" this matter!
Trying to certify the linux kernel for DO178B: result? not even the E level, the lowest!
And that's enough for me, as there are versions of VxWorks that pass DO178B/LevelA!

The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 5083
  • Country: gb
Re: Converting assembly to C
« Reply #129 on: April 12, 2025, 08:18:57 pm »
what is shit is irrelevant, because it produced one of the best software projects known to human kind

It's irrelevant as well as it's irrelevant what *you* consider "best": if we go by objective criteria, well...
... the kernel doesn't even pass level E. And it's not a matter of taste, it's a matter of facts.
(well, it's a matter of budget, to be honest ...
certifing Linux would cost more than buying a full licence of VxWorks/levelA)

I'm not saying that Linux sucks, I'm just saying that *for the industry*
there are objectively better ways to manage a project's code.
Which is what DO178B covers.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 5083
  • Country: gb
Re: Converting assembly to C
« Reply #130 on: April 12, 2025, 08:50:28 pm »
Only very highly 'skilled' people can write and maintain code like that. Judging by how Linus tends to react

Precisely.
worse, since Robert Love's patches (kernel 2.2-2.4), have split the community in two:
  • highly 'skilled' people can write and maintain code like that
  • common developers, who are struggling along, with crumbs falling from the table of highly 'skilled' people

A good example is SHARP kernels for the Japanese Zaurus PDAs.
When SHARP paid professionals to develop, fix and mantein the linux kernel (2.4) for the PDA, everything worked fine
When SHARP discontinued the Zaurus... things started to break quickly.

In this, it is not that Zaurus was of interest to only a few hobbyists.
It rather that people with high Linux skills tend not to work on anything that is paid less than 50USD/hour.
And without money, no one, or rather a few people in the whole world, have been able to fix those problems.

It took me more than 10 years to fully fix an old 2.6.23 kernel, and I can't afford anything else on my PDA!
All the other kernels, even the ones that the OE devs worked on between 2005 and 2015, are either totally unstable.
Or don't have working suspend, which on a portable device kills the battery in less than 45 minutes.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 8349
  • Country: fi
    • My home page and email address
Re: Converting assembly to C
« Reply #131 on: April 13, 2025, 05:34:12 am »
you'll see that the suggestions made by Radiolistener do make sense
That is a very generous interpretation from words like "However, it is important to understand that by [using goto], you are introducing poor quality shitty-code, and you should be aware of the potential consequences that may arise from it."

That, and its numerous repetitions using slightly different wording, is what I object.  As useful as some of Radiolistener's posts are to learners, I am seriously unhappy about how they utterly reject any rational criticisms of their opinions stated as facts.  I am unhappy, because it leads others astray.  The opinion itself is based on a logical fallacy –– that all uses of goto leads to "poor quality shitty-code" ––, as provably true and easier to maintain patterns with lower occurrence of bugs have already been shown and explained.  You can disagree however much you want, but that is an observable fact.  Just because you insist on calling the sky yellow doesn't make it so.

And telling someone not very experienced in C to avoid preprocessor macros is just sheer stupidity.  They are a very useful tool that when correctly used, makes project maintenance much easier.  Before C23, the only actual constants there are are literal constants and preprocessor macros (that preprocess to literal constants), for fucks sake!  const does not declare a constant; it is simply a promise by the programmer to the compiler that the code will not try to modify this variable in the current scope, that's all.  And if you want to do polymorphism (using e.g. C11 _Generic), like say signbit(x), you have to use preprocessor macros.

Claiming absolute rules like that apply is idiotic and counter to observable reality, and shows that the person hasn't had sufficient experience in diverse software projects, because otherwise they'd know from experience such rules always have their exceptions.  Doubling down, and not admitting it, is obnoxious social gaming that I detest, and is why I called radiolistener full of shit.

I compared the Linux kernel to other similar enterprise projects.  At the rate it is developed –– so rapid that even the largest companies have trouble keeping up –– its bug density is lower than in any enterprise projects I've seen.

The code you have in projects complying with MISRA-C or DO178B is not so because it is better written from the get go: it is so because it has gone through a rigorous review, test, and verification cycles.  Those standards are not coding standards, they are development process standards.

For minimal bug density C code, I did tell you to go look at Dan J. Bernstein's C code; and 5U4GB added Wietse Venema.



This thread went to shit the moment radiolistener told metertech58761 to not use goto and preprocessor macros.  It was just utter shit advice, stemming from misunderstanding process-based choices as axiomatic universal truths.  The "bad example" in #13 is just laughable: the exact same pattern is extremely useful if you just rename the macro from true to say enable_print.

Instead, we should have told metertech58761 and others reading the thread trying to convert assembly to C without that much experience in C that this intermediate form is not maintainable in long term, and needs to be refactored or rewritten to a more maintainable form without changing the functionality.  In fact, that was exactly what metertech58761 was asking: how to go on from here.

All of the patterns shown in the assembly code can be converted to easily maintained while or do..while loops and inlineable/static sub-functions.  (In C, "static" and "static inline" have the exact same effect on a function: it will only be accessible from the current compilation unit (typically current .c source file), and the compiler is free to inline the function to its caller, and not generate a callable function for it at all.  I use the distinction to help us humans distinguish between internal functions and accessor/helper functions.)

Instead of what not to do, radiolistener and others should have suggested what to do instead.  And that applies to you too, Picuino: instead of claiming "X is harmful", you should show that "Y is better than X, because Z".

This is a technical discussion.  Telling others "don't do that!" is useless –– no, trolling! –– when better alternatives are not shown.

I have now repeatedly tried to show better patterns, to no avail.  In #100, I recap the timeline.  In #110, I tried to go back to the interesting stuff, although I was still a bit too emotionally invested to have much thought in the patterns themselves; with #123 showing a better one (splitting the Repeat/Next into a separate function, then combining the rest of the loops into while loops and subfunction calls).  No bites.  You guys are more interested in nitpicking on what degree of "goto" and preprocessor macros in C can be considered "harmful", than actually looking at the actual code.  Nctnico claims to have decades of Linux kernel experience, but still complains how hard it is to fix things with it, especially because things change –– I bet they find the lack of stable internal APIs also a sign of "low quality code", even though the actual reasons are well known.  I've done similar work, even just to help other members here, and I just don't find it difficult to work with the Linux kernel code.  Perhaps it is because I've worked with all sorts of stuff, and not just regimented centrally-controlled codebases, so I've learned to understand better?  In any case, I just cannot take such complaints seriously, because they rationally simplify to "the code is horrible, because any modification I make causes bugs", which is not an indication of the code per se, but one of the programmer at hand.

I keep telling you, making arguments from authority –– be it your work experience or whatever else –– just does not cut it.  You need to show the actual better patterns, instead of claim they exist, or worse, just tell others to avoid some patterns without suggesting any replacements.  It is not just silly, it makes it very difficult for learners to discern which suggestions to follow.  Essentially, you're shittifying the entire thread with your crap.

Stop it.  Do like I've done, and suggest something better instead.
 

Offline Tation

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: pt
Re: Converting assembly to C
« Reply #132 on: April 13, 2025, 07:14:16 am »
Basically your argument is 'it works, so it can't be bad'. But that is the wrong way to look at it. Linux kernel coding style is like Evel Knievel coding. Only very highly 'skilled' people can write and maintain code like that. Judging by how Linus tends to react to (cancel) people wanting to simplify things you can argue he only likes coding acrobats / stunt people to work on the kernel. The problem is that not everyone is a coding acrobat and stunts tend to go wrong every now and then. Evel Knievel broke many bones in his carreer as a stunt man...

If you lead a team of programmers to work on a commercial project, you will want to lay down some coding style ground rules (like MISRA for example) and probably some additional ones as well to make sure the code has a certain level of quality (as in maintainability and testability) so it is easy (cheap) to maintain and your team doesn't require super skilled people to get a decent amount of work done. You may think Radiolistener is full of BS right now, but once you gain more coding experience on larger projects, you'll see that the suggestions made by Radiolistener do make sense (especially when working in bigger teams on large code bases). And if this sounds like a 'lowest common denominator' approach then the message has come across; that is the case. KISS rules.


I not only think exactly like you, Nctnico, but I also "measured" this matter!
Trying to certify the linux kernel for DO178B: result? not even the E level, the lowest!
And that's enough for me, as there are versions of VxWorks that pass DO178B/LevelA!

I've seen code, certified to the A level, that anybody with minimal experience will identify as ugly, utter crap, or using a term seen here, shitty. Thus, I do not see the relation between "quality" and "certifiable".

That code worked, so also no relation between correctness and quality. Well, there is a relation, quality code must be correct.

But, such code was hardly maintenable. In fact, sometime later, the company (a big one with government funding) dismissed parts of its codebase and recoded them from scratch, certification of the old code was costlier.
« Last Edit: April 13, 2025, 07:27:31 am by Tation »
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 30117
  • Country: nl
    • NCT Developments
Re: Converting assembly to C
« Reply #133 on: April 13, 2025, 09:49:30 am »
you'll see that the suggestions made by Radiolistener do make sense
That is a very generous interpretation from words like "However, it is important to understand that by [using goto], you are introducing poor quality shitty-code, and you should be aware of the potential consequences that may arise from it."
You are taking these claims way too literally. Look at the context instead. Radiolistener obviously works in an environment dealing with extremely large code bases where they probably use code analysis and automatic (unit) test tools (maybe even proprietary ones) and so on. In such environments you need to have strict rules to allow for the review process and code analysis / automated testing tools to work and actually catch potential bugs. Also keep in mind that every different execution path adds an extra test vector and thus testing time. However, in the context of this forum you really should read posts like Radiolistener writes as good advice to create maintainable code. I do. The bottom line is: think about the structure before writing code and don't obfustigate what doesn't need to be obfustigated (like having complex macros for things that are better implemented by a function). Avoiding complex macros and gotos are good mental excersises to get to code which has a better structure.

An example of a project where somebody just started coding without thinking about the structure: Very early on in my career I worked on a large C project for automating paid phone & TV services in hospitals. This software was written by a self thaught 'programmer'. Most was in a single C source file with very few functions. So I ended up printing it (on a matrix printer with continous paper). Even with 100 lines / page I ended up with a big stack of paper and then I annoted all the nested ifs, switches and for/while loops so I could get a sense what the code was doing. But it was clear the guy just started coding in a delerium. I recall needing to make a change to one part which consisted of an entire page of code. So I took out paper and pencil and drew out a flow-chart with the conditions. Subsequently I replaced the 100 lines of code with about 15 lines which did exactly the same + the modification.
« Last Edit: April 13, 2025, 10:09:28 am by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 8349
  • Country: fi
    • My home page and email address
Re: Converting assembly to C
« Reply #134 on: April 13, 2025, 09:57:26 am »
you'll see that the suggestions made by Radiolistener do make sense
That is a very generous interpretation from words like "However, it is important to understand that by [using goto], you are introducing poor quality shitty-code, and you should be aware of the potential consequences that may arise from it."
You are taking these claims way too literally.
No.

If you were right and I was wrong (about taking claims way too literally), metertech58761 would still be participating in this thread having progress with the task they started this thread for.  Because they aren't, and what I've described about how and why the thread fails to help, your interpretation is unrealistic.  I posit your stance is based on personal feelings and opinions, and not observable facts at hand.
 

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 5083
  • Country: gb
Re: Converting assembly to C
« Reply #135 on: April 13, 2025, 12:07:53 pm »
I've seen code, certified to the A level, that anybody with minimal experience will identify as ugly, utter crap, or using a term seen here, shitty. Thus, I do not see the relation between "quality" and "certifiable".

That code worked, so also no relation between correctness and quality. Well, there is a relation, quality code must be correct.

But, such code was hardly maintenable. In fact, sometime later, the company (a big one with government funding) dismissed parts of its codebase and recoded them from scratch, certification of the old code was costlier.


 :bullshit:

Given that the "goal of a code" is to satisfy constraints and requirements
For DO178B, in short, the "quality of the code" is a measurement vector that branches out into different aspects
  • (1) how efficient it is in time, rapid execution of algorithms
  • (2) how efficient it is in space, how much the data structures have been designed ad hoc to occupy the least possible memory
  • (3) estimate of how much time it takes to implement and test new features without having to rewrite the code entirely
  • (4) how much time it takes to write ad hoc test-cases to verify that the various constraints and requirements are satisfied in the various working conditions, i.e. { "normal", "abnormal", "simulated hw malfunction" }
    Plus, what is the probability that, in the case of { "abnormal condition", "hardware malfunctions" }, the code will behave in a way that is dangerous for the mission (level B, C), or mission + crew and pilots(level A);
    this involves activities to completely eliminate deadcode, and various points of "defensive code", especially in the "critical" parts
  • (5) how much time does the QA-team take to analyze the documents produced by { 1,2,3,4 } verifying, on the basis of the test-report documents and various produced, that the constraints and requirements given by the customer are actually respected.

Points 1, 2, 3 quantify the hours of engineering
Points 3 and 4 quantify the hours of testing
Point 5 determines customer satisfaction

Development activities focus primarily, first on design, then on error propagation, then on the final drafting of the documentation that describes these design aspects, finally on the implementation of the "engineering versions" (also known as "draft"), which are passed to the testing team, which produces documents on what is good, what needs to be changed, to minimize testing times!

Feedbacks here have the effect of producing
  • much more readable code (to make life easier for the QA team)
  • better structured (to make life easier for the testing team)

One of the goal of DO178B is to improve the code lifecycle as much as possible.
Which means making everyone within the various teams able to operate on the code and documentation.

Exactly the opposite of what is on Linux kernel.
Which is much more vertical, concentrating development, and the attention given
to public patches, only to developers with very high skills.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline metertech58761Topic starter

  • Frequent Contributor
  • **
  • Posts: 271
  • Country: us
Re: Converting assembly to C
« Reply #136 on: April 14, 2025, 11:36:24 pm »
I have to decide if I want to pursue this project any further.

Watching a seemingly simple question on the initial stages of the app rewrite, bloom into a hurricane in a thimble, leaves me with zero hope whatsoever for one of the more intricate (and critical) sections of code deeper within.  :palm:
 

Offline shabaz

  • Super Contributor
  • ***
  • Posts: 1011
Re: Converting assembly to C
« Reply #137 on: April 15, 2025, 12:03:34 am »
I have to decide if I want to pursue this project any further.

Watching a seemingly simple question on the initial stages of the app rewrite, bloom into a hurricane in a thimble, leaves me with zero hope whatsoever for one of the more intricate (and critical) sections of code deeper within.  :palm:

I don't see why that's an issue. One body of engineers doesn't have to agree with another body.

Some very successful people ask around just to get different opinions, before they make their own decision. You've got the benefit of multiple opinions, no-one deliberately gave you bad advice from what I can see, and just like others in the thread, I too certainly wouldn't agree with all of it either, but you've got the reasonings from people in the comments to make your decisions.

It's quite discouraging to see your criticism (not for the first time I believe) and when you blame your lack of enthusiasm on all the effort from your peers in writing the extensive comments above.

 

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Converting assembly to C
« Reply #138 on: April 15, 2025, 09:38:21 am »
I have to decide if I want to pursue this project any further.

Watching a seemingly simple question on the initial stages of the app rewrite, bloom into a hurricane in a thimble, leaves me with zero hope whatsoever for one of the more intricate (and critical) sections of code deeper within.  :palm:

I don't see why that's an issue. One body of engineers doesn't have to agree with another body.

Some very successful people ask around just to get different opinions, before they make their own decision. You've got the benefit of multiple opinions, no-one deliberately gave you bad advice from what I can see, and just like others in the thread, I too certainly wouldn't agree with all of it either, but you've got the reasonings from people in the comments to make your decisions.

It's quite discouraging to see your criticism (not for the first time I believe) and when you blame your lack of enthusiasm on all the effort from your peers in writing the extensive comments above.

Yes.

I wonder if the OP has got "better" responses from forums such as stackexchange/stackoverflow etc.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 

Offline metertech58761Topic starter

  • Frequent Contributor
  • **
  • Posts: 271
  • Country: us
Re: Converting assembly to C
« Reply #139 on: April 15, 2025, 01:24:56 pm »
I never got around to asking anywhere else. Besides, after seeing the reaction this spawned, people talking over each other, telling me to ignore other posters, etc... how does this help me?
What assurance have I that regulars at any other forum would be any more patient with my approach or questions?
This had always been a 'when I have time' thing. I found a couple other things I hadn't expected to find and which currently have my attention, and other RL tasks are taking priority.
 

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Converting assembly to C
« Reply #140 on: April 15, 2025, 01:33:11 pm »
I never got around to asking anywhere else. Besides, after seeing the reaction this spawned, people talking over each other, telling me to ignore other posters, etc... how does this help me?

That kind of thing happens all the time, in all walks of life.

One key life skill is working out ways to decide who to trust and to what extent.

Another key life skill is to work out to what extent other people's advice and experience applies to you. If you understand the general fundamentals, then it you are more likely to be able to see how specific tricks and techniques are or aren't relevant.

That's why theoretical and practical experience is valuable - and that takes time to learn.

Quote
What assurance have I that regulars at any other forum would be any more patient with my approach or questions?

None whatsoever, of course.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 
The following users thanked this post: Siwastaja


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf