Adam Sh
Adam Sh

Reputation: 8577

Assembly - add words with EAX

I have the next code:

Var1: dw 0xFE
Var2: db 0xAB
Var3: db 0xBC

And I want add one to each of the variable.

As I understood, the memory looks like this: FE00ABBC

and for that, The next command should work:

mov eax, 0x010000101
add dword [Var1], eax

But this one is working:

mov eax, 0x01010001
add dword [Var1], eax

Why? Thanks.

Upvotes: 0

Views: 408

Answers (3)

gusbro
gusbro

Reputation: 22585

Your problem lies in that you didn't take account for the endianness of the architecture.

Upvotes: 1

Gunther Piez
Gunther Piez

Reputation: 30449

Your achitecure is low-endian, the lowest significant bits are at the lowest memory address. This is reverse to the usual western writing direction.

The memory looks exactly as you described, but when it ist loaded, seemingly gets reversed:

`FE 00 AB BC`
 low    high    address

 mov eax, 0x1010001

 01 00 01 01
 low    high    byte in eax

So the content of eax is now 01010001 if you write it from left to write.

The problem arises because the order in which you write numerical constants in the assembler source is different from the order in which the bytes actually are stored in memory.

Upvotes: 0

Nathan Fellman
Nathan Fellman

Reputation: 127508

it looks like your memory map is wrong. I would interpret it like this:

 BCAB00FE
 ^      ^
 |      |
MSB    LSB

Which would exactly explain the behavior you see.

Upvotes: 0

Related Questions