Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSP430 CMP operator

Let's say I have the MSP430 assembly segment below:

r15:

439c

Memory map:

4390:   6045 0200 9c43 6400 8844 5044 363a 0000

Code:

448a:  cmp  #0x363a, 0x0(r15)
4490:  jnz  $+0x1c
4492:  Code continues
.
.
.
44ac: Jump to location

The goal is to have the Z flag high. To do this with a cmp, both src and dst must be equal. If I have 363a in the memory location of r15, why is it that the resulting cmp does not trigger the Z flag?

Through experimentation, I found that putting 3a36 in the memory location of r15 did in fact trigger the Z flag, but I don't understand why.

If anyone could bring this to light, I would greatly appreciate it.

If more information is needed, I will gladly provide it.

like image 695
KKK Avatar asked Aug 20 '14 17:08

KKK


1 Answers

The reason is that the MSP430 is a little endian machine.
I.e. the byte at the lower addres 0x439C is interpreted as the Least Significant Byte.
The byte at the higher address 0x439D is interpreted as the Most Significant Byte.
So the 16-bit value in memory actually is interpreted as 0x36 + (0x3A << 8) = 0x3A36.

This explains also why you get Z flag set if you swap both bytes.

Note:
In memory dumps bytes are listed from left to right from lower to higher addresses.
Only for big endian machines (e.g. MC680x0) you can interpret multi byte values as they are listed in the memory dump; for little endian machines (e.g. x86, MSP430) you have to reverse the order of bytes of multi byte values to interpet them correctly.

like image 52
Curd Avatar answered Nov 25 '22 16:11

Curd