Assembly language programming vs Other programming languages

Click For Summary
Assembly language is generally faster than higher-level languages like C, C++, or Python due to its closer proximity to machine code, allowing for more direct control over hardware. While compilers translate C code into machine code, they may introduce inefficiencies, whereas assembly allows programmers to optimize performance by eliminating unnecessary code. The size of executables can vary significantly between languages, with assembly producing much smaller binaries compared to C or VB.NET. However, for most applications, using assembly is considered overkill due to the extensive time required for development and maintenance compared to higher-level languages. Ultimately, while assembly can yield performance benefits, the trade-offs in productivity and maintainability often lead developers to choose higher-level languages for most tasks.
  • #121
glappkaeft said:
Edit: Note that is is considered bad practice to initialize an int with a double value.
hmm yeh, I had started it double, but then it complained with the usage of << . I changed it to integer but I forgot to remove the .0 ...

Hm, so that means the assembler code would be something like:
movl $0x6e , -0x4 (%rbp)
(sum is registered in 0x4 and moves 110 in it)
without a for loop?
Does that happen because the compiler runs the code and gets the result before producing the output?
 
Technology news on Phys.org
  • #122
ChrisVer said:
without a for loop?
Does that happen because the compiler runs the code and gets the result before producing the output?
It doesn't really run the code, but it optimizes it.

int chrisver()
{
int sum = 0;
for(int i=1; i<=10; i++)
{
sum+= 2*i;​
}​
return sum;​
0FB94090 mov eax,6Eh
}​
0FB94095 ret
 
  • #123
ChrisVer said:
Does that happen because the compiler runs the code and gets the result before producing the output?

Since it doesn't really run the code it is usually said that the compiler evaluates the code at compile time. If you are interested in these things the compiler optimization section on wikipedia is a pretty good place to start. For instance constant folding is here: https://en.wikipedia.org/wiki/Constant_folding

Added: If you want to see what the compiler does with your code in a real world scenario you need to make sure the number of times the for-loop is repeated is not known at compile time, e.g. depends on user keyboard input, data from a file or similar.
 
Last edited:

Similar threads

Replies
6
Views
3K
  • · Replies 397 ·
14
Replies
397
Views
19K
Replies
6
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 29 ·
Replies
29
Views
3K
  • · Replies 4 ·
Replies
4
Views
15K
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
59
Views
8K