Close

Pong in ASM (Display "something")

A project log for AVR Asm Exam

Doing the "exam" of the AVR assembly course

michael-mllerMichael Möller 02/27/2021 at 00:100 Comments

After all those "getting to know the lay of the land", I am ready to write for final product. I did this a few hours after the last class. It, too, was not without a "stupid" mistake.

Like the C code, we first put something on the 8x16 display. This runs on the simulator.

;;; "PONG" in AVR assembly

; V0.1 - turn on something on the LED array
;      - requires sending two SPI bytes (four to get to the 2nd half)

#define __SFR_OFFSET 0 // makes "PORTB" etc fit the IN/OUT Port, instead of the memory equivalent
#include <avr/io.h>    // Get the "basic" symbolic names for registers
.global main           ; Interface to the Arduino/C startup
                       ; (therefore?) stackpointer and vector table initialized

CSS = PB2               ; Arduino pin 10 (portB, pin 2) is the CSS line

;====== MAIN, START HERE ====
main:

   cli                  ; we live without interrupts
  
   ; ---- Setup SPI mode
   ldi  r16,0B00101100  ; bit 2, 3 and 5 for Arduino pins 10, 11 and 13 as OUTPUT
   out  DDRB,r16        ;   the rest as INPUT
   ldi  r16,0B00000100  ; Bit 2/pin 10
   out  PORTB,r16       ;   HIGH
   ldi  r16,0B01010001  ; Bit 0 enable SPI, bit 5 to ?, bit 7 to ?
   out  SPCR,r16        ; Set these on the SPI control register

   ; ---- Push two bytes to SPI, bracket by LOW pin 10
   cbi  PORTB,CSS       ; Trigger a "new transaction" on MAX7219 (pin 10 LOW)
   ldi  r17,5           ; 5th row
   out  SPDR,r17        ; send the first byte - put it in SPI data register
1: in   r18,SPSR        ; Read SPI status register 
   sbrs r18,SPIF        ; was the done bit set ?
   rjmp 1b              ; nope, keep waiting

   ldi  r17,0B10101010  ; LED pattern
   out  SPDR,r17        ; ditto 2nd byte
1: in   r18,SPSR        ; Read SPI status register 
   sbrs r18,SPIF        ; was the done bit set ?
   rjmp 1b              ; nope, keep waiting

   sbi  PORTB,CSS       ; pin 10 HIGH again

loop:
  ; ==== Loop ====
  nop
  rjmp   loop      ; We do nothing, really fast :-)

I got lost in the address mode (memorymapped vs Port#) and symbolic names, as I did want to use plain numbers for everything. For a long while everything was right but nothing came up on the display. I took a hint by reading the disassembly from the working C code, and discovered I had forgotten to toggle the pin10/CSS line.

I have this sinking feeling that debugging in assembly is going to be hard. Actually, I used to earn my living doing assembly programming between '77 and '82 (yeah, I am not that young) so I am simply rediscovering old skills. 

Onwards to doing the bouncing ball

Discussions