Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Would Appreciate Help Understanding Some Assembler Code

Status
Not open for further replies.

Brandy

Newbie level 2
Newbie level 2
Joined
Jan 25, 2013
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,301
Hello everyone.
I've started getting into microcontrollers and such as there is a project that I am very interested in doing (you might have seen it on youtube) it involves a dice box that when turned in a certain order pops the lid open via a solenoid.
I found a code for it from the maker but he had to get it off the chip so it's missing any labels and comments. I understand what all (well I should say most) of the assembler functions do but I have a hard time following all of the connections, basically with any new language; I know the vocabulary but have a hard time reading and writing full sentences.
I should note this project involves a PIC16F84 connected to homemade position sensor (6 copper tubes shaped like a cube with metal ball inside) and a solenoid holding a long pin attached to the lid.
If someone could explain sort of the big picture of what the code is doing that would really be helpful, also if you need more info please don't tell me how absurdly stupid I'm being like so many often do on these sites, just explain what else you need and I'll try and explain it.
Code:
        LIST	P=16F84A
	ORG	00H
	DCF	03,5
	MOVLW	07
	MOVWF	19
	BSF	03,5
	CLRF	1F
START_
1	GOTO	0X8
2	NOP
3	NOP
4	NOP
5	MOVF	0X6,F
6	BCF	0Xb,0
7	BCF	0Xb,0X3
8	RETFIE
9	MOVLW	0x10
10	MOVWF	0x4
11	MOVLW	0x16
12	SUBWF	0x4,W
13	BTFSC	0x3,0X2
14	GOTO	0x70
15	INCF	0x3,F	
16	CALL	0x23
17	SUBWF	0x31,F
18	BTFSS	0x3,0x2
19	GOTO	0x18
20	DECFSZ	0x32,F
21	GOTO	0x19
22	DECFSZ	0xe,F
23	GOTO	0x19
24	GOTO	0x51
25	CLRF	0xe
26	MOVWF	0x31
27	SUBWF	0,F
28	BTFSC	0x3,0x2
29	GOTO	0xa
30	DECF	0x4,F
31	SUBWF	0,F
32	BTFSS	0x3,0x2
33	GOTO	0x8
34	INCF	0x4,F
35	GOTO	0xf
36	MOVLW	0x1
37	MOVWF	0x10
38	MOVLW	0x1
39	MOVWF	0x11
40	MOVLW	0x3
41	MOVWF	0x12
42	MOVLW	0x5
43	MOVWF	0x13
44	MOVLW	0x2
45	MOVWF	0x14
46	MOVLW	0x4
47	MOVWF	0x15
48	MOVLW	0x6
49	MOVEWF	0x16
50	BSF	0x3,0x5
51	BCF	0x1,0x7
52	CLRF	0x5
53	MOVLW	0xe0
54	MOVWF	0x5
55	BCF	0x3,0x5
56	MOVLW	0x1f
57	MOVWF	0x5
58	BCF	0x6,0x4
59	BTFSS	0x6,0x5
60	RETLW	0x1
61	BTFSS	0x6,0x6
62	RETLW	0x2
63	BTFSS	0x6,0x7
64	RETLW	0x3
65	BSF	0x3,0x5
66	MOVLW	0xd0
67	MOVWF	0x6
68	BCF	0x3,0x5
69	BCF	0x6,0x5
70	BTFSS	0x6,0x6
71	RETLW	0x4
72	BTFSS	0x6,0x7
73	RETLW	0x6
74	BSF	0x3,0x5
75	MOVLW	0xb0
76	MOVWF	0x6
77	BCF	0x3,0x5
78	BCF	0x6,0x6
79	BTFSS	0x6,0x7
80	RETLW	0x5
81	GOTO	0x23
82	CALL	0x23
83	MOVWF	0xc
84	BSF	0x3,0x5
85	BSF	0x1,0x7
86	CALL	0x60
87	MOVWF	0x6
88	BCF	0x3,0x5
89	MOVF	0xc,W
90	CALL	0x68
91	ANDWF	0x6,F
92	BCF	0xb,0
93	BSF	0xb,0x7
94	BSF	0xb,0x3
95	SLEEP	
96	GOTO	0x8
97	ADDWF	0x2,F
98	NOP	
99	RETLW	0xe0
100	RETLW	0xe0
101	RETLW	0xe0	
102	RETLW	0xd0
103	RETLW	0xb0
104	RETLW	0xd0
105	ADDWF	0x2,F
106	NOP	
107	RETLW	0xef
108	RETLW	0xef
109	RETLW	0xef
110	RETLW	0xdf
111	RETLW	0xbf
112	RETLW	0xdf
113	CLRF	0xc
114	CLRF	0xd
115	MOVLW	0x40
116	MOVWF	0xe
117	DECFSZ	0xc,F
118	GOTO	0x74
119	DECFSZ	0xd,F
120	GOTO	0x74
121	BCF	0x5,0
122	DECFSZ	0xc,F
123	GOTO	0x79
124	DECFSZ	0xe,F
125	GOTO	0x79
126	BSF	0x5,0
127	GOTO	0x8
128	ADDLW	0xff
129	ADDLW	0xff
130	ADDLW	0xff
131	ADDLW	0xff
132	ADDLW	0xff
133	ADDLW	0xff
134	ADDLW	0xff
135	ADDLW	0xff
136	ADDLW	0xff
137	ADDLW	0xff
138	ADDLW	0xff
139	ADDLW	0xff
140	ADDLW	0xff
141	ADDLW	0xff
142	ADDLW	0xff
143	ADDLW	0xff
144	ADDLW	0xff
145	ADDLW	0xff
146	ADDLW	0xff
147	ADDLW	0xff
148	ADDLW	0xff
149	ADDLW	0xff
150	ADDLW	0xff
151	ADDLW	0xff
152	ADDLW	0xff
153	ADDLW	0xff
154	ADDLW	0xff
155	ADDLW	0xff
156	ADDLW	0xff
157	ADDLW	0xff
158	ADDLW	0xff
159	ADDLW	0xff
160	ADDLW	0xff
161	ADDLW	0xff
162	ADDLW	0xff
163	ADDLW	0xff
164	ADDLW	0xff
165	ADDLW	0xff
166	ADDLW	0xff
167	ADDLW	0xff
168	ADDLW	0xff
169	ADDLW	0xff
170	ADDLW	0xff
171	ADDLW	0xff
172	ADDLW	0xff
173	ADDLW	0xff
174	ADDLW	0xff
175	ADDLW	0xff
176	ADDLW	0xff
177	ADDLW	0xff
178	ADDLW	0xff
179	ADDLW	0xff
180	ADDLW	0xff
181	ADDLW	0xff
182	ADDLW	0xff
183	ADDLW	0xff

[/url
 
Last edited:

Hi,

I won't say you are "absurdly stupid" but I will say you are misguided.

Trying to take some disassembled code like that and turn it into something coherent is difficult to say the least.

I would say you have two clear choices, continue to search for some ready made project code that does have the assembly code or sit down and learn to write your own code, it can be very rewarding.

Here are two good tutorial sites covering both assembler and C

http://www.winpicprog.co.uk/pic_tutorial.htm
http://www.gooligum.com.au/tutorials/PIC_Intro_0.pdf
 
  • Like
Reactions: IanP

    IanP

    Points: 2
    Helpful Answer Positive Rating
Reverse engineering code isn't easy but you have two things in your favour. Firstly, it appears to be hand coded, there are some tell-tale signs when high level languages are used and this doesn't seem to have them although it isn't conclusive. Hand coded work is usually easier to unscramble. Secondly, the 16F84A is a relatively simple device and only has a few instructions.

I would note there is no DCF instruction (top of the code), I think this is probably supposed to be BCF.
Also be careful because the addresses in the first column are decimal but in the instructions they are all in hexadecimal. I would re-write the number in the first column in hex before starting to make things easier.

The way I reverse engineer something like this is to print it out on paper then see if obvious blocks of code are present.

1. Look for RET (RETurn) and GOTO instructions which do not have a bit test (BTFSC or BTFSS) immediately before them. These are places where the code has to divert elsewhere so draw a arrow from the address in the instruction to the address in the code. An example of this is at address 24 which has to go to address 0x51 (81 decimal) so draw a line underneath it as this must be the end of a block of code.

2. Look for is a group of consecutive RETLW instructons, as the program flow cannot progress to the next line, these must be a look-up table. Back-track to the top of the RETLWs and you see the classic ADDWF and NOP instructions which are to force a jump into the table. Draw a line above the ADDWF and below the last RETLW as these must also be a block of code. There will be a CALL instruction somewhere that 'lands' on the ADDWF at the top of the block.

3. In this PIC, and in most of the 16F series, the interrupt routine must be at address 0x04 and it must end with a RETFIE instruction so you can mark off that block as being an ISR. In this program there is an ISR but it doesn't appear to be used for anything.

4. Where an instruction refers to a register, change the number to its name. This will make the program much easier to follow. For example at address 10, change the instruction "MOVWF" 0x4 to "MOVWF FSR". You will see references to registers 5 and 6 which are the PORTA and PORTB pins on the IC respectively. This will give you clues to what the pins do in the rest of the device.

5. Finally, the fun bit, almost always when there is a BTFSC or BTFSS instruction there will be a jump to a new address. Draw arrows on the listing to see where they go and it should start to be obvious how it all fits together.

There is no need to worry about addresses 128 onward, they are all empty.

Brian.
 

Thank you both so much for your help, I know this is a weird method for learning but it really does help me (and I enjoy it). I'll be learning to write my own code in the process, so thanks for the tutorials, and betwixt that is extremely helpful thanks :)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top