languages

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

project2.hla (5773B)


      1 /* Individual Project 2
      2  *
      3  * Evan Alba
      4  * 12/11/2022
      5  */
      6 program dynamic_array;
      7 
      8 #include("stdlib.hhf");
      9 
     10 static
     11 	address: dword;
     12 	size: uns32 := 0;
     13 	sum: int8 := 0;
     14 	average: int8 := 0;
     15 	minimum: int8 := 0;
     16 	maximum: int8 := 0;
     17 
     18 /*
     19  * Allow the user to input to pick the size of a dynamic array and return that 
     20  * size they want between a minimum and maximum range.
     21  */
     22 procedure pick_size(min: int8; max: int8);
     23 begin pick_size;
     24 	forever
     25 		stdout.put("Enter the number of values desired (", min, 
     26 		" to ", max, "): ");
     27 		try
     28 			stdin.geti8();
     29 			unprotected
     30         			stdin.flushInput();
     31 				breakif((type int8 al) >= min && 
     32 				(type int8 al) <= max);
     33         			stdout.put("Input is not between ", min, 
     34 				" and ", max, ".", nl, nl);
     35 		anyexception
     36 			stdout.put("Not a valid signed 8-bit integer.", nl, 
     37 			nl);
     38 		endtry;
     39 	endfor;
     40 	mov(eax, size);
     41 end pick_size;
     42 
     43 /* 
     44  * Dynamically allocate a number of bytes to match the number of integers.
     45  * (int8) 
     46  */ 
     47 procedure allocate_array;
     48 begin allocate_array;
     49 	mov(@size(int8), eax);
     50 	mul(size);
     51 	mem.zalloc(eax);
     52 	mov(eax, address);
     53 	stdout.put(size, " bytes have been allocated.", nl, nl);
     54 	stdout.put("Press ENTER to continue");
     55 	stdin.readLn();
     56 	console.cls();
     57 end allocate_array;
     58 
     59 /* Allow the user to manually enter values into their new dynamic array. */
     60 procedure manually_enter_values;
     61 begin manually_enter_values;
     62 	for (mov(0, ecx); ecx < size; inc(ecx)) do
     63 		forever
     64 			stdout.put("Enter a value from -10 to 10 ", 
     65 			"for index #", (type int32 ecx), ": ");
     66 			try
     67 				stdin.geti8();
     68 				unprotected
     69         				stdin.flushInput();
     70 					breakif((type int8 al) >= -10 
     71 					&& (type int8 al) <= 10);
     72         				stdout.put("Input is not an ", 
     73 					"integer from -10 to 10.", nl, 
     74 					nl);
     75 			anyexception
     76 			stdout.put("Not a valid signed 8-bit ", "integer.", 
     77 			nl, nl);
     78 			endtry;
     79 		endfor;
     80 		mov(al, [ebx + ecx * 1]);
     81 	endfor;
     82 end manually_enter_values;
     83 
     84 /* Fill the dynamic array with random values varying a range of -10 to 10. */
     85 procedure use_random_values;
     86 begin use_random_values;
     87 	for (mov(0, ecx); ecx < size; inc(ecx)) do
     88 		rand.urange(-10, 10);
     89 		mov(eax, [ebx + ecx * 1]);
     90 	endfor;
     91 end use_random_values;
     92 
     93 /* 
     94  * Allow the user to select a mechanism for populating the array with values.
     95  * Mechanisms available:
     96  * 1. Manually enter values
     97  * 2. Use random values
     98  */
     99 procedure populate_array;
    100 begin populate_array;
    101 	forever
    102 		stdout.put("[1] Manually enter values", nl, "[2] Use random ",
    103 		"values", nl, "your choice:  (1 to 2): ");
    104 		try
    105 			stdin.geti8();
    106 			unprotected
    107         		stdin.flushInput();
    108 				breakif((type int8 al) == 1 || (type int8 al)
    109 				== 2);
    110         			stdout.put("Input is not 1 or 2.", nl, nl);
    111 		anyexception
    112 			stdout.put("Not a valid signed 8-bit integer.", nl, 
    113 			nl);
    114 		endtry;
    115 	endfor;
    116 	console.cls();
    117 	mov(address, ebx);
    118 	if (al == 1) then
    119 		manually_enter_values();
    120 	else
    121 		use_random_values();	
    122 		
    123 	endif;
    124 end populate_array;
    125 
    126 /* Show the array on the screen. */
    127 procedure show_array;
    128 begin show_array;
    129     	mov(address, ebx);
    130 	stdout.put("[ ");
    131     	for (mov(0, ecx); ecx < size; inc(ecx)) do
    132     	    stdout.put((type int8 [ebx + ecx * 1]), " ");
    133     	endfor;
    134 	stdout.put("]", nl);
    135 end show_array;
    136 
    137 /* Get the average of the array. */
    138 procedure get_average;
    139 begin get_average;
    140 	mov(sum, al);
    141 	cbw();
    142 	idiv((type int8 size), ax);
    143 	mov(al, average);
    144 end get_average;
    145 
    146 /* Get the sum of the array. */
    147 procedure get_sum;
    148 begin get_sum;
    149 	mov(address, ebx);
    150 	mov(0, sum);
    151 	for (mov(0, ecx); ecx < size; inc(ecx)) do
    152 		mov(sum, al);
    153 		add([ebx + ecx * 1], al);
    154 		mov(al, sum);
    155 	endfor;
    156 end get_sum;
    157 
    158 /* Get the minimum of the array. */
    159 procedure get_minimum;
    160 begin get_minimum;
    161 	mov(address, ebx);
    162 	mov([ebx + 0], al);
    163 	for (mov(1, ecx); ecx < size; inc(ecx)) do
    164 		if ((type int8 [ebx + ecx * 1]) < al) then
    165 			mov([ebx + ecx * 1], al);
    166 		endif;
    167 	endfor;
    168 	mov(al, minimum);
    169 end get_minimum;
    170 
    171 /* Get the maximum of the array. */
    172 procedure get_maximum;
    173 begin get_maximum;
    174 	mov(address, ebx);
    175 	mov([ebx + 0], al);
    176 	for (mov(1, ecx); ecx < size; inc(ecx)) do
    177 		if ((type int8 [ebx + ecx * 1]) > al) then
    178 			mov([ebx + ecx * 1], al);
    179 		endif;
    180 	endfor;
    181 	mov(al, maximum);
    182 end get_maximum;
    183 
    184 /* 
    185  * Post the following details about dynamic array: 
    186  * - Contents of the Array
    187  * - Sum
    188  * - Average
    189  * - Minimum
    190  * - Maximum
    191  */
    192 procedure post_details;
    193 begin post_details;
    194 	console.cls();
    195 	show_array();
    196 	stdout.put("Calculations", nl, "------------------", nl);
    197 	get_sum();
    198 	stdout.put("SUM        = ", sum, nl);
    199 	get_average();
    200 	stdout.put("AVERAGE    = ", average, nl);
    201 	get_minimum();
    202 	stdout.put("MINIMUM    = ", minimum, nl);
    203 	get_maximum();
    204 	stdout.put("MAXIMUM    = ", maximum, nl);
    205 	stdout.put("------------------", nl);
    206 end post_details;
    207 
    208 /* Free the dynamic array and tell the user the array memory has freed. */
    209 procedure delete_array;
    210 begin delete_array;
    211 	mov(address, eax);
    212 	stdout.put(nl, "Memory has been freed", nl, nl);
    213 	mem.free(address);
    214 	mov(0, address);
    215 end delete_array;
    216 
    217 /* 
    218  * Asks the user if they want to go again. "Returns" true in al if they 
    219  * answer yes, otherwise returns false in al.
    220  */
    221 procedure go_again;
    222 begin go_again;
    223 	stdout.put("Go again (y/n)? ");	
    224 	stdin.getc();
    225 	stdin.flushInput();
    226 	if (al == 'y' || al == 'Y') then
    227 		mov(true, al);
    228 	else
    229 		mov(false, al);
    230 	endif;
    231 end go_again;
    232 
    233 begin dynamic_array;
    234 	repeat
    235 		console.cls();
    236 		pick_size(2, 10);
    237 		allocate_array();
    238 		populate_array();
    239 		post_details();
    240 		delete_array();
    241 		go_again();
    242 	until(al == false);	
    243 end dynamic_array;