Two elements are compared: (the pointer to a passed to the function)

 mov eax, [a] add eax, 1 xor ebx, ebx mov ebx, [eax] mov temp_char, ebx add eax, 1 mov ebx, [eax] cmp temp_char, ebx JNE exit mov [eax], 's' 

But for some reason, it’s not the element of the array itself that is put into ebx , but a terrible multivalued number. Why?

UPD: This is an assembler insert in C. There is an array, you need to use the assembler insert to find the same elements and remove them to replace all but one, say, $ . That's the whole task. Here I tried to just look at the comparison work: he should compare the second and third elements, in case of coincidence, put in place of the third s . In the variable I saved, because cmp did not want to compare the two registers.

  • a is a link to two links. Moreover, one is located in memory at [[a + 1]] , but this cannot be, since the 32-bit register and addressing are also (otherwise there would be a movzx instruction). - Indy
  • By the way, the address may contain an offset, that is, for example [eax + 1] . And the registers to zero before loading them makes no sense. Saving somewhere in a variable value is also meaningless, since there are still unused general-purpose ecx : ecx and edx . - Indy
  • And mov [eax], 's' is not the Intel syntax: let the segment be omitted ( ds / ss ), but the size of the operand is not specified. A type CHAR has the size of one byte, so the dvard cannot be loaded into it (maybe bl used or the names of the curves). - Indy
  • To begin with, try to write code in C. And the second and third elements of the array are simply compared (provided that, say, eax is the array address and element size - 1 byte): mov bl, [eax+1]; cmp bl, [eax+2] mov bl, [eax+1]; cmp bl, [eax+2] But this is almost useless information to solve this problem :) - user6550

1 answer 1

There is an array, you need to use the assembler insert in C to find the same elements and remove them to replace all but one, say, '$'

Let's start with the most primitive algorithm in C. We will stupidly change all duplicates to the '$' symbol, leaving only the first of the duplicate characters:

 #include <stdio.h> static char * find_and_replace_dups( char * data, size_t n, char to ) { size_t i, j; for( i = 0; i < n; i++ ) { for( j = i + 1; j < n; j++ ) { if( data[i] == data[j] ) { data[j] = to; } } } return data; } #define SIZE ((sizeof(data)/sizeof(data[0]))-1) int main(void) { char data[] = "abcdabcdabcdef"; printf( "%s", find_and_replace_dups( data, SIZE, '$' ) ); return 0; } 

Output: abcd $$$$$$$$$ ef

And now the same thing in assembler. Apparently, Intel is needed, but this is not at hand, like Windows. Therefore, NASM and under Linux: the syntax will be closer, and the rest of the type int 80h - little things, everything is the same main.

 ; ------------------------------------------ section .data ; ------------------------------------------ data db 'abcdabcdabcdef' data_length equ $-data ; ------------------------------------------ section .text ; ------------------------------------------ global main main: cld mov ah, '$' mov esi, data mov ecx, data_length mov edx, ecx add edx, esi for1: lodsb cmp esi, edx je print mov ebx, esi for2: cmp al, [ebx] jne next1 mov [ebx], ah next1: inc ebx cmp ebx, edx jne for2 jmp for1 print: mov ecx, data mov edx, data_length mov eax,4 mov ebx,1 int 80h mov eax, 1 xor ebx, ebx int 80h ; ------------------------------------------ ; That's all, folks! ; ------------------------------------------ 

The conclusion is the same, hooray! abcd $$$$$$$$$ ef

Well, how to turn this into an inline insert is already an independent comprehension :)

PS Well, to the heap: tyk myshy .