mirror of
https://github.com/Asraelite/littlebigcomputer.git
synced 2025-07-20 09:06:51 +00:00
Add compiler
This commit is contained in:
parent
476972f85a
commit
3f3125ef43
31 changed files with 2625 additions and 3 deletions
61
programs/parva_0.2/cat.parvalang
Normal file
61
programs/parva_0.2/cat.parvalang
Normal file
|
@ -0,0 +1,61 @@
|
|||
# cat
|
||||
|
||||
.extern f_open, f_close, f_read, write
|
||||
.string_encoding system
|
||||
|
||||
main: # fn (args: [cstr8]) -> int
|
||||
let argv = %a0;
|
||||
let argc = %a1;
|
||||
# mv s0, a0
|
||||
# push a1
|
||||
let arg_index = 0;
|
||||
|
||||
arg_loop:
|
||||
beq arg_index, argc, end;
|
||||
let filename = lw [argv + arg_index];
|
||||
%a0 = filename;
|
||||
call f_open
|
||||
let fd = %a0;
|
||||
bltz fd, error_open;
|
||||
%a0 = fd;
|
||||
call f_read;
|
||||
let file_contents = %a0;
|
||||
|
||||
|
||||
arg_loop:
|
||||
lw t0, 1(sp)
|
||||
beq s1, t0, end
|
||||
lw a0, 0(s0)
|
||||
call f_open # a0 = file descriptor
|
||||
bltz a0, error_open
|
||||
push a0
|
||||
call f_read # a0 = pointer to contents
|
||||
bltz a0, error_read
|
||||
mv a1, a0
|
||||
li a0, 1 # stdout
|
||||
call write
|
||||
pop a0
|
||||
call f_close
|
||||
addi s1, s1, 1
|
||||
b arg_loop
|
||||
|
||||
error_read:
|
||||
pop zero
|
||||
mv s0, a1
|
||||
call f_close
|
||||
mv a1, s0
|
||||
error_open:
|
||||
mv s0, a1
|
||||
li a0, 1
|
||||
li a1, error_message
|
||||
call write
|
||||
mv a1, s0
|
||||
call write
|
||||
b end
|
||||
end:
|
||||
li a0, 0
|
||||
ret
|
||||
|
||||
error_message:
|
||||
.string "\fr" # color red
|
||||
.string "Error: \0"
|
Loading…
Add table
Add a link
Reference in a new issue