3 ms·
The solution is that a string instruction can be interrupted in the middle of the instruction, unlike most instructions. This is true even for later x86 proces
by userbinator 4y ago
The solution is that a string instruction can be interrupted in the middle of the instruction, unlike most instructions.
This is true even for later x86 processors that move entire cachelines at a time, and also leads to one sneaky technique for detection of VMs or being debugged/emulated:
https://repzret.org/p/rep-prefix-and-detecting-valgrind/ https://repzret.org/p/rep-prefix-and-detecting-valgrind/
https://silviocesare.wordpress.com/2009/02/02/anti-debugging-prefetch-tricks-and-single-stepping-through-a-rep-stosmovs/ https://silviocesare.wordpress.com/2009/02/02/anti-debugging...
- rep_lodsb 4y agoSmall nitpick: the REP STOSB isn't left in the prefetch queue while it's being executed by the microcode engine. The queue always contains the next instructions to execute (obviously it's not as simple for out-of-order architectures). The following should load AL with 0 on an 8088, 1 on an 8086: mov bx,test_instr+1 mov byte [bx],1 ;make sure immediate operand is set to one mov cl,255 ;max shift count to give time for prefetch cli ;no interrupts allowed align 2 shl byte [bx],cl ;clears byte at [bx] after wasting lots of time nop ;first byte in queue nop ;2nd nop ;3rd sti ;4th (intr. recognized after next instruction) test_instr: mov al,0ffh ;opcode is 5th, operand is 6th The 255x left shift is done in an internal register, during which the 8086 has more than enough time to fetch all six following bytes. I didn't test this code but am 99% certain it works.
- secondcoming 4y agousername checks out!
- saagarjha 4y agoI’m curious what rep lodsb even does lol
- rep_lodsb 4y agoPotentially read lots of stuff, without doing anything useful ;) Sort of a self-deprecating name I chose. But it's also an interesting instruction, in that Intel had to actually write microcode specifically to support it in every x86 compatible chip going forward. It loads CX bytes from memory at DS:SI into the accumulator. Overwriting whatever was there previously every time. The only uses I can think of: - test memory speed - if CX is known to be 0 or 1, conditionally read one byte - trigger some I/O device where the value read isn't important
- 1000100_1000101 4y agoI think rep is expected to be combined with a condition. repne lodsb will read while not fetching 0, so a search for end of C string. Using the remaining cx or si value you can determine the length fetched until the condition failed.
- rep_lodsb 4y agoLODS, STOS, MOVS, INS and OUTS are all unconditional (doesn't matter which version of the prefix you use, but officially REP should be the same opcode as REPE). SCAS is the one that scans for e.g. a nul byte.