This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

signed/unsigned comparison



Hi!, this is the code I'm working. I don't have much experience with assembler, but this does not wok as I supose... what I'm forgetting?

assuming R6=20000(decimal)

            cmp        #16384, R6           
            jge        SV_1            ;THIS JUMPS TO SV_1....OK!
            mov        #1, sv_quadrant
            clr        sv_transport   
            jmp        SV_QSET
                   
SV_1        cmp        #32768, R6      ;THIS JUMPS TO SV_2.... NOT OK!!
            jge        SV_2
            mov        #2, sv_quadrant
            mov        #16384, sv_transport
            jmp        SV_QSET
           
SV_2        cmp        #49152, R6

 

Thank you in advance!!

Tico

  • Hi Tico,

    can we please describe what you want to do? What about coding it in C, compiling it and then you can have a look at the disassembly to see what the resulting assembly instructions are (if you need/want to do it in assembler).

    Rgds
    aBUGSworstnightmare 

  • Hi,

    "cmp" instruction effects all the 4 status bits, V,N,Z & C in the Status register(SR). If you read the msp2xx user guide, CPU register description for Status Register(SR) you will find when these bits are set. N bit is set when the result is negative. Z bit is set when result is 0. C bit is set when there is a carry. The V bit is set for cmp instruction when, Positive - Negative = Negative or Negative - Positive = Positive.Otherwise it is reset to 0.

    "cmp" instruction is basically dst - src. So the first cmp gets translated to R6 - 16384 or 20000 - 16384. Now the result is positive so N bit is 0. Also positive - negative = positive and hence V bit is also not set. Next the "jge" instruction translates to jump is N XOR V is 0. In this case , 0 XOR 0 is 0 and hence you take the jump.

    The second cmp instruction translates to 20000-32768. Here is result is negative so N bit is set. Next positive - negative is giving us negative so even the V bit is set. So the for the "jge" instruction N XOR V is 1 XOR 1 = 0 and hence you again take the jump. Therefore the code seems to be working fine. You may want to use the JN instruction to jump rather than jge. JN will lead to a jump is the negative bit is set.

    Regards,

    Praval

  • Praval,

    Does that mean that for an unsigned jump you have to fill in two instructions:

    JN (to evaluate the negative flag) and JC (to evaluate the carry flag) ?

    This means that there is no direct instruction for unsigned jumps(read jump if result of unsigned operation < or > than given amount),

    and that jumps on the MSP430 are implicitly signed.

     

    I am talking assembly, I don't want to code C for now.

    Thank you,

    Mihai

  • mihaigalos said:
    Does that mean that for an unsigned jump you have to fill in two instructions:

    What do you mean with signed and unsigned jumps? Jumps are based on status register bit values, independently of how you got them. You may even manually set them or get them as results of a function call (rather than a return value).

    mihaigalos said:
    read jump if result of unsigned operation < or > than given amount

    isn't this equal to "!="? There is a JNE/JNZ (jump if not equal/zero) instruction that jumps on zero bit clear.
    Or did you mean two jumps: JG and JL? There's JLO (jump if lower) and if you need JG, use a combination of JHS (jump if higher or same) with JZ/JNZ (to strip the 'S' from the JHS comparison)

    The reason for the missing conditions is that the jump instrucitons include the (signed) offset to the jump destinations in the instruction itself, removing the need for a second parameter. So if you need two jumps, you're not worse off than with systems which implement all variations, but in most cases you're faster and smaller.

**Attention** This is a public forum