ASMC SupportSupport the custom ASM Craft language. LicenseBefore copying any part of this project, please read the LICENSE file to understand the terms and conditions. 1. Program StructureAn ASMC source file consists of two primary sections, optional inline comments, and code labels.
2. Registers & Calling ConventionsASMC provides 16 general-purpose 64-bit virtual registers.
3. Data Section (
|
| Directive | Size Allocated | Description |
|---|---|---|
ds |
16 bytes | Data Small |
dm |
32 bytes | Data Medium |
db |
64 bytes | Data Big |
- If the defined string or numeric sequence is smaller than the allocation size, it is padded with
0x00(null bytes). - If it exceeds the maximum allocation size, it is truncated.
- Escape sequences (e.g.,
\n,\t) within string literals are interpreted.
Example
section .data
msg dm "Hello, World!\n" ; Allocates 32 bytes
user_buf db 0 ; Allocates 64 bytes
4. Instruction Set Architecture (ISA)
ASMC instructions support three types of operands:
- Registers: e.g.,
rax,rdi - Variables: Refers to the memory address of a defined variable.
- Immediates: Integer values or labels, e.g.,
42,0x10,my_label
Data Transfer Instructions
mov dest, srcMoves the value ofsrc(register, variable address, or immediate) into the destination registerdest.push srcDecrementsrspby 4 bytes and stores the 32-bit value ofsrcon the stack.pop destPops a 32-bit value off the stack intodestand incrementsrspby 4 bytes.
Arithmetic & Logical Instructions
All arithmetic operations write their result directly to the destination register (dest = dest <op> src).
add dest, src— Addssrctodest.sub dest, src— Subtractssrcfromdest.mul dest, src— Multipliesdestbysrc.div dest, src— Performs integer division (dest // src). Halts execution on division by zero.xor dest, src— Bitwise XOR operation.
Comparison & Control Flow
Branching relies on internal CPU flags set by the cmp instruction:
- Zero Flag (
zf): Set toTrueifv1 == v2. - Sign Flag (
sf): Set toTrueifv1 < v2.
Comparison
cmp arg1, arg2— Comparesarg1andarg2and sets internal status flags.
Unconditional Jump & Subroutines
jmp target— Jumps unconditionally totarget(label or address).call target— Pushes current Instruction Pointer (IP) onto the stack and jumps totarget.ret— Pops saved IP from stack and returns execution control.
Conditional Jumps
je target— Jump if equal (zf == True)jne target— Jump if not equal (zf == False)jg target— Jump if greater (zf == Falseandsf == False)jl target— Jump if less (sf == Trueandzf == False)jge target— Jump if greater or equal (sf == False)jle target— Jump if less or equal (sf == Trueorzf == True)
System Calls
syscallTriggers a system call based on the integer value stored inrax.
5. System Calls Reference (Firmware v1)
rax Code |
Name | Argument 1 (rdi) |
Argument 2 (rsi) |
Argument 3 (rdx) |
Argument 4 (r10) |
Argument 5 (r8) |
Argument 6 (r9) |
Return Value (rax) |
Description |
|---|---|---|---|---|---|---|---|---|---|
| 0 | sys_exit |
Exit Code | — | — | — | — | — | — | Terminates program execution with specified code. |
| 1 | sys_write |
Buffer / Address | — | — | — | — | — | — | Prints string or memory content located at address to stdout. |
| 2 | sys_read |
— | — | — | — | — | — | Read String/Bytes | Reads a string from stdin and stores the output in rax. |
6. Code Examples
Example 1: Hello World
section .data
msg dm "Hello, World!\n"
section .text
main:
; Set up sys_write (1)
mov rax, 1
mov rdi, msg
syscall
; Set up sys_exit (0)
mov rax, 0
mov rdi, 0
syscall
Example 2: Interactive Echo & Loop
section .data
prompt dm "Enter text: "
newline dm "\n"
section .text
main:
; Prompt user
mov rax, 1
mov rdi, prompt
syscall
; Read user input into rax
mov rax, 2
syscall
; Store user input in rbx and print back
mov rbx, rax
mov rax, 1
mov rdi, rbx
syscall
; Exit program
mov rax, 0
mov rdi, 0
syscall