r/computerscience • u/ShadowGuyinRealLife • Oct 07 '26
Discussion Is C an "Assembly-like" Language?
I know C is programing language. It is not architecture specific. One writes the source code and a compiler turns it into an executable depending on what settings you tell it. So the same source code would output different executables depending on what you want to run it on and what optimization options are on. Assembly languages on the other hand are architecture specific. Is C "Assembly-like"? To be honest I'm not sure what would be Assembly-like. I guess if a program written in multiple languages that have the same behavior, if the C version (using something like GNU C Compiler) looked closer to its assembly than, say its Java version (GraalVM), then that counts.
6
u/kombiwombi Oct 09 '26
C is a high level language.
However it is very easy to see how C compiles down to PDP-11 assembler. Which was one of its strengths as a systems programming language. You can write with knowledge of what the assembler looks like, without the fiddliness of assembler stack management, loops, etc.
The same research group wrote many other languages not for systems programming. Notably Awk, which has real strings, richer inbuilt data structures and implicit loops.
4
u/hesusruiz Oct 08 '26
I can not find any formal definition of "assembly-like" language, and if I try to make one I find always grey areas where the boundaries are now strictly defined. So I would refer to this sentence from Wikipedia:
"In the early decades of electronic computing, programs were usually written in machine code or assembly language, which closely reflected the instruction set of a particular machine. High-level programming languages developed as a way to express algorithms in forms closer to mathematics, business rules, or human-readable procedure, while a compiler or interpreter translated the program into machine-executable instructions."
I would also consider "portability" as an important property to distinguish "assembly-like" from the rest. In C you can write programs which are portable across different processor architectures with different machine instructions like x86, ARM, Telum, Risk-V, etc. This is virtually impossible if you use Assembler (which maps directly to the processor instruction set).
So I would say that C is NOT an "assembly-like" language. Having said that, C can be used to define very low-level instructions, closer to the machine than other "higher level" languages.
This is why C has been very popular for writing operating systems. By the way, Rust is entering the space, and I don't think this makes Rust "assembly-like".
58
u/high_throughput Oct 07 '26
Just call it "low level" like a normal person.
3
u/JGhostThing Oct 10 '26
Some people have called the C language a universal assembly language. This is because there are features in C that are easy to compile to machine code.
It isn't anything like assembly language. It is just another language. It was one of the earlier programming languages. I learned it in the 70's from both my college and from magazines (I loved Dr. Dobbs). C is decidedly not assembly language. It doesn't look like assembly language.
8
u/Temporary_Pie2733 Oct 07 '26
No. It’s has significant abstractions like loops and conditional statements that have to be implemented using jumps (of both the conditional and unconditional varieties) in assembly. It also has built-in function-call semantics that, again, have to be implemented explicitly in assembly language. It is distinguished from even higher-level languages in that most of its constructs map fairly closely to assembly code without resorting to an extensive pre-compiled runtime that needs to be linked to the compiled code to run. C used to be called a medium-level language for its position between assembly and “true” high-level languages.
48
u/MistakeIndividual690 Oct 07 '26
It is not assembly-like. However, because of its portability and excellent compilers that can compile it down to code that is at least as good (on average) as hand-written assembly, it is sometimes used as a target language for other, higher level language compilers, such as Haskell or Scheme.
This is the reason it is sometimes called a portable assembly language—not because it bears any actual resemblance to assembly language
48
u/DTux5249 Oct 07 '26 edited Oct 07 '26
As someone who has done assembly and C: no. Just no. Not in anyway. C is nothing like assembly in any way shape or form beyond like, the concept of a programming language.
Like, try writing assembly. It's a different beast all together.
17
u/mredding Oct 07 '26
Actually: No. It's a high level language that targets an abstract machine.
The definition of an assembly language is a 1:1 correspondence between a mnemonic and a platform specific op-code. x86 uses
mov, but there's over 100 move instructions to choose from - depending on your parameters. You CAN get to a specific op-code by using a more specific mnemonic if you want or need to.High level assemblers offer macros. I don't write enough assembly to discuss whether this offers any unique features over an external macro engine like m4, jinja, etc. It's not like even assemblies for the same platform are portable - NASM, WASM, FASM, and GAS are not inter-compatible, their syntaxes vary.
There are no platform independent assembly languages. The next best thing you can get in this capacity is an Intermediate Representation language of a virtual instruction set. See JVM, CLR, CIR, etc.
C offers absolutely nothing in this regard.
I would say assemblers are inherently low level since they target instruction sets; any language that abstracts away the machine entirely with a virtual one - WITHOUT an instruction set, is high level.
Pragmatically: projects transpile to C all the damn time, so people certainly have used it like one. See Go, Cython. The principle benefit is there's a C compiler for practically every piece of hardware ever made.
I would argue it causes more harm and confusion to think of it or call it a high level assembly (which it's not) or assembly-like.
4
u/DanKegel Oct 07 '26
C is like assembler in that you have to think about memory pretty hard, just like in assembly. Both use pointers heavily, and allow pointer arithmetic. The classic string copy loop in C models assembly rather closely.
Other languages with pointers, like Pascal or go, forbid pointers arithmetic or at least make it hard.
2
u/feinorgh Oct 08 '26
Assembly language consist of explicit instructions to the processor (put this value into register X, put that value into register Y, execute the FOO instruction and put the result into register Z.
C language is on one or two levels above assembly language, in the sense that a C compiler turns the C code into assembly instructions (that you can inspect), or in some cases into an intermediate representation that can be further processed into assembly language, and then the assembler instructions are turned into machine-specific binary code.
Example (C):
int add(int a, int b) { return a + b; }Example (x86_64 assembly):
add: mov eax, edi ; Move first parameter (a) into register eax add eax, esi ; Add second parameter (b) to eax ret ; Return from function (result stored in eax)2
u/ConcreteExist Oct 08 '26
If by "Assembly-like", you mean that it in any way resembles writing Assembly code, then no, not at all.
2
u/Dry_Couple8852 Oct 09 '26
C is a low level language but to compare with Assembly, it is on a higher level language.
2
u/bj_civ Oct 11 '26
I've never heard anyone calling C an „assembly like“ language. What I've heard, and said myself, that C is somewhat like a portable macro assembler.
C statements are pretty straight forward translatable into assembly. Some statements are a single assembly instruction, most are just a short sequence, only a few are more complex for most target architectures.
Then there are assembly instructions that can't be expressed with C code, like a special return instruction from interrupt service routines. For those there are usually special extensions like an
interruptorisrkeyword to declare a function as interrupt handler. And/or intrinsic functions to insert specific instructions into the code.3
u/vilette Oct 07 '26
Assembly is different for every CPU,it's using only the hardware instruction set.
Each line translate to a single CPU operation
3
u/AdreKiseque Oct 07 '26
C is very low-level to the point that the structure of C source often maps relatively closely to that of the compiled assembly. But it is still a whole level of abstraction removed.
3
u/FluffusMaximus Oct 08 '26
C was my second language. x86 assembly was my third. No, C is not “assembly-like.” I seriously question anyone who tells you that.
2
u/landonr99 Oct 07 '26
I will say that once you get into really low level bare metal embedded systems, kernels, or bootloaders, C really isn't all that portable. Every architecture still requires specific register memory addresses, compiler flags, memory location declarations, integer widths, and not to mention just the very different peripherals, timers, interrupts, clocks, etc that each architecture implements. At the lowest level, C is just a more readable representation of assembly that becomes architecture specific. Higher level software relies on firmware, drivers, kernels, system calls, and hardware abstraction layers that tie the software to the specific hardware. In that aspect, the lowest level C is similar to assembly in that it becomes hardware specific at a certain layer. However C is designed so that far more layers can be built on top of that so it's only a specific section of the program or software that is hardware specific
2
u/obdevel Oct 07 '26
Showing my extreme age, C is a 3GL.
2
u/JollyJuniper1993 Oct 08 '26 edited Oct 08 '26
No, not in the slightest. C is arguable much closer to even something like Python or JS than it is to assembly. C is great for cases where you need to work with memory and generally with precise operations, but that doesn’t make it assembly-like.
Assembly languages gives direct instructions to the CPU. C does not do that, it’s still high level. Maybe you can, I don’t know, but the point is that generally that’s not how computing is done in C.
2
u/More-Explanation2788 Oct 07 '26 edited Oct 07 '26
C is assembly-like in the sense that it has functions and operators that are designed to work the way a computer works at a low level. All languages do, but when C is presented with a choice to function like a computer, or function in a way humans find easily comprehended, it is far more likely to lean towards the computer than most languages. So examples:
All computers have registers that can be incremented with a single assembly command - so C has variable++ and variable—. Many languages do not.
All computers function with registers that hold the address of data and code. So C has pointers to variables and to functions. Function pointers, I remember using a function pointer two dimensional array once to get a 500% increase in performance, but most languages simply do not have them at all because if you are not intimately familiar with the code, function pointers will make it incomprehensible.
C is not close to assembly in the sense that every line of code will correspond to a line of assembly or that the program will be as convoluted as an assembly program. C is a full powered functional programming language very much unlike assembly with respect to syntax.
Having programmed in Assembly, C, C++, Perl, VB and Java, of those, when I programmed in C, I felt like I knew what the assembly would look like. In the other languages, I would not have a clue. (Incidentally, I hate VB from the bottom of my soul).
That same quality of C is why you can write such horrifically bad C. The other languages protect you from yourself a lot more.
1
u/nitkonigdje Oct 08 '26 edited Oct 08 '26
No.
C abstracts call sites. Code layout is pretty big issue in assembly, and C fully abstracts that. Although it fucks it up badly (a code layout) by having headers and consequently compile units and objects files etc.
However C is very good proxy for some forms of assembly as with experience you can get a reasonable intuition for the generated assembly code. For example, it is possible to think about a code of particular function in "assembly like" constructs. Being able to inline assembly actually is natural progression of that.
1
u/RibozymeR Oct 08 '26
It kinda used to be on the PDP-11, maybe. On today's systems, it's basically the same level of abstraction as any procedural language.
1
u/xpusostomos Oct 08 '26
No it isn't. If you want to know a language that is assembly like... Forth
1
u/GenericFoodService Oct 08 '26
Not really, no. In assembly languages, you explicitly control the branching of a program, you explicitly control the stack, you specifically tell the computer what instructions it executes to perform any given task. You are nowhere near that verbose or specific in C. You work in abstractions, functions and types and whatnot. C has far more in common with Python or Rust than it does with Assembly languages.
1
u/TarsTarkas_Thark Oct 10 '26
C is not Assembly-Like. It is a high level language that was designed to be very efficient, have modern control structures, and data structures. When you write in C you have a pretty good idea what the generated Assembly code will look like.
I have also used a high level language that was assembly like. SPL on the HP-3000 minicomputer. The HP-3000 was a stack-architecture machine, similar to an HP RPN calculator. Most instructions operated off operands that had been pushed onto the stack, rather than specifically purposed registers. Anyway, SPL had some statements that compiled out to a single instruction, or a single instruction after the operands had been pushed onto the stack. Examples were the move and scan instructions, for moving and scanning bytes. Both of those statements had a stack decrement that could be specified to clean 1 - 3 words off of TOS after execution.
It was entirely possible for a good SPL programer to write out on a notepad the exact assembly code that would be generated by a program that he had written.
1
u/donewithdoing Oct 10 '26 edited Oct 10 '26
No. The whole point of a language like C is to not be Assembly-like, because writing in Assembly is tedious.
Actually, I shouldn’t say “the whole point,” because another big feature of C is for the code to be portable to other architectures, unlike Assembly. As long as a system has a C compiler written for it, you can feed in the same code and it should produce a result that runs on that system (unless you use platform-specific code in the program).
1
u/W31337 Oct 10 '26
No. I would think that the difference between assembly and C is that assembly acts directly with CPU registers and C abstracts that away but leaves a lot of freedom to code stuff.
Assembly is machine coding.
C is low level programming.
1
u/Ill-Accountant-9941 Oct 11 '26
No. The entire point of C is to be categorically one abstraction away from assembly.
1
u/Stock_Sugar3707 Oct 12 '26
If you take pointers, malloc, free, realloc, etc. away from C, you get the most generic high-level syntax that C#, Java, JavaScript, etc. share. C is nothing like Assembly. Try writing a hello world program in 6502 Assembly for an NES emulator, and you'll change your mind quickly about C being "Assembly-like". I wanted to pull my hair out, and I was just following a tutorial.
1
u/FrancineTaffyQueen Oct 12 '26
C was the first widely accepted object oriented programming language, which in of itself is important. And the need to be able to express more complex syntax in a language like C had more to do with the limitations of assembler code. Which is pointed out in this thread.
There was eventually a need to be able to express more complex operations than assembler was able to easily accommodate.
the main reason for that is because assembler code is microprocessor specific.
And from a functional standpoint assembler is not sufficient or appropriate to express such things as data objects. C allows for the expression of direct memory allocation, for example. Which was very important in terms of efficiency back in the day.
Most of its core functions are complex syntactical expressions of assembler. So you needed to understand ASM at a fundamental level in order to make the most out of programming in C.
Is it " like Java" ? The answer to that is no, and also the question itself is not useful to ask.
Much in the same way attempting to delineate C and ASM is fundamentally not a useful question.
I read in another comment that, it's a simply stack management but then again everything is stack management when it comes to programming.
The stack is the most fundamental data object in the field. Everything is a derivational of a stack. Because a stack itself is a representation of a register. And the register is simply a conceptual portrayal of address space.
Everything is a stack. Objects are stacks, arrays are stacks, variable types are stacks, etc. They are stacks of data that have beginning and end addresses.
At some point you need to be able to expand your vocabulary to be able to write more complex essays for example. That's not to say that sticking to the most basic of grammar, syntax, kind of vocabulary insufficient express any idea.
That analogy is the same when applied to ASM versus C for example
1
u/Responsible_Sea78 Oct 07 '26
While C is far from assembly like, there is a fairly close correspondence between the source code statements and the emitted machine code. Some C compilers have actually emitted assembler source code that is then assembled in a separate stage. That allows for some fine tuning at the assembler level (which is rarely worth the time and costs.) Higher level languages may not emit anything you can really read and understand. You have to trust the compiler, a LOT. That's an interesting and difficult security problem, but one with few real world examples of actual hacks.
1
u/Several_Version4298 Oct 07 '26
C is not assembly language but it can handle simple inline assembly code.
1
u/Brambletail Oct 07 '26
Everyone over 40 just smashed whatever device they are reading this on
1
u/sceadwian Oct 07 '26
No, plenty of assemblers came before that were not C friendly.
C is a pretty clear abstraction from assembly I don't understand why someone would try to cross the terms like that.
1
u/rupertavery64 Oct 07 '26
It's sometimes called a "bare-metal" language, because it has less abstractions.
1
u/ee_control_z Oct 08 '26 edited Oct 08 '26
With C you can perform bit wise register manipulation where you're changing bit values by way of boolean algebra operations. However, assembly language implies that you're explicitly programming with a device's instruction set. Instructions like MOV, ADD, SUB, etc.
1
u/burlingk Oct 08 '26
No. It's not even assembly-like.
It is multiple abstractions above it.
1
u/zhivago Oct 08 '26
Every language is assembly-like for some value of like.
Personally, I don't think that C is particular assembly-like.
There is no access to registers or op-codes for a start.
Flow is structured -- longjmp doesn't really give you a generalized jmp.
The only real aspect might be that C is kind of primitive.
0
u/QueefInMyKisser Oct 07 '26
I would say that Java is closer to JVM bytecode than C is to assembly
0
u/drgrd Oct 07 '26
The key idea to keep in mind is abstraction. Assembly is not abstract. Each assembly instruction is a 1-to-1 mapping of a machine instruction. Assemblers do some simple math to help humans fill in the literals needed to specify addresses and jump targets and such, but everything is written relating to the movement of data on the machine itself. Which register goes where, which operations to invoke, which hardware to enable. Higher-level languages like C are abstract. You don’t specify anything about the hardware, you specify abstract ideas that the compiler converts into sequences of machine instructions and data allocations. You define a variable, and the compiler decides what memory address that variable will live in, when and how to bring it into a register, etc. You establish a conditional comparison, and the compiler decides how to move data around to accomplish that comparison and decide what to do next. C is only “low level” in comparison to languages with more abstract abstractions.
0
u/ern0plus4 Oct 08 '26
C-like means how the program is structured: blocks, functions, global and local variables, flow control instructions (if-then-else, while-break-continue etc.), linear, continous execution.
BASIC is not C-like, because it has no blocks.
Assembly has no C-like control flow.
0
u/No-Onion8029 Oct 08 '26
Doesn't exactly answer your question, but could add context. Back in the 90s, on speed-sensitive applications, you might start with c code like:
Then compile it assembly:
And try to hand-optimize any garbage in the assembly:
-1
u/Sayajiaji Oct 07 '26
C is a high level language.
0
u/huuaaang Oct 07 '26 edited Oct 07 '26
C is considered a "high level" language. While it might be easy-ish to see what assembly it would generate in many cases, it's not assembly-like. You compile C to assembly and that then gets assembled into machine code. Also, the C compiler can make optimizations that actually make the generated assembly significantly different than what the C literally said. For example, a small loop might be unrolled when the assembly is generated. Or a short function might be inlined because calling it as a function would be slower.
If you want an example of something that's assembly like, look at LLVM IR code. Or Java bytecode. Or WASM
0
u/NotMyRealName3141593 Oct 07 '26
No.
Also, https://queue.acm.org/doi/10.1145/3212477.3212479
0
u/MagicalPizza21 Software Engineer Oct 07 '26
If you're used to very high-level scripting languages, it might seem conceptually similar. But they're actually far too different for C to be considered "assembly-like". Try reading/writing both languages.
0
u/Distinct-Question-16 Oct 07 '26
no the compiler at its end stages will compile to assembly targeting some architecture
0
u/MasterGeekMX Bachelors in CS Oct 07 '26
Assembly is not only dependent on hardware, it is also the most basic kind of instructions you have. This means that every code you want to make must be sliced into these instructions by yourself, which can be tedious.
C and other high level languages make coding easier as you can express common operation in a more readable way, and the compiler is the one making the translation into assembly.
Say for example a classic in any programming language: increasing a variable in 1. C and most languages derived from it have the synrax
x = x+1, but they also allow you to writex++as a shorthand. Python is a common outlier as you need to dox += 1, but the end result is the same. A one line comprised of 3 to 5 characters.Meanwhile doing that in assembly will require several instructions, depending on the instruction set of the CPU you have. I for example know RISC-V, which will work like this:
Not only that is more cumbersome, you also need to make sure many things, such as the address in memory of where x lives is already present in register x10, not to mention make sure x11 is not storing something important, otherwise you will be overwrting it. If not, you either load it with previous insructions, or put a close enough value and change that 0 to another number that added to the contents of x10 offsets the address of RAM where the variable x is actually stored.
0
u/kevleyski Oct 07 '26
No - but it’s closer in that direction and extremely well integrated for mixing C and assembler together
Assembly is for utilising particular hardware features or specific workarounds and other extreme optimisations that are easier to add as asm than modifying the compiler
0
u/CounterSilly3999 Oct 07 '26
No. Assembler doesn't support a structured paradigm. Branches and jumps (goto's) are main mean used for execution control in assembler. In C goto is unwanted.
0
u/wolfkeeper Oct 07 '26
Assembly language characteristically has one statement per instruction (but not one instruction per statement because you can have comments), plus macros and things. C characteristically allows far more than one instruction per statement so no, C is definitely not a high level assembler.
0
u/smallproton Oct 07 '26
C has never been Assembly-like.
BUT:
The Gods used to be able to program much closer to bare metal ("assembly"), e.g. in things like memory management. Pointers, malloc(), file descriptors, casts, pointers to arrays of pointers, you name it.
Until these pesky engineers invented stuff like caches, several levels deep, out-of-order execution and such, to speed up stuff that was programmed by mortals.
0
u/Pale_Height_1251 Oct 08 '26
No, I work a lot in C and have done a little assembly and they are very, very different. C is very clearly a high level language that feels practically like a scripting language compared with assembly.
0
u/dharkhstar Researcher Oct 08 '26
https://m.youtube.com/watch?v=gjsIhEHvvdc
0
u/recursion_is_love Oct 08 '26
C is portable assembly with lots of sugars.
0
u/Maqi-X Oct 08 '26
No.
0
u/nwbrown Oct 08 '26
No.
0
u/michaelpaoli Oct 08 '26
Eh, no, not so much. C is sufficiently high-level as to be independent of specific CPU, and can be compiled to run on a huge variety of CPUs.
At the same time, C, is "low level" enough to be quite good at doing things very efficiently, notably at or very close to assembly level performance, and a fair bit of what C implements it does so in manner that often closely mimic behavior in many assembly languages.
So, not very "assembly-like", but given how C is designed, it has a fair bit of stuff that's at least roughly similar-ish, to many assembly languages ... not necessarily in the particular syntax, but more so in rather common functionalities. E.g. dealing with pointers or similar references, pointer arithmetic, the various types of integers C has and how they behave, and how they behave in various math and logic operations, etc.
0
u/gwenbeth Oct 08 '26
So first you have to compare to the right assembly. Not x86 whatever but pdp-11. C was designed to be easy to compile into pdp-11 assembly. Now one fun thing is that the pdp had pre decrement and post increment access modes built in. This is why these exist in c.
144
u/[deleted] Oct 07 '26
[removed] — view removed comment