views:

161

answers:

1

How can you XOR the value stored in EAX?

The problem is at this line:

xor eax, key

EAX contains the address of the value i want to XOR. How can I accomplish this? I though it would be something along the lines of:

xor [eax], key

but that doesn't work (syntax error)

 decrypt proc startAddress:DWORD , sizeOfSegment:DWORD , key:DWORD


  xor ecx, ecx    ; clear the ecx register for the counter
  mov eax, startAddress  ; copy the start address to eax
  .while ecx < sizeOfSegment  ; loop through the code
  xor eax, key    ; XOR decrypt the word
  inc eax
  inc ecx
  .endw

   ret

  decrypt endp
+8  A: 

You said you're doing ...

xor eax, key    ; XOR decrypt the word

... but I'm guessing that's a typo and you're actually trying to do ...

xor [eax], key    ; XOR decrypt the word

The reason why that won't work is that key isn't a register: it's probably, I don't know, a synonym for something like [ebp+4].

x86 (not just MASM, but nasm too: the x86 instruction set) allows register-to-register and register-to-memory and memory-to-register operands, but not memory-to-memory.

So, you need to load key into some spare register, e.g.:

  mov eax, startAddress
  mov ebx, key ; move key into a register, which can be XORed with [eax]
  .while ecx < sizeOfSegment
  xor [eax], ebx


On a separate matter, do you really want to do inc eax or should it be add eax,4 ? I mean, you say "XOR decrypt the word": do you mean "word", or "byte", or "double-word"?

ChrisW
Oh, you're right. D'oh.
Jed Smith