[Education] Demystifying Microcontrollers: Using ATmega328P and Assembly to Understand Registers

not want to try to modify for insert some custom instructions? i think that this assembler have already some custom functions!

but 1001_1010_AAAA_Abbb what value return?

sts is 16 bit like jmp?

with this table i think that is some easy to create your own compiler, i try it but in another manner!

i see that some users of this thread intervent in this thread but none did also me to intervent with asm!:grinning_face:

Sorry, I don't understand. STS has 32-bit Opcode. No matter what, the byte at the address 0x20 stays 0x00. 0x20 is the first byte in 64 I/O register space and it is reserved.
Of course, as I wrote, it was in Atmel Studio, not real chip.

you are right ,i want did 32bit same as image

but you simulate ? not have a real atmega328 ? arduino nano/uno ?

Yes, it was simulation. I don't have anything usable with 328P right now.
Almel Studio is pretty accurate.
You try it. What's the worst that can happen?

Nothing!

I put also blink to d13 to verify if mcu is not blocking!

But not! Led blinking , seem that mcu ignore sbi cbi 2,5

Certainly,D13 in this mode is with pullup , but i can see led blinking!

And disassemble:

??

because really i ask myself because "reserved" !

YESSSSSS!!!!!:grinning_face:

This is atmega2560 https://ww1.microchip.com/downloads/en/devicedoc/atmel-2549-8-bit-avr-microcontroller-atmega640-1280-1281-2560-2561_datasheet.pdf

So the ATmega328 follows the same design approach as the CH32V001, where a unified register map reserves spaces for missing GPIO ports on lower pin-count packages.

Also ch32v001 is interesting to study in asm , and write a uploader based on webusb i understand that is some sample , but problem is a assembler in js because have more instructions.

Anyway , i did , first i begin to write by hand a avr assembler (without help of ai) but after days of time spend i find avrlass.js e avrdass.js that was ready to use , so i not continue the tool Create and upload Arduino hex file directly from a web page

This first tool only these instructions accepted (i stop here because i find avrlass.js):

					case 'LDI':	code.push(1,1);break;
					case 'OUT':	code.push(1,1);break;
					case 'RJMP':code.push(1,1);break;
					case 'BRNE':code.push(1,1);break;
					case 'DEC':code.push(1,1);break;
					case 'INC':code.push(1,1);break;
					case 'EOR':code.push(1,1);break;
					case 'RET':code.push(1,1);break;
					case 'RCALL':code.push(1,1);break;
					case 'CBI':code.push(1,1);break;
					case 'SBI':code.push(1,1);break;
					case 'SBIC':code.push(1,1);break;
					case 'SBRC':code.push(1,1);break;
					
					case 'NOP':code.push(1,1);break;

My m328p board returns mostly 0xB8 from PORTA and DDRA, no matter what is written to them....


#define PINA _SFR_IO8(0x00)
#define DDRA _SFR_IO8(0x01)
#define PORTA _SFR_IO8(0x02)

void setup() {
  Serial.begin(115200);
  Serial.println("Test (non-existant) Port A registers");
  for (int i = 0; i < 256; i++) {
    DDRA = i;
    PORTA = i;
    Serial.print("i=0x"); Serial.print(i, HEX);
    Serial.print("  DDRA=0x"); Serial.print((unsigned int)DDRA, HEX);
    Serial.print("  PORTA=0x"); Serial.println((unsigned int)PORTA, HEX);
    delay(500);
  }
}

void loop() {}

says:

Test (non-existant) Port A registers
i=0x0  DDRA=0xB8  PORTA=0xB8
i=0x1  DDRA=0xB8  PORTA=0xB8
i=0x2  DDRA=0xB8  PORTA=0x2
i=0x3  DDRA=0xB8  PORTA=0xB8
i=0x4  DDRA=0xB8  PORTA=0xB8
i=0x5  DDRA=0xB8  PORTA=0xB8
i=0x6  DDRA=0xB8  PORTA=0x2
i=0x7  DDRA=0xB8  PORTA=0xB8
i=0x8  DDRA=0xB8  PORTA=0xB8
i=0x9  DDRA=0xB8  PORTA=0xB8
i=0xA  DDRA=0xB8  PORTA=0xB8
i=0xB  DDRA=0xB8  PORTA=0xB8
i=0xC  DDRA=0xB8  PORTA=0xB8
i=0xD  DDRA=0xB8  PORTA=0xB8
i=0xE  DDRA=0xB8  PORTA=0x2
i=0xF  DDRA=0xB8  PORTA=0xB8
i=0x10  DDRA=0xB8  PORTA=0xB8
(all remaining values yield B8)

It'd probably be "interesting" to see if the results are the same on other 28pin AVRs....

I tried your sketch, results similar, irregularities similar.
I modified your sketch and ran it:

#define PINA _SFR_IO8(0x00)
#define DDRA _SFR_IO8(0x01)
#define PORTA _SFR_IO8(0x02)

void setup() {
  Serial.begin(115200);
  Serial.println("Test (non-existant) Port A registers");
  for (int i = 0; i < 256; i++) {
    DDRA = i;
    PORTA = i;
    uint8_t pinA = PINA;
    uint8_t ddrA = DDRB;
    uint8_t portA = PORTA;
    Serial.print("i=0x"); Serial.print(i, HEX);
    Serial.print("  PINA=0x"); Serial.print(pinA, HEX);
    Serial.print("  DDRA=0x"); Serial.print(ddrA, HEX);
    Serial.print("  PORTA=0x"); Serial.println(portA, HEX);
    delay(100);
  }
}

void loop() {}
22:26:07.342 -> Test (non-existant) Port A registers
22:26:07.342 -> i=0x0  PINA=0xB8  DDRA=0x0  PORTA=0x0
22:26:07.444 -> i=0x1  PINA=0x6  DDRA=0x0  PORTA=0x0
22:26:07.560 -> i=0x2  PINA=0x6  DDRA=0x0  PORTA=0x0
22:26:07.663 -> i=0x3  PINA=0x6  DDRA=0x0  PORTA=0x0
22:26:07.732 -> i=0x4  PINA=0x6  DDRA=0x0  PORTA=0x0
22:26:07.857 -> i=0x5  PINA=0x6  DDRA=0x0  PORTA=0x0

without further irregularities.

I speculate that the databus has pulldown resistors and all pripherials have an open drain output.
The databus lines, connecting all peripherials, will have significant parasitic capacitance.
Suppose the RC-time is just not short enough. The 0xB8 may be the last value on the databus after Serial.print() and other irregularities may be remnants from the millis()interrupt.

Still, I have the feeling there is more to it.

For they that have 100% confident ai:

2. sbi and cbi instructions:

sbi 2,5 → Set Bit in I/O register: sets to 1 bit 5 of the I/O register at address 2.

Address 2 = PORTB (on ATmega328P)

Bit 5 = pin 13 (Arduino built-in LED)

sbi 5,5 → Sets bit 5 of the I/O register at address 5.

Address 5 = PORTD (on ATmega328P)

Bit 5 = pin 5 of Arduino (if used with an external LED)

You still trust AI?
You'd better read the datasheet.

No... I put this for those who say I write Assembly with AI.

Perhaps for C++ it's easier, but for ASM it's a battle with AI. It's not easy to write pure ASM with AI.

Anyway, lately I see AI is more friendly with sbi 5,5. Perhaps also for my posts. I wrote many months of tutorials with sbi 5,5, but a long time ago it was a battle with AI that didn't accept sbi 5,5, but only PORTB and PB5!

Before, AI used to insist that Assembly is useless and too complicated, that it's not even worth trying! But since I created my AVR tool and many tutorials, it seems that AI's mindset is finally changing a bit!

No... i put this for them that did that i use ai for create asm code!

Perhaps for c++ is more easy , but for asm is a battle with ai ,is not easy to write pure asm with ai.

Anyway ,ultimate i see ai more friendly with sbi 5,5 perhaps also for my posts, i write many months tutorial with sbi 5,5 , but more time ago was a battle with ai that not accepted sbi 5,5, but PORTB and PB5 only!

Okay. Now you're just making things up.

@EmilyJane ask ai for "costycnc avr tool"

im curious what did ai!:grinning_face:

I
It says " the underlying idea is excellent, but I would use the present version cautiously because some instructional material contains significant technical errors."
Huh!, my impression of AI has changed. It's more intuitive than I gave it credit for.

If it doesn't cite which page, which instruction, which register, and what the error is, the phrase “significant technical errors” is basically an accusation without evidence.

I'm paying the subscription for my AI. Why would I give you advice for free?

Anyway ,if your payed professional AI (CLAUDE?) did only "some" , i can to go to sleep reassured! For a carpenter is more that enough!:wink:

Yes, we're well aware of your low standards of excellence. :smiling_face_with_sunglasses: