CET 3510 Lab 3

Lab 3

Objective: The objective of this experiment was to become familiarized with real Assembly language by implementing the functions of label and jump commands in HLA coding. In HLA and most high level programing languages, loops such as “while”, “if”, etc. are often primitives which makes it easy for a programmer to create specific features in a code. However, these loops must be broken down into assembly language in order for the computer processor to execute the command. Assembly language is used by the processor to execute these commands. In Assembly, there are no loop functions. Instead label and jumps are used to execute the same functions. Labels and jumps are a bit more difficult to crate than a loop for a programmer, but it is much faster and easier for a computer processor to execute. By using a pre-existing calculator program and replacing all loops with labels and jumps, this allowed further insight on how the CPU translates high level languages, into Assembly language.

Code: Below is the code for a calculator program tha uses labels and jumps instead of loops.

/*CET 3510 Shinquella Glasgow 
Lab# 3
This is the calculator that uses jumps and labels instead of loops.*/

program calculator;

#include ("stdlib.hhf");

//Assign variables

static

	x: int32;
	y: int32;
	z: int32;
	c: int32;

begin calculator;

//This label allows a user to enter their choice

menulabel:

	stdout.put(nl, "Enter 1 for addition, 2 for subtraction, or 3 for multiplication.")
		stdout.put(nl);
		stdin.get(c);

//This checks if the users input is a valid choice

operation:
      mov(0,ebx);
      mov(c,eax);
      mov(4,ecx);
      cmp(eax,ebx);
           jle error1;
      cmp(eax,ecx);
           jge error2;
      cmp(eax,1);
           je addition;
      cmp(eax,2);
           je subtraction;
      cmp(eax,3);
           je multiplication;

//||||||||||||||||||||||||||||||Addition|||||||||||||||||||||||||||||||||||||||||

addition:

stdout.put("Enter a value for x:");

	stdin.get(x);
		mov(x, eax);

stdout.put("Enter a value for y:");

	stdin.get(y);
		mov(y, ebx);

//Add numbers

	add(eax,ebx);
		mov(ebx,z);
		stdout.put(z, nl);

jmp questionlabel;
stdout.put(nl);

//|||||||||||||||||||||||||||||||||||||Subtraction||||||||||||||||||||||||||||||||||||

subtraction:

stdout.put("Enter a value for x:");

	stdin.get(x);
		mov(x,eax);

stdout.put("Enter a value for y:");

	stdin.get(y);
		mov(y,ebx);

// Subtract numbers

	mov(x,eax);
	mov(y,ebx);

	sub(ebx,eax);
		mov(eax,z);
		stdout.puti32(z);
		stdout.put(nl);

jmp questionlabel;
stdout.put(nl);

//||||||||||||||||||||||||Multiplication|||||||||||||||||||||||||||||||

multiplication:

stdout.put("Enter a value for x:");

	stdin.get(x);
	mov(x,eax);

stdout.put("Enter a value for y:");

	stdin.get(y);
	mov(y,ebx);

// Multipy numbers
multiply1:
	mov(0,ecx);

// These are the loops used to multiply

multiply2: 

	cmp(ecx,ebx);
	jnl multiply3;
	inc(ecx);
	add(eax,z);
	jmp multiply2;

multiply3:
	stdout.puti32(z);
	stdout.put(nl);
	jmp questionlabel;
	stdout.put(nl);

//All labels used in the program to display messages are defined below.

questionlabel: 
	stdout.put( nl, "Would you like to  do another operation? Type 1 for yes or enter any other value to exit");
	stdout.put(nl);
	stdin.get(c);
	mov(c,eax);
	cmp(c,1);

je menulabel;
jne TheEndLabel;

error1:
	stdout.put(nl, "Invalid entry");
		jmp menulabel;

error2:
	stdout.put(nl, "Invalid entry");
		jmp menulabel;

TheEndLabel:
	end calculator;

 

Screenshots: The image below depicts what the program enviroment looks like once it is executed.

lab3jump

 

 

Leave a Reply

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