Vadim Kudryavtsev home page

Useful dowloads  & programms
 

  1. XCODE.EXE - "clever" transcoder of text files between russian codepages. Press XCODE.EXE имя файла and it will recode the file to DOS cyrillics codepage automatically. It is very useful when receiving e-mails with corrupted codepage info. It has possibility of double recoding.
  2. RUS2ENG.EXE - encoder of russian (DOS cyrillics) text to translit. Very useful if you want to send e-mails to your friends having PC with non-installed russian codepages. Just press RUS2ENG.EXE имя файла, and encoded files with extension *.eng appears in the same directory.
  3. ReadCD32.EXE - checking readability of CD/DVD check all files in directory-tree. Counts error number. Written by Mikhail Nikolaew (NML).
  4. "Однокристальные 4-х разрядные микроЭВМ серии КР1820"("4-bit On-Chip Microcomputers of series KR1820") - brochure on series KR1820, produced by RPC "Integral".
  5. Manual to amateur electronic oscilloscope H3015.

ARM2C - Converter of ARM code to C

ARM2C.zip - download sources and example

History:

2012.09.18    - Original release
2012.09.23    - Update
                            - pattern recognition in LDR/STR commands fixed
                            - crash during instruction parsing is catched in order to place the error message in translated C code
2012.10.03    - Update
                            - pattern recognition in the presense of suffix and flag fixed
                            - "op2" operand problem in pattern recognition for LDR/STR instruction fixed
                            - LDRB/STRB, LDM/STM instructions added
2012.10.08    - Update
                        - Condition table
                        - CLZ and other instructions added to Macros table
                        - Condition processing improved

Similar projects:


This project was born from the situations when I found that some of C reference file was lost or never been created. And I was involved to work in cross-compiler environment. Compiling and especially PC simulation became very slow compared to the case when I used Visual Studio projects written totally in C reference code.
I found several decompiler projects in the internet, but only one of them (Hex-Rays) is has ability of ARM assembler translation to C code. Nevertheless it is excelent tool (regards for developers), it is shareware, and for a considerable price.
So I decided ti use free time (at home) for writing simple ARM assembler to C translation routine that would allow me to create C reference files (even if the translation result will be quite dummy) and run the full project in Visual Studio.

This is first version, and my target was prepare simple routine after which I can get some reasonable C code for the function bodies (no optimization, no good readability) and after small manual correction (proper function declarations instead of dummy ones etc) to get C code able to compile in VS. So please, don't kick me by foots :-), but I'll sincerely accept good critics.

How to translate:

1. Directory "Original_asm_Biquad_Project" consist original project that for basic audio processing. But one file is written on ARM assembler. Our goal is to convert it to C equivalent in order to have the whole project to be written fully in C.

2. Lets transform it by ARM2C routine
ARM2C.exe -oARM2C_biquad.c -ibiquad.s
Below you can see part of our example file translation

Table 1. Partial translation result.

ARM Assembler (biquad.s) C Equivalent (ARM2C_biquad.c)
    ; load states
    ldmia    r3, {r8-r9}
   
    ; load counter
    ldr    r14, [sp,#4*9]
   
biquad_Loop

    ldrsh    r2, [r1],#2*2
    smlawb    r2, r8, r7, r2
    smlawt    r2, r9, r7, r2
    smulwb    r12, r8, r6
    smlawt    r12, r9, r6, r12
    mov        r2, r2, lsl#(16-QCOEFF)
    smlawb    r12, r2, r5, r12
    strh    r12, [r0],#2*2

    mov        r9, r8                ; update states
    mov        r8, r2
   
    subs r14, r14, #1
    bgt biquad_Loop
   
    ; save states
    stmia    r3, {r8-r9}
// load states

{ int* p=(int*)r3; {

 r8 = *p; p+=1;
 r9 = *p; p+=1;
} }

// load counter

r14 = *((int*)(sp+(4*9)));

biquad_Loop:
r2 = (int)(*((short*)r1)); r1+=2*2;
r2 = (int)((((INT64)r8 * (short)r7)>>16) + r2);
r2 = (int)((((INT64)r9 * (short)(r7>>16))>>16) + r2);
r12 = (int)(((INT64)r8 * (short)r6)>>16);
r12 = (int)((((INT64)r9 * (short)(r6>>16))>>16) + r12);
r2 = (r2<<((16-QCOEFF)));
r12 = (int)((((INT64)r2 * (short)r5)>>16) + r12);
(*((unsigned short*)r0)) = (unsigned short)r12; r0+=2*2;
// update states
r9 = r8;
r8 = r2;

r14 = r14 - (1);s=r14;
if (s>0){goto biquad_Loop;}

// save states

{ int* p=(int*)r3; {

 *p = r8; p+=1;
 *p = r9; p+=1;
} }


3. The resultung file ARM2C_biquad.c is not ready yet for compiling.
    - first lets see through all file whether there are some error indicators about non-translated lines (something like "ERROR !!! ->  some  code   <- this line is not recognized"). If it is not important for the code (some prerocessor routine or comment) just comment it. Otherwise, translate it to the C equivalent manually. (Sorry, until now not the full set of AR< instruction is supported).
    - second, we have to exchange fake function declaration by the real one from the corresponding header.
    - third, we have to set proper number of parameters to the PARAMETER_NUMBER declaration, and then uncomment required number operators placing parameters p4 and higher to the array imitating stack. This is needed because by C call agreement, four first parameters are placed to the register r0-r3, but remained parameters are placed to the stack. So we are using internal array imitating stack that consists function parameters.

Table 2. Translation result manual corretcion

Original translation Required correction
// Replace this function declaration by true declaration from corresponding header file
int Biquad_proc (int p0, int p1, int p2, int p3)
{
    int r0=(int)p0, r1=(int)p1, r2=(int)p2, r3=(int)p3;
    int r4=0, r5=0, r6=0, r7=0, r8=0, r9=0, r10=0, r11=0, r12=0, /*r13=0,*/ r14=0, r15=0;
    int s=0;
#define STACK_DEPTH 20
#define PARAMETER_NUM 4            // number of function parameters
    int stack_array[STACK_DEPTH+PARAMETER_NUM-4];        // parameters after 4th one are stored in the stack
                                                        // stack allocated for these parameters
    int* sp_p=(stack_array+STACK_DEPTH);
    int sp=(int)sp_p;
// sp_p[0]=(int)p4; sp_p[1]=(int)p5;    //...                                // store all parameters after 4th one in the stack
#undef STACK_DEPTH
#undef PARAMETER_NUM
// Replace this function declaration by true declaration from corresponding header file
//int biquad (int p0, int p1, int p2, int p3)
void Biquad_proc( short(*p0/*out*/)[2], short(*p1/*in*/)[2], short* p2/*coeffs*/, int* p3/*state*/, int p4/*n*/)
{
    int r0=(int)p0, r1=(int)p1, r2=(int)p2, r3=(int)p3;
    int r4=0, r5=0, r6=0, r7=0, r8=0, r9=0, r10=0, r11=0, r12=0, /*r13=0,*/ r14=0, r15=0;
    int s=0;
#define STACK_LENGTH 20
#define PARAMETER_NUM 5            // number of function parameters
    int stack_array[STACK_LENGTH+PARAMETER_NUM-4];        // parameters after 4th one are stored in the stack
                                                        // stack allocated for these parameters
    int* sp_p=(stack_array+STACK_LENGTH);
    int sp=(int)sp_p;
    sp_p[0]=p4;// sp_p[1]=p5;    //...                                // store all parameters after 4th one in the stack
#undef STACK_DEPTH
#undef PARAMETER_NUM


4. Doing previous modifications for all functions in the translated code, we will obtain the C code that is equivalent code for the original ARM assembler file. Probably it is looking not elegant, not optimal, but nevertheless, it could be compiled by C compiler.

5. Now we have the entire project written only in C. Don't forget to include ARM_to_C.h file in the resulting project as well. See "Full_Creference_Biquad_Project" directory consisting resulting project.

When somebody would like to improve it or make some comments, welcome. This code is free and with no any guarantee.
It is only oriented to the ARM code written without errors. Not the full set of ARM instructions supported yet.

No boundaries for improvement :-)