This LC-3 assembly language program will compute the Least Common Multiple (LCM) of two or three positive integers. The LCM of two numbers {a, b} is the smallest number that is a multiple of both a and b. The LCM of three numbers {a, b, c} is the smallest number that is a multiple of a, b, and c. The program must start at address x3000. Here’s how the program must behave:
1. Before running the program, put the values into R0, R1, and R2. If you only want to find the LCM of two numbers, then put non-zero values into R0 and R1, and put zero in R2. The input numbers must all be 16-bit non-zero, positive 2’s complement integers (between x0001 and x7FFF).
2. Next, run the program until it reaches the end (HALT).
3. After the halt, R3 must contain the LCM value. • If R2 was initially zero, then R3 must be the LCM of R0 and R1. • If R2 was initially non-zero, then R3 must be the LCM of R0, R1, and R2. • In either case, if the LCM cannot be calculated because it is out of range (larger than x7FFF), then R3 must contain -1 (xFFFF). 2 The only value that matters when the program ends is R3. All other registers (including R0, R1, and R2) are allowed to change during execution.
How would you accomplish this using LC-3? it has 15 opcodes
Operate instructions: ADD, AND, NOT
Data movement instructions: LD, LDI, LDR, LEA, ST, STR, STI
Control instructions: BR, JSR/JSRR, JMP, RTI, TRAP and three condition codes, n,z,p.
Here is how to do it with C
#include<stdio.h>#include<stdlib.h>
//function gcd_lcm takes parametrs by refferencevoid gcd_lcm(int &x,int &y, int &gcd, int &lcm){int temp1,temp2;temp1 = x;int b = y;while (b != 0) {temp2 = b;b = temp1 % b;temp1 = temp2;}//set the GCD valuegcd = temp1;//set the LCM valuelcm = (x*y)/gcd;}
--------------------------
//main functionint main() {//local variablesint x, y, gcd, lcm;//Prompt to enter the numbersprintf("Enter the numbers\n");scanf("%d%d", &x, &y);if(x<=0||y<=0){printf("The number enter is not positive\n");system("Pause");exit(0);}gcd_lcm(x,y,gcd,lcm);printf("GCD : %d\n",gcd);printf("LCM : %d\n",lcm);//for visual studio console freezesystem("Pause");return 0;}
START a = b? No p=a q=b p<q? Yes p=p+a Yes- -No- LCM = a p=q? No q<p? Yes q=q+b -No- -Yes- DONE LCM = p Yes q=p? No
This LC-3 assembly language program will compute the Least Common Multiple (LCM) of two or three positive integers. The
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am