X-ASM

From Esolang
Jump to navigation Jump to search
This is still a work in progress. It may be changed in the future.

X-ASM is an Assembly-like programming language designed by PSTF.

Language Overview

X-ASM is a variant of Assembly Language, but it is not real Assembly Language(such as like x86, ARM, MASM, etc.), thus it is a Medium-level language. It is inspired by X-script, Assembly/Assemble, MASM, and C++.

It uses register, memory, a stack, ten accumulators, and variables.

Basic Syntax Overview

Procedural framework

X-ASM uses this procedural framework:

alloc cs: code, ds: data, ss: stack, bs: buffer
# Main label(main) is the entrance of the program.
buffer seg
	dat rep(8) 0
buffer endseg

data seg
	dat rep(8) 0
data endseg

stack seg
	dat rep(128) 0 # Allocate stack space in advance to prevent stack overflow and stack underflow.
stack endseg

code seg
main: 
	nop # Put your code here
	jmp ok
ok: 
	mov fr, 0
	ext 0
code endseg

Arithmetic operations

add x, y
sub x, y
mul x, y
div x, y
mod x, y
log x, y
nrt x, y
exp x, y
mov x, y
  1. Addition.
  2. Subtraction.
  3. Multiplication.
  4. Division.
  5. Modulo.
  6. Logarithm.
  7. N-root.
  8. Exponent.
  9. Assignment.

Bitwise operations

and x, y
or x, y
not x
xor x, y
shl x, y
shr x, y
rtl x, y
rtr x, y
  1. Bitwise and.
  2. Bitwise or.
  3. Bitwise not Negates the value. Note: You can add mov bw, 1 to make bitwise not, and set bw to 0 to make arithmetic not.
  4. Bitwise xor.
  5. Multiply x by 2y.
  6. Divide x by 2y.
  7. Cycle shift x to the left by y. The highest level is shifted to the left to become the lowest level, and vice versa.
  8. Cycle shift x to the right by y.

I/O

lsn x, y
rea x
wrt x
say x
dis x
dbf
dbfws
  1. Inputs a string, parse out the number with base-y(leave blank is ten), then store it into x.
  2. Inputs a string and directly store it into x.
  3. Print the value of x as character(Unicode).
  4. Print the value of x as integer.
  5. Print the value of x.
  6. Print the whole buffer as character array.
  7. Print the whole buffer as integer array, and seperate with spaces.

Conditional

cmp a, b
jeq l
jne l
jab l
jna l
jbl l
jnb l
aje x, l
ajd x, l
aja x, l
aju x, l
ajb x, l
ajv x, l
if cond do code else code2 end
  1. Compares a and b and stores the results in the SF and ZF registers.
  2. If ZF is 1, then jump to specified label.[1]
  3. If ZF is 0, then jump to specified label.
  4. If SF is 1 and ZF is 0, then jump to specified label.[2]
  5. If SF is 1 or ZF is 1, then jump to specified label.
  6. If SF is 1, then jump to specified label.
  7. If SF is 0, then jump to specified label.
  8. If accumulator is equal to x, then jump to specified label.
  9. If accumulator isn't equal to x, then jump to specified label.
  10. If accumulator is greater than x, then jump to specified label.
  11. If accumulator is under x, then jump to specified label.
  12. If accumulator is less than x, then jump to specified label.
  13. If accumulator is over x, then jump to specified label.
  14. IF-ELSE statement. Also you can use elif cond to make more branch.

Buffer operations

jin
cbf
  1. Join the result of accumulator(acc) to the buffer.
  2. Clear the buffer.

Stack operations

psh x
pop x
  1. Push the value of x to the stack.
  2. Pop the ToS and store it into x. If leave x blank, then discard ToS.

Accumulators

psa x
ppa x
cac
chg x
  1. Push the value of x to the accumulator.
  2. Assign x to the value of accumulator and reset the accumulator.
  3. Reset accumulator to 0.
  4. Change all accumulator-operations to on x-th accumulator.

Variable

dcl a, t, v
  1. Declare a variable with the name a, type t, and v as value. Leave t blank is integer, leave v blank is initial value.

Data types

Currently, we have these types:

  • Numbers, such as 3, 7, 3.14, -2, 114514, 1e20, 3.683E-34, 3.130582375184619046479671967486, etc. Initial value is 0 for int, and 0.0 for float.
  • Strings, such as 'Hello' (or "Hello"). Strings can be enclosed in double quotes or single quotes, just like work in Python. Initial value is "".
  • Booleans. It can only be True, or False. Initial value is False.
  • Tables. This is the most important data type in X-ASM, it can express a list or a dictionary, such as {1, 2, 3}, or {key: value}. Initial value is {}. Also, if you define a table as a list, then you use brackets instead of braces.
    • List. It uses the brackets for table parentheses. initial value is []. You can use slice, as slice in Python.
    • Set. It is the common type of table, and it will sort when initialized, and sort after every deletion and insertion. Initial value is {}.
    • Dictionary. It is a table with key-value bind. Initial value is {}.
  • Tuples. It is just a "unchangable table", such as (5, 2), (4, 8, 3, 8, 9), but (1, ) instead of (1). Initial value is (,).
  • Functions. They're 1st-class citizens, which can be passed and returned as arguments.
  • Empty types. They has only a value, None. They converts to 0, but they can used to be true. When they converted to string, they becomes "".
  • User-defined types. They're defined by "structure" command.
  • Undefined behaviour. This often occurs when you trying to do something incorrect, such as 5/0. They converts to 0, but they can used to be false. When they converted to string, they becomes 99 bottles of beer.
  • Thread. It can fork a process out to do another program.

Loop

loop k
for iter in iter_cup do code end
while cond do code end
  1. Jump to label k and decrease LR with 1.
  2. For-loop.
  3. While-loop.

Function

Header

func_alloc name

Real code

func name (type) param_list
code
ret retval_list
end

Categories and References

  1. Note: The ZF register is used to determine whether the value of an operation is 0. Mathematically, if two numbers are subtracted and the result is 0, then the two numbers are equal.
  2. Note: SF stores the symbols of the calculation results. If the result is positive or 0, the value is 0, and if the result is negative, the value is 1.