luken8r 0 Newbie Poster

Im modifing an assembly module to add a branch to a new C function but I cant seem to get it working right. My stack is crashing and I cant track it down because, unfortunately, I dont have an emulator

The original asm file is a 16 bit signed division library that just jumps to a 32 bit library to do the divide. Im trying to add a branch to a C function in here but it keeps crashing on me. Heres the original:

.state16

    .global I$DIV
    .global I_DIV

dvs	.set	r2		; WORK COPY OF THE DIVISOR (SHIFTED)
quo	.set	r3		; WORK COPY OF THE QUOTIENT
negs	.set	r4		; SAVED COPY OF THE SIGNS

I$DIV:	
    PUSH {lr}

    NOP
    BX  pc		; Change to 32-bit state
    NOP
    .align
    .state32
    BL  I_MOD
    ADD lr, pc, #0x1
    BX  lr
    .state16
    POP {pc}
	
    .end

This is just the standard library file that comes with my TI compiler. What I want to do is add a compare and if that compare is == 0, branch to a C file.

I wrote a C function to do what I want so I can see what the listing file looks like

void brtodiv(ui16_t foo){

	if (foo == 0){
	  gohere(); // eventual target function
 	}
 	else{
 		somefunct(); // some other place
 	}
	test(foo);

}

void gohere(void){

  	volatile ui16_t addr = 272;
	volatile ui16_t foo = 0xffed;

  	Wrt2EEP(addr,foo);

}

Nice and easy

The resulting list file looks like this

198                    ;*****************************************************************************
     199                    ;* FUNCTION NAME: $brtodiv                                                   *
     200                    ;*                                                                           *
     201                    ;*   Regs Modified     : A1,A2,A3,A4,V9,SP,LR,SR                             *
     202                    ;*   Regs Used         : A1,A2,A3,A4,V9,SP,LR,SR                             *
     203                    ;*   Local Frame Size  : 0 Args + 0 Auto + 4 Save = 4 byte                   *
     204                    ;*****************************************************************************
     205 0000002a           $brtodiv:
     206 0000002a B500              PUSH      {LR}
     207 0000002c 2800              CMP       A1, #0                ; |1155| 
     208 0000002e D101              BNE       L1                    ; |1155| 
     209                            ; |1155| 
     210 00000030 F7FF              BL        $gohere            ; |1156| 
         00000032 FFE6     
     211                            ; |1156| 
     212 00000034           L1:    
     213 00000034 F7FF              BL        $test                 ; |1161| 
         00000036 FFF1

Push the LR to stack, do the comparison, if not equal do some stub function, if equal, branch to my target routine

So I tried this in my asm file but no dice.

I$DIV:	
    PUSH {lr}
    CMP R1, #0
    BNE L1 
	
   	BL $gohere
L1: 
    <original code>

    .end

So as a test, I removed the call to Wrt2EEP(addr,foo); in the gohere() routine and it didnt crash on me. This function is going to write some code foo to an address in EEPROM. Anyway, because this didnt crash, its obviously killing the stack. What am I not passing to the stack from my assembly file?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.