Lab 2
Objective: The objective of this lab was to observe the characteristics of computer calculations through creating an HLA based calculator. By doing so, it was possible to observe how data is stored and produced through registers. Primitive functions such as ‘pow’ and ‘mul’, were prohibited when programming in order to gain further insight on how computers translate high level commands to simple machine language. Furthermore, the functions of the ‘for’ loop was observed.
Code: The source code for the calculator can be seen below. This calculator also incorporates the use of a Arabic to Roman numeral converter that was previously programmed in Lab 1. This function was implemented into the program by using it as a procedure, similarly to a function in C++ programming. Unfortunately, the exponential features of the calculator still contains errors.
//CET 3510 Shinquella Glasgow Lab# 2 /*This is a calculator that has exponent, multiplication, addition,subtraction, and arabic to roman numeral converter feature*/ program calculator; #include ("stdlib.hhf"); //Assign variables static a: int32; x: int32; y: int32; z: int32; j: int32; i: int32; //Procecure for roman numeral converter procedure roman_numerals; begin roman_numerals; repeat stdout.put ("Arabic to Roman numeral converter. Enter a number from 1 to. Press 0 to exit and use calculator", nl); //get user input from keyboard stdin.get(a); //Move a user input to register mov(a,eax); mov(a,ebx); //Roman numeral Conversion Loop while(eax>0) do if(eax>=100) then stdout.put("C"); sub(100,eax); elseif (eax>=99)then stdout.put("IC"); sub(99,eax); elseif (eax>=90)then stdout.put("XC"); sub(90,eax); elseif(eax>=50)then stdout.put("L"); sub(50,eax); elseif(eax>=49)then stdout.put("IL"); sub(49,eax); elseif(eax>=40)then stdout.put("XL"); sub(40,eax); elseif(eax>=10)then stdout.put("X"); sub(10,eax); elseif(eax>=9)then stdout.put("IX"); sub(9,eax); elseif(eax>=5)then stdout.put("V"); sub(5,eax); elseif(eax>=4)then stdout.put("IV"); sub(4,eax); elseif(eax>=1)then stdout.put("I"); sub(1,eax); endif; endwhile; stdout.put(nl); until(ebx==0); end roman_numerals; //begin program begin calculator; call roman_numerals; repeat //||||||||||||||||||||||||||||||Addition||||||||||||||||||||||||||||||||||||||||||||| stdout.put("Addition") stdout.put(nl); stdout.put("Enter a value for x:"); stdin.geti32(); mov(eax, x); stdout.put("Enter a value for y:"); stdin.geti32(); mov(eax, y); //Add numbers mov(x,eax); mov(y,ebx); add(eax, ebx); mov(ebx, z); stdout.put(z, nl); //|||||||||||||||||||||||||||||||||||||Subtraction|||||||||||||||||||||||||||||||||||| stdout.put("Subtraction") stdout.put(nl); stdout.put("Enter a value for x:"); stdin.geti32(); mov(eax, x); stdout.put("Enter a value for y:"); stdin.geti32(); mov(eax, y); // Subtract numbers mov(x,eax); mov(y,ebx); sub(ebx,eax); mov(eax,z); stdout.puti32(z); stdout.put(nl); //|||||||||||||||||||||||||||||Multiplication||||||||||||||||||||||||||||||||||||||| stdout.put("Multiplication") stdout.put(nl); stdout.put("Enter a value for x:"); stdin.geti32(); mov(eax, x); stdout.put("Enter a value for y:"); stdin.geti32(); mov(eax, y); // Multipy numbers mov(0,eax); mov(y,ebx); for (mov(0,ecx);ecx<ebx; inc(ecx)) do add(x,eax); endfor; stdout.puti32(eax); stdout.put(nl); //|||||||||||||||||||||||||||||Exponent||||||||||||||||||||||||||||||||||||||| stdout.put("Exponent") stdout.put(nl); stdout.put("Enter a value for x:"); stdin.geti32(); mov(eax, x); stdout.put("Enter a value for y:"); stdin.geti32(); mov(eax, y); // Exponent numbers mov(x,eax); mov(y,ebx); mov(1,eax); for (mov(0,ecx);ecx<ebx; inc(ecx)) do mov(0,ecx); for (mov(0,ecx);ecx<ebx; inc(ecx)) do add(ebx,y); mov(ebx,z); endfor; endfor; stdout.puti32(z); stdout.put(nl); stdout.put(nl); until(ebx==0); end calculator;
Screen Shot: The image below is what the program outputs once executed in the terminal.