* Unable to use a variable in instruction
@ 2007-10-14 23:37 A D
2007-10-15 1:45 ` Frank Kotler
2007-10-15 2:01 ` Robert Plantz
0 siblings, 2 replies; 4+ messages in thread
From: A D @ 2007-10-14 23:37 UTC (permalink / raw)
To: linux-assembly@vger.kernel.org
I'm having strange problem accessing a variable in a gnu assembly language program.
When i write a code:
.section .data
x = 4
mm:
.int 1, 2
.section .text
.globl _start
_start:
nop
movl x, %ecx #can't do this
movl $1, %eax
movl $0, %ebx
int $0x80
I can't move the contents of variable x to %ecx register. This program compiles
and links without giving any error message. But when i run the program I get:
Segmentation fault(core dumped)
Debugging with gdb using core:
Core was generated by `./test'.
Program terminated with signal 11, Segmentation fault.
#0 _start () at test.s:14
10 movl x, %ecx #can't do this
Another odd thing about it is when i modify the program:
.section .data
x = 4
mm:
.int 1, 2
.section .text
.globl _start
_start:
nop
movl $0, %ecx
movl mm(, %ecx, x), %eax
movl $1, %eax
movl $0, %ebx
int $0x80
the code runs without any problem. Even %ecx register contains the correct value.
Is it possible to explain the reason why I can't move directly the value of x in the first
place? Isn't x a variable? If not how come it can store value(in this case 4) and
be used as a size of an array's element? Thanks.
_________________________________________________________________
Express yourself with free Messenger emoticons. Get them today!
http://www.freemessengeremoticons.ca/?icid=EMENCA122
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Unable to use a variable in instruction
2007-10-14 23:37 Unable to use a variable in instruction A D
@ 2007-10-15 1:45 ` Frank Kotler
2007-10-15 2:01 ` Robert Plantz
1 sibling, 0 replies; 4+ messages in thread
From: Frank Kotler @ 2007-10-15 1:45 UTC (permalink / raw)
To: A D; +Cc: linux-assembly@vger.kernel.org
A D wrote:
> I'm having strange problem accessing a variable in a gnu assembly language program.
> When i write a code:
>
> .section .data
> x = 4
> mm:
> .int 1, 2
>
> .section .text
> .globl _start
> _start:
> nop
> movl x, %ecx #can't do this
You're trying to access memory at address 4, not move the immediate
value 4. Try "movl $x, %ecx".
Best,
Frank
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Unable to use a variable in instruction
2007-10-14 23:37 Unable to use a variable in instruction A D
2007-10-15 1:45 ` Frank Kotler
@ 2007-10-15 2:01 ` Robert Plantz
2007-10-15 13:31 ` A D
1 sibling, 1 reply; 4+ messages in thread
From: Robert Plantz @ 2007-10-15 2:01 UTC (permalink / raw)
To: A D; +Cc: linux-assembly@vger.kernel.org
On Sun, 2007-10-14 at 19:37 -0400, A D wrote:
> I'm having strange problem accessing a variable in a gnu assembly language program.
> When i write a code:
>
> .section .data
> x = 4
This is equivalent to writing
.equ x,4
> mm:
> .int 1, 2
>
> .section .text
> .globl _start
> _start:
> nop
So this instruction:
> movl x, %ecx #can't do this
is trying to move the 4 bytes stored at memory location 4 into the ecx
register. As the OS told you, you cannot do that.
The way to allocate an initialized 32-bit int in the .data section is:
x: .long 4
^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: Unable to use a variable in instruction
2007-10-15 2:01 ` Robert Plantz
@ 2007-10-15 13:31 ` A D
0 siblings, 0 replies; 4+ messages in thread
From: A D @ 2007-10-15 13:31 UTC (permalink / raw)
Cc: linux-assembly@vger.kernel.org
>A D wrote:
> I'm having strange problem accessing a variable in a gnu assembly language program.
> When i write a code:
>
> .section .data
> x = 4
> mm:
> .int 1, 2
>
> .section .text
> .globl _start
> _start:
> nop
> movl x, %ecx #can't do this
>
>
>
>
Kotler wrote:
>>You're trying to access memory at address 4, not move the immediate
>>value 4. Try "movl $x, %ecx".
>>
>>
>>
Plantz wrote:
>>So this instruction:
>> movl x, %ecx #can't do this
>>
>>is trying to move the 4 bytes stored at memory location 4 into the ecx
>>register. As the OS told you, you cannot do that.
>>
>>The way to allocate an initialized 32-bit int in the .data section is:
>>
>>x: .long 4
This problem is resolved now. Thanks for clarifying this.
--DC
_________________________________________________________________
Send a smile, make someone laugh, have some fun! Start now!
http://www.freemessengeremoticons.ca/?icid=EMENCA122
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2007-10-15 13:31 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-14 23:37 Unable to use a variable in instruction A D
2007-10-15 1:45 ` Frank Kotler
2007-10-15 2:01 ` Robert Plantz
2007-10-15 13:31 ` A D
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).