2

Could any one tell me the following's means. I can't understand the insl instruction

static inline void
    insl(uint32_t port, void *addr, int cnt) {
        asm volatile (
            "cld;"
            "repne; insl;"
            : "=D" (addr), "=c" (cnt)
            : "d" (port), "0" (addr), "1" (cnt)
            : "memory", "cc");
    }
Tianxin
  • 177
  • 11
  • 1
    Have you tried searching for the instruction? There are plenty of reference all over the Internet for x86 assembly. – Some programmer dude Jul 16 '16 at 11:43
  • 1
    This is gcc extended assembler syntax. Take a look at the [gcc assembler extensions](https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html). – Rudy Velthuis Jul 16 '16 at 12:01
  • That is not an instruction, but a function. What **specifically** do you not understand from the documentation? What have you tried? – too honest for this site Jul 16 '16 at 12:06
  • 3
    @Olaf: it is an instruction. – Rudy Velthuis Jul 16 '16 at 13:48
  • 1
    I've found no indication that `repne` is a valid prefix for `insl`. – EOF Jul 16 '16 at 14:33
  • Yeah~ I understand the difference between AT&T syntax and Intel syntax. In AT&T syntax, the instruction insl is just ins + l, the l suffix represents double words. But In Intel syntax, it doesn't have l suffix. – Tianxin Jul 16 '16 at 14:56
  • 1
    @Tianxin: Not what I mean. `ins` is documented with `rep`-prefix. However, `ins` doesn't set flags. `repne ins` doesn't make sense and is not documented as valid (it is also, strangely, not documented as UD, AFAICT). – EOF Jul 16 '16 at 15:21
  • @RudyVelthuis: Well, there is a function and an instruction with that name. As OP shows the whole function, I assumed she wants to know how that works. For the instruction, IIRC, Intel provides the instruction manuals for free download. – too honest for this site Jul 16 '16 at 17:00
  • 2
    In the Intel manuals, it is not called `insl`, it is called `INSD`. There is no `INSL` in the manuals. – Rudy Velthuis Jul 16 '16 at 17:31
  • 1
    @EOF: Prefixes that don't have any meaning for an instruction are silently ignored, [like `rep ret`](http://stackoverflow.com/a/32347393/224132). I'd expect `repne; insl` to execute the same as `insl`. It's surprising to see this in existing code, although the OP isn't claiming that it worked. Possibly it executes the same as `rep; insl`, but I'd be surprised because I just checked [the manual for `rep`](http://www.felixcloutier.com/x86/REP:REPE:REPZ:REPNE:REPNZ.html) and it doesn't say anything about fallbacks like that. – Peter Cordes Jul 16 '16 at 20:12
  • 1
    @PeterCordes I'm pretty sure it repeats. It might help to think of it this way: There really isn't a REP prefix, just REPE and REPNE. You can use either as a prefix for a string instruction, and if it doesn't set the flags then ZF is ignored. – Ross Ridge Jul 16 '16 at 21:34
  • 2
    I'm with EOF that `repne ins` doesn't make sense from the perspective of checking for flags on an in/out instruction that don't set the flags. Intel's documentation say one thing, but in practice over the years the reality is something else. AFAIK the actual physical x86 processor does allow `repne` on `ins` instruction but it will act just like `rep`. I haven't seen a physical processor throw UD when encountering it. I have seen virtual machines/emulators throw an exception! It is best to not use it to have your software work in the widest range of environments. – Michael Petch Jul 16 '16 at 23:59
  • 2
    I did a bit of research, and it appears in at least one documented environment (an old version of Xen3) the virtual environment didn't fault, but it didn't work as expected. This was discovered by someone working on NetBSD : https://mail-index.netbsd.org/port-i386/2006/01/22/0000.html – Michael Petch Jul 17 '16 at 00:14
  • These instructions are in fact documented. See page 147 in the Intel 80386 programmer's manual: https://www.google.co.uk/url?sa=t&source=web&rct=j&url=http://css.csail.mit.edu/6.858/2015/readings/i386.pdf&ved=0ahUKEwjtzKqApfrNAhUJK8AKHYekDbIQFggbMAA&usg=AFQjCNG4U_zrk7-xYOLma4v4bgBXkHl6iQ&sig2=-DT7NSMM6M59UCcyYpa88g – linguamachina Jul 17 '16 at 10:34

1 Answers1

6

That function will read cnt dwords from the input port specified by port into the supplied output array addr.

insl is equivalent to insd: http://x86.renejeschke.de/html/file_module_x86_id_141.html

GAS syntax uses the l suffix to denote instructions operating on dword (32-bit sized) data.

linguamachina
  • 5,785
  • 1
  • 22
  • 22
  • Why the input parameters not used by insl instruction. Just like insl (%0), %1:(%2); – Tianxin Jul 16 '16 at 13:31
  • 4
    @Tianxin It's just a relic of the old x86 instruction set. Back when we had fewer registers, each had its own intended purpose. Many instructions used implicit input/output registers... `in`/`out` use `ax` and `dx`, while the string instructions use `si`/`di`. Implicit registers keep the opcodes shorter. – linguamachina Jul 16 '16 at 14:08
  • Thank you a lot, I get it. ^ ^ – Tianxin Jul 16 '16 at 14:57