Assembly and C can they live together?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Assembly and C can they live together?

Jump to solution
3,411 Views
Anastasios
Contributor I
Hello all (again :smileyhappy: ),

I want to write a function in c that looks like this

void defuzzification_function(const Fuzzy_Outputs *si,Fuzzy_Outputs *fi,const unsigned char n,unsigned short *result){

asm{
ldx si
ldy fi
ldab n
wav
ediv
sty result //PROBLEM HERE
}

};

And this is how i call her:

//the definitions of variables

const Fuzzy_outputs Output_Membership_Function={...};
Fuzzy_outputs FO;
const unsigned char outputlabels=5;
static unsigned short Qipercent;

void main(){
defuzzification_function(&Output_Membership_Function,&FO,outputlabels,&Qipercent);
}

logically the result of the ediv should be stored at Qipercent but this doesn't happen and Idon't know where it is stored. The generated assembly looks fine :

(Output_Membership_Function 35377
FO 2056
Qipercent 2071)

LDD #35377
PUSHD
LDX #2056
PUSHX
LDAB #5
PUSHB
LDD #2071
JSR SOMEPLACE

SOMEPLACE:
PUSHD
LDX 7,SP
LDY 5,SP
LDAB 4,SP
WAV
EDIV
STY 0,SP //this should do the job
LEAS 2,SP
RTS
--------
Any Ideas on what is happening here?

Thank you in advance

Anastasios
Labels (1)
Tags (1)
0 Kudos
1 Solution
749 Views
CrasyCat
Specialist III

Hello

In fact the code you have provided is not correct. When mixing ANSI C and assembler or inline assembler you have to respect th eANSI C calling convention in your assembler source code. You find information around teh calling convention in the Manual_Compiler_HC12.pdf, chapter "Motorola HC12 Back End", section "Call Protocol and Calling Conventions".

Basically the last parameter is passed into register. You then have to save it on the stack and to make sure you do not loose it.

I would write the code as follows:

void defuzzification_function(const Fuzzy_Outputs *si,Fuzzy_Outputs *fi,const unsigned char n,unsigned short *result){

asm{
PSHD  ; Push result
ldx si
ldy fi
ldab n
wav
ediv
PULX  ; Pull result
sty 0,x
}

};

I hope this helps.

CrasyCat

View solution in original post

0 Kudos
2 Replies
750 Views
CrasyCat
Specialist III

Hello

In fact the code you have provided is not correct. When mixing ANSI C and assembler or inline assembler you have to respect th eANSI C calling convention in your assembler source code. You find information around teh calling convention in the Manual_Compiler_HC12.pdf, chapter "Motorola HC12 Back End", section "Call Protocol and Calling Conventions".

Basically the last parameter is passed into register. You then have to save it on the stack and to make sure you do not loose it.

I would write the code as follows:

void defuzzification_function(const Fuzzy_Outputs *si,Fuzzy_Outputs *fi,const unsigned char n,unsigned short *result){

asm{
PSHD  ; Push result
ldx si
ldy fi
ldab n
wav
ediv
PULX  ; Pull result
sty 0,x
}

};

I hope this helps.

CrasyCat

0 Kudos
749 Views
Anastasios
Contributor I
It does not only help,it solves my problem : ).Thanks CrasyCat, it never crossed my mind to check with manual compiler hc12.

Regards

Anastasios
0 Kudos