2016-05-04 42 views
0

我试图写用于复制零终止的字符串终止0复印零包括x86Assembly(MASM)终止0结尾的字符串

后我请stringCopy(STR1,STR2)另一个字符串包括函数;输出应该好,bye0ld0再见 :(我错过什么? 如何打印正确的结果?

;------------------ in my .cpp file 

extern "C" void __stdcall stringCopy(char[], char[]); 


int main() 
{ 

    char str1[] = { 'h','e','l','l','o',' ','w','o','r','l','d',0 }; 
    cout << str1 << endl; 
    char str2[] = { 'G','o','o','d','-','b','y','e',0}; 
    cout << str2 << endl; 

    stringCopy(str1, str2); 
    cout << str1 << endl; ;should be Good-bye0ld0 
          ;but is Good-bye 
} 


;------------------ in my .asm file 
; Copy zero terminated string2 (including terminating 0) 

stringCopy PROC uses ecx eax esi edi, ;save registers used 
         string1:DWORD, ;address of string1 
         string2:DWORD ;address of string2 

    cld        ;forward direction - clear direction flag 
    push string2      ;address of str2 arg to StrlenAsm 
    call getStringLength    ;get length of str2 
             ;called function responsible for stack cleanup 
    mov ecx,eax      ;length of string in ecx for rep 
    mov edi,string1     ;edi gets destination address for copy 
    mov esi,string2     ;esi gets source address for copy 
    rep movsb       ;copy byte from source to desintation ecx times 
    mov byte ptr[edi],0    ;null terminate copied string 

    ret 

stringCopy ENDP 


getStringLength PROC uses edi,   ;save edi 
      strAdd:DWORD    ;address of string to find length of 

    mov edi, strAdd     ;edi = address of string to get length of 
    xor eax,eax      ;eax to hold length so 0 it out 

looptop: 
    cmp byte ptr [edi],0    ;have we reached the end of the string yet? 
    je done       ;Yes, done 
    inc edi       ;no, increment to next character 
    inc eax       ;and increment length 
    jmp looptop      ;repeat 

done: 

ret 

getStringLength ENDP 
+2

为什么不简化成一个循环,以便同时复制和查找终止0?这是实现'strcpy(3)'的“正常”方式。另外,MASM不会让你在'push' /'call'之后修复堆栈吗? 'getStringLength'返回'ret',而不是'ret 4',所以它不会将操作数弹出堆栈。无论如何,**在调试器**中逐步完成,或者查看反汇编输出以查看MASM指令产生的代码。 –

回答

1

输出应该好,bye0ld0再见 :(我错过什么?

IIUC,你缺少的事实,C/C++字符串处理FUNC tions在第一个遇到的空字节停止(这就是为什么字符串被称为“零终止”)。因此,在将整个str2字符串复制到str1之后,标准C++ lib将打印它的8个首字节。

如何打印正确的结果?

这是正确的结果。如果您希望打印空字节或初始str1内容的字符数,则可以在初始长度str1上循环一次以使用cout << str1[counter]来放置一个字符。您可能会看到一些对应于空字节的微笑或空白字符。

int l = sizeof(str1); 
for(int i=0; i<l; i++){ 
    cout << str1[i]; 
} 
cout << endl;