Question

I have issue with writing into allocated memory. Here's the code.

total: .long 1       # my memory

movl total, %eax     # I believe I'm copying address of total into eax
movl $53, %esi       # set some value in esi
movl (%esi), %eax    # and put this value into total 

Why its not working? It gives : "Naruszenie ochrony pamięci" (core dumped) this is in Polish and means something like memory access violation.

Was it helpful?

Solution

First, in AT&T syntax, to store the address of a label into a register, instead of

movl total, %eax

which disassembles in Intel syntax as:

8b042500000000   mov eax, [0x0]

you need to use:

movl $total, %eax

which disassembles as something like this:

b800000000       mov eax, 0x0

In this sense when using AT&T syntax one has to be very careful to avoid bugs.

Then, if you fix the above problem, you still need to fix the parentheses of the last instruction, as mentioned by Seva and harold. It should be like this:

movl %esi, (%eax)

The code causes a segmentation fault because it accesses memory regions you don't have access to. In protected mode you can't access whatever memory address you wish, you need to have read/write/execute rights for that memory region.

OTHER TIPS

Replace with

movl %esi, (%eax)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top