Search code examples
memoryassemblyatt

ASM x86 - Move value into memory


I'm using a 32 bit Linux system and I can't understand how to put a value into memory. This is an example:

str:    .asciz "AAA"
p:    .long 0

.text
.globl  main

main:
    movl $str, p    #Save the address of str into p (?)

I know that I can do movl $str, %eax to store the str address inside the eax register, but I can't do the same thing with p because I receive a segmentation fault error.

I tried also this alternative, but the result is always a segmentation fault:

main:
call self
self:
    pop %ebp
    movl $str, (p-self) (%ebp)

Can someone explain to me how to use mov in the right way? I'd like also to know if it's possible store the str address into p at compile time.


Solution

  • Can someone explain to me how to use mov in the right way?

    It isn't about the mov. That's fine. You need to specify a section to put the data into. Add .data at the front of the program to put it in the .data section. Otheriwise, the data are put into the .text section by default, which is read-only on modern OSes. Writing to it leads to a Segmentation fault.

    I'd like also to know if it's possible store the str address into p at compile time.

    It is possible:

    p: .long str