Trao đổi với tôi

http://www.buidao.com

11/5/09

[MASM] Using C Run-time Library (libc.lib) with Masm

Using C Run-time Library (libc.lib) with Masm

Link: http://nhatphuongle.spaces.live.com/blog/cns!320FF19317F0C9A2!749.entry

Đây là ví dụ về cách sử dụng thư viện Run-time của C vào trong MASM. Ví dụ này cũng rất đơn giản, chỉ là sử dụng các hàm về chuỗi như:

strcat (Hàm nối chuỗi)

char * strcat ( char * destination, const char * source );

Concatenate strings

Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a new null-character is appended at the end of the new string formed by the concatenation of both in destination.

Parameters
destination
Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.
source
C string to be appended. This should not overlap destination.
Return Value: destination is returned.

Source Code in VC++:

/* strcat example */
#include
#include

int main ()
{
char str[80];
strcpy (str,"these ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str,"concatenated.");
puts (str);
return 0;
}

Output:

these strings are concatenated.

strstr (Hàm tìm chuỗi con)

const char * strstr ( const char * str1, const char * str2 );
char * strstr ( char * str1, const char * str2 );

Locate substring

Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
The matching process does not include the terminating null-characters.

Parameters
str1
C string to be scanned.
str2
C string containing the sequence of characters to match.
Return Value

A pointer to the first occurrence in str1 of any of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.

Source Code in VC++:

/* strstr example */
#include
#include

int main ()
{
char str[] ="This is a simple string";
char * pch;
pch = strstr (str,"simple");
strncpy (pch,"sample",6);
puts (str);
return 0;
}

Output:

This is a sample string

Using C Run-time Library (libc.lib) with Masm:

;------------------------------------------------------------
; Using C Run-time Library with MASM
; Coded by: NhatPhuongLe
; Homepage: http://nhatphuongle.spaces.live.com/default.aspx
;------------------------------------------------------------

include \masm32\include\masm32rt.inc
includelib libc.lib ; CRT static library

;Define prototype these function from CRT Lib

strcat PROTO C :DWORD,:DWORD
strstr PROTO C :DWORD,:DWORD
strchr PROTO C :DWORD,:DWORD

.data

szDest db 'This is a sample ',0
db 10 dup(0)
szSource db 'using CRT Library in MASM',13,10,0
szString db 'Library',0
crlf db 13,10,0

.data?
szbuffer db 20 dup(?)

.code

start:
invoke strcat,ADDR szDest,ADDR szSource
invoke StdOut,ADDR szDest
invoke strstr,ADDR szDest,ADDR szString
call print
invoke strchr,ADDR szDest,'i'
call print
invoke ExitProcess,0

print PROC
invoke dw2hex,eax,ADDR szbuffer
invoke StdOut,ADDR szbuffer
invoke StdOut,ADDR crlf
ret
print ENDP

END start