Skip to content

Commit

Permalink
docs: 📚 add some examples
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbarsukov committed Jun 20, 2024
1 parent 408863d commit 75991ca
Show file tree
Hide file tree
Showing 3 changed files with 292 additions and 0 deletions.
102 changes: 102 additions & 0 deletions examples/hello_username.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
.data
question: "> What is your name?", 10
hello_str: "> Hello, "
mark: "!"
buffer: 0


.text

_start:
push 1
push question
call extand_string

push question
call print_string

push buffer
call read_string

push hello_str
call print_string

push buffer
call print_string

push mark
call print_string
halt


; --- lib ---

;;; Ввод строки из IO 0
;; На вершине стека данных адрес строки, в которую будем записывать
;; > ->[address]
;; < ->[]
read_string:
read: ; ->[a] = address
input 0 ; ->[char, a]
push -1 ; ->[-1, char, a]
cmp ; ->[-1, char, a]
jz ret_r ; ->[-1, char, a]
pop ; ->[char, a]

swap ; ->[a, char]
load ; ->[l, a, char]
inc ; ->[l+1, a, char]
store ; ->[l+1, a, char] -- [a] (len) <- l+1 (len++)
pop ; ->[a, char]
dup ; ->[a, a, char]
load ; ->[ll, a, a, char]
add ; ->[ll+a, a, char]
over3 ; ->[char, ll+a, a, char]
store ; ->[char, ll+a, a, char] -- [ll+a] <- char
pop ; ->[ll+a, a, char]
pop ; ->[a, char]
swap ; ->[char, a]
pop ; ->[a]
jmp read ; ->[a]
ret_r:
pop ; ->[char, a]
pop ; ->[a]
pop ; ->[]
ret ; ->[]


;;; Выводит строку
;; На вершине стека адрес строки
;; > ->[address]
;; < ->[]
print_string:
load
cycle:
jz ret_w
swap
inc
load
output 1
pop
swap
dec
jmp cycle
ret_w:
pop
pop
ret


;;; Увеличивает длину строки
;; На вершине стека адрес строки, ниже - количество символов для добавления
;; > ->[address; n]
;; < ->[]
extand_string:
load ; ->[v, a, n]
over3 ; ->[n, v, a, n]
add ; ->[n+v, a, n]
store ; ->[n+v, a, n]
pop ; ->[a, n]
pop ; ->[n]
pop ; ->[]
ret
81 changes: 81 additions & 0 deletions examples/prob2.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
;;; ANS: 4613732

; cutoff=4000000
; first_even = 2
; second_even = 8
; even_sum = first_even + second_even
;
; while even_sum < cutoff:
; even_fib = ((4 * second_even) + first_even)
; even_sum += even_fib
; first_even = second_even
; second_even = even_fib
;
; print(even_sum)

.data
cutoff: 4000000
first_even: 2
second_even: 8
even_sum: 10

.text
load_value:
load
swap
pop
ret

_start:
calculate_sum: ; []
push even_sum ; [even_sum]
call load_value

push cutoff ; [even_sum, cutoff]
call load_value

cmp ; [even_sum - cutoff]
js print_result ; если even_sum >= cutoff, перейти к выводу результата
pop
pop


; even_fib = (4 * second_even) + first_even
push 4
push second_even ; [4, second_even]
call load_value
mul ; [4 * second_even]
push first_even ; [4 * second_even, first_even]
call load_value
add ; [even_fib]

; even_sum += even_fib
push even_sum ; [even_fib, even_sum]
load ; [even_fib, even_sum_r, even_sum]
over3 ; [even_fib, even_sum_r, even_sum, even_fib]
add ; [even_fib, even_sum_r, even_sum+even_fib]
store ; сохранить новый even_sum
pop
pop ; [even_fib]

;; first_even = second_even; second_even = even_fib
push second_even ; [even_fib, second_even_r]
call load_value ; [even_fib, second_even]
push first_even ; [even_fib, second_even, first_even_r]
swap ; [even_fib, first_even_r, second_even]
store ; сохранить first_even <- second_even
pop
pop ; [even_fib]
push second_even ; [even_fib, second_even_r]
swap ; [second_even_r, even_fib]
store ; [second_even_r, even_fib]
pop
pop

jmp calculate_sum ; перейти к началу цикла

print_result:
push even_sum
load
output 2 ; вывод результата
halt
109 changes: 109 additions & 0 deletions examples/string_utils.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
.data
buf: 0


.text

;;; Ввод строки из IO 0
;; На вершине стека данных адрес строки, в которую будем записывать
;; > ->[address]
;; < ->[]
read_string:
read: ; ->[a] = address
input 0 ; ->[char, a]
push -1 ; ->[-1, char, a]
cmp ; ->[-1, char, a]
jz ret_r ; ->[-1, char, a]
pop ; ->[char, a]

swap ; ->[a, char]
load ; ->[l, a, char]
inc ; ->[l+1, a, char]
store ; ->[l+1, a, char] -- [a] (len) <- l+1 (len++)
pop ; ->[a, char]
dup ; ->[a, a, char]
load ; ->[ll, a, a, char]
add ; ->[ll+a, a, char]
over3 ; ->[char, ll+a, a, char]
store ; ->[char, ll+a, a, char] -- [ll+a] <- char
pop ; ->[ll+a, a, char]
pop ; ->[a, char]
swap ; ->[char, a]
pop ; ->[a]
jmp read ; ->[a]
ret_r:
pop ; ->[char, a]
pop ; ->[a]
pop ; ->[]
ret ; ->[]


;;; Выводит строку
;; На вершине стека адрес строки
;; > ->[address]
;; < ->[]
print_string:
load
cycle:
jz ret_w
swap
inc
load
output 1
pop
swap
dec
jmp cycle
ret_w:
pop
pop
ret


;;; Увеличивает длину строки
;; На вершине стека адрес строки, ниже - количество символов для добавления
;; > ->[address; n]
;; < ->[]
extand_string:
load ; ->[v, a, n]
over3 ; ->[n, v, a, n]
add ; ->[n+v, a, n]
store ; ->[n+v, a, n]
pop ; ->[a, n]
pop ; ->[n]
pop ; ->[]
ret


;;; Добавляет символ в конец строки
;; На вершине стека адрес строки, ниже - символ для добавления
;; > ->[address; sym]
;; < ->[]
push_char:
dup
push 1
swap
call extand_string

load
add
swap
store
pop
pop
ret


;;; Добавляет первод строки к строке
;; На вершине стека адрес строки
;; > ->[address]
;; < ->[]
add_new_line:
push 10
swap
call push_char
ret


_start:
halt

0 comments on commit 75991ca

Please sign in to comment.