Last active
January 17, 2022 13:28
-
-
Save fpigerre/4b2819e1a241b1782f12870b68a0474c to your computer and use it in GitHub Desktop.
Common multi-byte operations in Assembly for the Atmel AVR instruction set
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 constants for the ATMega324A micro-controller | |
; This file defines register names and names for I/O ports | |
.include "m324adef.inc" | |
; Attempt to perform operations on multi-byte values | |
; It is assumed the following values are unsigned | |
; First value = r15:r14 = 0xF8AD | |
; Second value = r17:r16 = 0xFFDE | |
START: | |
ldi r19, 0xF8 | |
ldi r18, 0xAD | |
mov r15, r19 ; High byte | |
mov r14, r18 ; Low byte | |
ldi r17, 0xFF ; High byte | |
ldi r16, 0xDE ; Low byte | |
; Multi-byte Two's Complement of r15:r14 | |
clr r18 | |
ldi r19, 0x01 | |
com r14 | |
com r15 | |
add r14, r19 ; Add 1 to lower byte | |
adc r15, r18 ; Add carry value to upper byte | |
; Multi-byte Addition | |
add r14, r16 | |
adc r15, r17 | |
; Set T Flag of status register if overflow occurred during addition | |
; BST Machine Code: 1111 101d dddd 0bbb | |
brvs STATUS | |
jmp CONTINUE | |
STATUS: | |
set ; Sets the T Flag of the status register bit | |
; bset 6 can also be used | |
; The T Flag can also be set using the BST instruction | |
ldi r18, 0x01 | |
; bst uses T <- Rd(b), where b is a particular bit in Rd | |
bst r18, 1 | |
CONTINUE: | |
; Multi-byte Subtraction | |
sub r14, r16 | |
sbc r15, r17 | |
; Multi-byte Multiplication (x2) | |
lsl r14 | |
rol r15 | |
; Multiplication of two 16-bit numbers | |
; 32-bit result is stored in r13:r12:r11:r10 | |
clr r12 | |
clr r13 | |
clr r18 | |
mul r14, r16 | |
movw r11:r10, r1:r0 | |
mul r16, r15 | |
add r11, r0 | |
adc r12, r1 | |
adc r13, r18 | |
mul r14, r17 | |
add r11, r0 | |
adc r12, r1 | |
adc r13, r18 | |
mul r15, r17 | |
add r12, r0 | |
adc r13, r1 | |
; Multi-byte Division (/2) | |
lsr r15 | |
ror r14 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment