help in converting asm code to c code?

Discussion in 'Programmer's Corner' started by alkid, Nov 7, 2007.

  1. alkid

    Thread Starter Member

    Sep 20, 2007
    12
    0
    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?
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    Two steps:

    1. Understand the function of the assembler code.

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

    Active Member

    Oct 24, 2007
    37
    0
    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
     
  4. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    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
     
  5. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,789
    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:
    Code ( (Unknown Language)):
    1.  
    2. void INT0(void)
    3. {
    4.     CSN = 0 ;
    5.     SPI0DAT = 0x20 ;
    6.     SPI() ;
    7.     SPI0DAT = 0x08 ;
    8.     SPI() ;
    9.     CSN = 1 ;
    10.     .
    11.     .
    12.     .
    13.  
    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.
     
Loading...