Skip to content

Instantly share code, notes, and snippets.

@marekjm
Created July 26, 2015 22:19
Show Gist options
  • Save marekjm/d00687c7e4c89cf3653b to your computer and use it in GitHub Desktop.
Save marekjm/d00687c7e4c89cf3653b to your computer and use it in GitHub Desktop.
99 bottles for Viua VM assembly written using new, extended syntax (36 lines, could be shortert if not for the shortcomings of the tokenizer)
; 99 bottles of beer program for Viua VM Assembler (modified)
; Copyright (C) 2015 Marek Marecki
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>.
.function: bottles_on_the_wall
strstore 1 " bottles"
strstore 3 " on the wall"
branch (ieq 4 (arg 0 0) (istore 4 1)) +1 +2
strstore 1 " bottle"
branch (ieq 4 0 (izero 4)) zero_bottles
; hack: we want to print register 3 so lets just count the previous ones (since we "echo" three of them)
print [(echo 0) (echo 1) (echo (strstore 2 " of beer"))]
jump quit
.mark: zero_bottles
print (strstore 1 "No more bottles of beer on the wall")
.mark: quit
end
.end
.function: bottles
strstore 1 " bottles"
branch (ieq 4 (arg 0 0) (istore 4 1)) +1 +2
strstore 1 " bottle"
; hack #1: first, we want to echo register 0, then register 1 - compress the lines by counting subsubexpressions!
; hack #2: then, we want to print register 2 so lets just count the two subexpressions
print [(echo {(echo 0)}) (strstore 2 " of beer")]
idec 0
end
.end
.function: report_state_of_the_wall
call (frame ^[(param 0 (arg 0 0))]) bottles_on_the_wall
; co... co... co... co... COMBOBREAKER!!!1!1!
; DOUBLE pass by reference - "bottles" function will *really* decrement the counter!
; we're showing off, so... why not?
call (frame ^[(paref 0 0)]) bottles
print (strstore 4 "Take one down, pass it around")
call (frame ^[(param 0 0)]) bottles_on_the_wall
print (strstore 5 "")
end
.end
.function: main
; hack: we want to store 1 in register 7 so just count the expression "istore 0 99"
; since we also want to store 99 in register 0
istore 7 [(istore 0 99)]
.mark: again
; pass by reference - "report_state_of_the_wall" function decrements the counter!
call (frame ^[(paref 0 0)]) report_state_of_the_wall
branch ^(izero 0) again
end
.end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment