Something is crashing in my asm and I cant track it down

Im modifying an assembly module to add a branch to a new C function but I cant seem to get it working right.

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:

======asm file===========

.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. As a quick test, this works:

The C function at this point is just a stub

========C File=============

void foo(void){ ; }

=========================

and the modification to the asm module

========asm file===========:

I$DIV:

PUSH {lr}

CMP R1, #0 BEQ asm_label

asm_label: BL $foo POP {pc}

======================

Ok, at this point it doesnt crash. It doesnt do anything, but it doesnt crash either. I now add my code to the C function which works being called with other C and asm functions and attempt to account for that in my asm code:

========asm file===========:

I$DIV: I$MOD: PUSH {lr} CMP R1, #0 BEQ asm_label ; CHECK FOR DIVISION BY ZERO

NOP BX pc ; Change to 32-bit state NOP

.align .state32

BL I_MOD ADD lr, pc, #0x1 BX lr

.state16

POP {pc}

asm_label: PUSH {LR} BL $foo POP {pc} BX LR

.end

==========================

This crashes with the only addition being the pushing of LR to the stack. What else do I need to save/restore? The listing file for the C function looks something like this:

======list file=============

58 00000000 _foo: 59 00000000 E92D4000 STMFD sp!, {lr} 60 00000004 E28FE001 ADD lr, pc, #1 61 00000008 E12FFF1E BX lr 62 0000000c .state16 63 0000000c F7FF' BL $foo 0000000e FFF8 64 00000010 4778 BX pc 65 00000012 46C0 NOP 66 00000014 .state32 67 00000014 E8BD8000 LDMFD sp!, {pc} 68 00000018 .state16 69 70 71 00000000 .sect ".text" 72 .clink 73 .global $foo 74 75 82 00000000 $foo: 83 00000000 B57C PUSH {A3, A4, V1, V2, V3, LR}

126 ; |1127| 127 0000004c BD7C POP {A3, A4, V1, V2, V3, PC} 128 129 130 131 00000000 .sect ".text:v$1" 132 .clink 133 00000000 .state32

Reply to
luken8r
Loading thread data ...

You should start by telling that you're attempting to code assembler for an ARM (guessed, but maybe correct).

You are trashing your stack, at least.

--

Tauno Voipio
tauno voipio (at) iki fi
Reply to
Tauno Voipio

Yes, my apologies. The target is an ARM7 using the TI Code composer studio.

It is evident that the stack is crash> snipped-for-privacy@gmail.com wrote:

OR DIVISION BY ZERO

Reply to
luken8r

If the compilers and assembly code follow the usual ARM calling conventions, you should not need to push any, except LR.

To be safe, push R4 - R12 and LR.

entry: push {r4-r12,lr} cmp r1,#0 beq divzr

adr lr,go32 bx lr

.align .state 32

go32: bl I_MOD adr lr,exit+1 bx lr

.state16

divzr: bl $foo

exit: pop {r4-r12, pc}

--

Tauno Voipio
tauno voipio (at) iki fi
Reply to
Tauno Voipio

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.