-
Notifications
You must be signed in to change notification settings - Fork 0
6 ‐ Example Programs
QualityCroissant edited this page Jun 5, 2025
·
2 revisions
Here is an example of a program that prints “Hello, world!” to Standard Output:
pl string mar ; Start printing at the beginning of the "Hello, world!\n\0" string
print: pl mem mch ; Get the character of the string
ld
mv mdr acc ; If it's 0, end the program
jc print_end
mv mar acc ; Increment the address for the next character
ai
mv acc dat
pl out mch ; Print the character
pl [0]d mar
st
mv dat mar ; Set mar to get the next character
jm print ; Go to print the next character
print_end: fi
string: [Hello, world!]s [10]d [0]d
Here is a program that prompts the user to enter a string, and converts the ASCII letters of the string to uppercase:
pl string mar ; Prompt the user to enter a string for conversion
cl print
pl string acc ; Read in their response
cl scan
pl string mar ; Convert the string to uppercase
cl to_uppercase
pl string mar ; Print it
cl print
fi
print: pl mem mch ; Expects string address in mar
ld
; Check if the current character is 0:
mv mdr acc
jc print_end ; If so, return
; Set the reading-position to the next character:
mv mar acc
ai
mv acc dat
; Print the current character:
pl out mch
pl [0]d mar
st
; Restart the loop:
mv dat mar
jm print
; Function end:
print_end: rt
scan: pl inp mch ; Expects string address in acc
; Get the char from input:
pl [0]d mar
ld
; Store it and a 0 after it, in-doing-so setting the position of the written 0 to be where the next read-character is written
pl mem mch
mv acc mar
st
ai
mv acc mar
mv mdr acc
pl [0]d mdr
st
; Check if the read-character was a newline, and if so, return:
pl [10]d dat
eq
js scan_end
; Otherwise, restart the loop:
mv mar acc
jm scan
; Function end:
scan_end: rt
to_uppercase: pl mem mch ; Expects string address in mar
to_uppercase_loop: ld
; Check if the current character in the string is 0:
mv mdr acc
jc to_uppercase_end ; If so, return
; Check if the current character is a lowercase letter:
mv mdr acc
pl [a]s dat
ge
jc to_uppercase_next ; If not, move onto the next character
mv mdr acc
pl [z]s dat
le
jc to_uppercase_next ; ^
; If it is, convert it to uppercase:
mv mdr acc
pl [0010'0000]b dat
a-
mv acc mdr
st
; Move onto the next character:
to_uppercase_next: mv mar acc
ai
mv acc mar
jm to_uppercase_loop ; Restart the loop
; Function end:
to_uppercase_end: rt
string: [Enter string for conversion: ]s [0]d
Here is an implementation of the classic FizzBuzz challenge (I am definitely aware that there are optimisations that could be made to this!):
jm start
was_fizzbuzz: [0]d
fizzbuzz_iter: [1]d
print_num_num: [0]d
print_num_tens: [0]d
print_num_buff: [0]d
fizz: [fizz]s [0]d
buzz: [buzz]s [0]d
fizz_ptr: fizz
buzz_ptr: buzz
print: mv dat acc ; Expects string ptr in dat
ai
mv dat mar
pl mem mch
ld
mv acc dat
mv mdr acc
jc print_end
pl out mch
pl [0]d mar
st
jm print
print_end: rt
get_tens: pl [10]d dat ; Expects num in acc, returns in mdr
pl [10]d mdr
get_tens_loop: a/
js get_tens_cont
rt
get_tens_cont: mv acc mar
mv mdr acc
a*
mv acc mdr
mv mar acc
jm get_tens_loop
print_num: pl mem mch ; Expects num in mdr
pl print_num_num mar
st
mv mdr acc
cl get_tens
pl print_num_tens mar
st
; For each digit (num % 10000...) / 1000...
; (num % 1000) / 100
; (num % 100) / 10
; (num % 10) / 1
print_num_loop: pl print_num_num mar
ld
mv mdr acc
pl print_num_tens mar
ld
mv mdr dat
a/
a*
mv acc dat
pl print_num_num mar
ld
mv mdr acc
a-
pl print_num_buff mar
mv acc mdr
st
pl print_num_tens mar
ld
mv mdr acc
pl [10]d dat
a/
mv acc mdr
st
pl print_num_buff mar
ld
mv acc dat
mv mdr acc
a/
pl [48]d dat
a+
mv acc mdr
pl out mch
pl [0]d mar
st
pl mem mch
pl print_num_tens mar
ld
mv mdr acc
pl [1]d dat
eq
jc print_num_loop
rt
start: pl mem mch
pl was_fizzbuzz mar
pl [0]d mdr
st
pl fizzbuzz_iter mar
ld
mv mdr acc
pl [100]d dat
gt
js end
mv mdr acc
pl [3]d dat
a/
a*
mv acc dat
mv mdr acc
a-
js buzz_check
pl was_fizzbuzz mar
pl [1]d mdr
st
pl fizz dat
cl print
buzz_check: pl mem mch
pl fizzbuzz_iter mar
ld
mv mdr acc
pl [5]d dat
a/
a*
mv acc dat
mv mdr acc
a-
js num_pass
pl was_fizzbuzz mar
pl [1]d mdr
st
pl buzz dat
cl print
num_pass: pl mem mch
pl was_fizzbuzz mar
ld
mv mdr acc
js iter
pl fizzbuzz_iter mar
ld
cl print_num
pl mem mch
iter: pl fizzbuzz_iter mar
ld
mv mdr acc
ai
mv acc mdr
st
pl out mch
pl [0]d mar
pl [10]d mdr
st
jm start
end: fi
An implementation of the arcade game Snake can be found here!