I’m working with a ATmega128 microcontroller and supposedly need to add two 16-bit numbers. I’m using AVR Studio and this is what I got so far:
.include "m128def.inc";
.equ ramstart = 0x100
.def temp = r16
.dseg
.org ramstart
number1: .byte 2
number2: .byte 2
.cseg
.org 0
rjmp start
start:
; number1 := 0x7856
ldi temp, low(number1)
sts number1, temp
ldi temp, high(number1)
sts number1+1, temp
; number2 := 0x34B2
lds temp, number1
sts number2, temp
lds temp, number1+1
sts number2+1, temp
slutt:
rjmp slutt
This is not far from the first time I’m using any type of assembly, I know I’m doing something wrong, but can’t seem to figure out what. Am I missing the carry flag?
Back to gradeschool with pencil and paper. If I want to add 1234 and 5678
4+8 is 2 carry the 1
and so on
the carry bit above the ones column is significant, it is called the carry in, and the carry
bit that leaves the leftmost column is carry out.
What if I only had paper wide enough to add two columns at a time?
I start with the two lower sets of digits, and I require a zero as a carry in. I get a result 12 with a carry out.
Now I take that carry out, use it as a carry in for the next two digits. This adder I must be able to take a carry out from a prior add and use it as the carry in for this add.
When all is said and done I get 69 and 12, put those together I get 6912 but didnt need a full 4 digit adder to get there. You can repeat this forever or until you run out of memory, registers or clock cycles.
The avr may have other ways to solve the problem, but most processors at least have two forms of add and two forms of subtract so that you can cascade the adder to be as wide as you need. Examine the instruction set for the avr and what is going on above should jump out at you.
EDIT:
A C example might help…(switching to hex)
EDIT2:
I hope this is not a homework assignment…
result is in r20 high byte, and r21 low byte
if you need to read from ram there are many ways, this assumes the 16 bit numbers are little endian
r0 low half of result, r1 upper half.
or use one of the x,y,or z pointer registers