Lab 3

Lab Description:

To understand and demonstrate how to code in HLA (Higher Language Assembly) by creating a program that has 4 functions. The first function will calculate the Summation of a given number. For example, when given a number 4, the result will be 1+2+3+4 = 10. The second function calculates the Product of a number. An example would be, when the input is 4, 4*3*2*1 = 24. The third function will calculate the powers of 2 when asked for the exponent. Any other input will yield an error and will ask the user to select again. The last function will allow the user to exit. The restriction placed upon this program will be no use of loops; such as for or while. Another restriction will be no uses of mathematical functions within the library such as mul (multiplication function in HLA) and so forth. This will allow us to explore the area of what is called “Spaghetti Coding”; the use of jumps (similar to goto from C++). This practice will allow the programmer to use less resources and less work placed on the machine with the use of short and pure assembly language.

Code:

program lab4;
#include("stdlib.hhf")
static //Declaration of variables
	choice: int32;
	one: int32:=1;
	two: int32:=2;
	three: int32:=3;
	four: int32:=4;
	num: int32;
	num2: int32;
	i: int32:=0;
	ii: int32:=0;
	ans: int32;
	zero: int32:=0;

begin lab4;
menu: //menu label
	stdout.put("-----|------------------------|-----", nl);
	stdout.put("-----|          Menu          |-----", nl);
	stdout.put("-----|------------------------|-----", nl);
	stdout.put("-----| 1) Sum Of Number       |-----", nl);
	stdout.put("-----| 2) Product of Number   |-----", nl);
	stdout.put("-----| 3) 2^n                 |-----", nl);
	stdout.put("-----| 4) Exit                |-----", nl);
	stdout.put("-----|------------------------|-----", nl);
	stdout.put("Enter in choice: ");
	stdin.get(choice);
	mov(choice, eax);
	mov(four, ebx);
	cmp(eax,ebx);
	je choice4;
	mov(three,ebx);
	cmp(eax,ebx);
	je choice3;
	mov(two,ebx);
	cmp(eax,ebx);
	je choice2;
	mov(one,ebx);
	cmp(eax,ebx);
	je choice1;
	cmp(eax,ebx);
	ja anythingelse;
	cmp(eax,ebx);
	jb anythingelse;

anythingelse: //anything else label
	stdout.put("Wrong Selection...try again.",nl);
	jmp menu;

choice1: //Menu choice 1 label
	stdout.put("Enter in sum of number: ");
	stdin.get(num);
	mov(num, eax);
	mov(i,ebx);
	mov(0,ans);
	mov(ans,ecx);
	add(1,eax);
	jmp son;

son: //Sum of Numbers label
	add(ebx,ecx);
	add(1,ebx);
	cmp(ebx,eax);
	jb son;
	cmp(ebx,eax);
	je answer;

sop: //Sum of Products Label
	add(eax,ecx);
	add(1,ebx);
	mov(eax,num2);
	stdout.put("ecx: ", num2, nl);
	cmp(ebx,eax);
	jb sop;
	cmp(ebx,eax);
	je sop2;

sop2: //Sum of Products label 2
	sub(1,eax);
	mov(0,ebx);
	mov(eax,num);
	stdout.put("ans: ", num, nl);
	cmp(eax,zero);
	jne sop;
	cmp(eax,zero);
	je answer;

po2: //Power of 2 label
	add(ecx,ecx);
	add(1,ebx);
	cmp(ebx,eax);
	jb po2;
	cmp(ebx,eax);
	je answer;

answer: //Answer Label
	mov(ecx,ans);
	stdout.put("The Answer is: ", ans, nl);
	mov(0,ans);
	jmp menu;

choice2: //option 2 label
	stdout.put("Enter in product of number: ");
	stdin.get(num);
	mov(num, eax);
	mov(i,ebx);
	mov(num,ecx);
	jmp sop;

choice3: //option 3 label
	stdout.put("Enter in power: ");
	stdin.get(num);
	mov(num, eax);
	mov(i,ebx);
	mov(1,ans);
	mov(ans,ecx);
	jmp po2;

choice4: //exit label
	stdout.put("thank you for using my program.", nl);
	jmp endprogram;	

jmp menu;
endprogram: 
end lab4;

Screenshot:

Screenshot from 2014-03-27 06:01:02