Assembly Language Check for ODD / EVEN

From TRCCompSci - AQA Computer Science
Revision as of 14:03, 22 December 2018 by Admin (talk | contribs) (Created page with "==C# example== <syntaxhighlight lang=csharp> int y = 20; if (y % 2 == 0) { x = 9; } </syntaxhighlight> ==Assembly Version== MOV R0,#20 MOV R1,#1 AND R3...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

C# example

int y = 20;
if (y % 2 == 0)
{
    x = 9;
}

Assembly Version

     MOV R0,#20
     MOV R1,#1
     AND R3,R0,R1
     CMP R3, R1
     BEQ end
     MOV R9,#9 
end:
     halt


Line 1 sets the value of Register 0 to the value 20 (this is the value to test).
Line 2 sets the value of Register 1 to the value 1 (this is used in the and).
Line 3 is a bitwise AND between 20 and 1, this will check if the binary value ends in a 1 (ie odd).
Line 4 compare the outcome of the AND with the value #1.
Line 5 will branch to the end label if the compare was not equal (ie it was odd).
Line 6 will be run if the compare was equal. Line 7 is the end label Line 8 is the halt