<aside> <img src="/icons/map-pin_gray.svg" alt="/icons/map-pin_gray.svg" width="40px" /> Relevant Notes/Resources:

4 Advanced Assembly Programming

Registers (Page 1)

</aside>



1. Filling in the Blank

For assembly language programming, you will typically be programming a finite state machine — usually ranging from 2 to 4 states. The structure of the code more-so remains the same given the different variations.

It begins by initializing the input and output ports.

state     ds.b     1                     ; reserve a byte for the state variable
          movb     #$00,DDRP             ; configure pin 0 of Port P (PTP) as input pin
          movb     #$FF,DDRB             ; configure Port B (PTB) as output port

States

Each state pretty much follows the same format:

  1. Set the current state in state variable.
  2. Update the display by branching to display subroutine.
  3. Check pin 0 of Port P if it reads a 0 or 1.
    1. Use brset or brclr to branch to the next state.
    2. Otherwise, remain in the same state.
SA        movb     #$00,state            ; the current is state A
					bsr      display               ; display the number that represent the state
          brset    PTP,$01,SB            ; switch to state B
					bra      SA                    ; remain in state A

SB        movb     #$01,state            ; the current is state B
					bsr      display               ; display the number that represent the state
          brclr    PTP,$01,SC            ; switch to state C
					bra      SB                    ; remain in state B

SC        movb     #$02,state            ; the current is state C
					bsr      display               ; display the number that represent the state
          brset    PTP,$01,SD            ; switch to state D
					bra      SC                    ; remain in state C

SD        movb     #$03,state            ; the current is state D
					bsr      display               ; display the number that represent the state
          brclr    PTP,$01,SA            ; switch to state A
					bra      SD                    ; remain in state D

Display

The display then checks the current state which we stored in state variable. A table is used to store the different seven-segment displays for each state.

  1. Load the state variable is stored in acc. B. Remember that we set $00 for state A, $01 for state B, and etc.