Oct 29, 2016

How to use Assembly in Virtual Pascal

Virtual Pascal is the tool of choice for 32-bit cross-platform development using the Pascal language. It is compatible with Borland Pascal and Delphi, including the Run-Time Library (RTL). Application is equipped with its own debugger what makes the work more easier. I love both Virtual Pascal, Borland Pascal and Borland Delphi

The author of Virtual Pascal (VP) is Mertner Allan. He created a Ning Network for Virtual Pascal here. VP is now a freeware Pascal compiler. The lastest version of VPC is 2.1 Build 279. You can get it at here.

Pascal allows to use assembly. Mixed code likes:
asm
 // Assembly Code here
end;
Example 1:

function min (a, b: Longint): Longint;
begin
    asm
        mov eax, a
        cmp eax, b
        jb @ex
        mov eax, b
        jmp @ex
        @ex:
            mov result, eax
    end;
end;
begin
    Writeln (min (2, 3));
    Writeln (min (1, 2));
    Readln
end.
Example 2:

function min (a, b: longint): longint;
begin
    asm
        mov eax, a // eax:= a;
        cmp eax, b // eax< b?
        jb @_below // jump if below

        mov eax, a
        cmp eax, b
        ja @_above

        @_above:
            mov eax, b
            mov result, eax
        @_below:
            mov result, eax
    end;
end;
begin
    Writeln (min (7, 6));
    Writeln (min (5, 8));
    Readln
end.
Note that, in C/C++, PHP we use return keyword. Virtual Pascal uses the local variable result to return values of function.
int min (int a, b) {
 return ([value]);
}
It's equivalent to following Virtual Pascal Code

function min (a, b: longint): longint;
begin
 result:= [value];
end;

No comments:

Post a Comment