-
-
Save Aditya-A-garwal/02befd607d04c05b153ed626157c37b5 to your computer and use it in GitHub Desktop.
Code snippets for Dumblebots AVR Assembly blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
instruction operand1, operand2... ; remarks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
avr-gcc -mmcu=atmega328P -nostdlib -g build/main.s -o build/main.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apt-get install gcc-avr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
avr-ld -o build/main.elf build/main.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
avr-objcopy build/main.elf build/main.hex -O ihex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
avr-gcc -DF_CPU=16000000UL -mmcu=atmega328P -E main.S -o build/main.s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
avr-gcc --target-help |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
avr-gcc --version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
avr-size --format=avr --mcu=atmega328p build/main.elf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
avrdude |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apt-get install avrdude |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
avrdude -c -h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
avrdude -v -p atmega328p -c arduino -P COM1 -b 19200 -U flash:w:build/main.hex:i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
avrdude -v -p atmega328p -c stk500 -P COM1 -b 19200 -U flash:w:build/main.hex:i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MSG: .ASCII "Hello World" | |
MSGEND: .EQU MSGLEN, (MSGEND - MSG) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <avr/io.h> | |
.section .data | |
.section .bss | |
.section .text | |
.org 0x00 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SBI _SFR_IO_ADDR (DDRB), PB5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LDI R16, 0b00100000 | |
LDI R17, 0b00100000 | |
OUT 0x24, R16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LDI R16, 0b00100000 | |
LDI R17, 0b00100000 | |
OUT 0x04, R16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LDI R16, (1<<PB5) | |
LDI R17, (1<<PB5) | |
OUT _SFR_IO_ADDR (DDRB), R16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LOOP: OUT _SFR_IO_ADDR (PORTB), R16 | |
EOR R16, R17 | |
RJMP LOOP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELAY_1S: | |
; some code to perform the delay | |
RET |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LOOP: OUT _SFR_IO_ADDR (PORTB), R16 | |
EOR R16, R17 | |
RCALL DELAY_1S | |
RJMP LOOP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ls /dev/tty* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment