Difference between revisions of "Assembly Language FOR"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "==C# example== <syntaxhighlight lang=csharp> for (int i=0; i < 10; i++ { } </syntaxhighlight> ==Assembly Version== MOV R0, #0 MOV R1, #10 .start CMP R0, R1...")
 
(C# example)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
==C# example==
 
==C# example==
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>
for (int i=0; i < 10; i++
+
for (int i=0; i < 10; i++)
 
{
 
{
 
+
  //doCodeHere
 
}
 
}
  
Line 12: Line 12:
 
     MOV R0, #0
 
     MOV R0, #0
 
     MOV R1, #10
 
     MOV R1, #10
  .start
+
  start:
 
     CMP R0, R1
 
     CMP R0, R1
     BEQ .end
+
     BEQ end
     BGT .end
+
     BGT end
 
     ADD R0, R0, #1
 
     ADD R0, R0, #1
  .doCodeHere
+
  doCodeHere:
     B .start
+
     B start
  .end
+
  end:
  
 
Line 1 sets the value of Register 0 to the value 0. i.e. i in the C# code.<br>
 
Line 1 sets the value of Register 0 to the value 0. i.e. i in the C# code.<br>

Latest revision as of 11:43, 23 August 2023

C# example

for (int i=0; i < 10; i++)
{
   //doCodeHere
}

Assembly Version

   MOV R0, #0
   MOV R1, #10
start:
   CMP R0, R1
   BEQ end
   BGT end
   ADD R0, R0, #1
doCodeHere:
   B start
end:

Line 1 sets the value of Register 0 to the value 0. i.e. i in the C# code.
Line 2 sets the value of Register 1 to the value 10. i.e. the condition to continue in the C# code.
Line 3 is the .start label.
Line 4 compares the value in Register 0 with the in Register 1.
Line 5 we branch to the end if equal to.
Line 6 we branch to the end if greater than.
Line 7 adds the value 1 to the current value in Register 0 and stores the value back into Register 0.
Line 8 a label to show where the code inside the for loop will go.
Line 9 will branch to the .start label.
Line 10 is label for the .end of the program.