I came across a piece of assembly code like following one
int fourth(char *str) {
return str[3];
{
0804834f <fourth>:
804834f: 55 push %ebp
8048350: 89 e5 mov %esp,%ebp
8048352: 8b 45 08 mov 0x8(%ebp),%eax
8048355: 83 c0 03 add $0x3,%eax
8048358: 0f be 00 movsbl (%eax),%eax
804835c: c9 leave
804835d: c3 ret
how come does it reach M[(%eax)+3] ?
thanks in advance
Yes of course. If you’re only addressing a
byte, then you have to be able to reach any address. (which is the case in the code snippet:movsblis a byte access)Furthermore, x86 allows misaligned memory access even for multi-byte words. (though usually at a performance cost) So even then, the address does not have to be a multiple of 4.
*The exception is with the SSE/AVX registers. Where you need to use explicit misaligned
movinstructions for misaligned memory access.