0

So, I'm learning Atmel AVR assembly from Huang's textbook. There is the following example in the book:

// Subtract 10 from mem(0x2000)

ldi  XL,        0x00        ; Load mem(0x2000) into X
ldi  XH,        0x20        ;
ld   r0,        X           ; Load the value into r0
sbi  r0,        10          ; Subtract 10 from r0.
st    X,        r0          ; Store the result back in mem(0x2000)

Isn't this incorrect? Shouldn't line 4 actually be subi, not sbi.

The documentation for sbi reads:

Description:
Sets a specified bit in an I/O register. This instruction operates on the 
lower 32 I/O registers - addresses 0-31.

That doesn't seem at all related to what the example is trying to do. Did I miss something, or should I notify the publisher?

Bassinator
  • 1,682
  • 3
  • 23
  • 50
  • Quite right - and XL and XH are parts of X, not Y (the comment is misleading). Someone mixed things up. – Yann Vernier Aug 23 '17 at 22:20
  • It was originally Y in my solution to the example, but I changed to to reflect the book's example solution (which used X). I guess I missed the comment. Fixing now – Bassinator Aug 23 '17 at 22:22

1 Answers1

1

The instruction should be subi r0, 10, or "subtract immediate" to subtract the value 10 from register r0.

All the immediate address instructions refer to the number literally in the instruction, as opposed to other addressing modes that refer to registers, or offset from an address stored in an index register such as X or Y, etc.

For example, ld r0, X loads the value stored at the address stored in X, here 0x2000. (It doesn't load 0x2000 into r0.)

UncleO
  • 8,299
  • 21
  • 29