Mega Code Archive

 
Categories / Delphi / Hardware
 

Delphi içinde asembler kullanımı

Delphide Assembler kullanacaklar için aşağıda bir dizi örnektir listelenmiştir. //Bu Kod Binary bir değeri Tamsayıya çevirir. function BinToInt(P: PChar; S: Integer): LongWord; asm //P->EAX, S->EDX, Result<-EAX PUSH EBX //save function address MOV ECX,EAX //ECX:=P MOV EAX,0 //EAX:=0 @Cycle: //label @Cycle DEC EDX //EDX:=EDX-1 MOV EBX,[ECX] //EBX:=P^, char, P points on it CMP BL,'0' //if BL '0' JNE @1 // <> then @1 BTR EAX,EDX //clear bit in EAX on place EDX JMP @0 //jump on @0 @1: //label @1 BTS EAX,EDX //fill bit in EAX on place EDX @0: //label @0 INC ECX //Inc(P), next char CMP EDX,0 //if EDX 0 JNE @Cycle // <> then @Cycle POP EBX //open function address end; //Bu Kod n faktoriel hesaplar. function NFact(I: Integer): Integer; asm //I->EAX, Result<-EAX PUSH EBX //save function address MOV EBX,EAX //EBX:=I CMP EAX,1 //if I 1 JNE @Continue; // <> then @Continue POP EBX //open function address RET NEAR //Exit - end function @Continue: //label @Continue DEC EAX //I:=I-1; CALL NFact //call NFact(EAX)->EAX IMUL EBX //EAX:=EAX*EBX POP EBX //open function address end; //Bu kod beep sesi çıkaran bir assembler örneğidir. procedure TForm1.Button1Click(Sender: TObject); asm MOV AL,7 INT $29 end; //Bu Kod herhangi bir integer değerin Hex Karşılığını Bulur. function IntToHex(const V; S: LongInt): ShortString; const Hex: array[0..15] of Char = '0123456789ABCDEF'; asm //V->EAX, S->EDX, Result<-EAX ADD EDX,EDX //S:=S+S MOV BYTE PTR [ECX],DL //Result[0]:=S, set length PUSH EBX //save function address PUSH ESI //save register ESI MOV ESI,EAX //ESI:=V LEA EAX,DWORD PTR [ECX+EDX-1] //EAX points to S*2 char of Result XOR EBX,EBX //EBX:=0 @Cycle: //label @Cycle MOV BL,BYTE PTR [ESI] //BL:=V^, char, V points on it SHR BL,4 //BL:=BL shr 4 MOV BL,BYTE PTR [EBX+Hex] //BL:=Hex[BL] MOV BYTE PTR [EAX],BL //Result[EDX-1]:=BL MOV BL,BYTE PTR [ESI] //BL:=V^, char, V points on it AND BL,$F //BL:=BL and $F MOV BL,BYTE PTR [EBX+Hex] //BL:=Hex[BL] MOV BYTE PTR [EAX+1],BL //Result[EDX]:=BL INC ESI //Inc(V), next char SUB EDX,2 //EDX:=EDX-2 SUB EAX,2 //Dec(Result, 2), move back 2 chars CMP EDX,0 //if EDX 0 JNE @Cycle // <> then @Cycle POP ESI //open register ESI POP EBX //open function address end;