EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

undefined instruction exception caused by lsl instruction

Started by Kosta Kliakhandler June 27, 2008
Hi,

I am developing code for an lpc2138 (currently using an olimex dev
board). Right now I'm using gcc-4.2.4 and newlib-1.16 with -O1 (-OO
makes assembly which is hard to debug and some instructions seem to be
executed incorrectly)

I need to use sprintf to format floating point values for sending via
uart, and the line in C produces an undefined instruction exception.
Investigating further, I found the exception to be caused by the
following code which is newlib generated:

0x1cf0 <_vfprintf_r+76> beq 0x2a70 <_vfprintf_r+3532>
0x1cf4 <_vfprintf_r+80> ldr r0, [sp, #16]
0x1cf8 <_vfprintf_r+84> ldrh r5, [r0, #12]
0x1cfc <_vfprintf_r+88> lsl r3, r5, #16
0x1d00 <_vfprintf_r+92> lsr r2, r3, #16
0x1d04 <_vfprintf_r+96> tst r2, #8 ; 0x8|
0x1d08 <_vfprintf_r+100> beq 0x2a7c <_vfprintf_r+3544>

When stepping through the code with gdb I get the exception right
after stepping this line:
0x1cf8 <_vfprintf_r+84> ldrh r5, [r0, #12]

Thinking about it some more leads me to believe that the exception is
really caused by one of the following lines:

0x1cfc <_vfprintf_r+88> lsl r3, r5, #16
0x1d00 <_vfprintf_r+92> lsr r2, r3, #16

Because of how instructions are fetched, then decoded, etc... Looking
up the instructions and not finding lsl or lsl used as the actual
instruction further leads me to believe it is one of those instructions.

Assuming I correctly identified the cause, what can I do about it? Or
what is the real cause?

Thanks a lot in advance,
Kosta.

An Engineer's Guide to the LPC2100 Series

Alright, after further investigation I have discovered that there are
lsl and lsr instructions, but only in thumb mode.

Right before the exception the proccessor mode is:
1100000000000000000000000011111 = 0x6000001f

which means the processor is in ARM mode. I also tested this code with
gcc-4.1.2 and with different -O settings - all the same. I doubted it
would help, since the needed code is precompiled afaik, but...

are the contents of registers prior to the exception relevant? (and
should I post it?)

Have I found a newlib bug?

The 2024 Embedded Online Conference