x86: using memory / swapping values?

bclayman

I'm just getting into x86 for a systems class and have a couple of questions:

1) Say I reference a piece of memory with:

(%eax)

The parentheses indicate I'm accessing memory and %eax indicates I want to access that particular address in memory. So if register %eax stores 0x104, (%eax) will return whatever is stored at memory location 0x104. Is this correct?

2) Say I want to swap the values stored at (%eax) and (%ebx). Here's my attempt:

//store values in registers
movl (%eax) eax
movl (%ebx) ebx

//store values in opposite registers from where they came originally
movl ebx (%eax)
movl eax (%ebx)

Does this look right?

Thanks for the help, bclayman

David Hoelzer

That cannot be correct.

EAX and EBX start out containing memory locations. You then dereference the memory, moving the actual values into the registers of the same name:

 MOVL (%EAX), EAX
 MOVL (%EBX), EBX

EAX and EBX no longer contain memory addresses; they now contain the contents from those addresses. When you next do this:

 MOVL EBX, (%EAX)
 MOVL EAX, (%EBX)

you are storing the values from those memory locations into whatever memory location happens to be equal to those values which is almost certainly not what you want and almost guaranteed not to exchange the values.

The only way this would work if the memory locations were storing the value of the memory location in themselves, which wouldn't make a lot of sense.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Swapping values using registers

From Dev

Allocate large memory using x64 system with x86 application in c++

From Dev

swapping values in contiguous indices using return method

From Dev

Writing under the cursor in x86 assembly using video mapped memory

From Dev

Manual Null-Termination on Unix using Intel x86 Assembly (address vs value in memory operands)?

From Dev

Confusion in Memory segmentation in x86

From Dev

x86 and MIPS memory addressing

From Dev

x86 Procedure Call Memory Allocation

From Dev

x86 memory access segmentation fault

From Dev

Java memory model - volatile and x86

From Dev

x86 Procedure Call Memory Allocation

From Dev

Memory Swapping and Virtual Memory on iOS

From Dev

Swapping values of two variables without using a third one- Array

From Dev

Swapping values between two columns using data.table

From Dev

Swapping values of two variables without using a third one- Array

From Java

x86 Assembly; overwriting .bss values?

From Dev

How to read stale values on x86

From Dev

x86: register operation as memory content and memory address?

From Dev

Swap x and y axis without manually swapping values

From Dev

Swapping Values with XOR

From Dev

Swapping column values in Oracle

From Dev

swapping values of database table

From Dev

Error while swapping values

From Dev

Using BIOS interrupts in x86

From Dev

using atof function in x86 NASM

From Dev

Semaphore implementation using x86 assembly

From Dev

Using x86 CUPS filter on Raspbian

From Dev

Permanent swapping with lots of free memory

From Dev

restrict memory usage to prevent swapping