A tool called a compiler is then used to convert the high-level code into machine language. A program can be written in C and compiled for any computer, it's up to the compiler to get the hardware-specific instructions right.
To see just how readable C is compared to Assembly language, take a look at the following tiny program written in each:
Example 1-1. C vs. Assembly language
.section .rodata
.LC0:
.string "Tax Due: %d\n"
.text
.align 2
.globl main
.type main,@function
main:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
movl $1000, %eax
movl $400, %edx
movl $0x3e6147ae, -12(%ebp)
subl %edx, %eax
pushl %eax
fildl (%esp)
leal 4(%esp), %esp
fmuls -12(%ebp)
fnstcw -18(%ebp)
movw -18(%ebp), %ax
movb $12, %ah
movw %ax, -20(%ebp)
fldcw -20(%ebp)
fistpl -16(%ebp)
fldcw -18(%ebp)
subl $8, %esp
pushl -16(%ebp)
pushl $.LC0
call printf
addl $16, %esp
movl $1, %eax
leave
ret
.Lfe1:
.size main,.Lfe1-main
And the program in C:
#include <stdio.h>
int
main()
{
int wages = 1000;
int tax_allowance = 400;
float tax_rate = 0.22;
int tax_due;
tax_due = (wages - tax_allowance) * tax_rate;
printf("Tax Due: %d euro\n", tax_due);
return 0;
}
No comments:
Post a Comment