-2

I'm trying to write simple assembly program, which takes 2 strings from command line and then counts occurrences of first one in the second one.
For example: x="abc" y="abcabc abc". It should print 3.

I can't compile this code, because of too many memory references for 'cmp'.
I think that problem is between //here. How can I solved it?

.intel_syntax noprefix
.global main
.text

main:

    mov eax, [esp+4]
    cmp eax, 3
    je fun

    mov eax, offset error
    push eax
    call printf
    add esp, 4
    mov eax, 0
    ret

fun:

    mov eax, [esp+8]
    mov ebx, [esp+12]
    xor ecx,ecx
    push edx
    call loop

loop_z: 

    xor edx,edx

loop:

    inc ecx
    cmp byte ptr [eax+ecx-2],0
    je end
    cmp byte ptr [ebx+edx-1],0
    je zer

    jump:


    //here
    cmp byte ptr [eax+ecx-2],[ebx+edx-1]
    //here


    jne loop_z
    inc edx
    jmp loop

zer:

    pop edx
    inc edx
    push edx
    xor edx,edx
    jmp jump

end:

    call printf
    add esp, 4
    mov eax, 0
    ret


.data
error:
.asciz " 2 arg \n"
  • 1
    CMP doesn't take two memory operands thus the error here `cmp byte ptr [eax+ecx-2],[ebx+edx-1]` . You'll have to move one of them into a register and then compare the other against the register. – Michael Petch Dec 12 '17 at 17:52
  • 1
    Compare in two steps, using an intermediate register. It's unfortunate that you used up all the byte-addressable registers already. – Jester Dec 12 '17 at 17:52
  • Any suggestions how can I get empty register to do this? Writing from beginning is the only way? – J.Brown Dec 12 '17 at 18:19
  • You still have `esi` and `edi` you could use one of them instead of `eax`, freeing up `al`. If you are lazy and don't care, you can `movzx` both bytes into `esi` and `edi` respectively and compare them. That takes the least amount of rewrite. You could also `push`/`pop` `eax`. – Jester Dec 12 '17 at 18:39

1 Answers1

0

Writing from the beginning is not the only way, but in this case, probably the better alternative. Homework assignments are always problematic, in trying to balance between help and actually doing the task.

Consider;

str1    db   'We will always wonder how the will of those without a '
             'will can be so unwilling', 0
str2    db   'will', 0

IA32 is ideally suited to this kind of task and the two instructions you want to pay attention too are SCAS & CMPS. They are predicated on the use of ECX, ESI & EDI.

    mov     esi, str1
    mov     edi, str2
    mov     ecx, 74          ; Length of str1 - str2
    xor     ebx, ebx

NOTE: I've hard coded the length in, but you'll need to device something where the program will calculate the value for ECX.

Those are the only 4 registers you need to do the job. Visually we can see there are 4 occurrences of one in the other, but you'll have to devise a means by which to differentiate between upper and lower case as 'W' is not equal to 'w'.

Review the instructions CMPS & SCAS and see what you come up with.

Shift_Left
  • 1,208
  • 8
  • 17