stnovel
一般會員
發表:4 回覆:4 積分:1 註冊:2007-01-10
發送簡訊給我
|
看到很多人在問如何在 BCB 內加入 Assembly ?
所以寫了一個小 Sample 給大家參考一下.
<textarea class="cpp" rows="10" cols="60" name="code">
//--------------------------------------------------------------------------- #include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1; char* StrRemoveAsciiC(char *str1,int ascii)
{
char tmp[2]={0};
char str2[512]={0}; tmp[1]='\0';
for (int i = 0; i <= strlen(str1); i )
{
if(str1[i] != ascii)
{
tmp[0]=str1[i];
lstrcat(str2,tmp);
}
}
return str2;
} char* StrRemoveAsciiM(char *str1,int ascii)
{
int len=strlen(str1);
BYTE abyte=ascii; _asm {
std
mov edi,str1 //String
mov al,abyte //Remove ASCII
mov ecx,len //String Length
sub ebx,ebx
mov dword ptr [ebp-04h],0h //Set a Template Index = 0
mLoop: or ecx,ecx
jle mLoopEnd
dec ecx
cmp byte ptr [edi ebx],al //Compare String Index with AL
je mCompare
mov dl,byte ptr [edi ebx] //Save char to DL
push ebx //Push ebx
mov ebx,dword ptr [ebp-04h] //Set ebx to Template Index
mov byte ptr [edi ebx],dl //Replace char as DL
inc dword ptr [ebp-04h]
pop ebx //Release ebx
mCompare: inc ebx
jmp mLoop
mLoopEnd: mov ebx,dword ptr [ebp-04h]
mov byte ptr [edi ebx],0h
cld } return str1;
} char* StrReplaceAsciiM(char *str1,int ascii,int replace)
{
int len=strlen(str1) -1;
BYTE abyte=ascii;
BYTE bbyte=replace; _asm{
std
mov edi,str1 //String
mov al,abyte //Be Replace ASCII
mov dl,bbyte //Replace ASCII
mov ecx,len //String Length
sub ebx,ebx //Initinal string index
mLoop: or ecx,ecx
jle mLoopEnd
dec ecx
cmp BYTE PTR [edi ebx],al //Compare String Index with AL
jne mCompare:
mov BYTE PTR [edi ebx],dl //Replace AL to be DL
mCompare: inc ebx //Increase Index
jmp mLoop
mLoopEnd: cld } return str1;
} //---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char *aa;
char *rtn; aa="abc de f gh ijk lm n op"; rtn=StrRemoveAsciiC(aa,32); ShowMessage(rtn);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
char *aa;
char *rtn; aa="abc de f gh ijk lm n op"; rtn=StrRemoveAsciiM(aa,32); ShowMessage(rtn);
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Button3Click(TObject *Sender)
{
char *aa;
char *rtn; aa="abc de f gh ijk lm n op"; rtn = StrReplaceAsciiM(aa,32,45); ShowMessage(rtn);
}
//--------------------------------------------------------------------------- </textarea>
|
stnovel
一般會員
發表:4 回覆:4 積分:1 註冊:2007-01-10
發送簡訊給我
|
char* StrRemoveAsciiC(char *str1,int ascii)
這是VC 的寫法,Function 是用來將字串中不要的字元去除掉
str1: 是字串指標
ascii : 要去除掉的字元的 ASCII 碼
char* StrRemoveAsciiM(char *str1,int ascii)
這是 Assembly 的寫法,Function 是用來將字串中不要的字元去除掉
str1: 是字串指標
ascii : 要去除掉的字元的 ASCII 碼
char* StrReplaceAsciiM(char *str1,int ascii,int replace)
這是 Assembly 的寫法,Function 是用來取代字串中的某些字元
str1: 是字串指標
ascii : 被取代的字元的 ASCII 碼
replace : 要去取代的字元的 ASCII 碼
|