# help in converting asm code to c code?

#### alkid

Joined Sep 20, 2007
12
INT0: CLR CSN
MOV SPI0DAT, #20H
LCALL SPI
MOV SPI0DAT, #08H
LCALL SPI
SETB CSN
CLR CSN
MOV SPI0DAT, #27H
LCALL SPI
MOV SPI0DAT, #7EH
LCALL SPI
SETB CSN
MOV SPI0DAT, #0E1H
LCALL SPI
SETB CSN
CLR CSN
MOV SPI0DAT, #0E2H
LCALL SPI
SETB CSN
RET
anyone know of anyway i can change the asm code into c code?

#### beenthere

Joined Apr 20, 2004
15,819
Two steps:

1. Understand the function of the assembler code.

2. Write the code in C that does the same thing.

#### jpitz31

Joined Oct 24, 2007
37
If you do not know assembly, then it would be much easier to just write the code from scratch in C.

To decipher the indicated asm code you would have spend time figuring out the hex addresses indicated and what SPI0DAT is pointing to.

This is chip related and you do not give us the chip that you are working with?

It would be much easier to just write the code in C.

You are talking the difference of moving a bunch of bits around versus writing a much higher level English like language.

I do not know about you but I would write C any day over assembly.

What operation do you want the chip to perform and then write that functionality in C in the first place.

Then it becomes your code, code that you understand and then can enhance if needed.

Just my 2 cents.

Thanks

Joe

#### RiJoRI

Joined Aug 15, 2007
536
MOV SPI0DAT, #20H
LCALL SPI

My guess is that this is loading a variable called SPIODAT (SPI Output DATa, maybe?) with some number, then calling a function SPI to send it out the SPI pin. The CSN is some type of control. You will have to find out how your C compiler deals with bits in order to do the SBIT part.

The RET is handled by C's end function:
void myFunc(void)
{
/* Do nothing */
} <- This will cause the compiler to generate the RTS command.

HTH,
--Rich

#### Papabravo

Joined Feb 24, 2006
14,465
I'm guessing this is 8051 code. The label INT0 suggests an interrupt routine, except for the small detail that it ends with a RET instead of a RETI.

The basic example would be like this:
Rich (BB code):
void INT0(void)
{
CSN = 0 ;
SPI0DAT = 0x20 ;
SPI() ;
SPI0DAT = 0x08 ;
SPI() ;
CSN = 1 ;
.
.
.
This presupposes that you have a header file and a C compiler that defines "CSN", "SPI0DAT", and the function "SPI()" appropriately. You still have a job of work to do on this one.