Basic Examples

Simple Romasm programs to get you started

Hello, Romasm!

Example 1: Add Two Numbers

; Add 10 + 20
LOAD R0, 10      ; Load 10 into R0
LOAD R1, 20      ; Load 20 into R1
ADD R0, R1       ; R0 = R0 + R1 (now 30)
PRINT R0         ; Output: 30

Example 2: Multiply

; Calculate 5 * 7
LOAD R0, 5
LOAD R1, 7
MUL R0, R1       ; R0 = 5 * 7 = 35
PRINT R0         ; Output: 35

Example 3: Division

; Calculate 100 / 4
LOAD R0, 100
LOAD R1, 4
DIV R0, R1       ; R0 = 100 / 4 = 25
PRINT R0         ; Output: 25

Loops

Example 4: Count from 1 to 10

; Print numbers 1 through 10
LOAD R0, 1       ; Counter starts at 1
LOAD R1, 10      ; Max value

loop:
    PRINT R0     ; Print current number
    INC R0       ; Increment counter
    CMP R0, R1   ; Compare with max
    JLE loop     ; Jump if R0 <= 10

Example 5: Sum 1 to 10

; Calculate sum of 1+2+...+10
LOAD R0, 0       ; Sum (starts at 0)
LOAD R1, 1       ; Counter
LOAD R2, 10      ; Max value

loop:
    ADD R0, R1   ; Add counter to sum
    INC R1       ; Increment counter
    CMP R1, R2   ; Compare with max
    JLE loop     ; Continue if R1 <= 10

PRINT R0         ; Output: 55

Conditionals

Example 6: Check if Even

; Check if 8 is even
LOAD R0, 8
LOAD R1, 2
MOD R0, R1       ; R0 = 8 % 2 = 0
LOAD R2, 0
CMP R0, R2       ; Compare remainder with 0
JEQ is_even      ; Jump if equal (even)
; Odd
LOAD R3, 0
PRINT R3         ; Output: 0 (false)
JMP done

is_even:
LOAD R3, 1
PRINT R3         ; Output: 1 (true)

done:

Example 7: Find Maximum

; Find max of 15 and 23
LOAD R0, 15
LOAD R1, 23
CMP R0, R1       ; Compare R0 with R1
JGT r0_greater   ; Jump if R0 > R1
; R1 is greater
LOAD R2, R1
JMP done

r0_greater:
LOAD R2, R0

done:
PRINT R2         ; Output: 23

Memory Operations

Example 8: Store and Load from Memory

; Store value to memory, then retrieve it
LOAD R0, 42
STORE R0, [100]  ; Store 42 to memory address 100
LOAD R1, [100]   ; Load from memory address 100
PRINT R1         ; Output: 42

Stack Operations

Example 9: Using the Stack

; Save and restore registers using stack
LOAD R0, 10
LOAD R1, 20

; Save to stack
PUSH R0
PUSH R1

; Modify registers
LOAD R0, 100
LOAD R1, 200

; Restore from stack
POP R1           ; Restores 20
POP R0           ; Restores 10

PRINT R0         ; Output: 10
PRINT R1         ; Output: 20

Next Steps