0

IA-32 assembly:

pushl   %ebp
movl    %esp, %ebp
movl    12(%ebp), %eax
addl    8(%ebp), %eax
movzbl  (%eax), %eax    # <-
movsbl  %al,%eax        # <-
popl    %ebp
ret

I highlighted which lines are confusing for me. I kind of understand what movzbl and movsbl do separately, but what purpose do they serve by being side by side?

Why not just use movl. If we wanted to use sign extend, why don't we just use movsbl from the start, or aren't we losing data when we are extending al register, which is only only byte (when size of data is more than 1 byte)?

EDIT: i found this Q&A, but I still could not get it: need help understanding the movzbl call in this function

EDIT2: I realized that I'm dealing with characters, so that clears some things up.

  • 1
    Probably reading from a `signed char` array but the function returns a `signed int`. The compiler preferred to overwrite the whole `eax` when reading from the array instead of writing only `al` to prevent stalls on old/side architectures. – Margaret Bloom Apr 30 '21 at 09:22
  • Ah, yeah. I'm dealing with chars and that's why 1-bit register is satisfactory. So, I understand that part. – giorgi shengelaia Apr 30 '21 at 09:33
  • Everything else is still confusing for me. Could it be that it is some weird compiler stuff that we are dealing with? – giorgi shengelaia Apr 30 '21 at 09:35
  • 1
    If you compile with optimization disabled, the compiler sometimes makes extra-inefficient asm... Go figure. (And yes, in the mainstream x86 calling conventions, `char` is signed.) – Peter Cordes Apr 30 '21 at 09:39

0 Answers0