Convert C code into assembly PIC12F508 - Page 1
Forum section: electronics / beginners

author: Vindhyachal.takniki
author_rank: Frequent Contributor
timestamp: November 18, 2014, 08:29:35 am
content: 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:00F A9F GOTO 0x9f0A0 912 CALL 0x120A1 800 RETLW 0013 A9E GOTO 0x9e014 FFF XORLW 0xff015 FFF XORLW 0xff016 FFF XORLW 0xff017 FFF XORLW 0xff018 FFF XORLW 0xff019 FFF XORLW 0xff01A FFF XORLW 0xff01B FFF XORLW 0xff01C FFF XORLW 0xff01D FFF XORLW 0xff01E FFF XORLW 0xff01F FFF XORLW 0xff020 FFF XORLW 0xff021 FFF XORLW 0xff022 FFF XORLW 0xff023 FFF XORLW 0xff024 FFF XORLW 0xff025 FFF XORLW 0xff026 FFF XORLW 0xff027 FFF XORLW 0xff028 FFF XORLW 0xff029 FFF XORLW 0xff02A FFF XORLW 0xff02B FFF XORLW 0xff02C FFF XORLW 0xff02D FFF XORLW 0xff02E FFF XORLW 0xff02F FFF XORLW 0xff030 FFF XORLW 0xff031 FFF XORLW 0xff032 FFF XORLW 0xff033 FFF XORLW 0xff034 FFF XORLW 0xff035 FFF XORLW 0xff036 FFF XORLW 0xff037 FFF XORLW 0xff038 FFF XORLW 0xff039 FFF XORLW 0xff03A FFF XORLW 0xff03B FFF XORLW 0xff03C FFF XORLW 0xff03D FFF XORLW 0xff03E FFF XORLW 0xff03F FFF XORLW 0xff040 FFF XORLW 0xff041 FFF XORLW 0xff042 FFF XORLW 0xff043 FFF XORLW 0xff044 FFF XORLW 0xff045 FFF XORLW 0xff046 FFF XORLW 0xff047 FFF XORLW 0xff048 FFF XORLW 0xff049 FFF XORLW 0xff04A FFF XORLW 0xff04B FFF XORLW 0xff04C FFF XORLW 0xff04D FFF XORLW 0xff04E FFF XORLW 0xff04F FFF XORLW 0xff050 FFF XORLW 0xff051 FFF XORLW 0xff052 FFF XORLW 0xff053 FFF XORLW 0xff054 FFF XORLW 0xff055 FFF XORLW 0xff056 FFF XORLW 0xff057 FFF XORLW 0xff058 FFF XORLW 0xff059 FFF XORLW 0xff05A FFF XORLW 0xff05B FFF XORLW 0xff05C FFF XORLW 0xff05D FFF XORLW 0xff05E FFF XORLW 0xff05F FFF XORLW 0xff060 FFF XORLW 0xff061 FFF XORLW 0xff062 FFF XORLW 0xff063 FFF XORLW 0xff064 FFF XORLW 0xff065 FFF XORLW 0xff066 FFF XORLW 0xff067 FFF XORLW 0xff068 FFF XORLW 0xff069 FFF XORLW 0xff06A FFF XORLW 0xff06B FFF XORLW 0xff06C FFF XORLW 0xff06D FFF XORLW 0xff06E FFF XORLW 0xff06F FFF XORLW 0xff070 FFF XORLW 0xff071 FFF XORLW 0xff072 FFF XORLW 0xff073 FFF XORLW 0xff074 FFF XORLW 0xff075 FFF XORLW 0xff076 FFF XORLW 0xff077 FFF XORLW 0xff078 FFF XORLW 0xff079 FFF XORLW 0xff07A FFF XORLW 0xff07B FFF XORLW 0xff07C FFF XORLW 0xff07D FFF XORLW 0xff07E FFF XORLW 0xff07F FFF XORLW 0xff080 FFF XORLW 0xff081 FFF XORLW 0xff082 FFF XORLW 0xff083 FFF XORLW 0xff084 FFF XORLW 0xff085 FFF XORLW 0xff086 FFF XORLW 0xff087 FFF XORLW 0xff088 FFF XORLW 0xff089 FFF XORLW 0xff08A FFF XORLW 0xff08B FFF XORLW 0xff08C FFF XORLW 0xff08D FFF XORLW 0xff08E FFF XORLW 0xff08F FFF XORLW 0xff090 FFF XORLW 0xff091 FFF XORLW 0xff092 FFF XORLW 0xff093 FFF XORLW 0xff094 FFF XORLW 0xff095 FFF XORLW 0xff096 FFF XORLW 0xff097 FFF XORLW 0xff098 FFF XORLW 0xff099 FFF XORLW 0xff09A FFF XORLW 0xff09B FFF XORLW 0xff09C FFF XORLW 0xff09D FFF XORLW 0xff09E 800 RETLW 00A2 90F CALL 0xf0A3 90E CALL 0xe0A4 AA4 GOTO 0xa40A5 C20 MOVLW 0x203. 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

author: Wilksey
author_rank: Super Contributor
timestamp: November 18, 2014, 12:01:39 pm
content: 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!

author: bookaboo
author_rank: Frequent Contributor
timestamp: November 18, 2014, 12:08:03 pm
content: I've never done C for 12F but wouldn't MPLABX and one of the free compilers (XC16) do this?

author: Bored@Work
author_rank: Super Contributor
timestamp: November 18, 2014, 05:04:35 pm
content: Quote from: Wilksey on November 18, 2014, 12:01:39 pmTook 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.

author: Wilksey
author_rank: Super Contributor
timestamp: November 18, 2014, 05:49:11 pm
content: 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.

author: Vindhyachal.takniki
author_rank: Frequent Contributor
timestamp: November 21, 2014, 07:38:59 am
content: @bored@work1. 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.

author: Jebnor
author_rank: Regular Contributor
timestamp: November 21, 2014, 02:35:58 pm
content: Compilers are good. Really good. The only time youneedto 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.

author: Mechatrommer
author_rank: Super Contributor
timestamp: November 22, 2014, 11:29:14 am
content: Quoteyes.. 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.Quoteother than compiler? is porting c code to assembly code manually.

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: November 22, 2014, 12:03:09 pm
content: Quote from: Jebnor on November 21, 2014, 02:35:58 pmThere 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.

author: Flenser
author_rank: Regular Contributor
timestamp: November 23, 2014, 11:30:11 am
content: QuoteProbably 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 workshttp://ww1.microchip.com/downloads/en/DeviceDoc/HTC_PIC_manual.pdfTo 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