Finding errors in a section of microprocessor assembly code

In summary, the conversation is about a code written in a specific microprocessor, the 16F84. The code uses a subroutine for delay and involves input and output operations using push buttons. The conversation also includes a discussion about the instruction set, specifically the BTFSS and DECFSZ instructions, and how they work in the code. There is also a mention of a potential issue with the GOTO statement in the delay subroutine.
  • #1
SJC1981
4
1
Hi guys, apparently I've got both a linear and a subroutine programming error in the code below from lines 20-35? Could someone point me in the right direction. I think one might be the 'CALL wait' instruction, which should be 'CALL delay'? Not really sure as new to this, any help would be much appreciated, cheers!

Code:
   ; Slow output binary count is stopped, started
; and reset with push buttons. This version uses a
; subroutine for the delay...
; 
; *******************************************************************
processor 16f84
include <p16f84.inc>
 __config _RC_OSC & _WDT_OFF & _PWRTE_ON
; Register Label Equates.........
porta EQU 05 ; Port A Data Register
portb EQU 06 ; Port B Data Register
timer EQU 0C ; Spare register for delay

 ; Input Bit Label Equates..........
inres EQU 0 ; ‘Reset’ input button = RA0
inrun EQU 1 ; ‘Run’ input button = RA1

; Initialise Port B (Port A defaults to inputs)......
   MOVLW b’00000000’ ; Port B Data Direction Code
   TRIS portb ; Load the DDR code into F86
   GOTO reset

; ‘delay’ subroutine..........
delay MOVWF timer ; Copy W to timer register
down DECFSZ timer ; Decrement timer register
   GOTO reset ; and repeat until zero
   RETURN ; Jump back to main program103

; Start main loop.........
reset CLRF portb ; Clear Port B Data
start BTFSS porta,inres ; Test RA0 input button is set
   GOTO reset ; reset Port B if pressed
   BTFSC porta,inrun ; Test RA1 input button is clear
   GOTO start ; and run count if pressed

   INCF portb ; Increment count at Port B
   MOVLW 0FF ; Delay count literal
   CALL wait ; Jump to delay subroutine
   GOTO start ; Repeat main loop always
END ; Terminate source code

Mentor note:
Here's the original version:
1. ; Slow output binary count is stopped, started
2. ; and reset with push buttons. This version uses a
3. ; subroutine for the delay...
4. ;
5. ; *******************************************************************
6. processor 16f84
7. include <p16f84.inc>
8. __config _RC_OSC & _WDT_OFF & _PWRTE_ON

9. ; Register Label Equates.........
10. porta EQU 05 ; Port A Data Register
11. portb EQU 06 ; Port B Data Register
12. timer EQU 0C ; Spare register for delay

13. ; Input Bit Label Equates..........
14. inres EQU 0 ; ‘Reset’ input button = RA0
15. inrun EQU 1 ; ‘Run’ input button = RA1

16. ; Initialise Port B (Port A defaults to inputs).....
17. MOVLW b’00000000’ ; Port B Data Direction Code
18. TRIS portb ; Load the DDR code into F86
19. GOTO reset

20. ; ‘delay’ subroutine..........
21. delay MOVWF timer ; Copy W to timer register
22. down DECFSZ timer ; Decrement timer register
23. GOTO reset ; and repeat until zero
24. RETURN ; Jump back to main program103

25. ; Start main loop.........
26. reset CLRF portb ; Clear Port B Data
27. start BTFSS porta,inres ; Test RA0 input button is set
28. GOTO reset ; reset Port B if pressed
29. BTFSC porta,inrun ; Test RA1 input button is clear
30. GOTO start ; and run count if pressed
31. INCF portb ; Increment count at Port B
32. MOVLW 0FF ; Delay count literal
33. CALL wait ; Jump to delay subroutine
34. GOTO start ; Repeat main loop always
35. END ; Terminate source code
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Few if any on the forum will be familiar with this particular microprocessor, so you will need to explain some about the instruction set.
At 23 and 28 you have what appear to be unconditional branches, but it looks like they are supposed to be conditional in some way on the preceding statements. How does this work? Do the test statements 22, 27 conditionally skip the next instruction?

It also looks weird that the GOTO at 23 appears to be a branch out of a subroutine, but maybe that's ok. Depends how the stack works.
 
  • #3
haruspex said:
Few if any on the forum will be familiar with this particular microprocessor, so you will need to explain some about the instruction set.
At 23 and 28 you have what appear to be unconditional branches, but it looks like they are supposed to be conditional in some way on the preceding statements. How does this work? Do the test statements 22, 27 conditionally skip the next instruction?

It also looks weird that the GOTO at 23 appears to be a branch out of a subroutine, but maybe that's ok. Depends how the stack works.
This for microchip PIC 16F84 mid-range 8 bit processor. If u download data sheet all instruction sets are on pg 56. BTFSS means 'bit test, skip if set', so 0 means carry out next instruction and 1 means skip it. DECFSZ means contents of register is decremented by one, and if 0 then skip next instruction, carry out if it's a 1. I think ur correct about 23 branching out of a delay subroutine, should probable say GOTO Delay and repeat it rather than say GOTO reset. So that could be the subroutine error. Just struggling now to fine the 'linear' error?
 
Last edited by a moderator:
  • #4
SJC1981 said:
This for microchip PIC 16F84 mid-range 8 bit processor. If u download data sheet all instruction sets are on pg 56. BTFSS means 'bit test, skip if set', so 0 means carry out next instruction and 1 means skip it. DECFSZ means contents of register is decremented by one, and if 0 then skip next instruction, carry out if it's a 1. I think ur correct about 23 branching out of a delay subroutine, should probable say GOTO Delay and repeat it rather than say GOTO reset. So that could be the subroutine error. Just struggling now to fine the 'linear' error?
Please don't use "textspeak" with "u" for "you" and "ur" for "your". Per our rules (see ) this isn't permitted.

I've never heard of "linear error." Is there any documentation for this microprocessor that explains what a linear error is? As far as the subroutine error is concerned, I'm not sure that branching outside of a subroutine is the problem, since a branch just goes to a label, which could be anywhere. However the RETURN statement should execute after a CALL statement. You apparently have a mismatch in your code -- you CALL wait, but RETURN from delay. For the delay subroutine, with its RETURN statement, there should be a CALL delay somewhere.

BTW, I've surrounded your code with BBCode code tags, to (hopefully) make it more readable.
 
  • #5
It's okay I've managed to solve it now, but thanks for your help! It should say CALL delay not CALL wait. Also in the subroutine, it should say GOTO down not reset, so that it can keep going back and decrements to create the delay loop
 

1. How do you identify errors in microprocessor assembly code?

There are several methods for identifying errors in assembly code, such as using a debugger or a simulator to step through the code and track the values of registers and memory locations. Additionally, manual inspection and testing can also help identify errors.

2. What are some common errors found in microprocessor assembly code?

Some common errors in assembly code include incorrect syntax, incorrect use of registers or memory addresses, and logical errors such as infinite loops or incorrect conditional branching.

3. How can you prevent errors in microprocessor assembly code?

To prevent errors in assembly code, it is important to follow coding standards and best practices, use comments to make the code more understandable, and thoroughly test the code before deployment. Also, regularly reviewing and debugging the code can help catch and correct errors early on.

4. Can errors in assembly code cause malfunctions in the microprocessor?

Yes, errors in assembly code can cause malfunctions in the microprocessor. These errors can lead to unexpected behavior, crashes, or even damage to the microprocessor if incorrect instructions are executed.

5. How can you troubleshoot errors in microprocessor assembly code?

To troubleshoot errors in assembly code, you can use debugging tools, run test cases, and consult documentation or online resources for guidance. It can also be helpful to work with a team and collaborate to identify and resolve errors.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
13K
  • Electrical Engineering
Replies
10
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
Back
Top