I'm new to assembly and I'm learning it from Programming from the Ground Up. On pages 41 and 42, the book talks about indexed addressing mode.
The general form of memory address references is this:
ADDRESS_OR_OFFSET(%BASE_OR_OFFSET,%INDEX,MULTIPLIER)
All of the fields are optional. To calculate the address, simply perform the following calculation:
FINAL ADDRESS = ADDRESS_OR_OFFSET + %BASE_OR_OFFSET + MULTIPLIER * %INDEX
ADDRESS_OR_OFFSET and MULTIPLIER must both be constants, while the other two must be registers. If any of the pieces is left out, it is just substituted with zero in the equation.
So I decided to play around with this a little bit. I wrote the following piece of code:
.code32
.section .data
str:
.ascii "Hello world\0"
.section .text
.global _start
_start:
movl $2, %ecx # The index register.
mov str(, %ecx, ), %bl
movl $1, %eax
int $0x80
I expected to get 72 (ASCII code for H) as the exit result of the program since there isn't any multiplier (which based on the book, should be substituted with zero). But surprisingly I get 108 instead (ASCII code for l). I thought this might be an .ascii
thing and tried to see if I can get different results with different data types. I got the same results with the .byte
as well.
I tried to lookup indexed addressing mode in x86 assembly with AT&T syntax but I couldn't get anything useful (likely because I don't know what to search for).
Is there anything I'm missing or is it a mistake in the book? I really appreciate it if you elaborate given that I'm new to this field.