EmbeddedRelated.com
Forums

Assembly language - comparing two unknown, signed 16-bit integers

Started by David Feldman August 21, 2012
I am translating a few dozen lines of c code to msp430 assembler (so I am doing this manually rather than trying to integrate a small amount of inline C to what is otherwise an assembly language program).

I have several places where two 16-bit signed integers are being compared arithmetically. Either number may be negative, zero or positive, but is not known prior to assembly time (for example, either/both may be positive, zero or negative). I need to make a conditional jump based on < or > (or <= or >=) relationship between the source and destination values (which are both in registers.) I've not been able to work out a consistent way to code this.

I need help working out the conditional branch instruction(s) to use; particularly whether I need to pre-test either source or destination register so that appropriate use of cmp instruction can be made. If it would help to furnish an example, I'd be glad to, but given that the two register values are unknown, I'm not sure how best to express this.

Thanks for any assistance, example, starting point or guidance.

Beginning Microcontrollers with the MSP430

Look at the 'jmp' instructions. Some will work with unsigned and others will work with signed.

Emmett Redd, Ph.D., Professor mailto:E...@MissouriState.Edu
Physics, Astronomy, and Materials Science Office: 417-836-5221
Missouri State University Dept: 417-838-5131
901 S NATIONAL AVENUE FAX: 417-836-6226
SPRINGFIELD, MO 65897 USA Lab: 417-836-3770

It is clear that thought is not free if the profession of certain opinions make it impossible to earn a living. -- Bertrand Russell

> -----Original Message-----
> From: m... [mailto:m...] On Behalf
> Of David Feldman
> Sent: Tuesday, August 21, 2012 2:28 PM
> To: m...
> Subject: [msp430] Assembly language - comparing two unknown, signed 16-
> bit integers
> I am translating a few dozen lines of c code to msp430 assembler (so I
> am doing this manually rather than trying to integrate a small amount
> of inline C to what is otherwise an assembly language program).
>
> I have several places where two 16-bit signed integers are being
> compared arithmetically. Either number may be negative, zero or
> positive, but is not known prior to assembly time (for example,
> either/both may be positive, zero or negative). I need to make a
> conditional jump based on < or > (or <= or >=) relationship between the
> source and destination values (which are both in registers.) I've not
> been able to work out a consistent way to code this.
>
> I need help working out the conditional branch instruction(s) to use;
> particularly whether I need to pre-test either source or destination
> register so that appropriate use of cmp instruction can be made. If it
> would help to furnish an example, I'd be glad to, but given that the
> two register values are unknown, I'm not sure how best to express this.
>
> Thanks for any assistance, example, starting point or guidance.
>
>
On Tue, 21 Aug 2012 12:27:47 -0700 (PDT), you wrote:

> I am translating a few dozen lines of c code to msp430
> assembler (so I am doing this manually rather than trying to
> integrate a small amount of inline C to what is otherwise an
> assembly language program).
>
> I have several places where two 16-bit signed integers are
> being compared arithmetically. Either number may be
> negative, zero or positive, but is not known prior to
> assembly time (for example, either/both may be positive,
> zero or negative). I need to make a conditional jump based
> on < or > (or <= or >=) relationship between the source and
> destination values (which are both in registers.) I've not
> been able to work out a consistent way to code this.
>
> I need help working out the conditional branch
> instruction(s) to use; particularly whether I need to
> pre-test either source or destination register so that
> appropriate use of cmp instruction can be made. If it would
> help to furnish an example, I'd be glad to, but given that
> the two register values are unknown, I'm not sure how best
> to express this.
>
> Thanks for any assistance, example, starting point or guidance.

Um, is there a problem with JLT and JGE? You need to
understand how the overflow bit works, I suppose. The AV bit
is usually the XOR of the carry outputs of the two most
significant adder stages.

Jon
On Tue, 21 Aug 2012 13:35:08 -0700, I wrote:

>The AV bit
>is usually the XOR of the carry outputs of the two most
>significant adder stages.

Sorry, meant V bit. AV is what it is called in the ADSP-21xx
series.

Jon
i HAVE A RATHER LONG WINDED EXPLANATION OF THIS in a chapter of a book
i started to write a few years ago. If you want a copy of that let me
know. I have also posted it to the group files site.

Al

On 22/08/2012 4:57 AM, David Feldman wrote:
> I am translating a few dozen lines of c code to msp430 assembler (so I am doing this manually rather than trying to integrate a small amount of inline C to what is otherwise an assembly language program).
>
> I have several places where two 16-bit signed integers are being compared arithmetically. Either number may be negative, zero or positive, but is not known prior to assembly time (for example, either/both may be positive, zero or negative). I need to make a conditional jump based on < or > (or <= or >=) relationship between the source and destination values (which are both in registers.) I've not been able to work out a consistent way to code this.
>
> I need help working out the conditional branch instruction(s) to use; particularly whether I need to pre-test either source or destination register so that appropriate use of cmp instruction can be made. If it would help to furnish an example, I'd be glad to, but given that the two register values are unknown, I'm not sure how best to express this.
>
> Thanks for any assistance, example, starting point or guidance.
>
>
When comparing signed arguments: CMP , you can use:

(a) JL to jump to a if is Less then
(b) JGE to jump to a if is Greater or Equal to

(c) JEQ to jump to a if is Equal to
(d) JNE to jump to a if is Not Equal to

JC, JNC, and JN are more or less useless in this situation.
--- In m..., David Feldman wrote:
> I am translating a few dozen lines of c code to msp430 assembler (so I
am doing this manually rather than trying to integrate a small amount of
inline C to what is otherwise an assembly language program).
>
> I have several places where two 16-bit signed integers are being
compared arithmetically. Either number may be negative, zero or
positive, but is not known prior to assembly time (for example,
either/both may be positive, zero or negative). I need to make a
conditional jump based on < or > (or <= or >=) relationship between the
source and destination values (which are both in registers.) I've not
been able to work out a consistent way to code this.
>
> I need help working out the conditional branch instruction(s) to use;
particularly whether I need to pre-test either source or destination
register so that appropriate use of cmp instruction can be made. If it
would help to furnish an example, I'd be glad to, but given that the two
register values are unknown, I'm not sure how best to express this.
>
> Thanks for any assistance, example, starting point or guidance.
>