2

I am using a TI MPS430G2553 launchpad and Code Composer Studio v6

I am trying to compare to numbers in an array. I am looping throught the array and at each number I compare that number to the next number in the array. If the next number is less than the current number, they switch positions. Otherwise, nothing happens

I am comparing them using this command:

cmp 0(array),1(array)
jl SWITCH

The problem is, some of the numbers in the array are negative and they are being treated as their twos complement, for example, when compare 55 and -9, it does not switch them because it is treating -9 as 246 (when i assign it to a register, and use debug mode, the register contains the number 246)

How do i solve this? everything I look up says the solution is to use the jl command, but I'm already doing that.

my memory window shows for this array 10 17 55 -9 22 36 -7 37 8 -77 8

Here is the instruction set my professor gave me

Matt
  • 2,232
  • 8
  • 35
  • 64
  • 2
    Subtract (or add) 128 to every number. The byte numbers are -128, ... -1, 0, ... 127 and adding 128 "modulo 256" you'll get the range 0, ... 255, Or maybe there is a compare-signed or jump-on-minus (jl=less being unsigned). – Joop Eggen Oct 22 '16 at 02:04
  • When I do this, it still treats the -9 as 246. I add 128 to 246 and get 374, which is still greater than 55+128 – Matt Oct 22 '16 at 02:41
  • You need to add modulo 256 (i.e. truncating to 8 bits). The low 8 bits of `128 + 246` gives you 118, which is below (128+55)=183. – Peter Cordes Oct 22 '16 at 03:52
  • @PeterCordes: How do I add modulo 256? its not in the instruction set. – Matt Oct 23 '16 at 01:00
  • Truncating to 8 bit = modulo 256. – Peter Cordes Oct 23 '16 at 01:02

2 Answers2

2

You're doing something wrong. jl branches on the signed less-than condition, in MSP430 assembly language, according to this insn set guide I googled up:
JL Label Jump to Label if (N .XOR. V) = 1 (where N and V are the Negative and signed-Overflow status bits, so it's identical to x86's JL instruction. See Understanding Carry vs. Overflow conditions/flags for signed vs. unsigned.

MSP430 has a JLO to branch on the unsigned lower-than condition (if Carry-bit is reset), and you're not using that (which is correct since you want to branch on a signed condition). It also has JHS (Higher or Same, a synonym for JC), but I don't see an equivalent for x86's JA (above: C==0 && Z==0). Anyway, you don't want unsigned branches anyway.


I have a guess at what you're doing wrong:

You say that you see 246 in a register. So your numbers are definitely only 8-bit. You're using CMP, not CMP.B, so I think that means you're comparing word-size array elements (unless your assembler magically infers the .B from something about array being used as an operand). The insn set ref lists it as CMP(.B) src,dst.

Did you zero-extend your numbers to 16-bit instead of sign-extending? That would explain why CMP isn't setting flags the way you want.

Also, you're only using 0 vs. 1 for array offsets, but that's only one byte and CMP (not CMP.B) uses larger elements than that, so maybe you're doing a 16-bit access that covers two array elements?

(Some of this may be wrong, since I've never used MSP430 before, but it looks like x86 / ARM / M68k / other typical assembly languages.)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • I get the same results whether I use cmp, cmp.b or cmp.w. Switching JL to JLO doesn't work either. How do I know whether I'm zero-extending or sign-extending? – Matt Oct 23 '16 at 00:51
  • @MattD: look at memory contents with a debugger. – Peter Cordes Oct 23 '16 at 00:52
  • That's what I've been doing this whole time. Is something specific I should look for? – Matt Oct 23 '16 at 00:58
  • @MattD: yes, whether it's `0 55 0 -9 ...` or whether it's `55 -9 ...`. It's not clear from the question what's actually in memory. `cmp.b 0(array), 1(array)` should work if they're packed without any zero-padding, unless there's something tricky about MSP430 that's different from what I expect. You only ever posted anything in your question about what you saw in registers, not memory. – Peter Cordes Oct 23 '16 at 01:05
0

I changed cmp 0(ptr),1(ptr) to cmp.b 0(ptr),1(ptr)and that worked

Matt
  • 2,232
  • 8
  • 35
  • 64