Reputation: 297
In a simple/trivial GNU Assembly (GAS/as) program (see interaction and source, after explanation), opened for debug with GDB.
Having trouble to set
the value of a variable through its symbol (name).
(Likely reason is, that text's example is quite outdated and there have been some changes in GDB since [e.g. stricter requirements]).
I launch it with gdb
, establish a breakpoint, print normally the variable, then stepping, then printing again to see how the program itself changed it.
And then I try to change the value through GDB.
First, GDB points out I need to declare variable's type:
(gdb) set constant = 666
'constant' has unknown type; cast it to its declared type
"No problem", I go for (int)
(as with the print
command).
But when I put the (int)
declarator I get:
(gdb) set (int) constant = 666
Left operand of assignment is not an lvalue.
I looked for a solution through other SO posts and elsewhere (manual), tried different kinds of delimiters (curly braces, brackets, quotes, etc) for the lefthand side of the equal sign, suspecting, that could have to do just with closing the expression to remove command string ambiguity but I still did not achieve the thing.
(I found a workaround through this. But is NOT what I want. I want to change the variable through its name):
(gdb) print &constant
$3 = (<data variable, no debug info> *) 0x804a000
(gdb) set *0x804a000 = 666
(gdb) print (int) constant
$4 = 666
All my interaction with gdb
:
(peter)-[~/ri/rfe/p88]
$ gdb -q moving_data_between_registers_and_memory
Reading symbols from moving_data_between_registers_and_memory...
(No debugging symbols found in moving_data_between_registers_and_memory)
(gdb) br _start
Breakpoint 1 at 0x8049000
(gdb) r
Starting program: /home/peter/Documents/.../moving_data_between_registers_and_memory
zshenv
Breakpoint 1, 0x08049000 in _start ()
(gdb) print constant
'constant' has unknown type; cast it to its declared type
(gdb) print (int) constant
$1 = 10
(gdb) si
0x08049001 in mov_immediate_data_between_registers_and_memory
()
(gdb) si
0x08049006 in mov_immediate_data_between_registers_and_memory
()
(gdb) si
0x0804900b in exit ()
(gdb) print (int) constant
$2 = 777
(gdb) set constant = 666
'constant' has unknown type; cast it to its declared type
(gdb) set (int) constant = 666
Left operand of assignment is not an lvalue.
(gdb) print &constant
$3 = (<data variable, no debug info> *) 0x804a000
(gdb) set *0x804a000 = 666
(gdb) print (int) constant
$4 = 666
(gdb)
(source code raw with cat
)
$ cat moving_data_between_registers_and_memory.s
#moving_data_between_registers_and_memory: mov data between regs and mem
.section .data
#.section .bss
constant:
.int 10
# .int 0
.section .text
.globl _start
_start:
nop #used for debugging purposes
mov_immediate_data_between_registers_and_memory:
movl $777, %eax #mov immediate value 777 to eax
movl %eax, constant #mov eax value into constant mem value
exit:
movl $1, %eax #sys_exit system call
movl $0, %ebx #exit code 0 successful execution
int $0x80
Upvotes: 3
Views: 213