-5

I am just getting into assembly and was wondering is there any way of xor'ing more than 1 byte at a time. I would like to do this in 1 command:

IDEAL
    MODEL small
    STACK 100h
    DATASEG

    msg db "I LIKE ASSEMBLY$"

    CODESEG
    start:
        mov ax,@data
        mov ds, ax
        xor [msg],01001010b

    exit:
        mov ax, 4c00h
        int 21h
    END start

But with this piece of code it only xors the 'I'

Koren Minchev
  • 81
  • 1
  • 1
  • 7
  • 1
    On an 8086 you can xor two bytes at a time, but to xor that string you'll have to loop through the characters and xor each. – Michael Petch Oct 29 '16 at 21:41
  • 1
    https://hjlebbink.github.io/x86doc/html/PXOR.html – Hans Passant Oct 29 '16 at 21:52
  • Let us be specific here. What processor are you running this on? (x86 is ambiguous) although one might guess the code given is meant to be running in an environment that supports 16-bit code and MS-DOS. `IDEAL` at the top suggests you are using TASM that I don't recall supporting 64-bit instructions. – Michael Petch Oct 29 '16 at 21:55
  • Read the instruction-set reference. You can ask a question if you find it confusing, but check it first. Links in the [x86 tag wiki](http://stackoverflow.com/tags/x86/info). – Peter Cordes Oct 30 '16 at 01:19

2 Answers2

2

EMU8086 is limited to working with 16 bits in one go. To XOR the whole string you need to use a loop of some kind. Since every character in the string is represented by just 1 byte or 8 bits, it would be possible to XOR 2 characters at a time!

I'll first show a solution when processing 1 character at a time:

start:
    mov ax,@data
    mov ds, ax
    mov bx, offset msg
    jmp TestEnd
    again:
        xor [bx], 01001010b
        inc bx
    TestEnd:
        cmp [bx], '$'
        jne again

Now comes a solution that processes 2 characters at a time:

start:
    mov ax,@data
    mov ds, ax
    mov bx, offset msg
    again:
        cmp [bx], '$'
        je  EndOfString
        cmp [bx+1], '$'
        je  LastChar
        xor word ptr [bx], 0100101001001010b
        add bx, 2
        jmp again
    LastChar:
        xor [bx], 01001010b
        inc bx
    EndOfString:

Both these solutions leave the BX register pointing at the terminating $ character.

Fifoernik
  • 9,779
  • 1
  • 21
  • 27
0

Yes, there are 2, 4, and 8 byte opcodes (The 8 byte opcode requires x86-64, and only allows a sign-extended 32-bit immediate, so you need your constant in a register to use it with this pattern.)

xor dword [msg], 01001010010010100100101001001010b
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Joshua
  • 40,822
  • 8
  • 72
  • 132
  • in most flavours of asm, the operand-size doesn't go on the immediate (or has a potentially-different meaning there, forcing the use a of 32-bit immediate even if it would fit in an imm8). `xor qword [msg], -1` will flip every bit in 8 bytes. (There is a 64-bit operand-size form, but its immediate is still only 32-bit or 8-bit sign-extended, like for most insn: [`xor r/m64, imm32` or `xor r/m64, imm8`](http://www.felixcloutier.com/x86/XOR.html)) – Peter Cordes Oct 30 '16 at 01:17
  • @PeterCordes: And sign-extended is not what he wants. I guess I got used to nasm being rather forgiving on which side the size marker goes. – Joshua Oct 30 '16 at 02:34
  • yeah in this case the OP's constant would need to be in a 64-bit register, since 8 copies of it can't be represented by a sign-extended imm32. But anything with the upper 32 bits matching bit 31 can. Anyway, more importantly, `xor [msg], dword -1` would waste 3 bytes on an imm32 because you told NASM you wanted a dword immediate, so that stops it from using an imm8. It's a bad habit and you should never write it that way if you don't actually want that. – Peter Cordes Oct 30 '16 at 02:42
  • Actually, I just tested it, and `xor [a_buffer], dword -1` still assembles to an imm8 with `nasm -felf64`, but YASM says "error: invalid size for operand 1". Anyway, clearly it's better to put the `dword` on the memory operand, not the constant, so you should fix your answer. – Peter Cordes Oct 30 '16 at 02:43