Bassinator
Bassinator

Reputation: 1724

Suspected error in textbook example - Atmel Assembly

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?

Upvotes: 0

Views: 52

Answers (1)

UncleO
UncleO

Reputation: 8449

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.)

Upvotes: 1

Related Questions