Jump to content
Sign in to follow this  
jsobell

Major Bug In Typecasting Char To Int

Recommended Posts

When a char is cast to an integer, too many bytes are being copied, as shown in this example:

 

    char x=1;
05A8  0E01          MOVLW 0x01
05AA  6E0E          MOVWF main_1_x

    int y;
    y = (int)x;
05AC  6E0F          MOVWF main_1_y
05AE  500F          MOVF main_1_x+D'1', W
05B0  6E10          MOVWF main_1_y+D'1'

 

Location main_1_x+D'1' should obviously be set to zero when casting from and unsigned char, or 0xFF if from a signed char with the topmost bit set.

 

This is quite a serious problem, as the current implementation may appear fine as long as mem+1 is empty (for unsigned chars or signed chars<128), but in a live environment...

I found it when calling a function that takes an integer:

lprintf("%06d", x);

which is the equivalent of

lprintf("%06d", (int)x);

 

On the PIC16, the top byte is always cleared, whether the char is signed or not:

	char x=-1;
0003  30FF 	 MOVLW 0xFF
0004  1283 	 BCF STATUS, RP0
0005  1303 	 BCF STATUS, RP1
0006  008C 	 MOVWF main_1_x

int y;
y=x;
0007  008D 	 MOVWF main_1_y
0008  018E 	 CLRF main_1_y+D'1'

 

IDE version: 6.03

Compiler: BoostC

Compiler version: 6.03

Target device: PIC18F452, PIC16xx

OS: WinXP

 

 

 

Cheers,

Jason

Edited by jsobell

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...