High ASM

From Esolang
Jump to navigation Jump to search

High ASM is designed by PSTF.

Introduction

Although the name contains "ASM", this is not a really assembly language: It is actually a high-level language.

Command set

First, let's see what is the storage.

As it is in the ASM-style, we have registers from R0 to R7, and we have a stack pointer, a comparison flag and a program counter.

We uses stack and variables as our storage.

Variable definition

VAR:
    A: 5
    B: 10
    C: 12
    D: "Hello, world!" # This is same as D ARRAY(13): [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33].
END VAR

Functions

FUNC function_name [arg_list]:
    function_body
    RET return_value
END FUNC

Command set

MOV a, b: Move the data from b to a, and keep the data in b.
ADD a, b: Add b into a.
SUB a, b: Reduce b from a.
MUL a, b: Multiply a by b.
DIV a, b: Divide a by b, result in float number.
IDV a, b: Divide a by b, result in integer.
MOD a, b: Divide a by b and return its remainder, result in float number.
IMD a, b: Same as MOD but result in integer.
EXP a, b: Multiply a by a over and over for b times.
CMP a, b: Reduce b from a but save the result in the register CR.
JMP l: Jump to the label l.
JNE l: Jump to the label l if CR is not 0.
JEQ l: Jump to the label l if CR is 0.
JGT l: Jump to the label l if CR is 1.
JLE l: Jump to the label l if CR is not 1.
JLT l: Jump to the label l if CR is -1.
JGE l: Jump to the label l if CR is not -1.
IN x: Get an input and store it into x.
OUT x: Print x.
PUSH x: Push x into the stack.
POP x: Pop x from the stack.
HLT: Halt.

Example

Factorial

VAR n: 0
VAR result: 0

START:
    IN R0
    MOV [n], R0
    MOV R0, [n]
    CALL factorial
    OUT R0
    HALT

FUNC factorial:
    CMP R0, 1
    JLE base_case
    
    PUSH R0
    SUB R0, 1
    CALL factorial
    
    POP R1
    MUL R0, R1
    RET

base_case:
    MOV R0, 1
    RET

Categories