Chủ Nhật, 16 tháng 4, 2017

#include <stdio.h>
//#include <string.h>

//for mGW: gcc -o _Ptr4.cpp

static inline char * str_cpy(char *dst, const char *src) {
    int rsrc, rdst;
    __asm__ __volatile__(
        "1: \tlodsb\n\t;"
        "stosb\n\t;"
        "testb %%al,%%al\n\t;"
        "jne 1b;"
        : "=&S" (rsrc), "=&D" (rdst)
        : "0" (src),"1" (dst)
        );
    return dst;
}
/*
char* str_cpy(char *d, const char *s){
    while ((*d++ = *s++) != 0);
    return d;
}
*/
void AssignString(char *ptr){
    str_cpy(ptr, "This is the 2nd string");
}
int main(){
    //char *ptr = "This is original string";
    char ptr[] = "This is original string";
    printf("%s\n", ptr); //"This is original string"
   
    //ptr := "This is the 2nd string"; BNF (Backus-Naur Form) syntax
    AssignString(ptr);
    printf("%s\nDone, bye ..", ptr); //"This is the 2nd string"
   
    getchar();
}

Không có nhận xét nào:

Đăng nhận xét