languages

A collection of programs made with different programming languages.
git clone git://evanalba.com/languages
Log | Files | Refs

commit 33243650bfe20374a0cea098153a344e970dd49b
parent 5849b32ba608b3f782cb437ae04e5c0318f523fb
Author: Evan Alba <evanalba@protonmail.com>
Date:   Mon, 12 Dec 2022 09:20:22 -0800

feat: Added High Level Assembly directory.

Diffstat:
Ahigh level assembly/project2.hla | 243+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 243 insertions(+), 0 deletions(-)

diff --git a/high level assembly/project2.hla b/high level assembly/project2.hla @@ -0,0 +1,243 @@ +/* Individual Project 2 + * + * Evan Alba + * 12/11/2022 + */ +program dynamic_array; + +#include("stdlib.hhf"); + +static + address: dword; + size: uns32 := 0; + sum: int8 := 0; + average: int8 := 0; + minimum: int8 := 0; + maximum: int8 := 0; + +/* + * Allow the user to input to pick the size of a dynamic array and return that + * size they want between a minimum and maximum range. + */ +procedure pick_size(min: int8; max: int8); +begin pick_size; + forever + stdout.put("Enter the number of values desired (", min, + " to ", max, "): "); + try + stdin.geti8(); + unprotected + stdin.flushInput(); + breakif((type int8 al) >= min && + (type int8 al) <= max); + stdout.put("Input is not between ", min, + " and ", max, ".", nl, nl); + anyexception + stdout.put("Not a valid signed 8-bit integer.", nl, + nl); + endtry; + endfor; + mov(eax, size); +end pick_size; + +/* + * Dynamically allocate a number of bytes to match the number of integers. + * (int8) + */ +procedure allocate_array; +begin allocate_array; + mov(@size(int8), eax); + mul(size); + mem.zalloc(eax); + mov(eax, address); + stdout.put(size, " bytes have been allocated.", nl, nl); + stdout.put("Press ENTER to continue"); + stdin.readLn(); + console.cls(); +end allocate_array; + +/* Allow the user to manually enter values into their new dynamic array. */ +procedure manually_enter_values; +begin manually_enter_values; + for (mov(0, ecx); ecx < size; inc(ecx)) do + forever + stdout.put("Enter a value from -10 to 10 ", + "for index #", (type int32 ecx), ": "); + try + stdin.geti8(); + unprotected + stdin.flushInput(); + breakif((type int8 al) >= -10 + && (type int8 al) <= 10); + stdout.put("Input is not an ", + "integer from -10 to 10.", nl, + nl); + anyexception + stdout.put("Not a valid signed 8-bit ", "integer.", + nl, nl); + endtry; + endfor; + mov(al, [ebx + ecx * 1]); + endfor; +end manually_enter_values; + +/* Fill the dynamic array with random values varying a range of -10 to 10. */ +procedure use_random_values; +begin use_random_values; + for (mov(0, ecx); ecx < size; inc(ecx)) do + rand.urange(-10, 10); + mov(eax, [ebx + ecx * 1]); + endfor; +end use_random_values; + +/* + * Allow the user to select a mechanism for populating the array with values. + * Mechanisms available: + * 1. Manually enter values + * 2. Use random values + */ +procedure populate_array; +begin populate_array; + forever + stdout.put("[1] Manually enter values", nl, "[2] Use random ", + "values", nl, "your choice: (1 to 2): "); + try + stdin.geti8(); + unprotected + stdin.flushInput(); + breakif((type int8 al) == 1 || (type int8 al) + == 2); + stdout.put("Input is not 1 or 2.", nl, nl); + anyexception + stdout.put("Not a valid signed 8-bit integer.", nl, + nl); + endtry; + endfor; + console.cls(); + mov(address, ebx); + if (al == 1) then + manually_enter_values(); + else + use_random_values(); + + endif; +end populate_array; + +/* Show the array on the screen. */ +procedure show_array; +begin show_array; + mov(address, ebx); + stdout.put("[ "); + for (mov(0, ecx); ecx < size; inc(ecx)) do + stdout.put((type int8 [ebx + ecx * 1]), " "); + endfor; + stdout.put("]", nl); +end show_array; + +/* Get the average of the array. */ +procedure get_average; +begin get_average; + mov(sum, al); + cbw(); + idiv((type int8 size), ax); + mov(al, average); +end get_average; + +/* Get the sum of the array. */ +procedure get_sum; +begin get_sum; + mov(address, ebx); + mov(0, sum); + for (mov(0, ecx); ecx < size; inc(ecx)) do + mov(sum, al); + add([ebx + ecx * 1], al); + mov(al, sum); + endfor; +end get_sum; + +/* Get the minimum of the array. */ +procedure get_minimum; +begin get_minimum; + mov(address, ebx); + mov([ebx + 0], al); + for (mov(1, ecx); ecx < size; inc(ecx)) do + if ((type int8 [ebx + ecx * 1]) < al) then + mov([ebx + ecx * 1], al); + endif; + endfor; + mov(al, minimum); +end get_minimum; + +/* Get the maximum of the array. */ +procedure get_maximum; +begin get_maximum; + mov(address, ebx); + mov([ebx + 0], al); + for (mov(1, ecx); ecx < size; inc(ecx)) do + if ((type int8 [ebx + ecx * 1]) > al) then + mov([ebx + ecx * 1], al); + endif; + endfor; + mov(al, maximum); +end get_maximum; + +/* + * Post the following details about dynamic array: + * - Contents of the Array + * - Sum + * - Average + * - Minimum + * - Maximum + */ +procedure post_details; +begin post_details; + console.cls(); + show_array(); + stdout.put("Calculations", nl, "------------------", nl); + get_sum(); + stdout.put("SUM = ", sum, nl); + get_average(); + stdout.put("AVERAGE = ", average, nl); + get_minimum(); + stdout.put("MINIMUM = ", minimum, nl); + get_maximum(); + stdout.put("MAXIMUM = ", maximum, nl); + stdout.put("------------------", nl); +end post_details; + +/* Free the dynamic array and tell the user the array memory has freed. */ +procedure delete_array; +begin delete_array; + mov(address, eax); + stdout.put(nl, "Memory has been freed", nl, nl); + mem.free(address); + mov(0, address); +end delete_array; + +/* + * Asks the user if they want to go again. "Returns" true in al if they + * answer yes, otherwise returns false in al. + */ +procedure go_again; +begin go_again; + stdout.put("Go again (y/n)? "); + stdin.getc(); + stdin.flushInput(); + if (al == 'y' || al == 'Y') then + mov(true, al); + else + mov(false, al); + endif; +end go_again; + +begin dynamic_array; + repeat + console.cls(); + pick_size(2, 10); + allocate_array(); + populate_array(); + post_details(); + delete_array(); + go_again(); + until(al == false); +end dynamic_array;