Continuing the discussion from The Price of Scripting:
There you go
I´m sorry, but I’m not going to implement the code challenger we used in The Price of Scripting in assembler… it is just no point on doing that. So I will use a simple hello world program that will wait for the user to press a key, so we can check the memory usage of the process.
In order to be able to, somehow, compare this data with the previous table, I’m including the figures for C++ and C just for comparison purposes. However, for such a small program some values are nor much relevant. You will also notice the page size granularity (4Kb for linux)
All the programs are compiled as static binaries so we account for any dynamic library they use, as for instance the C and C++ standard libraries.
C++
So, the C++ code used is this:
#include <iostream>
int main (void)
{
std::cout << "Hello World" << std::endl;
std::cin.get();
return 0;
}
C
The C code is:
#include <stdio.h>
int main (void)
{
printf ("Hello World!\n");
getchar();
return 0;
}
Assembler
And the assembler code for Linux 64 bits is:
msg db "hello, world!",0x0a
key db 0
section .text
global _start
_start:
mov rax, 1
mov rdi, 1
mov rsi, msg
mov rdx, 14
syscall
mov rax, 0
mov rdi, 0
mov rsi, key
mov rdx, 1
syscall
mov rax, 60
mov rdi, 0
syscall
Results
For these three programs I had got the following results in my system:
Language | Size | Strip | Mem | Virt Mem | Res Mem | Wr Mem | Sh Mem
---------+------------+---------+------+----------+---------+--------+---------
C++ | 1.6 M | 1.3 M | 68 K | 1.6 M | 256 K | 68 K | 196 KB
C | 857 K | 778 K | 32 K | 1.1 M | 4 K | 52 K | N/A
Diet | 8.5 K | 4.9 K | 16 K | 156 K | 4 K | 16 K | N/A
Asm | 1002 | 544 | 8 K | 152 K | 4 K | 8 K | N/A
The 2 first columns are the binary size, just after compiling and after stripping
it.
Some conclusions:
- The file size of the assembler binary version is around 10 times smaller than the dietlibc version. And it goes as low as 544 bytes for a static binary (no library dependency at all)
- At run-time, the difference between dietlibc and the assembler version, resources-wise is not much for this very small program.
So, unless you really have a size constraint (what it usually happens with shellcodes and remote payloads), you will not gain much benefit coding your application in assembler… But, as usual, it always depends on what you are doing. If you are good at assembler, your code will likely run a lot faster.