Am I trying to do this?
Yes, you can change the .text binary file.
Please note that this change will only affect the current execution; after run your change will "evaporate" (if you want to permanently fix the binary, this is possible, but the procedure is different).
If so, am I doing something wrong?
Most likely. You did not tell us that you are trying to change the instructions.
If so, what am I doing wrong and how can I fix it?
Using (gdb) disas/r will show you the actual raw command bytes and will most likely simplify what you did wrong. When I use it, I see the following:
0x080483ed <+9>: c7 44 24 1c d0 84 04 08 movl $0x80484d0,0x1c(%esp)
That is, the address (which you apparently wanted to rewrite) for the instruction above [1] does not start with &instruction+1 , it starts with &instruction+4 . Also, you shouldn't discard bytes when you ask GDB to write a word (I assume you need a new address 0x0804785b , not 0x5b870408 ):
(gdb) set *(0x080483ed+4)=0x01020304 (gdb) disas Dump of assembler code for function main: 0x080483e4 <+0>: push %ebp 0x080483e5 <+1>: mov %esp,%ebp 0x080483e7 <+3>: and $0xfffffff0,%esp 0x080483ea <+6>: sub $0x20,%esp => 0x080483ed <+9>: movl $0x1020304,0x1c(%esp) 0x080483f5 <+17>: mov 0x1c(%esp),%eax 0x080483f9 <+21>: mov %eax,(%esp) 0x080483fc <+24>: call 0x8048318 < puts@plt > 0x08048401 <+29>: mov $0x0,%eax 0x08048406 <+34>: leave 0x08048407 <+35>: ret
[1] It is very likely that your instruction:
0x080487e0 <+17>: movl $0x8048640,0x20(%esp)
has the same encoding as my instruction:
0x080483ed <+9>: movl $0x80484d0,0x1c(%esp)
since they are βthe sameβ and have the same length of 8 bytes, but, as FrankH noted, there may be a different encoding of the same instruction. In any case, disas/r will show you everything you need to know.