2

This line is not very clear to me (I'm very new to Assembly):

movsbl 0xffffffff(%edx,%ebx,1),%eax

I understand mov, but movsbl is a new one to me. In a simpler example that uses foo instead of 0xffffffff(%edx,%ebx,1) I understand it to be this (not at all sure this is right, just searched a related topic):

eax = foo&0x800000ff;

I've never had a line of Assembly refer to -1 (0xffffffff), where is the information being put into %eax coming from exactly? Is it whatever is stored at:

[%edx + %ebx -1]
asimes
  • 5,749
  • 5
  • 39
  • 76

2 Answers2

6
movsbl <%x, %y, 1>, %z

Says, read one byte from the memory location addressed by the first operand (x), extend the byte to 32 bits, and store the result in the register (z).

<%x, %y, 1> is the memory address formed by adding together the values of x and y; 1 is the multiplier applied to y.

Ian Atkin
  • 6,302
  • 2
  • 17
  • 24
  • Could you explain what is happening when a byte is extended to 32 bits? Does it fill the 3 most significant bytes with zeroes? I saw from a different explanation that it "sign extends". I tried to show that by writing 0x800000ff, is that correct / incorrect? – asimes Feb 19 '13 at 05:28
  • Extend = extend from byte to longword. In Intel syntax, the mnemonic of this instruction is MOVSX. A C compiler may use this instruction when a variable of type int8_t needs to be converted to int, which happens automatically on arithmetic and a few other operations such as integer promotion. The instruction writes to all 32 bits (or whatever) of the destination register, it avoids performance penalties that may result from writing to only the low 8 bits of a register. – Ian Atkin Feb 19 '13 at 05:32
  • What I meant was what gets put into the 3 most significant bytes. I'm assuming the least significant byte is the copied byte, but what about the other ones? Are they all zeroes no matter what? – asimes Feb 19 '13 at 05:35
  • Yes, the register is filled with zeros. – Ian Atkin Feb 19 '13 at 05:45
  • Thank you, I don't understand what was meant by sign extending then, that's why I threw the 8 into the mask. – asimes Feb 19 '13 at 05:45
  • 3
    With `movsx` the top 24 bits end up equal to the most significant bit of the source byte, hence *sign* extension. It's `movzx` that fills those 24 bits with 0, hence *zero* extension. – Alexey Frunze Feb 19 '13 at 07:11
1

If you'd write it in C, the line would be something akin to:

#include <stdlib.h>

int loadByte(char *base, size_t index)
{
    return (int)base[index - 1];
}

Compiling this (on UN*X, for 64bit x86) results in the following object code:

Disassembly of section .text:

0000000000000000 :
   0:   0f be 44 37 ff          movsbl 0xffffffffffffffff(%rdi,%rsi,1),%eax
   5:   c3                      retq

As previously said, movsb means move (load) a byte, sign extend it to ... (so there are movsbw, movsbl and movsbq for conversions to word / short, long / int and quad / long long).

Your assembly is for 32bit (because the registers used for addressing are 32bit), but otherwise the meaning is the same.

FrankH.
  • 17,675
  • 3
  • 44
  • 63