Lab 1 — Decimal to Roman Numeral

Lab Description:

This lab was about converting decimal numbers to roman numerals. The way the program works is that the program ask the user if they would like to run the program when they want to convert a decimal to roman number and the user enters “1”. Then the program asks the user to enter a decimal number when prompted with the message “Enter Decimal Number”. After the user enters the decimal number, the program outputs the decimal number’s Roman Numeral equivalent. The way I created this program through the use of a repeat statement with the until expression “ebx=0” which contains while statement which has if statements inside of its body. After the program goes through the list of if and elseif statements and finds every expression is false then it ends the while statement. So it outputs the code and asks the user want to run the program again. If they do not want to, they press “0” to exit and the repeat loop ends.

Code:

program DecimalToRoman;
#include( "stdlib.hhf");

static
	Decimal: int32;
	Decision: int32;

begin DecimalToRoman;

repeat
	//User enters the decision

	stdout.put("Do you want to run Decimal to Roman Numeral?, Type 1 for Yes and  Type 0 for No", nl);
	stdin.get(Decision);
	mov(Decision, ebx); 

		if(Decision = 1) then			

			// User enters a decimal
				stdout.put("Enter an Decimal Number: ");	
				stdin.get(Decimal);
				mov(Decimal, eax);

				//Convert Decimal to Roman Numeral

				while( eax > 0) do

					if ( eax >= 1000) then	
						sub( 1000, eax);
						stdout.put("M");

					elseif( eax >= 900) then
						sub( 900, eax);
						stdout.put("CM");

					elseif( eax >= 500) then
						sub( 500, eax);
						stdout.put("D");

					elseif( eax >= 400) then
						sub( 400, eax);
						stdout.put("CD");

					elseif( eax >= 100) then
						sub( 100, eax);
						stdout.put("C");

					elseif( eax >= 90) then
						sub( 90, eax);
						stdout.put("XC");

					elseif( eax >= 50) then
						sub( 50, eax);
						stdout.put("L");

					elseif( eax >= 49) then
						sub( 49, eax);
						stdout.put("IL");

					elseif( eax >= 40) then
						sub( 40, eax);
						stdout.put("XL");

					elseif( eax >= 10) then
						sub( 10, eax);
						stdout.put("X");

					elseif( eax >= 5) then
						sub( 5, eax);
						stdout.put("V");

					elseif( eax >= 4) then
						sub( 4, eax);
						stdout.put("IV");

					elseif( eax >= 1) then
						sub( 1, eax);
						stdout.put("I");
					endif;

				endwhile;

			endif;

		stdout.put(nl);

	until(ebx = 0);

stdout.put(nl);

end DecimalToRoman;

 

Screenshots:

decimaltoroman

Leave a Reply

Your email address will not be published. Required fields are marked *