0

I have a question that begins with this:

.dseg

.org 0x200

value1: .byte 2

value2: .byte 2

res: .byte 2

Im supposed to compute the sum of res = value1 + value2

0x200 value1: 0xCD
0x201         oxAB
0x202 value2: 0x34
0x203         0x12
0x204 res:
0x205

and we assume

ldi XH,high(value1)
ldi XL,low(value1)
ldi YH,high(value2)
ldi YL,low(value2)
ldi ZH,high(res)
ldi ZL,low(res)

Im not sure if this is correct so im trying to get a better understanding of X Y Z and high/low byte. Heres what I did

add XL, YL
adc XH, YH
st zh, xh
st zl, xl

If this is incorrect how do i get the the high and low bytes into res?

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89

1 Answers1

1

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.

Edgar Bonet
  • 3,416
  • 15
  • 18
  • This was not for marks. I think I eventually got the answer after playing around by using the ld to load into registers and then st to store after Adding. Thanks for this explanation it gives more insight to the problem. – user3286125 Mar 03 '15 at 17:10