2016-05-07 9 views
1

Für meine Aufgabe möchte ich zwei Zeichenfolge verketten. Das ist bisher mein Code. das Ergebnis sollte Hello World sein, aber es zeigt nur hallo :-(Fehler bei Verkettung zwei Zeichenfolge in Assembly

Wie soll ich Quelle String am Ende von Ziel?

Was mir persönlich fehlt hinzufügen?

;--------------------In main.CPP 

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

    ;main 
       char str1[] = {'h','e','l','l','o',0}; 
       char str2[] = {'w','o','r','l','d',0}; 
       StrCatAsm(str1,str2) 
       string1 = 'h','e','l','l','o','w','o','r','l','d',0 
    ;------------------In main.asm 

    Concat PROC uses eax edi ecx esi, 
         destination:DWORD, 
         source:DWORD 

     mov al,0    ;looking for zero terminated 
     mov ecx,100   ;number of possible loop 
     mov edi, source  
     repne scasb   ;look for end of source 
     not ecx    ;ecx is 6 
     mov esi, destination 
     rep movsb   ;loop for copy ESI into EDI 

     ret 
    Concat ENDP 
+2

Vermutlich nicht verwandt, aber: Sind Sie sicher, dass nach "Hallo" genug Platz ist, um etwas zu verketten? Oder solltest du * '' hallo' 'in (unbenutztes) 'destination' kopieren und dann' 'world'' danach einfügen? – usr2564301

Antwort

1

Wie soll ich eine Quellzeichenfolge am Ende des Ziels hinzufügen?

Wenn das ist, was Sie brauchen, dann sollten Sie die abschließende Null im Ziel suchen (und nicht in der Quelle als Ihr Code tut!).
Sobald die abschließende Null gefunden wird, ist Ihr EDI Register hinter seinem Standort und Sie müssen daher 1 Position sichern!

mov al, 0    ;looking for zero terminated 
mov ecx, 100   ;any large number will do! 
mov edi, DESTINATION 
repne scasb   ;look for end of DESTINATION 
DEC EDI 
mov ecx, 6   ;length of SOURCE {'w','o','r','l','d',0}; 
mov esi, SOURCE 
rep movsb    ;loop for copy ESI into EDI 
0

Hier ist der Pseudocode für die String-Verkettung:

t1 = get string length of str1   ; find length not including zero terminate 
t2 = get string length of str2   ; find length not including zero terminate 
ct = (t1 + t2) + 1      ; total length + 1 for zero terminate 
dest = allocate memory for size ct  ; get total string length 

n = 0 
nt = 0 

while str1[n] != 0      ; Until you hit str1 zero terminate 
    dest[nt] = str1[n] 
    n = n + 1 
    nt = nt+1 

n = 0 

while str2[n] != 0      ; Until you hit str1 zero terminate 
    dest[nt] = str2[n] 
    n = n + 1 
    nt = nt + 1 

dest[nt] = 0       ; final terminate string 
Verwandte Themen