user1753100
user1753100

Reputation: 1979

Assembly understanding ldi for high and low bytes

I am having trouble understanding what the code fragment below loads into each register. What will each register store after the code is executed?

ldi r20, low(-1)
ldi r21, high(-1)
ldi r17, low(0x600)
ldi r18, high(0x600)

EDIT: Fixed my markdown, sorry about that.

Upvotes: 1

Views: 9493

Answers (1)

Greg
Greg

Reputation: 350

Ok, so since this is AVR Assembly, and assuming 8 bit registers (like an Atmega32 or something similar, judging by the register names).

First, let's take a look at what low() and high() do in AVR Assembly. According to this source and from personal experience, it only works with 16-bit numbers, and gives either the upper byte or the lower byte, going most-significant-bit(MSB) on the left.

-1 as a 16-bit number = 0b1111111111111111 or 0xFFFF (both are equal), since negatives are calculated using the 2's complement, so taking the low() and high() of each should yield the following:

ldi r20, low(0b1111111111111111)
ldi r21, high(0xFFFF)

r20 and r21 will both hold the value of 0b11111111, or -1 in decimal notation

As for the others, 0x600, let's first show it as a full 16-bit number to make it easier. 0x600 == 0x0600 (just throw a 0 in the MSB spot, since you aren't actually adding anything)

If we take high(0x0600), we get the top two numbers, the upper byte, or 0x06.
If we take low(0x0600), we get the bottom two numbers, the lower byte, or 0x00.

Upvotes: 2

Related Questions