Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why REP LODS AL instruction exists?

In other words, is there any case I might need this instruction?

From Intel Instructions Manual, this is what the instruction do:

Load (E)CX bytes from DS:[(E)SI] to AL.

Take the following example in NASM:

section .data
    src: db 0, 1, 2, 3

section .code
    mov esi, src
    mov ecx, 4
    rep lodsb    ; mov al, byte[esi + ecx -1]; ecx++  

While the instruction rep lodsb is executing, I don't have any control over the value loaded in al. All I can do is waiting, until the instruction terminates to get the last value of al, which, of course I can get directly, without the loop.

The same question goes for the rest of the family: REP LODS AX, REP LODS EAX and REP LODS RAX.

like image 225
Bite Bytes Avatar asked Jun 11 '17 17:06

Bite Bytes


1 Answers

Reading from memory can have side effects in some cases. For example, this can happen if the memory is mapped to hardware registers. The exact side effect would depend on the hardware. So rep lodsb can be useful in very rare cases where you need the side-effect to happen, but don't need the values that were read.

like image 139
interjay Avatar answered Sep 21 '22 16:09

interjay