So I started coding x86 assembly a week ago and I tried some size comparison of the output programs in both languages.
First of all, a simple program that displays "Hello".
C Code
Code:
x86 ASM Code
Code:
Size of the output in C: 8.2KB
Size of the output in ASM: 1.2KB
I thought the difference was huge and tried another way to do it in C (more low level way):
Code:
12 is the number of bytes (I counted them, to make it smaller)
Size of the output: 8.2KB
The size is exactly the same, probably because GCC does all the Maths before creating the final program and that makes it have the same size.
Then I tried a program that loops forever "Hello" on different lines.
x86 ASM Code
Code:
C Code
Code:
Output of C: 8.2KB
Output of ASM: 1.3KB
I really don't know if I did anything wrong in my experiments. Any conclusions on your side or opinions? Thanks
First of all, a simple program that displays "Hello".
C Code
Code:
#include <stdio.h>
main()
{
printf("Hello World\n");
}
x86 ASM Code
Code:
section .data
hello: db 'Hello World',10 ; 'Hello' plus a linefeed character
helloLen: equ $-hello ; Length of the 'Hello' string
section .text
global _start
_start:
mov eax,4 ; The system call for write (sys_write)
mov ebx,1 ; File descriptor 1 - standard output
mov ecx,hello ; Move the string 'Hello' to ecx to be printed
mov edx,helloLen ; Move the length of 'Hello' to edx to be printed
int 80h ; Display Hello
mov eax,1 ; The system call for exit (sys_exit)
mov ebx,0 ; Exit with return code of 0 (no error)
int 80h ; Exit program
Size of the output in C: 8.2KB
Size of the output in ASM: 1.2KB
I thought the difference was huge and tried another way to do it in C (more low level way):
Code:
main()
{
write(1,"Hello World\n",12);
}
12 is the number of bytes (I counted them, to make it smaller)
Size of the output: 8.2KB
The size is exactly the same, probably because GCC does all the Maths before creating the final program and that makes it have the same size.
Then I tried a program that loops forever "Hello" on different lines.
x86 ASM Code
Code:
section .data
hello: db 'Hello',10 ; 'Hello' plus a linefeed character
helloLen: equ $-hello ; Length of the 'Hello' string
section .text
global _start
_start:
mov eax,4 ; The system call for write (sys_write)
mov ebx,1 ; File descriptor 1 - standard output
mov ecx,hello ; Move the string 'Hello' to ecx to be printed
mov edx,helloLen ; Move the length of 'Hello' to edx to be printed
int 80h ; Display Hello
jnz _start
mov eax,1 ; The system call for exit (sys_exit)
mov ebx,0 ; Exit with return code of 0 (no error)
int 80h ; Exit program
C Code
Code:
main()
{
while (1) {
write(1,"Hello\n",6);
}
}
Output of C: 8.2KB
Output of ASM: 1.3KB
I really don't know if I did anything wrong in my experiments. Any conclusions on your side or opinions? Thanks