1

I am learning assembly and compiled my some of my C code to assembly to see what it's doing. I had this piece of code which was ignoring whitespaces in my string.

while ((str[index] == ' ') || (str[index] == '\n') || (str[index] == '\t')
        || (str[index] == '\f') || (str[index] == '\r') || (str[index] == '\v'))
        index++;

In normal compilation, assembly compares every one of these characters and I understand that. But if I compile with -O for optimised version, I get a much neater version but I'm not really sure what it's doing.

LBB0_12:                                ##   in Loop: Header=BB0_1 Depth=1
    incq    %rax
LBB0_1:                                 ## =>This Inner Loop Header: Depth=1
    movzbl  (%rdi,%rax), %ecx
    leal    -9(%rcx), %edx
    cmpb    $5, %dl
    jb  LBB0_12
## %bb.2:                               ##   in Loop: Header=BB0_1 Depth=1
    cmpb    $32, %cl
    je  LBB0_12

I get that 32 is checking for space. But all other characters seem to be included in label 1 and there I get lost. Could someone please explain this? Thank you so much in advance!

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
vendelle
  • 31
  • 4
  • 4
    The compiler used the fact that the other 5 are consecutive in the ascii table so it's doing a range check for 9..13 – Jester Mar 23 '21 at 22:06
  • Basically a duplicate of [double condition checking in assembly](https://stackoverflow.com/a/5197264) for *how* the compiler is doing the range check that @Jester pointed out. Maybe there's another Q&A about ASCII whitespace other than `' '` being contiguous? – Peter Cordes Mar 23 '21 at 22:42
  • [All the Whitespace Characters? Is it language independent?](https://stackoverflow.com/q/18169006) mentions the relevant ASCII codes, but isn't about the fact that they're contiguous. – Peter Cordes Mar 24 '21 at 02:48

0 Answers0