This code comes as an example for a first blinking program. It does work on my board, however I don't understand why. I don't understand why sbi PINB, 5 ; Toggle PINB should toggle the pin instead of just turning it on. I am a total newb, so please be gentle.
#define __SFR_OFFSET 0
#include "avr/io.h"
.global main
main:
sbi DDRB, 5 ; Set PB5 as output
blink:
sbi PINB, 5 ; Toggle PINB
ldi r25, hi8(500)
ldi r24, lo8(500)
call delay_ms
jmp blink
delay_ms:
; Delay about (r25:r24)*ms. Clobbers r30, and r31.
; One millisecond is about 16000 cycles at 16MHz.
; The inner loop takes 4 cycles, so we repeat it 3000 times
ldi r31, hi8(4000)
ldi r30, lo8(4000)
inner:
sbiw r30, 1
brne inner
sbiw r24, 1
brne delay_ms
ret
But isn't sbi just setting the bit? I think that that is where OP's confusion comes from. I did not look at the hardware implementation so it might toggle in hardware.
Where did you find the example? Please give a link.
As noted above, that's how the particular AVR hardware works.
As an example, the author really should have explained that - it's a bit of a "quirk" of these AVRs.
When programming in assembler, you directly affect the internal HW of the MCU. To understand what happens, you have to understand how this HW works. The main documentation for this is the datasheet of the MCU. But this is not easy reading.
I think these are not the best prerequisites to dive deep into the MCU hardware already