# Comparison always true in Target code

**URL:** <https://forum.sierrawireless.com/t/comparison-always-true-in-target-code/1966>\
**Category:** Development tools\
**Created:** [March 5, 2008, 8:45am UTC](https://forum.sierrawireless.com/t/comparison-always-true-in-target-code/1966 "2008-03-05T08:45:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyssy](https://avatars.discourse-cdn.com/v4/letter/l/ed655f/32.png) [@lyssy](https://forum.sierrawireless.com/u/lyssy)\
**Post date:** [March 5, 2008, 8:45am UTC](https://forum.sierrawireless.com/t/comparison-always-true-in-target-code/1966/1 "2008-03-05T08:45:44Z")

</div>

Hello to All!

I’m new to Open AT world (but not to embedded world 🙂 ).

I’m facing some problems developing my first Open AT application. It is aimed on communicating with external uC (LPC2000 family).

Sample code - small routine having to UUencode portion of data:

```auto
u32 lpcb_LoadUUData(ascii *out, ascii *in, u8 cnt)
{
    u8 ptr;
    u32 znaki;
    u32 sum;
    u8 z;
	
	if (!cnt || (cnt > 45))
		return 0;

	*out++ = cnt+32;
	
	sum = 0;
	
	do {
		
		for (ptr=0; ptr<3; ptr++) {
			znaki <<= 8;
			
			if (cnt) {
				z = *in++;
				znaki |= z;
				sum += z;
				cnt--;
			}
		}

		for (ptr=0; ptr<4; ptr++) {	
			z = ((znaki >> (18-6*ptr)) & 0x3F) + 32;
            if (z == 32)
                z = 0x60;
			*out++ = z;
		}
	
	} while (cnt);

	*out = 0;	
	return sum;

}
```

The problem is that, while it works perfectly when compiled in RTE mode, when compiled and run Target mode, result of (z == 32) comparison is ALWAYS true, no matter what value ‘z’ holds.  
I’ve checked this by making some ‘debug output’ through serial port - ‘z’ printed right before comparison shows proper values and printed right after ‘if (…) …’ block it always shows ‘grave accent’ (0x60) character 😮

Whole LoadUUData function is called from timer handler with ‘in’ pointing to globally declared array and ‘out’ pointing to array declared within timer handler.

I suspect some kind of bug in GCC compiler, although I work with GCC for ARM7 daily and didn’t have such problems yet so it is hard to believe for me 😉

Any hints why my code is ‘failing’? Thanks in advance for answers!

PS. Open AT 4.24, Open AT Firmware 663, GCC 4.0.1.0, Eclipse 3.2.2, hardware: Q2686.

---

<div class="post-metadata">

**Author:** ![tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tobias](https://forum.sierrawireless.com/u/tobias)\
**Post date:** [March 5, 2008, 9:00am UTC](https://forum.sierrawireless.com/t/comparison-always-true-in-target-code/1966/2 "2008-03-05T09:00:56Z")

</div>

Try adding some {} for the if section… The version of GCC used might very well have some weird bug…

---

<div class="post-metadata">

**Author:** ![lyssy](https://avatars.discourse-cdn.com/v4/letter/l/ed655f/32.png) [@lyssy](https://forum.sierrawireless.com/u/lyssy)\
**Post date:** [March 5, 2008, 9:11am UTC](https://forum.sierrawireless.com/t/comparison-always-true-in-target-code/1966/3 "2008-03-05T09:11:33Z")

</div>

> [@tobias](#):
>
> Try adding some {} for the if section…

Didn’t help… ☹  
Furthermore, adding some code before mentioned ‘if’ section, changing name of ‘z’ variable, changing function input parameters and even changing type(!) of ‘z’ to u32 didn’t help either. Strange…

---

<div class="post-metadata">

**Author:** ![tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tobias](https://forum.sierrawireless.com/u/tobias)\
**Post date:** [March 5, 2008, 11:46am UTC](https://forum.sierrawireless.com/t/comparison-always-true-in-target-code/1966/4 "2008-03-05T11:46:25Z")

</div>

Is the code you posted exactly copied from your source?  
Would you mind posting the exact source of that function, including your debug output calls?

---

<div class="post-metadata">

**Author:** ![lyssy](https://avatars.discourse-cdn.com/v4/letter/l/ed655f/32.png) [@lyssy](https://forum.sierrawireless.com/u/lyssy)\
**Post date:** [March 5, 2008, 12:14pm UTC](https://forum.sierrawireless.com/t/comparison-always-true-in-target-code/1966/5 "2008-03-05T12:14:57Z")

</div>

Yes, the code was copy-pasted from Eclipse’s editor 🙂

As for debug output - if you think of mentioned output through serial port, I don’t have it right now. It was something like sprintf(txt, “z=%x”, z) followed by adl\_fcmSendData(trace\_port\_handle, …) etc. As I said, it only showed that ‘z’ was, say, 0x55 before and 0x60 after ‘if’ block.  
Furthermore, apart from this ‘debug code’ (which is removed now), I still have view on communication between Q2686 and external ARM uC - and it shows clearly that - no matter what is passed in input buffer - transmitted UUencoded lines contain “M``````````(etc)”, not expected “MZH`@L.$(```J?P12XP8(etc)”.  
Ah, one more thing: returned checksum value is correct and it reflects passed input data, so function really gets this data.

---

<div class="post-metadata">

**Author:** ![tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tobias](https://forum.sierrawireless.com/u/tobias)\
**Post date:** [March 5, 2008, 2:37pm UTC](https://forum.sierrawireless.com/t/comparison-always-true-in-target-code/1966/6 "2008-03-05T14:37:49Z")

</div>

If that really is the exact source, copied directly from Eclipse as-is, you should probably check your code better or work around it in case it’s really a compiler problem.

Other notes:  
znaki should be initialized before use, even if it shouldn’t change anything for you…  
Doublecheck and triplecheck that you don’t have any ; between if (z == 32) and z = 0x60.  
Or simply rewrite that part of the code entirely. Go the naive approach without the for-loop if needed…
