My teacher wants us to have a basic understanding of assembly, because later down the road, that might be helpful. I'm fine with that, but my college provides zero documentation on the subject, apart from 4 small examples. I'm trying to figure out what is happening in this one:
.DEVICE ATmega32
.CSEG
main:
ldi ZL,low(varclist<<1)
ldi ZH,high(varclist<<1)
.CSEG
varclist:
.db 1, 2
.db 3, 4
.db 5, 6
.db 7, 8
.db 9, 0
I'm am working in AVR studio 5, and looking at the 'processor' tab. I can't seem to find any relation between the values in 'varclist', and the hexadecimal values the registers are showing. How do i look at this?
I would expect the Z register to contain 0x0100 as i would imagine the 'most-significant' bit to be the first one declared, and the least significant the last declared. I'm seeing 0x0070, however. I'm not sure what the shifting does, though.
To answer your wondering about the shifting, what it does is shift all the bits by one to the left adding a zero as first bit. In other words it multiplies by 2 the high (resp. low) value of the variable varclist
.
varclist
here in your program is the address of the array of bytes [ 0x01, 0x02, ... 0x00 ] in memory => varclist
is a pointer to the first byte of this array. To obtain the original address, we just need to divide by 2 (reverse operation of the multiply) and we obtain
0x0070 >> 1 = 0000 0000 0111 0000 >> 1 = 0000 0000 0011 1000 = 0x0038
So the varclist
address was 0x0038
. I don't know how you thought this was 0x0100 but I hope you now understand the code.
I think this address is multiplied by 2 because addresses to the program storage space are expressed in word offset, in which case you'll need to multiply by 2 to get the address in a byte offset. Later you can load that byte (the first byte of your array) into r0
thanks to the LPM
assembly instruction.
I would recommend you read the chapter 5.2 of this pdf and possibly the other chapters as well. The AVR Assembler User Guide is also probably a good bet.