Page 1 of 1

mov command with GAS?

Posted: Tue Mar 25, 2008 2:52 am
by junkoi
Hello,

I am writing a simple ASM code with GNU GAS. I am having some problems with mov data. Please could anybody tell me why 2 lines (*) and (**) are not equivalent?? (currently my code works as expected with (**), but not with (*)

(program is in 16bit mode)

---
var:
.long 0xf1234
...
.code16gcc
movl var, %ecx // (*)
movl $0xf1234, %ecx // (**)



Many thanks,
J

Posted: Tue Mar 25, 2008 2:58 am
by JamesM
Hi,

Code: Select all

movl var, %ecx
Actually means "take the address of the label 'var', and move it into %ecx."

Code: Select all

movl $0xf1234, %ecx
Actually means "take the constant 0xf1234, and move it into %ecx."

To make the first equivalent to the second, you really want a dereference, so

Code: Select all

movl 0(var), %ecx
Hope this helps.

James

Posted: Wed Mar 26, 2008 1:29 am
by junkoi
Hi james,

Unfortunately your code is incorrect here, because with GAS

"movl var, %ecx" and "movl (var), %ecx"

do absolutely the same thing, that is copy the value in var into %ecx.

I found the error in my code: that is "var" should be referred to using %cs, not %ds by default. So I fixed my code to smt like this:

movl %cs:var, %ecx

and it works.

Many thanks,
J

Posted: Wed Mar 26, 2008 2:46 am
by JamesM
Ah yes, quite.

I suggest you put your 'var' variable in the .data section to avoid having to change segment selector.