Reputation: 15099
Let's pretend that the variable a
is placed in the address 0xDEADBEEF
and it's value is 42
.
How can I compare the value of a
(42
) with another int?
Currently I have (intel syntax):
mov rax, 0xDEADBEEF;
mov rdi, 1;
cmp [rax], rdi;
Is that correct?
Upvotes: 1
Views: 3548
Reputation: 15090
Little endian processors (like most modern desktop ones) arrange values in memory backwards. For example if the value at 0xDEADBEEF is 42
, aka 0x0000002A
, then it will be stored as
2A 00 00 00
You can force how many bytes the cmp [rax], rdi;
command compares by prepending byte/word/double. For example in nasm
cmp BYTE [rax], rdi;
would compare only the first byte pointed at by the pointer rax
.
Check your assembler's documentation for exact syntax. Also check how many bytes your assembler compares with cmp
command by default.
Edit: Disregard everything I wrote above.
Since you your question is tagged x86-64
I assume your program is 64 bit.
rdi
is 8 bytes long. Your code does everything right IF the value at 0xDEADBEEF
is 8 bytes long as well:
2A 00 00 00 00 00 00 00
Otherwise if only the first 4 or 2 or bytes set to correct value, your program may or may not work correctly. For example if the value at 0xDEADBEEF
is 4 bytes long, then it will look like this
2A 00 00 00 ?? ?? ?? ??
The rest of the memooryy will contain some other random information. Your program will have the worst kind of bug - the one that occurs randomly. The cmp instruction will still compare 8 bytes, as rdi
is 8 bytes long.
And to answer your comment to Gunner's question (as I don't have privilege to comment):
mov rax, [0xDEADBEEF]
cmp rax, 1
IS the same as
mov rax, 0xDEADBEEF
cmp [rax], 1
Upvotes: 2
Reputation: 9377
Which assembler? For gas, assuming .intel_syntax noprefix
:
cmp QWORD PTR ds:0xdeadbeef, 1
Note that the 0xdeadbeef
will be sign extended. You may prefer to be explicit and include the ff
s to make that clear.
Usually you won't need to specify ds:
because you will be referring to a label and the assembler will handle things. That is:
cmp DWORD PTR [foo], 1
cmp DWORD PTR foo, 1 # same thing
Upvotes: 0
Reputation: 5884
You are mov
ing the value 0xDEADBEEF
into rax
, instead you need to L oad the E ffective A ddress into rax
Not sure of your Assembler, but in NASM:
lea rax, [SomeAddress]
Or:
mov rax, [SomeAddress]
Upvotes: -1