125 lines
1.7 KiB
NASM
125 lines
1.7 KiB
NASM
SYS_EXIT equ 0x1
|
|
SYS_READ equ 0x3
|
|
SYS_WRITE equ 0x4
|
|
|
|
STDIN equ 0x0
|
|
STDOUT equ 0x1
|
|
STDERR equ 0x2
|
|
|
|
; R15 is reserved for action
|
|
ACTION_LOOP equ 0x0
|
|
ACTION_CHAR equ 0x1
|
|
ACTION_BACK_TO_OPENING equ 0x2
|
|
|
|
section .data
|
|
space db " "
|
|
endl db 10
|
|
backslash db "\\"
|
|
quote db 0x22 ; "
|
|
opening_curly db "{"
|
|
opening_bracket db "["
|
|
closing_curly db "}"
|
|
closing_bracket db "]"
|
|
comma db ","
|
|
colon db ":"
|
|
indent dq 2 ; dq (64 bits) because we are working with 64 bit registers
|
|
|
|
section .bss
|
|
char resb 1
|
|
exitCode resb 1
|
|
scope resq 1
|
|
in_string resb 1
|
|
escape_next resb 1
|
|
|
|
section .text
|
|
global _start
|
|
|
|
_start:
|
|
mov byte[exitCode], 0
|
|
mov byte[scope], 0
|
|
|
|
loop:
|
|
mov rax, SYS_READ
|
|
mov rbx, STDIN
|
|
mov rcx, char
|
|
mov rdx, 1
|
|
int 0x80
|
|
|
|
; is at EOF?
|
|
cmp rax, 0
|
|
jle end
|
|
|
|
mov al, [char]
|
|
cmp al, [quote]
|
|
je case_quote
|
|
cmp al, [backslash]
|
|
je case_backslash
|
|
cmp al, [opening_curly]
|
|
je case_opening
|
|
cmp al, [opening_bracket]
|
|
je case_opening
|
|
|
|
mov r15, ACTION_LOOP
|
|
jmp print_char
|
|
|
|
case_quote:
|
|
mov al, [escape_next]
|
|
cmp al, 1
|
|
je print_char
|
|
|
|
not byte[in_string]
|
|
jmp print_char
|
|
|
|
case_backslash:
|
|
mov byte[escape_next], 1
|
|
jmp print_char
|
|
|
|
case_opening:
|
|
mov r5, ACTION_BACK_TO_OPENING
|
|
jmp print_char
|
|
|
|
inc byte[scope]
|
|
|
|
;je ln_start
|
|
;jmp loop
|
|
|
|
print_char:
|
|
mov rax, SYS_WRITE
|
|
mov rbx, STDOUT
|
|
mov rcx, char
|
|
mov rdx, 1
|
|
int 0x80
|
|
|
|
cmp r15, ACTION_LOOP
|
|
je loop
|
|
|
|
ln_start:
|
|
xor r8, r8
|
|
xor r9, r9
|
|
xor rax, rax
|
|
|
|
mov rax, [indent]
|
|
mov rbx, [scope]
|
|
imul rax, rbx
|
|
mov r9, rax
|
|
|
|
cmp qword[scope], 0
|
|
je loop
|
|
|
|
ln_loop:
|
|
mov rax, SYS_WRITE
|
|
mov rbx, STDOUT
|
|
mov rcx, space
|
|
mov rdx, 1
|
|
int 0x80
|
|
|
|
inc r8
|
|
cmp r8, r9
|
|
jne ln_loop
|
|
je loop
|
|
|
|
end:
|
|
mov rax, SYS_EXIT
|
|
mov rbx, [exitCode]
|
|
int 0x80
|