Jump to content

Array Address Math Broken In Subroutine


Recommended Posts

Bug description:

When array address math used inside a function using a function parameter, the compiler generates code that uses an uninitialized variable.

 

This small program:

 

#include <system.h>

void Junky(char *s)
{
}

void FunTime(char *s)
{
   Junky(&s[1]);  // Generates bad code
}

void main()
{
   char s[10];
   
   s[0] = '-';
   FunTime(&s[1]);  // Generates good code


   while(1);
}

 

produces the following code that adds the uninitialized CompTempVar53 to the address of s instead of 1 in the function FunTime at address 8. Here is the output of BoostC.

 

;/////////////////////////////////////////////////////////////////////////////////
;// Code Generator: BoostC Compiler - http://www.sourceboost.com
;// Version       : 6.35
;// License Type  : Full License
;// Limitations   : PIC12,PIC16 max code size:Unlimited, max RAM banks:Unlimited, Non commercial use only
;/////////////////////////////////////////////////////////////////////////////////

ORG 0x00000003
0003  281A 	 GOTO	_startup
ORG 0x00000006
0006        Junky_00000
; { Junky; function begin
0006  0008 	 RETURN
; } Junky function end

ORG 0x00000007
0007        FunTime_00000
; { FunTime; function begin
0007  082A 	 MOVF FunTime_00000_arg_s, W
0008  072E 	 ADDWF CompTempVar53, W
0009  00AC 	 MOVWF Junky_00000_arg_s
000A  082B 	 MOVF FunTime_00000_arg_s+D'1', W
000B  00AD 	 MOVWF Junky_00000_arg_s+D'1'
000C  1803 	 BTFSC STATUS,C
000D  0AAD 	 INCF Junky_00000_arg_s+D'1', F
000E  2006 	 CALL Junky_00000
000F  0008 	 RETURN
; } FunTime function end

ORG 0x00000010
0010        main
; { main; function begin
0010  302D 	 MOVLW 0x2D
0011  1283 	 BCF STATUS, RP0
0012  1303 	 BCF STATUS, RP1
0013  00A0 	 MOVWF main_1_s
0014  3000 	 MOVLW HIGH(main_1_s+D'1')
0015  00AB 	 MOVWF FunTime_00000_arg_s+D'1'
0016  3021 	 MOVLW LOW(main_1_s+D'1')
0017  00AA 	 MOVWF FunTime_00000_arg_s
0018  2007 	 CALL FunTime_00000
0019        label268436513
0019  2819 	 GOTO	label268436513
; } main function end

ORG 0x0000001A
001A        _startup
001A  118A 	 BCF PCLATH,3
001B  120A 	 BCF PCLATH,4
001C  2810 	 GOTO	main

 

Steps to reproduce:

Using a 16F873 target for this case but also fails for 16F876A. Just need to do the same in any subroutine but must use a call paramter of the subroutine to get it to fail.

 

Expected behaviour:

Should use s+1 insead of s+random_value in the FunTime subroutine.

 

Is the problem 100% reproduceable:

Yes

 

IDE version: 6.35

Compiler: BoostC

Compiler version: 6.35

Target device: PIC16F873

OS: WinXp

 

Comments:

I was able to work around the problem by changing:

 

Junky(&s[1]);

 

to

 

Junky(s+1);

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...
×
×
  • Create New...