0
0
ARM Architectureknowledge~5 mins

If-else implementation in assembly in ARM Architecture

Choose your learning style9 modes available
Introduction

If-else lets a program choose between two paths based on a condition. In assembly, it helps control what the processor does next.

Deciding if a number is positive or negative to print a message.
Checking if a user input matches a password to allow access.
Choosing between two calculations based on a value.
Turning on a light if a sensor reads above a threshold, else turning it off.
Core Concept
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.

Key Points
This checks if R0 is zero. If yes, it runs the code at is_zero, else it runs the code after BEQ.
ARM Architecture
CMP R0, #0
BEQ is_zero
; code if not zero
B end
is_zero:
; code if zero
end:
This compares R1 and R2. If R1 is greater, it jumps to greater, else continues with the next code.
ARM Architecture
CMP R1, R2
BGT greater
; code if R1 <= R2
B end
greater:
; code if R1 > R2
end:
Detailed Explanation

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).

ARM Architecture
; 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:
OutputSuccess
Important Notes

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.

Summary

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.