1.
Addition, Multiplication, Subtraction
; Load first number into R0
MOV R0, #10 ; R0 = 10
; Load second number into R1
MOV R1, #5 ; R1 = 5
; -----------------------------
; ADDITION
; R2 = R0 + R1
; -----------------------------
ADD R2, R0, R1 ; R2 = 10 + 5 = 15
; -----------------------------
; SUBTRACTION
; R3 = R0 - R1
; -----------------------------
SUB R3, R0, R1 ; R3 = 10 - 5 = 5
; -----------------------------
; MULTIPLICATION
; R4 = R0 * R1
; -----------------------------
MUL R4, R0, R1 ; R4 = 10 * 5 = 50
[Link] the data from memory and perform the Addition, Substation and multiplication
START
; -----------------------------
; Load NUM1 from memory
; -----------------------------
MOV R0, #0x40000000 ; Address of NUM1
LDR R1, [R0] ; R1 = NUM1
; -----------------------------
; Load NUM2 from memory
; -----------------------------
MOV R0, #0x40000004 ; Address of NUM2
LDR R2, [R0] ; R2 = NUM2
; -----------------------------
; ADDITION
; -----------------------------
ADD R3, R1, R2
MOV R0, #0x40000008 ; Address of ADD_RES
STR R3, [R0]
; -----------------------------
; SUBTRACTION
; -----------------------------
SUB R4, R1, R2
MOV R0, #0x4000000C ; Address of SUB_RES
STR R4, [R0]
; -----------------------------
; MULTIPLICATION
; -----------------------------
MUL R5, R1, R2
MOV R0, #0x40000010 ; Address of MUL_RES
STR R5, [R0]
[Link] Maximum of Two Numbers
MOV R0, #25 ; First number
MOV R1, #18 ; Second number
CMP R0, R1 ; Compare R0 and R1
MOVGT R2, R0 ; If R0 > R1, R2 = R0
MOVLE R2, R1 ; If R0 <= R1, R2 = R1
[Link] Even or Odd
MOV R0, #17 ; Input number
AND R1, R0, #1 ; R1 = R0 & 1 (bit 0)
; R1 = 0 (even), R1 = 1 (odd)
END
[Link] of First N Natural Numbers
Problem: Calculate sum of first N natural numbers (N=10).
Solution:
MOV R0, #10 ; N = 10
MOV R1, #0 ; Sum = 0
MOV R2, #1 ; Counter = 1
Loop
ADD R1, R1, R2 ; Sum += Counter
ADD R2, R2, #1 ; Counter++
CMP R2, R0 ; Compare with N
BLE Loop ; Continue if Counter <= N