Lorenz Leitner
Lorenz Leitner

Reputation: 591

Transform based indexed mode to indirect addressing mode (x86 assembly)

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?

Upvotes: 2

Views: 599

Answers (1)

Jester
Jester

Reputation: 58762

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.

Upvotes: 3

Related Questions