LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-14-2015, 04:19 PM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Rep: Reputation: 55
What's the difference between lea and mov in assembly?


What's the difference between lea and mov in assembly?

Code:
        
lea rax, [sample]
mov rbx, sample
mov rcx, [sample]
...
sample: db 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22
When viewing the registers, it seems the first 2 commands are the same, and then 3rd one puts the value of sample into rcx. I understand there is supposed to be some difference between the first 2 commands though.

GDB output:
Code:
print $rax
$1 = 6291708 (hex: 0x6000fc)

print $rbx
$7 = 6291708 (hex: 0x6000fc)

x/8xb 6291708
0x6000fc:       0xaa    0xbb    0xcc    0xdd    0xee    0xff    0x11    0x22

print $rcx
$8 = 2455024673282112426 (hex: 2211ffeeddccbbaa)

print &sample
$9 = (<data variable, no debug info> *) 0x6000fc

x/8xb 0x6000fc
0x6000fc:       0xaa    0xbb    0xcc    0xdd    0xee    0xff    0x11    0x22

x/8xb &sample
0x6000fc:       0xaa    0xbb    0xcc    0xdd    0xee    0xff    0x11    0x22

x/8xb $eax
0x6000fc:       0xaa    0xbb    0xcc    0xdd    0xee    0xff    0x11    0x22

x/8xb $ebx
0x6000fc:       0xaa    0xbb    0xcc    0xdd    0xee    0xff    0x11    0x22

x/8xb $ecx
0xffffffffddccbbaa:     Cannot access memory at address 0xffffffffddccbbaa
TIA

Last edited by abefroman; 09-14-2015 at 04:30 PM.
 
Old 09-14-2015, 07:18 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
to start you dont say what processor you are using which would help, anyway been a few years since i did any assembly programming but 'lea' stands for 'load efective address' and 'mov' is just short fore move, lea usualy takes an address plus a register and combines them to give the final or 'effective ' address which is where your data will be stored/loaded from, mov just uses a simple address, read up on indexed addressing for a full exxplanation.
 
Old 09-14-2015, 08:30 PM   #3
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Original Poster
Rep: Reputation: 55
Thanks for the reply, the CPU is Intel x86_64

Quote:
Originally Posted by Keith Hedger View Post
lea usualy takes an address plus a register and combines them to give the final or 'effective ' address which is where your data will be stored/loaded from
What do you mean combines them?
Code:
lea rax, [sample]
This is putting the address of sample into rax, no?

My question is how is that different from (besides the fact I'm using rbx instead of rax):
Code:
mov rbx, sample
Which also puts the address of sample into rbx.

Last edited by abefroman; 09-14-2015 at 08:33 PM.
 
Old 09-15-2015, 12:04 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
The Assembler-writers don't agree on this. You might want to get the address of a field or the content of a field. The following syntaxes are used (and perhaps more):

#1 (address)
Code:
LEA    AX,field
MOV    AX,field
MOV    AX,OFFSET field
#2 (content)
Code:
MOV    AX,field
MOV    AX,[field]
It doesn't look very clear; my suggestion is using the very first for the address and the very last for the content.

Last edited by NevemTeve; 09-15-2015 at 09:23 AM.
 
Old 09-15-2015, 03:26 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
nasm, for example, accepts these versions:
Code:
        mov     eax,[4*ecx+0x12] ; content
        lea     eax,[4*ecx+0x12] ; address
gdb-disass says this (set disassembly-flavor att; default):
Code:
   0x080483c0 <+0>:	mov    0x12(,%ecx,4),%eax
   0x080483c7 <+7>:	lea    0x12(,%ecx,4),%eax
or this (set disassembly-flavor intel):
Code:
   0x080483c0 <+0>:	mov    eax,DWORD PTR [ecx*4+0x12]
   0x080483c7 <+7>:	lea    eax,[ecx*4+0x12]
GNU-as expects the "att flavor":
Code:
        mov    0x12(,%ecx,4),%eax
        lea    0x12(,%ecx,4),%eax

Last edited by NevemTeve; 09-15-2015 at 03:57 AM.
 
Old 09-15-2015, 07:57 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,642
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
There are many, many online descriptions of the various flavors of the x86 instruction set, with examples . . .
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Linux Assembly: write and receive assembly data errigour Programming 2 09-22-2012 09:54 AM
among the assembly instructions...what is the LEA(load effective address)! gong.sncuse Programming 3 04-11-2011 12:47 AM
Is there a difference between 64 & 32bit assembly? davidguygc Programming 2 04-25-2007 03:58 PM
Assembly Mov instruction for qword lilzz Linux - Newbie 1 08-31-2006 07:43 PM
Trouble getting .avi .mov .mov files to run redman13 Linux - Newbie 4 11-09-2003 01:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration