Author Topic: Convert C code into assembly PIC12F508  (Read 4863 times)

0 Members and 1 Guest are viewing this topic.

Offline Vindhyachal.taknikiTopic starter

  • Frequent Contributor
  • **
  • Posts: 487
Convert C code into assembly PIC12F508
« on: November 18, 2014, 08:29:35 am »
1. I have written code for PIC12F508. Code has 3 C files & 4 header files.
Code compiles successfully.

2. I need to convert into assembly. First thing came into mind is to check diasssembly.
Below is the part of disassembly code & c code comments removed from it:

Code: [Select]
00F     A9F     GOTO 0x9f

   0A0     912     CALL 0x12

   0A1     800     RETLW 0

   013     A9E     GOTO 0x9e
   014     FFF     XORLW 0xff
   015     FFF     XORLW 0xff
   016     FFF     XORLW 0xff
   017     FFF     XORLW 0xff
   018     FFF     XORLW 0xff
   019     FFF     XORLW 0xff
   01A     FFF     XORLW 0xff
   01B     FFF     XORLW 0xff
   01C     FFF     XORLW 0xff
   01D     FFF     XORLW 0xff
   01E     FFF     XORLW 0xff
   01F     FFF     XORLW 0xff
   020     FFF     XORLW 0xff
   021     FFF     XORLW 0xff
   022     FFF     XORLW 0xff
   023     FFF     XORLW 0xff
   024     FFF     XORLW 0xff
   025     FFF     XORLW 0xff
   026     FFF     XORLW 0xff
   027     FFF     XORLW 0xff
   028     FFF     XORLW 0xff
   029     FFF     XORLW 0xff
   02A     FFF     XORLW 0xff
   02B     FFF     XORLW 0xff
   02C     FFF     XORLW 0xff
   02D     FFF     XORLW 0xff
   02E     FFF     XORLW 0xff
   02F     FFF     XORLW 0xff
   030     FFF     XORLW 0xff
   031     FFF     XORLW 0xff
   032     FFF     XORLW 0xff
   033     FFF     XORLW 0xff
   034     FFF     XORLW 0xff
   035     FFF     XORLW 0xff
   036     FFF     XORLW 0xff
   037     FFF     XORLW 0xff
   038     FFF     XORLW 0xff
   039     FFF     XORLW 0xff
   03A     FFF     XORLW 0xff
   03B     FFF     XORLW 0xff
   03C     FFF     XORLW 0xff
   03D     FFF     XORLW 0xff
   03E     FFF     XORLW 0xff
   03F     FFF     XORLW 0xff
   040     FFF     XORLW 0xff
   041     FFF     XORLW 0xff
   042     FFF     XORLW 0xff
   043     FFF     XORLW 0xff
   044     FFF     XORLW 0xff
   045     FFF     XORLW 0xff
   046     FFF     XORLW 0xff
   047     FFF     XORLW 0xff
   048     FFF     XORLW 0xff
   049     FFF     XORLW 0xff
   04A     FFF     XORLW 0xff
   04B     FFF     XORLW 0xff
   04C     FFF     XORLW 0xff
   04D     FFF     XORLW 0xff
   04E     FFF     XORLW 0xff
   04F     FFF     XORLW 0xff
   050     FFF     XORLW 0xff
   051     FFF     XORLW 0xff
   052     FFF     XORLW 0xff
   053     FFF     XORLW 0xff
   054     FFF     XORLW 0xff
   055     FFF     XORLW 0xff
   056     FFF     XORLW 0xff
   057     FFF     XORLW 0xff
   058     FFF     XORLW 0xff
   059     FFF     XORLW 0xff
   05A     FFF     XORLW 0xff
   05B     FFF     XORLW 0xff
   05C     FFF     XORLW 0xff
   05D     FFF     XORLW 0xff
   05E     FFF     XORLW 0xff
   05F     FFF     XORLW 0xff
   060     FFF     XORLW 0xff
   061     FFF     XORLW 0xff
   062     FFF     XORLW 0xff
   063     FFF     XORLW 0xff
   064     FFF     XORLW 0xff
   065     FFF     XORLW 0xff
   066     FFF     XORLW 0xff
   067     FFF     XORLW 0xff
   068     FFF     XORLW 0xff
   069     FFF     XORLW 0xff
   06A     FFF     XORLW 0xff
   06B     FFF     XORLW 0xff
   06C     FFF     XORLW 0xff
   06D     FFF     XORLW 0xff
   06E     FFF     XORLW 0xff
   06F     FFF     XORLW 0xff
   070     FFF     XORLW 0xff
   071     FFF     XORLW 0xff
   072     FFF     XORLW 0xff
   073     FFF     XORLW 0xff
   074     FFF     XORLW 0xff
   075     FFF     XORLW 0xff
   076     FFF     XORLW 0xff
   077     FFF     XORLW 0xff
   078     FFF     XORLW 0xff
   079     FFF     XORLW 0xff
   07A     FFF     XORLW 0xff
   07B     FFF     XORLW 0xff
   07C     FFF     XORLW 0xff
   07D     FFF     XORLW 0xff
   07E     FFF     XORLW 0xff
   07F     FFF     XORLW 0xff
   080     FFF     XORLW 0xff
   081     FFF     XORLW 0xff
   082     FFF     XORLW 0xff
   083     FFF     XORLW 0xff
   084     FFF     XORLW 0xff
   085     FFF     XORLW 0xff
   086     FFF     XORLW 0xff
   087     FFF     XORLW 0xff
   088     FFF     XORLW 0xff
   089     FFF     XORLW 0xff
   08A     FFF     XORLW 0xff
   08B     FFF     XORLW 0xff
   08C     FFF     XORLW 0xff
   08D     FFF     XORLW 0xff
   08E     FFF     XORLW 0xff
   08F     FFF     XORLW 0xff
   090     FFF     XORLW 0xff
   091     FFF     XORLW 0xff
   092     FFF     XORLW 0xff
   093     FFF     XORLW 0xff
   094     FFF     XORLW 0xff
   095     FFF     XORLW 0xff
   096     FFF     XORLW 0xff
   097     FFF     XORLW 0xff
   098     FFF     XORLW 0xff
   099     FFF     XORLW 0xff
   09A     FFF     XORLW 0xff
   09B     FFF     XORLW 0xff
   09C     FFF     XORLW 0xff
   09D     FFF     XORLW 0xff

   09E     800     RETLW 0

   0A2     90F     CALL 0xf

   0A3     90E     CALL 0xe

   0A4     AA4     GOTO 0xa4

   0A5     C20     MOVLW 0x20

3. If I copy code as it in assembly will it work??

4. Any other method to convert c code to assembly.

5. For code I am using MPLAB & Hi-tech C
 

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: Convert C code into assembly PIC12F508
« Reply #1 on: November 18, 2014, 12:01:39 pm »
The first question that springs to mind is WHY?!
If you have something working perfectly well in C why on earth do you want to convert it to assembly?

Looking at your "snippet" it doesn't appear to do much.

The easiest steps to convert C into assembly are as follows:
1. Learn assembly.
2. Re write your code in assembly.

Job done!
 

Offline bookaboo

  • Frequent Contributor
  • **
  • Posts: 729
  • Country: ie
Re: Convert C code into assembly PIC12F508
« Reply #2 on: November 18, 2014, 12:08:03 pm »
I've never done C for 12F but wouldn't MPLABX and one of the free compilers (XC16) do this?
 

Offline Bored@Work

  • Super Contributor
  • ***
  • Posts: 3932
  • Country: 00
Re: Convert C code into assembly PIC12F508
« Reply #3 on: November 18, 2014, 05:04:35 pm »
The first question that springs to mind is WHY?!

Took a job to write assembler, figured out it is easier to let the compiler do it?
About to learn that it is easy to spot the difference between hand-written assembler code and compiler-generated assembler code.

Note that within the course of two weeks the OP asked for code for a STM32F205, a STM8S003F3, and a PIC12F508. Interspersed with requests for help building a 6.5 digit voltmeter, interfacing a strain gauge, interfaceing an LCD and interfacing a GSM modem and UART.

I have the suspicion we have someone here who "outsources" tasks to the forum. Tasks he probably took on for pay on some of these rent-a-coder sites.
I delete PMs unread. If you have something to say, say it in public.
For all else: Profile->[Modify Profile]Buddies/Ignore List->Edit Ignore List
 

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: Convert C code into assembly PIC12F508
« Reply #4 on: November 18, 2014, 05:49:11 pm »
It seems a bit pointless to convert back, it is usually the other way around.

Maybe he has taken a job to write something in assembler, maybe found some C code and wanted to reverse engineer it?

Well, unless he gives a clear, concise answer as to why he wants to do it I don't think anyone wants to do someone elses work for them!

It struck me as a bit dodgy when I first read it.
 

Offline Vindhyachal.taknikiTopic starter

  • Frequent Contributor
  • **
  • Posts: 487
Re: Convert C code into assembly PIC12F508
« Reply #5 on: November 21, 2014, 07:38:59 am »
@bored@work

1. STM32F205 - problem solved as I had some bug in code. Code  written myself.
2. PIC12F508 - I had written C code myself. You will hardly find any code in C for pic12f508 for project. Individula peripheral you can find on googlium site.
3. STM8S003F3 - I had doubt regrading its SWIM pin, which is cleared now.
4. 4 bit lcd: problem solved you can check original post.
5. Strain & 6.5 voltmeter & Modem: three are interlinked. Project get delayed. Will get back when turn comes up.

Every code I am writing myself. So no outsorcing on forum.
I ask question here when I have doubt.

Regarding this query about converting c into assembly this was just curiosity.
Otherwise user usually ask other way around  i.e assembly to C.
 

Offline Jebnor

  • Regular Contributor
  • *
  • Posts: 170
  • Country: ca
  • Absolutely! Yes, kind of, sort of, not really, no.
Re: Convert C code into assembly PIC12F508
« Reply #6 on: November 21, 2014, 02:35:58 pm »
Compilers are good. Really good.  The only time you need to code in assembly is for really tight control loops.  Sometimes the assumptions compilers make are wrong.

Learning better algorithms will likely a better use of time.
Before this, there was a typo.
 

Online Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11639
  • Country: my
  • reassessing directives...
Re: Convert C code into assembly PIC12F508
« Reply #7 on: November 22, 2014, 11:29:14 am »
Quote
3. If I copy code as it in assembly will it work??
yes.. if you know what you are doing. such as where is entry and return point, memory housekeeping and re-offsetting function calls if needed to. but that is conditional answer. the unconditional answer is... it depends.
Quote
4. Any other method to convert c code to assembly.
other than compiler? is porting c code to assembly code manually.
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Online mikeselectricstuff

  • Super Contributor
  • ***
  • Posts: 13748
  • Country: gb
    • Mike's Electric Stuff
Re: Convert C code into assembly PIC12F508
« Reply #8 on: November 22, 2014, 12:03:09 pm »
Compilers are good. Really good.  The only time you need to code in assembly is for really tight control loops.  Sometimes the assumptions compilers make are wrong.
There is an issue for this specific case that the free version of the Microchip compiler generates very inefficient code.
The full version is quite useable on tiny parts though.

There can occasionally be cases where the desired functionality is a good fit for the instruction set but a poor fit for standard C syntax. e.g. bit rotates, nibble swaps, rotate and use the resulting carry bit.
However in some cases, once you're familiar with how the compiler generates code you can coerce it into doing what you want. e.g. on the PIC you can do a char rotate or add and then use the carry result by accessing the physical carry bit in the status register.
It is important to know enough assembly to be able to look at compiler output and determine if it is doing something poorly or just plain wrong.

Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 

Offline Flenser

  • Regular Contributor
  • *
  • Posts: 60
Re: Convert C code into assembly PIC12F508
« Reply #9 on: November 23, 2014, 11:30:11 am »
Quote
3. If I copy code as it in assembly will it work

Probably not. You have three C files so I expect the compiler will be producing 3 ASM files. The assembler compiles these three ASM files to produce three object files. The linker combines the object files to produce the hex file. See figure 2.1 in the Microchip HI-TECH C for PIC10/12/16 User’s Guide for a diagram of how this works http://ww1.microchip.com/downloads/en/DeviceDoc/HTC_PIC_manual.pdf

To get the compiler to generate a single ASM file that you can assemble directly to a hex file:
- copy all the C code into a single file.
- remove any duplicate "include" lines for header files
- Create a new MPLAB project with just the one C file plus the four header files
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf