Difference between revisions of "Assembly Language WHILE"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "==C# example== <syntaxhighlight lang=csharp> int a = 0; while ( a != 99) { a++ } </syntaxhighlight> ==Assembly Version== MOV R0, #0 .while CMP R0, #99 BNE ....")
 
(Assembly Version)
Line 12: Line 12:
  
 
     MOV R0, #0
 
     MOV R0, #0
.while
+
.while
 
     CMP R0, #99
 
     CMP R0, #99
 
     BNE .loop
 
     BNE .loop
 
     HLT
 
     HLT
.loop
+
.loop
 
     ADD R0, R0, #1
 
     ADD R0, R0, #1
 
     BRA .while
 
     BRA .while

Revision as of 13:13, 15 November 2018

C# example

int a = 0;
while ( a != 99)
{
    a++
}

Assembly Version

   MOV R0, #0
.while
   CMP R0, #99
   BNE .loop
   HLT
.loop
   ADD R0, R0, #1
   BRA .while


Line 1 sets the value of Register 0 to the value 0.
Line 2 is a label which will be used to branch too.
Line 3 compares the value in Register 0 with the value 99.
Line 4 will branch to the .loop label if the previous compare was NOT EQUAL.
Line 5 halt because condition met
Line 6 is a label which will be used to branch too.
Line 7 increment the register used for a counter.
line 8 will branch to the .while label to recheck the condition of the loop.