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

Thread Starter

mkbutan

Joined Sep 30, 2008
299
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 :-


Rich (BB code):
$mod52	        	
var2	equ 13h
lcd	equ P2		        	
buzz equ p3.4
rs equ p3.7
rw equ p3.6	
e  equ p3.5

org 00H						
main:	clr rw
	mov p2,#00H 			
	mov p1,#0fh
	acall lcdin				

start:
		mov a ,p1
		cjne a,#01h,skip1
		acall m25;

	skip1:	mov a ,p1
		cjne a,#03h,skip2
		acall m50;
		sjmp start

	skip2:	mov a ,p1
		cjne a,#07h,skip3
		acall m75;
		sjmp start

	skip3:	mov a ,p1
		cjne a,#0fh,skip4
		acall m100;
		sjmp start

	skip4:	mov a ,p1
		cjne a,#00h,skip5
		acall low1
		sjmp start

	skip5:	sjmp start;

m25:		mov a,#0c0h
		acall cmm
		mov dptr,#mydata2
		mov var2,#15d
	bck2:	clr a
		movc a,@a+dptr
		acall dat
		inc dptr
		djnz var2,bck2	 
		ret


m50:		mov a,#0c0h
		acall cmm
		mov dptr,#mydata3
		mov var2,#15d
	bck3:	clr a
		movc a,@a+dptr
		acall dat
		inc dptr
		djnz var2,bck3	 
		ret;


m75:		mov a,#0c0h
		acall cmm
		mov dptr,#mydata4
		mov var2,#15d
	bck4:	clr a
		movc a,@a+dptr
		acall dat
		inc dptr
		djnz var2,bck4	 
		ret


m100:		mov a,#0c0h
		acall cmm
		mov dptr,#mydata5
		mov var2,#16d
		setb buzz
	bck5:	clr a
		movc a,@a+dptr
		acall dat
		inc dptr
		djnz var2,bck5	 
		acall delay
		acall delay
		acall delay
		clr buzz
		ret

low1:		mov a,#0c0h
		acall cmm
		mov dptr,#mydata6
		mov var2,#15d
		setb buzz
	bck6:	clr a
		movc a,@a+dptr
		acall dat
		inc dptr
		djnz var2,bck6	 
		acall delay
		clr buzz
		ret

lcdin:
	mov a,#38h
	acall cmm
	mov a,#0ch
	acall cmm
	mov a,#01h
	acall cmm
	mov a,#06h
	acall cmm
	mov a,#86h
	acall cmm
	mov dptr,#mydata1
	mov var2,#3
bck1:	clr a
	movc a,@a+dptr
	acall dat
	inc dptr
	djnz var2,bck1
	ret	
cmm:
	mov lcd,a
	clr rs
	setb e
	acall delay
	clr e
	acall delay
	ret
dat:
	mov lcd,a
	setb rs
	setb e
	acall delay
	clr e
	acall delay
	ret
delay:
	mov r1,#50
ro1:	mov r2,#0ffh
ro2:	djnz r2,ro2
	djnz r1,ro1
	ret;
org 300
mydata1: db 'EFY'
mydata2: db 'Water level 25%' 
mydata3: db 'Water level 50%'
mydata4: db 'Water level 75%'
mydata5: db 'Water level 100%'
mydata6: db 'Water level low'
END						;End of program

pl any one help
thanks in advance
 

Attachments

Last edited by a moderator:

MrChips

Joined Oct 2, 2009
30,824
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:


Rich (BB code):
main: clr rw
mov p2,#00H
mov p1,#0fh
acall lcdin
translated to C:

Rich (BB code):
uint8_t rw, p1,p2;

  rw = 0;
  p2 = 0;
  p1 = 0x0F;
  lcdin();
 

Thread Starter

mkbutan

Joined Sep 30, 2008
299
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?
 

vpoko

Joined Jan 5, 2012
267
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).
 

RiJoRI

Joined Aug 15, 2007
536
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:
Rich (BB code):
void main(void)
{
  unsigned char Va;

    do{
        Va++;
    }while(1);
}
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
Rich (BB code):
start: mov a ,p1
cjne a,#01h,skip1
acall m25
Make it look like:
Rich (BB code):
start:
	mov a ,p1
	cjne a,#01h,skip1
	acall m25
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
 

K7GUH

Joined Jan 28, 2011
190
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.
 

MrChips

Joined Oct 2, 2009
30,824
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.
 

Thread Starter

mkbutan

Joined Sep 30, 2008
299
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?
 

t06afre

Joined May 11, 2009
5,934
Can we convert that HEX => Binary code's to Binary => "C" code's.
if yes how?
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 ;)
 

MrChips

Joined Oct 2, 2009
30,824
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.
 

Thread Starter

mkbutan

Joined Sep 30, 2008
299
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
 

K7GUH

Joined Jan 28, 2011
190
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.
 

maxpower097

Joined Feb 20, 2009
816
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.
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. :)
 

ajm113

Joined Feb 19, 2011
174
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:

maxpower097

Joined Feb 20, 2009
816
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.
 
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 :-


Rich (BB code):
$mod52               
var2    equ 13h
lcd    equ P2                   
buzz equ p3.4
rs equ p3.7
rw equ p3.6   
e  equ p3.5

org 00H                       
main:    clr rw
    mov p2,#00H            
    mov p1,#0fh
    acall lcdin               

start:
        mov a ,p1
        cjne a,#01h,skip1
        acall m25;

    skip1:    mov a ,p1
        cjne a,#03h,skip2
        acall m50;
        sjmp start

    skip2:    mov a ,p1
        cjne a,#07h,skip3
        acall m75;
        sjmp start

    skip3:    mov a ,p1
        cjne a,#0fh,skip4
        acall m100;
        sjmp start

    skip4:    mov a ,p1
        cjne a,#00h,skip5
        acall low1
        sjmp start

    skip5:    sjmp start;

m25:        mov a,#0c0h
        acall cmm
        mov dptr,#mydata2
        mov var2,#15d
    bck2:    clr a
        movc a,@a+dptr
        acall dat
        inc dptr
        djnz var2,bck2    
        ret


m50:        mov a,#0c0h
        acall cmm
        mov dptr,#mydata3
        mov var2,#15d
    bck3:    clr a
        movc a,@a+dptr
        acall dat
        inc dptr
        djnz var2,bck3    
        ret;


m75:        mov a,#0c0h
        acall cmm
        mov dptr,#mydata4
        mov var2,#15d
    bck4:    clr a
        movc a,@a+dptr
        acall dat
        inc dptr
        djnz var2,bck4    
        ret


m100:        mov a,#0c0h
        acall cmm
        mov dptr,#mydata5
        mov var2,#16d
        setb buzz
    bck5:    clr a
        movc a,@a+dptr
        acall dat
        inc dptr
        djnz var2,bck5    
        acall delay
        acall delay
        acall delay
        clr buzz
        ret

low1:        mov a,#0c0h
        acall cmm
        mov dptr,#mydata6
        mov var2,#15d
        setb buzz
    bck6:    clr a
        movc a,@a+dptr
        acall dat
        inc dptr
        djnz var2,bck6    
        acall delay
        clr buzz
        ret

lcdin:
    mov a,#38h
    acall cmm
    mov a,#0ch
    acall cmm
    mov a,#01h
    acall cmm
    mov a,#06h
    acall cmm
    mov a,#86h
    acall cmm
    mov dptr,#mydata1
    mov var2,#3
bck1:    clr a
    movc a,@a+dptr
    acall dat
    inc dptr
    djnz var2,bck1
    ret   
cmm:
    mov lcd,a
    clr rs
    setb e
    acall delay
    clr e
    acall delay
    ret
dat:
    mov lcd,a
    setb rs
    setb e
    acall delay
    clr e
    acall delay
    ret
delay:
    mov r1,#50
ro1:    mov r2,#0ffh
ro2:    djnz r2,ro2
    djnz r1,ro1
    ret;
org 300
mydata1: db 'EFY'
mydata2: db 'Water level 25%'
mydata3: db 'Water level 50%'
mydata4: db 'Water level 75%'
mydata5: db 'Water level 100%'
mydata6: db 'Water level low'
END                        ;End of program

pl any one help
thanks in advance




dear friend,

you can compile this program by using "mcs51asmide" compiler, and you can generate hex files, we are always doing this same stc ic
 
Top