Can someone help translate this traffic light system to asm volatile?

Hello! I have a traffic light system that i made in the arduino IDE that works perfectly fine! and i was interested into turning the code into assembly!
here's the normal C code :
https://www.codedump.xyz/coffeescript/Ynab43uR_k7DhLpD
and here is the assembly code i have attempted
https://www.codedump.xyz/cpp/YnlQQQCCnmEmxKWX

I'm struggling to keep the amber light on whilst the green one is on

thanks!

Why not use the assembly file that is output from the Arduino C compile?

1 Like

I have tried that and it gave me 100's of lines of code which didnt seem correct and when i tried to run it, it had a bunch of errors. Unless i did it completely wrong do you know?

Certainly will depend on the assembler you used! And what microprocessor you selected for both the C and the assembler.

hmmm, whats your best method of doing this? sorry I'm quite new to this

I only, ever, tried it once, back in about 1988. I was going to write data communication software for PCs using 80386 and 80486. I had to use assembly for speed, I thought. When I began in C and printed the assembly output to use as a base for the program, I immediately changed my mind. I ended up writing the whole program in C and never had a problem with speed!
So, I never went beyond that one time.
Perhaps you have a different reason?

I'd leave the original C++ lines in, and comment them as you add your asm volatile lines.

As is, it is unclear how you expect the these to match behavior:

int red =10;
int yellow =9;
int green =8;

void setup() {
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
  
}

void loop() {
  changeLights();
  delay(1000);
}
void changeLights(){
  //green off, yellow on for 3 seconds
  digitalWrite(green, LOW);
  digitalWrite(yellow, HIGH);
  delay(3000);
//turm off yellow, then turn red on for 5 seconds
  digitalWrite(yellow, LOW);
  digitalWrite(red, HIGH);
  delay(5000);
//red and yellow on for 2 seconds (red is still on from previous code)
  digitalWrite(yellow, HIGH);
  delay(2000);
//turn off red and yellow, then turn on green
  digitalWrite(yellow, LOW);
  digitalWrite(red, LOW);
  digitalWrite(green, HIGH);
  delay(3000); 
}
int green=13, yellow=12, red=11;
void setup() {
  Serial.begin(9600);
  pinMode(green, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(red, OUTPUT);
}

void loop() {
  asm volatile ("SBI 0x05,5");
  delay(1000);
  asm volatile ("CBI 0x05,5");
  delay(500);

  asm volatile ("SBI 0X05,4");
  delay(1000);
  asm volatile ("CBI 0x05,4");
  delay(500);

  asm volatile ("SBI 0X05,3");
  delay(1000);
  asm volatile ("CBI 0x05,3");
  delay(500);
}

Does the first work perfectly or not?

Maybe translating the perfect traffic light system into english would be a good first step.

Yeah the first one works absolutely fine!

ive just never used assembly code so when i run the assembly code the LED's do work i just want the amber light to stay on with the green light like the original code and im not sure how to do that

Yeah haha this is a task on an assignment of mine, I know how to do C perfectly but we have never done assembly and i dont really know why we have to do it haha but oh well. Im sure i can figure this out anyhow!

I believe you are required to include newlines and indentation in the inline assembly statements.

This is known to work and has some inline assembly that may help with troubleshooting.

Keep all the documentation on the microcontroller close by!

will do thanks mate!

Curious, what's your goal?

I'm not an assembly type person but I've read in this forum messages from assembly programmers looking at the output of the C compiler for hints on how to improve their code. However often the man made code was better but the compiler is very good and can provide useful hints on how to do certain coding functions.

My take away was the comparison between a programmer and the compiler is similar to a comparison between a programmer and another programmer.

My painful memory is the C compiler uses it's own names for all variables and any function calls, and all comments are missing!

My goal is to literally have the normal C code to work as assembly code aswell, when i have used a compiler it has given me 100's of lines of codes and gave me tons of errors, I may have done this wrong however so you got any way to do this correctly?

Are you using the assembler that the Arduino IDE uses? If not, errors are expected!

hmm no! i didnt know you could do this!
how do you go about it?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.