Analyse following code, meant to sort words by length, explain why it doesn’t return…
- User
Analyse following code, meant to sort words by length, explain why it doesn’t return "send string" but "st" as dest and solve this problem: #include #include using namespace std;
int main() { char source[] = "string send"; char dest[28] = "";
__asm { lea esi, source lea edi, dest mov ecx, 1 jmp start_loop loop_start : lodsb cmp al, ' ' je skip_word add ecx, 1 jmp loop_start skip_word : mov ebx, ecx dec ebx swap_loop : dec ebx jle next_word mov esi, edi mov al, byte ptr[esi] cmp byte ptr[edi + ebx], al jbe next_word mov ebx, ecx mov esi, edi sub ebx, 2 swap_chars: dec ebx cmp ebx, esi jbe next_word cmp byte ptr[edi + ebx], ' ' je next_word mov al, byte ptr[edi] xchg al, byte ptr[edi + ebx] xchg al, byte ptr[edi + ebx + 1] dec ebx mov al, byte ptr[edi + ebx] xchg al, byte ptr[edi + ebx + 1] jmp swap_chars next_word : jmp start_loop start_loop : lodsb stosb loop start_loop mov ecx, 1 jmp reverse_loop reverse_start : lodsb cmp al, ' ' je reverse_next_word add ecx, 1 jmp reverse_start reverse_next_word : cmp ecx, 1 jle next_reverse_word mov ebx, ecx swap_chars_reverse : dec ebx cmp ebx, 0 jbe reverse_start xchg al, byte ptr[edi + ebx] stosb jmp swap_chars_reverse next_reverse_word : jmp start_loop reverse_loop : lodsb stosb loop reverse_loop } cout << "source: " << source << endl; cout << "dest: " << dest << endl; return 0;}
- Assistant