Since this looks a lot like a homework, I am not providing the full
solution as code. But this should give you enough hints to fix the code
yourself.
.dseg
.org 0x200
value1: .byte 2
value2: .byte 2
res: .byte 2
Here, you are defining value1
, value2
and res
as symbols. These
symbols represent the RAM addresses you have just assigned for holding
the variables. When you build your program, the linker will replace any
usage of those symbols by the addresses they represent. In other words,
"value1", "value2" and "res" mean "0x0200", "0x0202" and "0x0204"
respectively.
ldi XH, high(value1)
ldi XL, low(value1)
ldi YH, high(value2)
ldi YL, low(value2)
ldi ZH, high(res)
ldi ZL, low(res)
ldi
(for "load immediate") means "fill this register with the value I
am providing here, in the source code". There is no RAM access involved,
as the value is carried in the instruction itself. Thus, you are filling
the registers X, Y and Z with the values 0x0200
, 0x0202
and
0x0204
, respectively. So far so good.
add XL, YL
Here you are adding the contents of XL (0x00) and YL (0x02), and storing
the result (0x02) back into XL.
adc XH, YH
Here you are doing the same with XH (0x02) and YH (0x02), and adding
also the carry bit (0). The result (0x04) is sored back into XH.
The previous two instructions combined result in adding X (0x0200) and
Y (0x0202) and soring the result (0x0402) back into X. You have added
the addresses of the variables, and the result is hardly meaningful.
What you probably want to do is add the values of the variables, i.e.
the contents of the RAM at those particular addresses. In order to do
so, you first need to load these values from RAM into some CPU
registers, then add the values, then store the sum back into RAM.
For loading from RAM, look at the documentation of the ld
and lds
instructions. Both can be used, in a different way, to solve your
problem. One of them, however, is more in line with the way your
instructor has stated the problem.