Difference between revisions of "Assembly Language IF"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
==C# example==
 
==C# example==
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>
int a = 0;
+
int y = 0;
do
+
int x = 0;
 +
if (y == 10)
 
{
 
{
     a++
+
     x = 9;
 
}
 
}
while ( A != 99);
+
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Assembly Version==
+
==Assembly Version 1==
 +
 
 +
    MOV R0, #0
 +
    MOV R1, #0
 +
    CMP R0, #10
 +
    BNE end
 +
    MOV R1, #9
 +
end:
 +
 
 +
 
 +
 
 +
Line 1 sets the value of Register 0 to the value 0.<br>
 +
Line 2 sets the value of Register 1 to the value 0.<br>
 +
Line 3 compares the value in Register 0 with the value 10.<br>
 +
Line 4 will branch to the .end label if the previous compare was NOT EQUAL.<br>
 +
Line 5 will move the value 9 into Register 1. <br>
 +
Line 6 is the .end label
 +
 
 +
==Assembly Version 2==
 +
 
 +
    MOV R0, #0
 +
    MOV R1, #0
 +
    CMP R0, #10
 +
    BEQ true
 +
    B end
 +
true:
 +
    MOV R1, #9
 +
end:
 +
 
 +
 
  
    LDR R0, #0
+
Line 1 sets the value of Register 0 to the value 0.<br>
.loop
+
Line 2 sets the value of Register 1 to the value 0.<br>
    ADD R0, R0, #1
+
Line 3 compares the value in Register 0 with the value 10.<br>
    CMP R0, #99
+
Line 4 will branch to the .true label if the previous compare was EQUAL.<br>
    BNE .loop
+
Line 5 will branch to the .end
 +
Line 6 is the label for the .true code
 +
Line 7 will move the value 9 into Register 1. <br>
 +
Line 8 is the .end label

Latest revision as of 09:28, 22 January 2020

C# example

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

Assembly Version 1

   MOV R0, #0
   MOV R1, #0
   CMP R0, #10
   BNE end
   MOV R1, #9
end:


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 .end label if the previous compare was NOT EQUAL.
Line 5 will move the value 9 into Register 1.
Line 6 is the .end label

Assembly Version 2

   MOV R0, #0
   MOV R1, #0
   CMP R0, #10
   BEQ true
   B end
true:
   MOV R1, #9
end:


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 .true label if the previous compare was EQUAL.
Line 5 will branch to the .end Line 6 is the label for the .true code Line 7 will move the value 9 into Register 1.
Line 8 is the .end label