2

I am using x86 AT&T Assembly on Ubuntu.

I need to rewrite any complex indirect addressing modes, like based indexed mode, into simple indirect addressing mode.

For example, take this snippet:

.L4:
    movl    i, %eax
    movzbl  ch, %edx
    movb    %dl, dict(%eax)

The line movb %dl, dict(%eax) is based indexed addressing I think. What it does is it takes dict+%eax and dereferences it, then places %dl into it, right?

Now I wrote it like this to make it simple indirect addressing:

.L4:
    movl    i, %eax
    movzbl  ch, %edx
    addl    dict, %eax
    movb    %dl, (%eax)

As you can see I first added dict and %eax and placed the result into %eax as well. Then on the next line I just dereference the result.

This should work the same way as above, right?

It compiles too, but when running it, I get a segmentation fault at my new movb line.

Why doesn't this work?

Jester
  • 56,577
  • 4
  • 81
  • 125
Lorenz Leitner
  • 521
  • 7
  • 22

1 Answers1

3

You got everything right except for a peculiarity of at&t syntax: you need $ sign for immediates, and you use the address as an immediate there. So what you really want is addl $dict, %eax. What you had loaded a value from memory at address dict and later used that as address, causing the fault.

Jester
  • 56,577
  • 4
  • 81
  • 125
  • Thank you, now it works! I forgot about the $ sign, works the same way as the & in C. Although now I get a segmentation fault later on in the code... – Lorenz Leitner Apr 07 '15 at 17:44
  • I know why I now get an error further down. Since I use `%eax` to store the address of `dict+%eax`, `%eax` is now a really long number, an address, and not my array index any longer. So when I use it again later I get a segmentation fault because it tries to do something like `dict[40203402]`. Maybe I can use another register I never use, like `%ebx` for this... Or I just move `i` back to `%eax` right after the indirect addressing. That worked! – Lorenz Leitner Apr 07 '15 at 17:59