How to resolve the algorithm 99 bottles of beer step by step in the Viua VM assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 99 bottles of beer step by step in the Viua VM assembly programming language

Table of Contents

Problem Statement

Display the complete lyrics for the song:     99 Bottles of Beer on the Wall.

The lyrics follow this form: ... and so on, until reaching   0     (zero). Grammatical support for   1 bottle of beer   is optional. As with any puzzle, try to do it in as creative/concise/comical a way as possible (simple, obvious solutions allowed, too).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm 99 bottles of beer step by step in the Viua VM assembly programming language

Source code in the viua programming language

.function: bottles_of_beer_text/1
    .name: %iota number_of_bottles
    arg %number_of_bottles %0

    .name: %iota bottles_of_beer
    ; support for "1 bottle of beer" and "N bottles of beer"
    if (eq %iota %number_of_bottles (istore %iota 1)) +1 +3
    text %bottles_of_beer " bottle of beer"
    jump +2
    text %bottles_of_beer " bottles of beer"

    move %0 %bottles_of_beer
    return
.end

.function: first_print/1
    ; this function prints the
    ;
    ;   N bottles of beer on the wall
    ;   N bottles of beer
    ;   Take one down, pass it around
    ;
    .name: %iota number_of_bottles
    arg %number_of_bottles %0

    .name: %iota bottles_of_beer
    frame ^[(param %0 %number_of_bottles)]
    call %bottles_of_beer bottles_of_beer_text/1

    echo %number_of_bottles
    echo %bottles_of_beer
    print (text %iota " on the wall")
    echo %number_of_bottles
    print %bottles_of_beer
    print (text %iota "Take one down, pass it around")

    return
.end

.function: second_print/1
    ; this function prints the
    ;
    ;   No more bottles of beer on the wall /
    ;   N bottles of beer on the wall
    ;
    ; i.e. the last line of a paragraph
    .name: %iota number_of_bottles
    arg %number_of_bottles %0

    .name: %iota bottles_of_beer
    frame ^[(param %0 %number_of_bottles)]
    call %bottles_of_beer bottles_of_beer_text/1

    .name: %iota on_the_wall
    text %on_the_wall " on the wall"

    ; say "No more" instead of "0 bottles"
    if %number_of_bottles +1 +3
    echo %number_of_bottles
    jump +3
    echo (text %iota "No more")

    echo %bottles_of_beer
    print %on_the_wall

    if %number_of_bottles +1 +3
    print (text %iota "")

    return
.end

.function: bottles_of_beer/1
    .name: %iota total_number_of_bottles
    arg %total_number_of_bottles %0

    ; display first three lines of a paragraph
    frame ^[(param %0 %total_number_of_bottles)]
    call void first_print/1

    ; decrement the number of bottles
    idec %total_number_of_bottles

    ; display last line of a paragraph
    frame ^[(param %0 %total_number_of_bottles)]
    call void second_print/1

    ; immediately return if there are no more bottles
    if %total_number_of_bottles theres_more +1
    return

    .mark: theres_more
    ; if there are more bottles
    ; call the function once more
    frame ^[(pamv %0 %total_number_of_bottles)]
    tailcall bottles_of_beer/1
.end

.function: main/0
    .name: %iota total_number_of_bottles
    istore %total_number_of_bottles 9

    frame ^[(pamv %0 %total_number_of_bottles)]
    call void bottles_of_beer/1

    izero %0 local
    return
.end

  

You may also check:How to resolve the algorithm Middle three digits step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the Scheme programming language
You may also check:How to resolve the algorithm Generic swap step by step in the Lua programming language
You may also check:How to resolve the algorithm Return multiple values step by step in the Harbour programming language
You may also check:How to resolve the algorithm Van Eck sequence step by step in the AppleScript programming language