Difference between revisions of "Assembly Language IF"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(C# example)
(Assembly Version)
Line 17: Line 17:
  
 
     LDR R0, #0
 
     LDR R0, #0
  .loop
+
    LDR R1, #0
 +
    CMP R0, #10
 +
    BNE .else
 +
    MOV R1, #9
 +
    B .endif
 +
  .else
 
     ADD R0, R0, #1
 
     ADD R0, R0, #1
    CMP R0, #99
+
.endif
    BNE .loop
+
 
 +
 
  
 
Line 1 sets the value of Register 0 to the value 0.<br>
 
Line 1 sets the value of Register 0 to the value 0.<br>
Line 2 is a label which will be used to branch too.<br>
+
Line 2 sets the value of Register 1 to the value 0.<br>
Line 3 adds the value 1 to the current value in Register 0 and stores the value back into Register 0.<br>
+
Line 3 compares the value in Register 0 with the value 10.<br>
Line 4 compares the value in Register 0 with the value 99.<br>
+
Line 4 will branch to the .else label if the previous compare was NOT EQUAL.<br>
Line 5 will branch to the .loop label if the previous compare was NOT EQUAL.<br>
+
Line 5 will move the value 9 into Register 1. <br>
 +
Line 6 will branch always to the .endif label. <br>
 +
Line 7 is a label called .else and is used in a branch. <br>
 +
Line 8 will add together the value 1 with the current value in Register 0, the output will be stored back in Register 0.

Revision as of 10:51, 16 June 2017

C# example

int y = 0;
int x = 0;
if (y == 10)
{
    x = 9;
}
else
{
    y++
}

Assembly Version

   LDR R0, #0
   LDR R1, #0
   CMP R0, #10
   BNE .else
   MOV R1, #9
   B .endif
.else
   ADD R0, R0, #1
.endif


Line 1 sets the value of Register 0 to the value 0.
Line 2 sets the value of Register 1 to the value 0.
Line 3 compares the value in Register 0 with the value 10.
Line 4 will branch to the .else label if the previous compare was NOT EQUAL.
Line 5 will move the value 9 into Register 1.
Line 6 will branch always to the .endif label.
Line 7 is a label called .else and is used in a branch.
Line 8 will add together the value 1 with the current value in Register 0, the output will be stored back in Register 0.