How to convert (*.asm;*.lst & *.hex) to "C" program

Discussion in 'Programmer's Corner' started by mkbutan, Mar 21, 2012.

  1. mkbutan

    Thread Starter Senior Member

    Sep 30, 2008
    270
    16
    hi,
    can any one guide me to convert the program Downloaded from the site is in (water.asm/water.lst/water.hex) can the same code's can be written in "C" language pl help me to write the same program in the "C" Language I am learning the "C" Language.

    the link of the Wireless Water-Level Indicator.http://efymag.com/previousissue.asp?month=January&year=2012&tot=1&id=12

    the "water.asm" program is as Follows :-


    Code ( (Unknown Language)):
    1. $mod52             
    2. var2    equ 13h
    3. lcd equ P2                 
    4. buzz equ p3.4
    5. rs equ p3.7
    6. rw equ p3.6
    7. e  equ p3.5
    8.  
    9. org 00H                    
    10. main:   clr rw
    11.     mov p2,#00H            
    12.     mov p1,#0fh
    13.     acall lcdin            
    14.  
    15. start:
    16.         mov a ,p1
    17.         cjne a,#01h,skip1
    18.         acall m25;
    19.  
    20.     skip1:  mov a ,p1
    21.         cjne a,#03h,skip2
    22.         acall m50;
    23.         sjmp start
    24.  
    25.     skip2:  mov a ,p1
    26.         cjne a,#07h,skip3
    27.         acall m75;
    28.         sjmp start
    29.  
    30.     skip3:  mov a ,p1
    31.         cjne a,#0fh,skip4
    32.         acall m100;
    33.         sjmp start
    34.  
    35.     skip4:  mov a ,p1
    36.         cjne a,#00h,skip5
    37.         acall low1
    38.         sjmp start
    39.  
    40.     skip5:  sjmp start;
    41.  
    42. m25:        mov a,#0c0h
    43.         acall cmm
    44.         mov dptr,#mydata2
    45.         mov var2,#15d
    46.     bck2:   clr a
    47.         movc a,@a+dptr
    48.         acall dat
    49.         inc dptr
    50.         djnz var2,bck2   
    51.         ret
    52.  
    53.  
    54. m50:        mov a,#0c0h
    55.         acall cmm
    56.         mov dptr,#mydata3
    57.         mov var2,#15d
    58.     bck3:   clr a
    59.         movc a,@a+dptr
    60.         acall dat
    61.         inc dptr
    62.         djnz var2,bck3   
    63.         ret;
    64.  
    65.  
    66. m75:        mov a,#0c0h
    67.         acall cmm
    68.         mov dptr,#mydata4
    69.         mov var2,#15d
    70.     bck4:   clr a
    71.         movc a,@a+dptr
    72.         acall dat
    73.         inc dptr
    74.         djnz var2,bck4   
    75.         ret
    76.  
    77.  
    78. m100:       mov a,#0c0h
    79.         acall cmm
    80.         mov dptr,#mydata5
    81.         mov var2,#16d
    82.         setb buzz
    83.     bck5:   clr a
    84.         movc a,@a+dptr
    85.         acall dat
    86.         inc dptr
    87.         djnz var2,bck5   
    88.         acall delay
    89.         acall delay
    90.         acall delay
    91.         clr buzz
    92.         ret
    93.  
    94. low1:       mov a,#0c0h
    95.         acall cmm
    96.         mov dptr,#mydata6
    97.         mov var2,#15d
    98.         setb buzz
    99.     bck6:   clr a
    100.         movc a,@a+dptr
    101.         acall dat
    102.         inc dptr
    103.         djnz var2,bck6   
    104.         acall delay
    105.         clr buzz
    106.         ret
    107.  
    108. lcdin:
    109.     mov a,#38h
    110.     acall cmm
    111.     mov a,#0ch
    112.     acall cmm
    113.     mov a,#01h
    114.     acall cmm
    115.     mov a,#06h
    116.     acall cmm
    117.     mov a,#86h
    118.     acall cmm
    119.     mov dptr,#mydata1
    120.     mov var2,#3
    121. bck1:   clr a
    122.     movc a,@a+dptr
    123.     acall dat
    124.     inc dptr
    125.     djnz var2,bck1
    126.     ret
    127. cmm:
    128.     mov lcd,a
    129.     clr rs
    130.     setb e
    131.     acall delay
    132.     clr e
    133.     acall delay
    134.     ret
    135. dat:
    136.     mov lcd,a
    137.     setb rs
    138.     setb e
    139.     acall delay
    140.     clr e
    141.     acall delay
    142.     ret
    143. delay:
    144.     mov r1,#50
    145. ro1:    mov r2,#0ffh
    146. ro2:    djnz r2,ro2
    147.     djnz r1,ro1
    148.     ret;
    149. org 300
    150. mydata1: db 'EFY'
    151. mydata2: db 'Water level 25%'
    152. mydata3: db 'Water level 50%'
    153. mydata4: db 'Water level 75%'
    154. mydata5: db 'Water level 100%'
    155. mydata6: db 'Water level low'
    156. END                     ;End of program
    157.  

    pl any one help
    thanks in advance
     
    Last edited by a moderator: Mar 21, 2012
  2. MrChips

    Moderator

    Oct 2, 2009
    12,447
    3,363
    Can be done by hand, one step at a time.
     
    mkbutan likes this.
  3. mkbutan

    Thread Starter Senior Member

    Sep 30, 2008
    270
    16
    sir how pl explain as i have only attended few classes of "c" programming
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,447
    3,363
    Basically, what I am saying is there is no automatic process that I am aware of. It is possible that someone can write a program to do this.

    Here is a small example of direct one-to-one translation:


    Code ( (Unknown Language)):
    1.  
    2. main: clr rw
    3. mov p2,#00H
    4. mov p1,#0fh
    5. acall lcdin
    6.  
    translated to C:

    Code ( (Unknown Language)):
    1.  
    2. uint8_t rw, p1,p2;
    3.  
    4.   rw = 0;
    5.   p2 = 0;
    6.   p1 = 0x0F;
    7.   lcdin();
    8.  
     
    mkbutan likes this.
  5. mkbutan

    Thread Starter Senior Member

    Sep 30, 2008
    270
    16
    i have 2 compilers
    1 dev c ++
    2 keil μver.4
    i wanted to write and load this program in P89V51RD2 with C language code's
    the same *.HEX is working in P89v51RD2
    pl tell me which compiler to use and how?
     
  6. vpoko

    Member

    Jan 5, 2012
    258
    47
    The best thing to do is to figure out what the existing assembly program does, and then write a brand new C program based on those specifications. Trying to go through the code and convert to C may give you C source that works but without really being C-like (meaning your source code will be in C, but it will be dealing with lower-level details than a C program would normally deal with).
     
  7. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    This isn't what I'd recommend for a C noob. What you are asking for requires a fairly good familiarity with compilers in general, and great familiarity with your compiler, as well as good familiarity with your chosen chip (8052 family).

    First, if it doesn't do so already, find out how to get your compiler to generate assembly code. Then, using a simple C program, look at the assembly output. Have ALL optimizations turned off! (You'll need to Read The Fine Manual to find out how this is done.)

    See what is generated for:
    Code ( (Unknown Language)):
    1.  
    2. void main(void)
    3. {
    4.   unsigned char Va;
    5.  
    6.     do{
    7.         Va++;
    8.     }while(1);
    9. }
    10.  
    Then, read up on bit and I/O port operations for your compiler.

    It will also help if you properly indent the ASM code. Instead of
    Code ( (Unknown Language)):
    1.  
    2. start: mov a ,p1
    3. cjne a,#01h,skip1
    4. acall m25
    5.  
    Make it look like:
    Code ( (Unknown Language)):
    1.  
    2. start:
    3.     mov a ,p1
    4.     cjne a,#01h,skip1
    5.     acall m25
    6.  
    After you're done with that, try to work out how CJNE A,#xx,Label works, and how to do it in C. Hint: Use negative logic.

    No one will blame you if you realize this is beyond your ability; that you'll come back to it in a few years!

    Good Luck!
    --Rich
     
    maxpower097 and mkbutan like this.
  8. K7GUH

    Member

    Jan 28, 2011
    191
    23
    There is no magic way to translate one low level language to another. If it is written in assembler, you have to know enough assembler to figure out what each line of code does. If you want the results in C (or any other language), you have to know enough of that language to achieve the same result as in the original when you write the code. If there were an automatic way to translate from assembler to C, you probably wouldn't be able to afford it.
     
    mkbutan likes this.
  9. MrChips

    Moderator

    Oct 2, 2009
    12,447
    3,363
    It is relatively easy to translate from C to assembler. That is because we can take something that is complex and break in down into its primitive parts. It is like taking a toy apart into pieces.

    It is much more difficult to reconstruct the complex part from simple pieces. If you placed all the parts of a complex toy into one box, you would have a difficult time recreating the toy from its pieces.
     
    maxpower097 and mkbutan like this.
  10. mkbutan

    Thread Starter Senior Member

    Sep 30, 2008
    270
    16
    sir,( to all the above )
    thanks for all you have suggested ;
    I have an idea but don't know how to implement it some thing like Reverse Engineering

    eg.:-

    "C" Language code's converted to the BINARY Language code's With the help of C Compiler (Like Dev C++ / Keil μVer 4 "C" code's converted to Binary Code's)

    And

    "ASM" Language Code's converted to the Binary Language Code's with the help of " 'ASM' Compiler ( like " ASM51 " ASSEMBLY Language code's converted to Binary Code's ( may be I don't know))

    Now

    can we convert that HEX => Binary code's to Binary => "C" code's.

    if yes how?
     
  11. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Unless you have some sort of magic wand. The only way to do this. Is by some effort done by you. You have received many good tips here. Instead of using a lot of effort to find some easy way out. Use your energy to solve the problem instead ;)
     
    mkbutan likes this.
  12. MrChips

    Moderator

    Oct 2, 2009
    12,447
    3,363
    You still don't get the message.

    The only way to do this is by hand, one instruction at a time.

    It would be easier to rewrite the code in C, knowing what the program is supposed to do.
     
    maxpower097 likes this.
  13. mkbutan

    Thread Starter Senior Member

    Sep 30, 2008
    270
    16
    thanks
    as its my beginning of learning C language
    but i will try doing some R/D on it
    but pl dont close the post if any one have some better ides pl guide me
     
  14. K7GUH

    Member

    Jan 28, 2011
    191
    23
    I spent 37 years fixing other people's broken computer programs. I cannot tell you the number of times I had to try to convince a desperate client that there is no way automatically to convert so many ones and zeroes into a human readable/understandable program. Over a fifty year period, there have been many claims made on behalf of disassembly programs, all of them based on the mystical belief that there must be some way to convert machine language to human language. There have been many attempts, but they all come down to reliance on a human who can read and interpret the best efforts of a software program to make sense out of the input. No one wants to hear that it hasn't been done successfully, and equally sad, no one wants to pay the price of having a knowledgeable human do it for a fee. That's the long and the short of it.
     
    mkbutan likes this.
  15. maxpower097

    Well-Known Member

    Feb 20, 2009
    795
    388
    Going thru that right now I can only imagine what 37 years would be like. Your my new iron man! Its even worse when the client thinks they know what their doing. :)
     
  16. K7GUH

    Member

    Jan 28, 2011
    191
    23
    It was either that or 42+ years teaching Foreign Languages to public high school students.
     
  17. ajm113

    Member

    Feb 19, 2011
    176
    5
    Not trying to hate or jump off topic, but read this:

    http://www.jasonbadams.net/20081218/why-you-shouldnt-use-dev-c/

    Now after that, download this:
    http://msdn.microsoft.com/en-us/vstudio/aa718325

    ... It's free if you get the learner edition and 120% better then Dev, trust me. I'm a ex Dev user and I bought 2005 and 2008 and I love them! Tottally worth the 100 bucks, plus if your wanting to make programming sort of a profession or a job Visual Studio is a industry stander so I would highly recommend it.

    If your running good old Linux just get code blocks otherwise, Xcode if OS X. ;)
     
    Last edited: Mar 29, 2012
    mkbutan likes this.
  18. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
  19. maxpower097

    Well-Known Member

    Feb 20, 2009
    795
    388
    Easy, Print your asm file and put it in jar1. Place an empty jar next too it we''ll call Jar2. Throw a black velvet sheet over it and knock on the tops of the jars repeating , "mekalekahi meka hiney ho!" 3 times. Then look inside Jar2.
     
Loading...