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

0 Members and 1 Guest are viewing this topic.

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Converting assembly to C
« Reply #25 on: March 30, 2025, 10:34:11 am »
I have some assembly code I want to eventually rewrite into C, and I have isolated the four main loops (there's still an interrupt, but that comes later).
...
So, how would I go about doing this?

Determine what the application (or part of application) is specified to do. Use any technique necessary to end up with a black box specification.

Then ignore the current implementation, and completely reimplement that specification in the language of your choice.

If you are required to reimplement X exactly including bugs, find another job.
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: Buriedcode

Online 5U4GB

  • Super Contributor
  • ***
  • Posts: 1723
  • Country: au
Re: Converting assembly to C
« Reply #26 on: March 30, 2025, 03:55:43 pm »
Is the code hand-written or the output of a compiler?  There are some pretty decent decompilers around that can often recreate the original C, you can use the dogbolt decompiler explorer to see which one works best.
 

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Converting assembly to C
« Reply #27 on: March 30, 2025, 06:10:09 pm »
Is the code hand-written or the output of a compiler?  There are some pretty decent decompilers around that can often recreate the original C, you can use the dogbolt decompiler explorer to see which one works best.

With -O3 etc optimisation? "Reversing" some of these optimisations is relatively straightforward, but others aren't: https://medium.com/@guannan.shen.ai/compiler-optimizations-46db19221947 I'm sure there are others :)

Don't forget that the variable qualifiers and compiler flags aren't available and won't be reinserted by the decompiler. Without those, any modification and recompilation of the decompiled code might cause it to subtly fail.
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 radiolistener

  • Super Contributor
  • ***
  • Posts: 5733
  • Country: Earth
Re: Converting assembly to C
« Reply #28 on: March 30, 2025, 06:40:06 pm »
Gotos can be useful in C, but only for particular purposes described.

I'm not aware of any specific scenarios where goto would be genuinely useful. Could you provide some examples?

Update: After some thought, I came up with a scenario where goto could be useful—if your goal is to obfuscate code, making it difficult to read and analyze, then goto can certainly help with that. This might be useful when writing viruses or other malicious software, but it has no place in conventional software development.

goto can be particularly effective when you need to make code extremely convoluted, transforming what would otherwise be a clean and structured execution flow into something resembling an explosion at a spaghetti factory. By scattering jumps across different parts of the program, it becomes significantly harder to understand, maintain, and debug—qualities that are the exact opposite of what good software engineering aims for.
« Last Edit: March 30, 2025, 09:07:05 pm by radiolistener »
 

Offline metertech58761Topic starter

  • Frequent Contributor
  • **
  • Posts: 271
  • Country: us
Re: Converting assembly to C
« Reply #29 on: March 30, 2025, 09:05:53 pm »
Good grief, Charlie Brown!

You'd think that I'd ripped a loud and vile-smelling one in the middle of a wedding or something.

Mind you, this code I presented as the starting point was AFTER I'd already gone through and rearranged it for a clearer flow, and the second round came after further insight.

I ALREADY try and replace as many goto statements as I can. As I gain a better understanding of this code, I'll have future opportunities to rewrite the code for better flow.
And, the act of rewriting the code has helped me to understand what it's doing, giving me further opportunities to streamline it even more.

I mean, COME ON. You can't expect everyone to have a [censored] PhD in CompSci to write C
 

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 5733
  • Country: Earth
Re: Converting assembly to C
« Reply #30 on: March 30, 2025, 09:16:11 pm »
Your code is extremely difficult to read and understand due to the excessive use of goto statements and labels. This creates chaotic control flow, making the execution fragmented, hard to follow, and prone to errors. Maintaining and modifying such code is unnecessarily complicated.

There is absolutely no need to use goto here. Instead, you should structure your logic using functions, loops, and conditional statements. Break the code into smaller, meaningful functions, and use structured programming principles to guide the execution flow naturally.

And let’s be clear - this isn't some advanced topic that requires a professor to solve. Any schoolboy with a little experience can rewrite this in a structured way without goto. It’s just a matter of approaching the problem correctly.

If you're struggling to refactor a specific part, describe what you're trying to achieve, and I'll help you restructure it properly.


The best way in this case in my opinion is just to discard this code entirely and start fresh with a properly structured approach. Trying to untangle and refactor this goto-ridden code will likely take more effort than writing a new, well-organized version from scratch.

If you don't have extensive experience, deciphering what this code actually does will be extremely challenging. Understanding such an unstructured flow requires deep knowledge of both low-level programming concepts and high-level software architecture, as well as significant experience in reading and restructuring complex code.

Instead of struggling with this, it's far more efficient to rethink the logic and implement it properly using functions, loops, and clean control flow from scratch. You'll end up with code that's easier to maintain, debug, and extend in the future.

In short, writing code without goto doesn’t require a PhD in Computer Science or even extensive experience. However, thoroughly understanding and untangling a codebase filled with goto statements—like the one you've presented—is a genuinely complex task. It demands significant expertise, deep knowledge of programming at both low and high levels of abstraction, and extensive experience. In this case, the skills and knowledge of a PhD in Computer Science would actually be highly beneficial.

Looking at this code, it's quite evident that the person who wrote it didn’t fully understand what the code actually does. The excessive use of goto and arbitrary jumps suggests a lack of structured design and a poor grasp of fundamental programming principles.

Because of this, refactoring this code properly is a difficult task - not just because you need to rewrite it correctly, but because you first have to decipher and reverse-engineer a messy and flawed implementation to even understand what it was supposed to do in the first place.
« Last Edit: March 30, 2025, 09:37:21 pm by radiolistener »
 

Offline Picuino

  • Super Contributor
  • ***
  • Posts: 1460
  • Country: es
    • Picuino
Re: Converting assembly to C
« Reply #31 on: March 30, 2025, 09:27:45 pm »
Gotos can be useful in C, but only for particular purposes described.

I'm not aware of any specific scenarios where goto would be genuinely useful. Could you provide some examples?

Update: After some thought, I came up with a scenario where goto could be useful—if your goal is to obfuscate code, making it difficult to read and analyze, then goto can certainly help with that. This might be useful when writing viruses or other malicious software, but it has no place in conventional software development.

goto can be particularly effective when you need to make code extremely convoluted, transforming what would otherwise be a clean and structured execution flow into something resembling an explosion at a spaghetti factory. By scattering jumps across different parts of the program, it becomes significantly harder to understand, maintain, and debug—qualities that are the exact opposite of what good software engineering aims for.

https://github.com/torvalds/linux/blob/master/fs/ext4/migrate.c

Linux kernel sources are plenty of examples.
« Last Edit: March 30, 2025, 09:57:27 pm by Picuino »
 

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 5733
  • Country: Earth
Re: Converting assembly to C
« Reply #32 on: March 30, 2025, 09:50:59 pm »
https://github.com/torvalds/linux/blob/master/fs/ext4/migrate.c

Linux kernel are plenty of examples.

The example you provided is not a case where goto should be used, rather, it's an instance of improper usage of goto, likely stemming from the author's lack of experience with structured programming in C.

The use of goto in this case mirrors low-level assembly-style thinking, which is often a result of an individual coming from an assembly language background. While assembly languages often use jump instructions like goto, C provides more expressive and structured control flow mechanisms that make the code more readable and maintainable.

Lets see how it use goto:
Code: [Select]
    if (retval < 0)
goto err_out;
path = ext4_ext_insert_extent(handle, inode, path, &newext, 0);
if (IS_ERR(path))
retval = PTR_ERR(path);
err_out:

this code fragment can be easily replaced with this one, which don't needs to use goto at all:
Code: [Select]
    if (retval >= 0) {
    path = ext4_ext_insert_extent(handle, inode, path, &newext, 0);
    if (IS_ERR(path))
    retval = PTR_ERR(path);
    }

This approach achieves the same result but avoids unnecessary jumps, improving the clarity of the logic.

In summary, this example demonstrates an unnecessary use of goto rather than a legitimate need for it. It's a common mistake made by programmers transitioning from low-level languages, where jumps are more common, to higher-level languages like C. With more experience in C, one would recognize that such a structure can be replaced with better alternatives.
 

Offline Tation

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: pt
Re: Converting assembly to C
« Reply #33 on: March 30, 2025, 10:07:33 pm »
Gotos can be useful in C, but only for particular purposes described.

I'm not aware of any specific scenarios where goto would be genuinely useful. Could you provide some examples?

Reading the book "Modern C" by Jens Gustedt will be, no doubt, beneficial in this case.
 

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 5733
  • Country: Earth
Re: Converting assembly to C
« Reply #34 on: March 30, 2025, 10:47:46 pm »
Reading the book "Modern C" by Jens Gustedt will be, no doubt, beneficial in this case.

I didn't ask for recommendations on what to read in order to write poor-quality code, as I strive to avoid that. I asked for specific examples where the use of goto is truly necessary and beneficial. Could you provide such examples?

From my experience, when you feel the urge to use goto, it usually indicates that something in your design or code structure is wrong. It suggests that perhaps the wrong design approach or structure has been chosen. In such cases, rather than resorting to goto, it's better to take a step back, analyze the code, and choose more suitable structures that eliminate the need for goto altogether.

If your goal is to learn how to write intentionally convoluted and low-quality code, I would suggest to learn the Brainfuck programming language written by Urban Müller. Here's an example of code written in this language:
Code: [Select]
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
You can do similar things in C, for example, here is working C code which demonstrate macros using:
https://github.com/ioccc-src/winner/blob/master/2020/endoh3/prog.c
Code: [Select]
#define/**/Q(x,y)char*/*                               */q=y#x","#y")",*p,s[x;}
  /*IOCCC'20*/#include/*                               */<stdio.h>/*-Qlock-*/
    int(y),x,i,k,r;Q(9/*              12               */<<9];float(o)[03];
      void(P)(){*o=r<0/*         11         1          */?r:-r;o[1]=39.5;
       o[2]=22.5;for(k/*      10               2       */=0;++k<39;*o*=i
        /6875.5/(k%2?k/*                               */:-k))y=o[1+k%2
         ]+=*o;k=o[2];/*     9         o-------> 3     */p=s+y+k/2*80;
          }int(main)()/*              /                */{for(p=s;+i<
          1839;*q>32?k/*       8     L         4       */=i++/80-11,y
           =(750>r*r+k/*          7         5          */*k*4)*4+y/2
           ,*p++=r<41?/*               6               */y?"0X+0X+!"
           [y-1]-1:+*q/*                               */++:10:*q++)
           r=i%80-38;;/*                               */;for(x=13,r
           =20;i=3600*/*     \       /    -------+     */--x,i;*p++=
          "OISEA2dC8e"/*      \     /     ------ |     */[x%10],*p+=x
          /10*41)P();r/*       \   /      ------ |     */=10;;sscanf(
         __TIME__,"%d"/*        \ /       ------ |     */":%d:%d",&k,&
        x,&i);for(i+=(/*         X        ------ |     */k*60+x)*60;18+
       r;*p=k%2?*p%2?+/*      __/ \__         |  |     */59:44:*p>39?59:
      39,i=!r--?i%3600/*     /  \ /  \        |  |     */*12:i)P();puts(s
    ),"#define/**/Q(x"/*     \__/ \__/        +--+     */",y)char*q=y#x\","
  "\"#y\")\",*p,s[x;}"/*                               */"/*IOCCC'20*/#inclu"
"de<stdio.h>/*-Qlock-"/*                               */"*/int(y),x,i,k,r;Q(")

Another example with macros:
https://github.com/ioccc-src/winner/blob/master/2020/carlini/prog.c
Code: [Select]
#include <stdio.h>

#define N(a)       "%"#a"$hhn"
#define O(a,b)     "%10$"#a"d"N(b)
#define U          "%10$.*37$d"
#define G(a)       "%"#a"$s"
#define H(a,b)     G(a)G(b)
#define T(a)       a a
#define s(a)       T(a)T(a)
#define A(a)       s(a)T(a)a
#define n(a)       A(a)a
#define D(a)       n(a)A(a)
#define C(a)       D(a)a
#define R          C(C(N(12)G(12)))
#define o(a,b,c)   C(H(a,a))D(G(a))C(H(b,b)G(b))n(G(b))O(32,c)R
#define SS         O(78,55)R "\n\033[2J\n%26$s";
#define E(a,b,c,d) H(a,b)G(c)O(253,11)R G(11)O(255,11)R H(11,d)N(d)O(253,35)R
#define S(a,b)     O(254,11)H(a,b)N(68)R G(68)O(255,68)N(12)H(12,68)G(67)N(67)

char* fmt = O(10,39)N(40)N(41)N(42)N(43)N(66)N(69)N(24)O(22,65)O(5,70)O(8,44)N(
            45)N(46)N    (47)N(48)N(    49)N( 50)N(     51)N(52)N(53    )O( 28,
            54)O(5,        55) O(2,    56)O(3,57)O(      4,58 )O(13,    73)O(4,
            71 )N(   72)O   (20,59    )N(60)N(61)N(       62)N (63)N    (64)R R
            E(1,2,   3,13   )E(4,    5,6,13)E(7,8,9        ,13)E(1,4    ,7,13)E
            (2,5,8,        13)E(    3,6,9,13)E(1,5,         9,13)E(3    ,5,7,13
            )E(14,15,    16,23)    E(17,18,19,23)E(          20, 21,    22,23)E
            (14,17,20,23)E(15,    18,21,23)E(16,19,    22     ,23)E(    14, 18,
            22,23)E(16,18,20,    23)R U O(255 ,38)R    G (     38)O(    255,36)
            R H(13,23)O(255,    11)R H(11,36) O(254    ,36)     R G(    36 ) O(
            255,36)R S(1,14    )S(2,15)S(3, 16)S(4,    17 )S     (5,    18)S(6,
            19)S(7,20)S(8,    21)S(9    ,22)H(13,23    )H(36,     67    )N(11)R
            G(11)""O(255,    25 )R        s(C(G(11)    ))n (G(          11) )G(
            11)N(54)R C(    "aa")   s(A(   G(25)))T    (G(25))N         (69)R o
            (14,1,26)o(    15, 2,   27)o   (16,3,28    )o( 17,4,        29)o(18
            ,5,30)o(19    ,6,31)o(        20,7,32)o    (21,8,33)o       (22 ,9,
            34)n(C(U)    )N( 68)R H(    36,13)G(23)    N(11)R C(D(      G(11)))
            D(G(11))G(68)N(68)R G(68)O(49,35)R H(13,23)G(67)N(11)R C(H(11,11)G(
            11))A(G(11))C(H(36,36)G(36))s(G(36))O(32,58)R C(D(G(36)))A(G(36))SS

#define arg d+6,d+8,d+10,d+12,d+14,d+16,d+18,d+20,d+22,0,d+46,d+52,d+48,d+24,d\
            +26,d+28,d+30,d+32,d+34,d+36,d+38,d+40,d+50,(scanf(d+126,d+4),d+(6\
            -2)+18*(1-d[2]%2)+d[4]*2),d,d+66,d+68,d+70, d+78,d+80,d+82,d+90,d+\
            92,d+94,d+97,d+54,d[2],d+2,d+71,d+77,d+83,d+89,d+95,d+72,d+73,d+74\
            ,d+75,d+76,d+84,d+85,d+86,d+87,d+88,d+100,d+101,d+96,d+102,d+99,d+\
            67,d+69,d+79,d+81,d+91,d+93,d+98,d+103,d+58,d+60,d+98,d+126,d+127,\
            d+128,d+129

char d[538] = {1,0,10,0,10};

int main() {
    while(*d) printf(fmt, arg);
}

There are many other ways to write code that is nearly unreadable and difficult to understand, but this is certainly not something you should focus on when aiming for good software engineering practices.
« Last Edit: March 30, 2025, 11:30:53 pm by radiolistener »
 

Offline Picuino

  • Super Contributor
  • ***
  • Posts: 1460
  • Country: es
    • Picuino
Re: Converting assembly to C
« Reply #35 on: March 31, 2025, 05:41:46 am »
https://www.eevblog.com/forum/general-computing/is-it-a-good-idea-to-use-goto-statement/

Code: [Select]
void foo()
{
    if (!doA())
        goto exit;
    if (!doB())
        goto cleanupA;
    if (!doC())
        goto cleanupB;

    /* everything has succeeded */
    return;

cleanupB:
    undoB();
cleanupA:
    undoA();
exit:
    return;
}
« Last Edit: March 31, 2025, 06:03:03 am by Picuino »
 

Offline Tation

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: pt
Re: Converting assembly to C
« Reply #36 on: March 31, 2025, 06:21:03 am »
Reading the book "Modern C" by Jens Gustedt will be, no doubt, beneficial in this case.

I didn't ask for recommendations on what to read in order to write poor-quality code, as I strive to avoid that. I asked for specific examples where the use of goto is truly necessary and beneficial. Could you provide such examples?

That book contains exactly what you asked for. But you will not read anything that, even remotely, goes against your "best practices". The rest of your message is unintended, irrelevant and childish. There's none so blind as he who will not see.
« Last Edit: March 31, 2025, 06:26:13 am by Tation »
 

Online 5U4GB

  • Super Contributor
  • ***
  • Posts: 1723
  • Country: au
Re: Converting assembly to C
« Reply #37 on: March 31, 2025, 07:12:29 am »
Is the code hand-written or the output of a compiler?  There are some pretty decent decompilers around that can often recreate the original C, you can use the dogbolt decompiler explorer to see which one works best.

With -O3 etc optimisation? "Reversing" some of these optimisations is relatively straightforward, but others aren't

It depends on how the code's built, I know some embedded code is built with -O0 to avoid unpleasant surprises inserted by the compiler, which produces roughly the same output as the CompCert verified compiler.  Also some of the reversers will recognise some compiler idioms and reproduce the original code even in the presence of optimisation... that's why I suggested dogbolt, it lets you explore which, if any, produce the best output.
 

Online 5U4GB

  • Super Contributor
  • ***
  • Posts: 1723
  • Country: au
Re: Converting assembly to C
« Reply #38 on: March 31, 2025, 07:18:22 am »
I'm not aware of any specific scenarios where goto would be genuinely useful. Could you provide some examples?

[Anti-goto tirade]

Just for reference the paper was called Go To Statement Considered Harmful and was specifically targeted at its overuse in FORTRAN which had little in the way of modern control statements.  We've moved on a bit in the half-century since then. 

It was also not called Goto Considered the Spawn of Beelzebub and Anyone Who Uses It is a Sinner who Should Burn in the Fires of Hell.
 

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Converting assembly to C
« Reply #39 on: March 31, 2025, 09:49:02 am »
Is the code hand-written or the output of a compiler?  There are some pretty decent decompilers around that can often recreate the original C, you can use the dogbolt decompiler explorer to see which one works best.

With -O3 etc optimisation? "Reversing" some of these optimisations is relatively straightforward, but others aren't

It depends on how the code's built, I know some embedded code is built with -O0 to avoid unpleasant surprises inserted by the compiler, which produces roughly the same output as the CompCert verified compiler.  Also some of the reversers will recognise some compiler idioms and reproduce the original code even in the presence of optimisation... that's why I suggested dogbolt, it lets you explore which, if any, produce the best output.

It does indeed "depend"; no general statement can be made.

Even with mildly optimised code I've seen some very confusing (but correct) code reordering. That was most clearly seen when single stepping and/or using the IDE's tools which revealed the control flow paths flow to determine the best/worst case "mainloop" execution time.

As for "surprises" from the compiler, strictly speaking the compiler would have been correct. But C/C++ is so complex and ill-defined that even the language design committee refused to believe what they had created. The tool becomes part of the problem, not part of the solution. But that's a different story :)
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 radiolistener

  • Super Contributor
  • ***
  • Posts: 5733
  • Country: Earth
Re: Converting assembly to C
« Reply #40 on: March 31, 2025, 01:07:01 pm »
Code: [Select]
void foo()
{
    if (!doA())
        goto exit;
    if (!doB())
        goto cleanupA;
    if (!doC())
        goto cleanupB;

    /* everything has succeeded */
    return;

cleanupB:
    undoB();
cleanupA:
    undoA();
exit:
    return;
}

what is the reason for goto in your example?

Here equivalent code but with no goto:
Code: [Select]
void foo() {
    if (!doA()) {
        return;
    }
    if (!doB()) {
        undoA();
        return;
    }
    if (!doC()) {
        undoB();
        undoA();
        return;
    }

    /* everything has succeeded */
    return;
}

As you can see, its much easier to read and support. And there is no need to use goto at all.
 

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 5733
  • Country: Earth
Re: Converting assembly to C
« Reply #41 on: March 31, 2025, 01:22:39 pm »
That book contains exactly what you asked for. But you will not read anything that, even remotely, goes against your "best practices". The rest of your message is unintended, irrelevant and childish. There's none so blind as he who will not see.

What you think about me is entirely irrelevant in this discussion - I didn’t ask for your opinion about me. I asked a specific technical question.

If you want to engage in a discussion, provide a concrete technical answer. Your personal thoughts about me are of no interest and have no place in this conversation.

In a professional discussion, evading a direct answer through manipulation is unacceptable. Instead of providing a concrete example when asked, you suggest reading an entire book. This is equivalent to being asked what your own statement means and responding by recommending an encyclopedia.

Such behavior is nothing more than an attempt to avoid answering, which is unacceptable in a mature and professional discussion. This is what constitutes childish behavior.

Additionally, attempting to shift the discussion from a technical topic to a discussion about individuals is equally unacceptable manipulation in a mature, professional debate. This is just another way to evade the question - by resorting to personal attacks in the hope that the opponent will become distracted and the original question will be forgotten, eliminating the need to answer it.

Please refrain from using such childish manipulation tactics on an engineering forum by avoiding direct answers and posting unintended and irrelevant responses. You were asked a specific question - if you can answer it, do so. If you don’t have an answer, simply say so.
« Last Edit: March 31, 2025, 01:45:07 pm by radiolistener »
 

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Converting assembly to C
« Reply #42 on: March 31, 2025, 01:42:31 pm »
In a professional discussion, evading a direct answer through manipulation is unacceptable.

Not always. As Wolfgang Pauli famously put it, "What you said was so confused that one could not tell whether it was nonsense or not" and "That is not only not right; it is not even wrong".

In such cases it is difficult and/or pointless to attempt to refute the specific point.

Quote
Instead of providing a concrete example when asked, you suggest reading an entire book.

Sometimes both parties can gain by referring to a well-thought through text on the subject, rather than wasting time on a poorly worded response to a particular point. One gains time, the other (possibly) gains understanding.
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: tooki, newbrain

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 5733
  • Country: Earth
Re: Converting assembly to C
« Reply #43 on: March 31, 2025, 02:12:18 pm »
In a professional discussion, evading a direct answer through manipulation is unacceptable.

Not always. As Wolfgang Pauli famously put it, "What you said was so confused that one could not tell whether it was nonsense or not" and "That is not only not right; it is not even wrong".

In such cases it is difficult and/or pointless to attempt to refute the specific point.

Your repeated attempts to evade direct answers and manipulate the discussion by labeling opposing views as "wrong" simply because you personally find them unclear indicate that you are not genuinely interested in a technical discussion. Instead, you are engaging in trolling -constantly avoiding direct responses and resorting to manipulative tactics in every reply.

This kind of behavior is unacceptable on an engineering forum. Please refrain from such manipulations and engage in a proper, technical discussion.

Quote
Instead of providing a concrete example when asked, you suggest reading an entire book.

Sometimes both parties can gain by referring to a well-thought through text on the subject, rather than wasting time on a poorly worded response to a particular point. One gains time, the other (possibly) gains understanding.

Do you seriously believe that directing someone to read general-purpose books, instead of providing a direct answer to a specific technical question, is an effective way to save their time?

If you are unable to formulate an answer yourself and instead suggest reading someone else’s book, this suggests that you have read the book but failed to fully grasp its content.

This makes it even more perplexing that you chose to engage in a debate without being able to substantiate your own opinion - while simultaneously recommending books that you yourself did not fully understand.


After all, it is quite evident that you are unable to substantiate your claim that the goto statement in C is beneficial. By continuously evading a direct answer, you have demonstrated that your opinion lacks any solid foundation or supporting arguments. Regardless of whether there are specific edge cases where goto might be justified, you have been unable to provide even a single concrete example to support your stance.

I, on the other hand, have already mentioned cases where goto could be justified - such as in code obfuscation, where the goal is to deliberately make the code difficult to read and analyze. It may be useful for different kind of malware, viruses, etc, where developer needs to hide code algorithms and make it more confusing for more difficult analysis. However, I have yet to see a situation in conventional programming where goto is truly necessary and cannot be replaced with a more structured approach.
« Last Edit: March 31, 2025, 02:27:45 pm by radiolistener »
 

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Converting assembly to C
« Reply #44 on: March 31, 2025, 02:26:08 pm »
In a professional discussion, evading a direct answer through manipulation is unacceptable.

Not always. As Wolfgang Pauli famously put it, "What you said was so confused that one could not tell whether it was nonsense or not" and "That is not only not right; it is not even wrong".

In such cases it is difficult and/or pointless to attempt to refute the specific point.

Your repeated attempts to evade direct answers and manipulate the discussion by labeling opposing views as "wrong" simply because you personally find them unclear indicate that you are not genuinely interested in a technical discussion. Instead, you are engaging in trolling -constantly avoiding direct responses and resorting to manipulative tactics in every reply.

This kind of behavior is unacceptable on an engineering forum. Please refrain from such manipulations and engage in a proper, technical discussion.

Quote
Instead of providing a concrete example when asked, you suggest reading an entire book.

Sometimes both parties can gain by referring to a well-thought through text on the subject, rather than wasting time on a poorly worded response to a particular point. One gains time, the other (possibly) gains understanding.

Do you seriously believe that directing someone to read general-purpose books, instead of providing a direct answer to a specific technical question, is an effective way to save their time?

If you are unable to formulate an answer yourself and instead suggest reading someone else’s book, this suggests that you have read the book but failed to fully grasp its content.

This makes it even more perplexing that you chose to engage in a debate without being able to substantiate your own opinion - while simultaneously recommending books that you yourself did not fully understand.


After all, it is quite evident that you are unable to substantiate your claim that the goto statement in C is beneficial. By continuously evading a direct answer, you have demonstrated that your opinion lacks any solid foundation or supporting arguments. Regardless of whether there are specific edge cases where goto might be justified, you have been unable to provide even a single concrete example to support your stance.

I, on the other hand, have already mentioned cases where goto could be justified - such as in code obfuscation, where the goal is to deliberately make the code difficult to read and analyze. However, I have yet to see a situation in conventional programming where goto is truly necessary and cannot be replaced with a more structured approach.

What are you on about?!

(And it would do you good to look in the mirror)
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: tooki

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 5733
  • Country: Earth
Re: Converting assembly to C
« Reply #45 on: March 31, 2025, 02:37:06 pm »
What are you on about?!

(And it would do you good to look in the mirror)

I am discussing the topic - the use of goto in C when translating code from assembly to C. Meanwhile, you continue attempting to shift the conversation toward personal matters, which is entirely inappropriate in a technical discussion.

Your suggestion to "look in the mirror" is irrelevant and out of place. I did not ask for personal advice, I asked a specific technical question - provide an example where goto is truly necessary and cannot be replaced with conventional structured code that avoids goto.

If you can provide an answer to the question asked, simply do so, and we can discuss it. If you cannot, then please refrain from childish posting irrelevant distractions, manipulative remarks, or attempts to shift the discussion toward personal matters. Let's keep the conversation technical and on-topic, okay?
 

Offline Picuino

  • Super Contributor
  • ***
  • Posts: 1460
  • Country: es
    • Picuino
 

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 5733
  • Country: Earth
Re: Converting assembly to C
« Reply #47 on: March 31, 2025, 03:13:31 pm »
https://www.wscubetech.com/resources/c-programming/goto

I didn't asked what is goto and how it works, I assume anyone here knows that.
I asked the example where you cannot replace it with conventional code which don't use goto.
 

Offline Picuino

  • Super Contributor
  • ***
  • Posts: 1460
  • Country: es
    • Picuino
Re: Converting assembly to C
« Reply #48 on: March 31, 2025, 03:20:23 pm »
In the same article, after the introduction, he explains some uses of goto, its advantages and disadvantages.

One application is to exit two nested loops at once.

Another application is the one I posted earlier, managing input/output files with errors and without having to repeat closing code of open files.

Quote
Advantages of goto in C

1. Simplifies Error Handling
The goto statement is useful for handling errors and cleanup in a program, especially when dealing with multiple resources like file operations, memory allocation, or sockets. It helps consolidate cleanup code in one place, improving maintainability.

2. Exiting Deeply Nested Loops
In complex programs with multiple nested loops, goto can simplify the process of breaking out of all loops when a specific condition is met, without additional flags or logic.

3. Reduces Code Duplication
The goto statement can eliminate repetitive code by jumping to a specific labeled section for common operations, such as cleanup tasks.

4. Improves Readability in Certain Scenarios
While generally avoided for readability, in some cases like simple error handling or single jumps, goto can make the program flow easier to understand.

5. Useful in Low-Level Programming
In systems programming, goto is often employed in assembly-level logic or hardware-related programming where precise control over the flow of execution is required.

6. Handling Unpredictable Conditions
When program logic involves dealing with unexpected or edge-case scenarios, goto provides a direct way to handle such situations without overly complex logic.

 

Offline Picuino

  • Super Contributor
  • ***
  • Posts: 1460
  • Country: es
    • Picuino
Re: Converting assembly to C
« Reply #49 on: March 31, 2025, 03:22:11 pm »
On the other hand...

Quote
Disadvantages of goto in C

1. Reduces Code Readability
Using goto can make the program flow non-linear, making it harder for others (and even yourself) to understand the logic at a glance.

2. Leads to "Spaghetti Code"
Excessive use of goto results in tangled, disorganized code that jumps arbitrarily, making debugging and maintenance a nightmare.

3. Difficult to Debug
Jumping across the program disrupts the natural execution flow, making it challenging to trace the sequence of execution during debugging.

4. Error-Prone
Improper use of goto can lead to logical errors, such as skipping important code sections or infinite loops, especially in large programs.

5. Breaks Structured Programming Principles
Modern programming emphasizes structured and modular code, where constructs like loops, functions, and conditionals are preferred. goto undermines these principles.

6. Alternatives Are Better
In most cases, structured constructs like break, continue, return, and exception handling can achieve the same result as goto in a cleaner way.

7. Not Recommended in Modern Programming
With the availability of advanced constructs and better error-handling mechanisms in modern languages, goto is considered outdated and unnecessary for most applications.

8. Leads to Poor Maintainability
As programs evolve, maintaining goto-heavy code becomes increasingly difficult, especially when trying to refactor or extend functionality.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf