TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Assembly's Perspective of C

164 pointsby pavehawk2007almost 5 years ago

8 comments

chrisseatonalmost 5 years ago
&gt; If you haven’t been living under a rock quarantined against COVID-19, you’ve probably heard about C becoming assembly 2.0, or the new assembly, or YES THIS.<p>What&#x27;s this referring to? Has something been proposed recently?<p>&gt; Now, many compilers have done some work with this using what are known as intrinsics. However, these do not allow us to fully exploit what we can in assembly. Most of the intrinsic instructions follow the C calling conventions, so they are required to load from RAM, do something with it, then store back into RAM.<p>&gt; Since we store the intermediate values, we’re exercising the memory controller for every intrinsic. For our assembly, we load at the beginning and store at the very end. Nothing is in between.<p>I don&#x27;t understand this claim. It seems demonstrably false to me. Intrinsics don&#x27;t have to follow the C calling convention. And they in practice don&#x27;t follow it. You can try it for yourself. It definitely doesn&#x27;t store intermediate results to memory for his example code.<p>In fact, isn&#x27;t what clang generates roughly the same assembly as the hand-written assembly in the article?<p><a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;rfmFB5" rel="nofollow">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;rfmFB5</a><p>The author looks like an expert so I guess I&#x27;m missing something.
评论 #23284393 未加载
评论 #23284347 未加载
rajeevkalmost 5 years ago
I wrote very similar article long back. <a href="http:&#x2F;&#x2F;www.avabodh.com&#x2F;cin&#x2F;cin.html" rel="nofollow">http:&#x2F;&#x2F;www.avabodh.com&#x2F;cin&#x2F;cin.html</a>
nayukialmost 5 years ago
&gt; All in all, the compiler, when emitting its assembly code, must make these decisions. One data type of note is char. The standard does not 100% state whether we should sign extend or zero extend a char. In fact, I noticed a difference between GNU’s RISC-V toolchain vs GNU’s Intel&#x2F;AMD toolchain. The former will sign extend, whereas the former zero extends.<p>The C standard states that an unqualified &quot;char&quot; type (without the &quot;signed&quot; or &quot;unsigned&quot; keywords) is implementation-defined to be either signed or unsigned.<p><a href="https:&#x2F;&#x2F;en.cppreference.com&#x2F;w&#x2F;cpp&#x2F;language&#x2F;types#Character_types" rel="nofollow">https:&#x2F;&#x2F;en.cppreference.com&#x2F;w&#x2F;cpp&#x2F;language&#x2F;types#Character_t...</a><p>&gt; char - type for character representation which can be most efficiently processed on the target system (has the same representation and alignment as either signed char or unsigned char, but is always a distinct type). Multibyte characters strings use this type to represent code units. The character types are large enough to represent any UTF-8 eight-bit code unit (since C++14). The signedness of char depends on the compiler and the target platform: the defaults for ARM and PowerPC are typically unsigned, the defaults for x86 and x64 are typically signed.
loosetypesalmost 5 years ago
Would assembly consider c a declarative language?
评论 #23288181 未加载
daniel-thompsonalmost 5 years ago
&gt; &quot;Another reason we use assembly over a high-level language is to use instructions that C or C++ cannot really use. For example, the advanced vector extensions (AVX) for an Intel processor don’t have a 1-to-1 conversion from C.&quot;<p>This might be beyond the scope of the article, but it may be worth noting <a href="https:&#x2F;&#x2F;ispc.github.io&#x2F;" rel="nofollow">https:&#x2F;&#x2F;ispc.github.io&#x2F;</a>, which is a C language extension and accompanying (LLVM-frontend) compiler. ISPC exposes an SPMD programming model that efficiently maps to the CPU&#x27;s vector lanes (think CUDA kernels, but running on those vector lanes instead of a GPU). Except for a few new keywords and some library functions, it is almost identical to C and has C linkage.
gorgoileralmost 5 years ago
The thing I adore about computer science is that it is quite tractable to understand absolutely everything from theories of data and computation, programming languages and operating systems, and hardware.<p>This article is a fantastic look at the lower parts of the stack, and it’s really made me look forward to the next time I’ll be teaching this topic in my own classes.<p>RISC looks much easier to learn than the code I’ve been using: clang generated asm in macOS.<p>I was actually thinking of moving to using assembler in DOSBox (1) for nostalgia reasons and (2) because it could be a lot simpler and less crufty.<p>What’s an equivalent platform &#x2F; set of tools with which I could do some real world RISC? Is my macOS assembler RISC already, but just not as elegant as this author’s examples?
评论 #23291061 未加载
pavehawk2007almost 5 years ago
I write OSes most of the time, but seeing how languages end up boiling down is a very good exercise in learning the architecture.
Ericson2314almost 5 years ago
The nice thing about assembly code isn&#x27;t the lack of abstractions, but the fact that the image of compilation (the assembly process) is bigger than most languages&#x27; compilers: just about any output you want has a corresponding input.<p>People always confuse these things, and it&#x27;s important that we don&#x27;t to continue striving for better programming languages.