Skip to content

Instantly share code, notes, and snippets.

@Aditya-A-garwal
Last active July 29, 2022 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aditya-A-garwal/02befd607d04c05b153ed626157c37b5 to your computer and use it in GitHub Desktop.
Save Aditya-A-garwal/02befd607d04c05b153ed626157c37b5 to your computer and use it in GitHub Desktop.
Code snippets for Dumblebots AVR Assembly blog
instruction operand1, operand2... ; remarks
avr-gcc -mmcu=atmega328P -nostdlib -g build/main.s -o build/main.o
apt-get install gcc-avr
avr-objcopy build/main.elf build/main.hex -O ihex
avr-gcc -DF_CPU=16000000UL -mmcu=atmega328P -E main.S -o build/main.s
avr-gcc --target-help
avr-gcc --version
avr-size --format=avr --mcu=atmega328p build/main.elf
apt-get install avrdude
avrdude -v -p atmega328p -c arduino -P COM1 -b 19200 -U flash:w:build/main.hex:i
avrdude -v -p atmega328p -c stk500 -P COM1 -b 19200 -U flash:w:build/main.hex:i
LDI R16, 2 ; load the constant 2 to register 16
LDI R17, 3 ; load the constant 3 to register 17
ADD R16, R17 ; add the values and store the sum in register 16
ANDI R16, 1 ; perform logical and with constant 1 (only preserved bit 0)
ADD R16, R16 ; left shift the value once (bit 0 is moved to bit 1)
SBI _SFR_IO_ADDR (DDRB), 1 ; put pin 1 of port B in output mode
OUT _SFR_IO_ADDR (PORTB), R16 ; write out the value of R16 to port B
; some code before the loop
LDI R16, 5 ; load the constant 5 to register 16
_loopstart:
; some code within the loop
DEC R16 ; decrement the value in register 16
BRNE _loopstart ; if the result of the previous operation was not 0, jump to _loopstart
_loopend:
; some code after the loop
; some code before the loop
LDI R16, 5 ; load the constant 5 to register 16
_loopstart: ; some code within the loop
DEC R16 ; decrement the value in register 16
BRNE _loopstart ; jump to _loopstart if the previous result was not 0
_loopend:
; some code after the loop
.global _loopstart
.global _loopend
; some code before the loop
LDI R16, 5 ; load the constant 5 to register 16
_loopstart: ; some code within the loop
DEC R16 ; decrement the value in register 16
BRNE _loopstart ; jump to _loopstart if the previous result was not 0
_loopend:
; some code after the loop
MSG: .ASCII "Hello World"
MSGEND: .EQU MSGLEN, (MSGEND - MSG)
.section .data
.section .bss
.section .text
.org 0x00
LDI R16, 2 ; load the constant 2 to register 16
LDI R17, 3 ; load the constant 3 to register 17
ADD R16, R17 ; add the values and store the sum in register 16
ANDI R16, 1 ; perform logical and with constant 1 (only preserves bit 0)
ADD R16, R16 ; left shift the value once (bit 0 is moved to bit 1)
SBI _SFR_IO_ADDR (DDRB), 1 ; put pin 1 on port b in output mode
OUT _SFR_IO_ADDR (PORB), R16 ; write out the value of R16 to port B
#include <avr/io.h>
.section .data
.section .bss
.section .text
.org 0x00
SBI _SFR_IO_ADDR (DDRB), PB5
LDI R16, 0b00100000
LDI R17, 0b00100000
OUT 0x24, R16
LDI R16, 0b00100000
LDI R17, 0b00100000
OUT 0x04, R16
LDI R16, (1<<PB5)
LDI R17, (1<<PB5)
OUT _SFR_IO_ADDR (DDRB), R16
LOOP: OUT _SFR_IO_ADDR (PORTB), R16
EOR R16, R17
RJMP LOOP
DELAY_1S:
; some code to perform the delay
RET
LDI R20, 255 ; 1 cycle (IGNORED)
LOOP: DEC R20 ; 1 cycle * 255 = 255 cycles
NOP ; 1 cycle * 255 = 255 cycles
BRNE LOOP ; 2 cycles * 255 = 510 cycles
; -1 cycle on last iteration (IGNORED)
DELAY_1S:
LDI R20, 64 ; 1 cycle * 1 = 1 cycle (IGNORED)
DELAY1: LDI R21, 250 ; 1 cycle * 64 = 64 cycles
DELAY2: LDI R22, 250 ; 1 cycle * 16,000 = 16,000 cycles
DELAY3: DEC R22 ; 1 cycle * 4e6 = 4e6 cycles
NOP ; 1 cycle * 4e6 = 4e6 cycles
BRNE DELAY3 ; 2 cycles * 4e6 = 8e6 cycles
; -1 cycle * 16,000 = -16,000 cycles
DEC r21 ; 1 cycle * 16,000 = 16,000 cycles
BRNE DELAY2 ; 2 cycles * 16,000 = 32,000 cycles
; -1 cycle * 64 = -64 cycles
DEC r20 ; 1 cycle * 64 = 64 cycles
BRNE DELAY1 ; 2 cycle * 64 = 128 cycles
; -1 cycle * 64 = -64 cycles
RET
LOOP: OUT _SFR_IO_ADDR (PORTB), R16
EOR R16, R17
RCALL DELAY_1S
RJMP LOOP
#include <avr/io.h>
.section .data
.section .bss
.section .text
.org 0x00
LDI R16, (1<<PB5)
LDI R17, (1<<PB5)
OUT _SFR_IO_ADDR (DDRB), R16
LOOP: OUT _SFR_IO_ADDR (PORTB), R16
RCALL DELAY_1S
EOR R16, R17
RJMP LOOP
DELAY_1S:
LDI R20, 64
DELAY1: LDI R21, 250
DELAY2: LDI R22, 250
DELAY3: DEC R22
NOP
BRNE DELAY3
DEC r21
BRNE DELAY2
DEC r20
BRNE DELAY1
RET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment