Jump to content

Talk:Low-level programming language

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Alksentrs (talk | contribs) at 00:11, 17 February 2008 (Examples: Added 3rd, 2nd and 1st generation labels to the different listings). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Examples

This page could use some examples. Thehotelambush 16:09, 30 July 2007 (UTC)[reply]

Example - a function that calculates the nth Fibonacci number.
First, in C [3rd generation]:
unsigned int fib(unsigned int n)
{
    if (n <= 0)
        return 0;
    else if (n <= 2)
        return 1;
    else {
        int a,b,c;
        a = 1;
        b = 1;
        while (1) {
            c = a + b;
            if (n <= 3) return c;
            a = b;
            b = c;
            n--;
        }
    }
}
The same function, but converted to x86 assembly language, specifically MASM, using the __cdecl calling convention [2nd generation]:
fib:
    mov edx, [esp+8]
    cmp edx, 0
    ja @f
    mov eax, 0
    ret
    
    @@:
    cmp edx, 2
    ja @f
    mov eax, 1
    ret
    
    @@:
    push ebx
    mov ebx, 1
    mov ecx, 1
    
    @@:
        lea eax, [ebx+ecx]
        cmp edx, 3
        jbe @f
        mov ebx, ecx
        mov ecx, eax
        dec edx
    jmp @b
    
    @@:
    pop ebx
    ret
Note:
@f refers to the closest following @@: label.
@b refers to the closest preceding @@: label.
I deliberately did not use proc.
And here is the final x86 machine code [1st generation]:
8B542408 83FA0077 06B80000 0000C383
FA027706 B8010000 00C353BB 01000000
B9010000 008D0419 83FA0376 078BD98B
C84AEBF1 5BC3
For comparison purposes, here is a disassembly listing:
00401000                    fib:
00401000 8B542408               mov     edx,[esp+8]
00401004 83FA00                 cmp     edx,0
00401007 7706                   ja      loc_0040100F
00401009 B800000000             mov     eax,0
0040100E C3                     ret
0040100F                    loc_0040100F:
0040100F 83FA02                 cmp     edx,2
00401012 7706                   ja      loc_0040101A
00401014 B801000000             mov     eax,1
00401019 C3                     ret
0040101A                    loc_0040101A:
0040101A 53                     push    ebx
0040101B BB01000000             mov     ebx,1
00401020 B901000000             mov     ecx,1
00401025                    loc_00401025:
00401025 8D0419                 lea     eax,[ecx+ebx]
00401028 83FA03                 cmp     edx,3
0040102B 7607                   jbe     loc_00401034
0040102D 8BD9                   mov     ebx,ecx
0040102F 8BC8                   mov     ecx,eax
00401031 4A                     dec     edx
00401032 EBF1                   jmp     loc_00401025
00401034                    loc_00401034:
00401034 5B                     pop     ebx
00401035 C3                     ret
Alksentrs (talk) 23:57, 16 February 2008 (UTC)[reply]