Title: Using BASM with Delphi
Question: Using BASM with Delphi
Answer:
BASM is the acronym for Borland Assembler. In fact is the ability to write low level code (assembler) inside your programs. This is great for low level functions and for high speed calculations.
When you decide to write your own assembler code using Delphi you must know:
1. You must conserve the value of EDI, ESI, ESP, EBP and EBX registers. If you want tu use this registers your code must look like:
asm
push EDI //Or any register you will use
//YOUR CODE
pop EDI
end;
2. You can modify as you wish the EAX, EBX, EDX registers.
3.You can use labels like in bellow example:
procedure some_code;
label ALabel;
begin
asm
jmp ALabel
end;
showmessage('This message will never appear!');
ALabel: showmessage('Here I have jump');
end;
3. You can use variables declared with var
Example:
procedure Using_Some_Vars;
var a,b:integer;
begin
asm
mov EAX,a //put a into ax
add EAX,b //add b to EAX
mov a, EAX // a is now a+b
end;
showmessage('The Sum Is: '+ inttostr(a));
end;
4. You can create entire procedures or functions in assembler
Example:
function A_Sum(a,b:integer):integer; assembler;
asm
mov EAX,a
add eax,b
mov @Result, EAX //Returning the result
end;
5. You can access record type
Example
type animal=record
size:integer;
weight:integer;
end;
procedure reading_a_record;
var my_animal:animal
begin
//some initialisations
asm
mov EAX, my_animal.size
end;
end;
Note: I know this is incomplete. There are some basics ideas.But, in the future, I will complete this article with many other complex examples.