Write assembly, step through it and watch the registers, LEDs and display
This simulator runs assembly programs for the PIC16F84A, the microcontroller that has been used for years to teach programming. It accepts MPASM syntax, counts clock cycles exactly and shows the registers and the state of every pin at all times.
Click a line number to set or clear a breakpoint. The program is saved in this browser.
Pins configured as outputs (TRIS = 0) light their LED. Inputs (TRIS = 1) are push buttons you can press.
| Program memory | 1024 words of 14 bits (flash) |
|---|---|
| Data RAM | 68 bytes (0Ch to 4Fh), plus the special function registers |
| Data EEPROM | 64 bytes |
| Stack | 8 levels, for return addresses only |
| Inputs and outputs | 13: RA0 to RA4 and RB0 to RB7 (RA4 is open drain) |
| Timer | 8-bit TMR0 with a 1:2 to 1:256 prescaler |
| Interrupts | TMR0, RB0/INT, change on RB4 to RB7 and EEPROM write complete |
| Clock | Up to 20 MHz; each instruction cycle is 4 clock periods (1 µs at 4 MHz) |
| Current per pin | 25 mA |
Each instruction takes one word and one instruction cycle, except jumps (GOTO, CALL, returns and conditional skips that are taken), which take two. So, with a known crystal, you can work out exactly how long any piece of code takes.
f is a register, d the destination (W = 0 puts the result in W; F = 1, back in the register), b a bit from 0 to 7 and k an 8-bit literal, or an 11-bit address in GOTO and CALL. Cycles in brackets apply when the skip is taken.
| Instruction | What it does | STATUS bits | Cycles |
|---|---|---|---|
| ADDWF f,d | W + f → d | C, DC, Z | 1 |
| ANDWF f,d | W AND f → d | Z | 1 |
| CLRF f | 0 → f | Z | 1 |
| CLRW | 0 → W | Z | 1 |
| COMF f,d | Complement of f → d | Z | 1 |
| DECF f,d | f − 1 → d | Z | 1 |
| DECFSZ f,d | f − 1 → d; skip next if result is 0 | – | 1 (2) |
| INCF f,d | f + 1 → d | Z | 1 |
| INCFSZ f,d | f + 1 → d; skip next if result is 0 | – | 1 (2) |
| IORWF f,d | W OR f → d | Z | 1 |
| MOVF f,d | f → d (with d = F it only updates Z) | Z | 1 |
| MOVWF f | W → f | – | 1 |
| NOP | No operation | – | 1 |
| RLF f,d | Rotate f left through C | C | 1 |
| RRF f,d | Rotate f right through C | C | 1 |
| SUBWF f,d | f − W → d | C, DC, Z | 1 |
| SWAPF f,d | Swap the two nibbles of f → d | – | 1 |
| XORWF f,d | W XOR f → d | Z | 1 |
| BCF f,b | Clear bit b of f | – | 1 |
| BSF f,b | Set bit b of f | – | 1 |
| BTFSC f,b | Skip next if bit b is 0 | – | 1 (2) |
| BTFSS f,b | Skip next if bit b is 1 | – | 1 (2) |
| ADDLW k | k + W → W | C, DC, Z | 1 |
| ANDLW k | k AND W → W | Z | 1 |
| CALL k | Call a subroutine, saving the return address on the stack | – | 2 |
| CLRWDT | Clear the watchdog timer | TO, PD | 1 |
| GOTO k | Jump to address k | – | 2 |
| IORLW k | k OR W → W | Z | 1 |
| MOVLW k | k → W | – | 1 |
| RETFIE | Return from interrupt and set GIE | – | 2 |
| RETLW k | Return from subroutine with k in W | – | 2 |
| RETURN | Return from subroutine | – | 2 |
| SLEEP | Low power mode until an interrupt or reset | TO, PD | 1 |
| SUBLW k | k − W → W | C, DC, Z | 1 |
| XORLW k | k XOR W → W | Z | 1 |
The registers are split into two banks. Bank 0 holds PORTA, PORTB and TMR0; bank 1 holds, at the same positions, TRISA, TRISB and OPTION_REG. The RP0 bit of STATUS selects the bank: to configure the ports you set RP0 to 1, write TRIS and go back to bank 0. RAM from 0Ch to 4Fh looks the same from both banks. The BANKSEL directive does this switch for you.
In TRIS, a 1 makes the pin an input (I for Input) and a 0 an output (O for Output). At power-up, all pins are inputs.
Without a timer, the simplest way to wait is to repeat a loop a known number of times. In the examples, the inner loop (NOP, NOP, DECFSZ and GOTO) takes 5 cycles and runs 200 times, so with a 4 MHz crystal it lasts 1 ms:
Repeated 250 times it gives 250 ms, and the espera subroutine repeats that as many times as W says. The count is not exact to the microsecond, because the load instructions and the last jump add a few cycles, but the simulator's cycle counter gives you the real duration. With another crystal the same program scales: at 20 MHz it is five times faster.
When an enabled interrupt occurs and GIE is 1, the PIC pushes the current address onto the stack, clears GIE and jumps to address 0004h. The interrupt routine must:
That is why programs with interrupts start with a GOTO at address 0000h: it jumps over the interrupt vector.
Unless told otherwise, MPASM reads numbers as hexadecimal: MOVLW 10 loads 16, not 10. To avoid surprises, always write the format:
| Format | Example | Value |
|---|---|---|
| Decimal | D'25' · .25 | 25 |
| Hexadecimal | H'1F' · 0x1F · 1Fh | 31 |
| Binary | B'00011111' | 31 |
| Character | 'A' | 65 |
Supported directives: LIST (with R=DEC to change the radix), #include <p16F84A.inc>, __CONFIG, ORG, EQU, CBLOCK and ENDC, #define, DT, BANKSEL, RADIX and END. Also the pseudo-instructions MOVFW, TSTF, CLRC, SETC, CLRZ, SETZ, SKPZ, SKPNZ, SKPC, SKPNC, B, BZ, BNZ, BC and BNC. Register and bit names from p16F84A.inc are always available.
Educational simulator by Winding Tools, not affiliated with Microchip. PIC and MPASM are trademarks of Microchip Technology.
Related: dsPIC30F5013 simulator, ladder simulator, number systems, BCD to 7-segment decoder, pull-up and pull-down resistors, push button debouncing and LED resistor.