If-else lets a program choose between two paths based on a condition. In assembly, it helps control what the processor does next.
If-else implementation in assembly in ARM Architecture
CMP Rn, Rm BEQ label_if_equal BNE label_if_not_equal ; code for if part B label_end label_if_not_equal: ; code for else part label_end:
CMP compares two registers by subtracting but does not store the result.
BEQ branches if the compared values are equal; BNE branches if not equal.
is_zero, else it runs the code after BEQ.CMP R0, #0 BEQ is_zero ; code if not zero B end is_zero: ; code if zero end:
greater, else continues with the next code.CMP R1, R2 BGT greater ; code if R1 <= R2 B end greater: ; code if R1 > R2 end:
This program compares the value in register R0 with zero. If R0 is greater than zero, it sets R1 to 1 (meaning positive). Otherwise, it sets R1 to 0 (not positive).
; Check if R0 is positive or not CMP R0, #0 BGT positive ; else part MOV R1, #0 ; R1 = 0 means not positive B end positive: MOV R1, #1 ; R1 = 1 means positive end:
Assembly uses labels and branch instructions to implement if-else logic.
Conditions like equal, not equal, greater than, less than are checked using flags set by CMP.
Always ensure branch labels are unique and placed correctly to avoid errors.
If-else in assembly uses CMP to compare values and branch instructions like BEQ, BNE, BGT to jump to code blocks.
Labels mark the start of if or else code sections.
This lets the processor choose which instructions to run based on conditions.