1

In ecx I have some string, like "abc".

mov ah, BYTE PTR [ecx+0]
mov al, BYTE PTR [ecx+1]

What does it exactly do? It's like in ah I have "a" and in al I have "b"?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Artur Pyśk
  • 51
  • 1
  • 1
  • 2
  • 1
    If `ecx`is a pointer to your string, then yes. Why haven't you tried it? – Jester Dec 14 '17 at 23:45
  • If you have the string itself in the `ecx`, then no, it's not clear what that code will do, probably crash on invalid memory access, as string "abc" will very likely not form a memory address accessible to your process. But you very likely meant "I have memory address of string in ecx", then it will do what you think. And as Jester wrote, you should try these yourself, at least you will practice usage of debugger. – Ped7g Dec 14 '17 at 23:48

2 Answers2

5

byte ptr indicates that the memory operand refers to a byte in memory as opposed to a word or dword. Usually, this can be omitted as the assembler can infer the operand size from registers used but there are some instructions like mov [eax],0 where the size cannot be inferred, so a byte ptr, word ptr or dword ptr prefix is needed.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • 2
    Or with assemblers like MASM, I think you need `mov al, byte ptr [my_dword_location]` to override the implied operand size associated with a label based on how you declared the data, if you want to load with a size that doesn't match the `dword` or `byte` or whatever following the label. – Peter Cordes Dec 15 '17 at 05:48
4

There are times when we need to assist assembler in translating references to data in memory.

byte ptr -> it simply means that you want to fetch a byte from the address. if it said word ptr or dword ptr, you would get a word or dword from the address in source index.

When you need something like byte ptr example you move an immediate value to an indirect address:

mov ebx, OFFSET some_symbol    ; or a pointer from another register
mov [ebx], 10

This won't normally be allowed -- the assembler doesn't know whether you want the 10 as a byte, a word, a double-word, or (in 64-bit code) a quad-word. You need to make it explicit with a size specification:

mov byte ptr [ebx], 10  ; write 10 into a byte
mov word ptr [ebx], 10  ; write 10 into a word
mov dword ptr [ebx], 10 ; write 10 into a dword
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Zahid Khan
  • 2,130
  • 2
  • 18
  • 31
  • The OP is using 32-bit code, no need to use a 16-bit example. Also, [`[dx]` isn't even a valid 16-bit addressing mode.](https://stackoverflow.com/questions/12474010/nasm-x86-16-bit-addressing-modes). Also, your last edit overwrote my spelling / other improvements edit. – Peter Cordes Dec 15 '17 at 08:32
  • 1
    @PeterCordes amended ☺ This is 4th time I stumble you in Stack Overflow Question Answers! Is that acceptable now? – Zahid Khan Dec 15 '17 at 08:35
  • Your last edit made it worse. `byte ptr dx` isn't valid in any asm syntax. I'll fix it. – Peter Cordes Dec 15 '17 at 08:35