简体   繁体   中英

Writing to memory address

the following compiles without a hitch :

movl %eax, 16

and segfaults whatever value 16 is replaced with.

I believe I read that modern systems don't allow processes to write to memory addresses. Do I remember correctly? If so, why does this syntax still survive in GAS? If not, where is my error, how can I make this line not segfault?

Thank you!

Edit: in case this was the cause for Kerrek SB's comment, this is GNU assembly (AT&T) syntax.

That instruction "moves" (actually copies) 32 bit ( l for long) value to memory address 16. On a modern OS, that address is not writable (it probably is readable, you can try), so you get segmentation fault.

To write to memory, you need a writable address, such as address of a variable. You specify this with the variables name (check exact syntax from assembler and compiler docs), not by providing an absolute literal address. It would work with literal address, you just can't easily know what it is, so you can't write it.

So writing to memory can and is done all the time in modern OS, it is what most programs spend most of their actual CPU time doing. You just need an address the process is allowed to write to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM