This is a simple "CPU emulator", which consists of the following components:
- Assembler [asm+.exe] - translates assembly (.asy) programs into bytecode (.bsy) "executable" files.
- Disassembler [asm-.exe] - translates back bytecode into assembly.
- CPU emulator [scpu.exe] - runs bytecode programs.
These are all the commands that are supported:
Input
- in
- out
Arithmetic
- add
- sub
- mul
- div
Math
- pow
- sqrt
- sin
- cos
- abs
- flr
Stack
- push
- pop
Control flow
- call
- ret
- jmp
- jae
- ja
- jb
- jbe
- je
- jne
- hlt
Graphics
- upd
- clr
; number of which to take the factorial
in
; calling fact function, after its execution the answer will be on top of the stack
call :fact
out
hlt
fact:
pop rax
push 1
call :fact_rec
ret
fact_rec:
push rax
mul
push rax
push 1
sub
pop rax
push 1
push rax
jae :end
call :fact_rec
end:
ret
; input a, b, c coefficients of a quadratic equation (ax^2 + bx + c = 0)
in
in
in
call :sqeq
hlt
; solves square equation
; takes three coefficients from stack
sqeq:
pop rcx
pop rbx
pop rax
; if rax == 0 solve linear
push rax
push 0
je :lineq
; if rax != 0 solve square
jmp :square
; solves linear equation
lineq:
push rcx
push rbx
push -1
mul
div
out
ret
; solves square equation in case rax != 0
square:
push rbx
push rbx
mul
push 4
push rax
push rcx
mul
mul
sub
pop rdx
push rdx
push 0
ja :twosols
push rdx
push 0
je :onesol
jmp :nosol
twosols:
push 2
out
push rdx
sqrt
pop rdx
push rbx
push -1
mul
push rdx
add
push 2
div
out
push rbx
push -1
mul
push rdx
sub
push 2
div
out
ret
onesol:
push 1
out
push rbx
push -1
mul
push 2
push rax
mul
div
out
ret
nosol:
push 0
out
this and the previous examples can be found in the examples/ folder
- SDL2
- (my) file_manager
- (my) stack
- (my) log_generator