Assembly to C

2»

Comments

  • : : Hi all,
    : : i have a piece of program written in assembly. I have to rewrite it
    : : to C, but i dont know how, can somebody help me with it ?
    : : [code]: :
    : : unsigned int iz32prvi( //read word from nonvolatile data buffer
    : : TNVVar *nvv, // pointer to nonvolatile memory
    : : UINT16 dat) // address of doulbe word in nonvolatile memory
    : : {
    : : UINT16 vh1 = dat;
    : : unsigned int bla=0;
    : : asm {
    : : LDD 7,SP
    : : ADDD 2,SP
    : : TFR D,X
    : : LDD 0,X
    : : STD 0,SP
    : : }
    : :
    : : return bla;
    : : }
    : : [/code]: :
    : : Thx and best regards,
    : : BoSCHoW.
    : :
    : Well, in the absence of context it is not easy to disassmeble this
    : code, even assuming you know the processor. Who wrote the code as
    : they should know what it is doing?
    : If you don't know, then a literal disassembly by hand may well be
    : wrong, but here goes (mind you my background is 6800/6802 not 6812):
    : LDD 7,SP 'Load D (16-bit Accumulator) Indexed Indirect from the
    : stack pointer (the 7 is probably an offset from the SP value).
    : ADDD 2,SP 'Add D Indexed Indirect from the stack pointer (offset of
    : 2)
    : TFR D,X 'Transfer the result to the Index Register.
    : LDD 0,X ' Load D with the value from the memory location in the
    : Index Register, no offset
    : STD 0,SP 'Store it in the location given by the stack pointer
    :
    : Mind you, as I said I am not familiar with anything near as new as
    : this chip (but starting to update) after not doing 6800 assembly
    : since 1985 or so. It seems highly odd to me that you would
    : calculate an address from old stack values, grab that value then
    : shove it on the stack; that is too dangerous for my style, but hey
    : whatever thrills you.

    P.S. I should have added that you need to get the right resources for the CPU chip. Download from freescale.com the CPU12RM and CPU12RG/D, they're PDF and free. That'll make you more independent as well.
    It would also help in future if you provided more information. As I said, code without context is risky to work with. Almost all of the traffic on your question has been trying to identify the CPU and gain the required information to help you.
  • You translate the code correctly, and that is why I said that it would definitely not work - you don't know where on the stack the compiler has placed the variables. To fiddle around with the variables placed on the stack by the compiler is to ask for trouble. In safety-critical systems, it is completely unacceptable.

    In this case the compiler is Codewarrior for HCS12, it will place the function parameters on the stack or in accumulators depending on their types. The last parameter in the list will always end up in an accumulator. There is an app not for Codewarrior describing this.

    Note that this is HCS12 and not HC12, the latter is obsolete, although the assemble is code-compatible with HCS12.

    To the OP: I can help, I have worked pretty much non-stop with this particular MCU and compiler/debugger since it was released five years ago. But I won't provide any complete solution without any effort made by the poster. As mentioned before, the code posted is really just nonsense and has nothing to do with nvm programming.

    It goes without saying that you need to get the manual and read the whole of it. The HCS12 is a particulary complicated MCU with 800 or so registers, banked memory and complex peripherals. If you just grab it and start programming without knowing the chip... best of luck with that.
  • : You translate the code correctly, and that is why I said that it
    : would definitely not work - you don't know where on the stack the
    : compiler has placed the variables. To fiddle around with the
    : variables placed on the stack by the compiler is to ask for trouble.
    : In safety-critical systems, it is completely unacceptable.
    :
    : In this case the compiler is Codewarrior for HCS12, it will place
    : the function parameters on the stack or in accumulators depending on
    : their types. The last parameter in the list will always end up in an
    : accumulator. There is an app not for Codewarrior describing this.
    :
    : Note that this is HCS12 and not HC12, the latter is obsolete,
    : although the assemble is code-compatible with HCS12.
    :
    : To the OP: I can help, I have worked pretty much non-stop with this
    : particular MCU and compiler/debugger since it was released five
    : years ago. But I won't provide any complete solution without any
    : effort made by the poster. As mentioned before, the code posted is
    : really just nonsense and has nothing to do with nvm programming.
    :
    : It goes without saying that you need to get the manual and read the
    : whole of it. The HCS12 is a particulary complicated MCU with 800 or
    : so registers, banked memory and complex peripherals. If you just
    : grab it and start programming without knowing the chip... best of
    : luck with that.

    1. Thanks for your comments, I wasn't sure of the translation, given lack of experience with the HCS 12 and time away from this stuff.
    2. I also didn't mention that I have only ever used hand assembly of small programs, so Code Warrior, et al are foreign to me.
    3. I found the posting on Google while searching for something else and only signed and and replied because it got me curious.
    4. Thanks for clearing explaining what bothered me but evaded clear explanation at the time: the risks of unknown stack alignment.
    5. The only thing that makes sense about this code is an academic exercise: a lecturer requiring students to convert this code. It only occurs to me but I have just had another close look at Ron Bishop's 1979 book: Basic Microprocessors and the 6800. Much of that code seems senseless in any useful manner.
    6. It's after midnight and I have the flu, better try and sleep now.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories