Write a program in LC-3 machine language to find if a given input is a power of
ID: 3600088 • Letter: W
Question
Write a program in LC-3 machine language to find if a given input is a power of 2 and a perfect square. If it is, then write its exponent (as power of 2) as the result. Otherwise, write-1 as the result. The input number is given to you in memory location x2FFE and the result has to be put in x2FFF. Start your program at x3000. First, develop an algorithm to capture your thoughts. Now translate the algorithm into code (assembly) and then write the code in binary. Verify your code is correct by writing the code in assembly, assembling it and see if the generated binary matches your code. Test your solution to make sure it works for all possible inputs. Be judicious in choosing your test cases so you don't have to be exhaustive. The first line of your program should be the binary of the address where your program will load to. The LC-3 simulator will place your program starting at that address. For this assignment, you A3 The Instruction Set 525 15 14 13 12 11 109 87 6 5 4 3 210 SR1 0 00 SR2 SR11 SR1 0 00 SR2 ADD AND AND+ BR JMP JSR DR 0101 0101 DR SR1 1 1100 0100 1 PCoffiset11 LD LDI LDR LEA NOT RET RTI ST STI STR TRAP 0000 0010 1010 PCoffiset 0110 DR BaseR 1110 1001 DR SR 1100 1000 0011 1011 0111 SR BaseR 1101 Figure A.2 Format of the entire LC-3 instruction set. Note: +indicates instructions that modify condition codesExplanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void ADD();
void Delete1();
void Display1();
void Alphabetical1();
void Number1();
void Random1();
void DeleteAll1();
typedef struct Phone_Book_List {
char *FName;
char *LName;
char *PhoneNumber1;
} list;
typedef struct Delete_Entry
{
char *FName;
char *LName;
} take;
//Pointer
list *lt1;
take *tk1;
//globally
int cnt = 0;
int delCnt = 0;
int main(void)
{
//Variables
int iSel;
do {
printf(" PHONE BOOK: ");
printf("1) ADD FRIEND ");
printf("2) DELETE FRIEND ");
printf("3) DISPLAY PHONE BOOK ");
printf("4) SORT ALPHABETICALLY ");
printf("5) FIND PHONE NUMBER ");
printf("6) RANDOM CONTACT ");
printf("7) DELETE ALL CONTACTS ");
printf("8) EXIT ");
printf("WHAT DO YOU WANT TO DO? ");
scanf("%d", &iSel);
switch (iSel) {
case 1: //Add
ADD();
break;
case 2: //Delete
Delete1();
break;
case 3: //Display
Display1();
break;
case 4: //Sort
Alphabetical1();
break;
case 5: //Find
Number1();
break;
case 6: //Random
Random1();
break;
case 7: //Delete all
DeleteAll1();
break;
case 8: //Quit
break;
default:
printf(" Invalid selection ");
break;
}
}
while (iSel != 8);
free(tk1);
free(lt1);
tk1 = NULL;
lt1 = NULL;
return 0;
}
//FUNCTION
void ADD()
{
char *leftName;
if (cnt == 0)
{
lt1 = (list *) malloc ((cnt*25) + 25);
}
else
{
lt1 = (list *) realloc (lt1, (cnt*50) + 50);
}
if (lt1 == NULL)
{
printf("YOU CANNOT ADD MORE MEMORY ");
}
else
{
lt1[cnt].FName = (char *) malloc(sizeof(char)*15);
lt1[cnt].LName = (char *) malloc(sizeof(char)*15);
lt1[cnt].PhoneNumber1 = (char *) malloc(sizeof(char)*15);
printf(" ENTER FIRST NAME: ");
scanf("%s", lt1[cnt].FName);
printf(" ENTER LAST NAME: ");
scanf("%s", lt1[cnt].LName);
printf(" ENTER PHONE NUMBER: ");
scanf("%s", lt1[cnt].PhoneNumber1);
printf(" CONTACT ADDED ");
}
cnt++;
}
//Deletes Contact
void Delete1()
{
int i;
int q = 0;
char *uName;
if (delCnt == 0)
{
tk1 = (take *) malloc ((delCnt*25) + 25);
}
else
{
tk1 = (take *) realloc (tk1, (delCnt*1) + 1);
}
if (tk1 == NULL)
{
printf("Out of memory can not Delete ");
}
else
{
tk1[delCnt].FName = (char *) malloc(sizeof(char)*15);
tk1[delCnt].LName = (char *) malloc(sizeof(char)*15);
printf(" ENTER THEIR FIRST NAME: ");
scanf("%s", tk1[delCnt].FName);
printf(" ENTER THEIR LAST NAME: ");
scanf("%s", tk1[delCnt].LName);
}
for (i = 0; i < cnt; i++)
{
if (lt1[i].FName == NULL && lt1[i].LName == NULL) continue;
if (strcmp(lt1[i].FName, tk1[delCnt].FName) == 0 && strcmp(lt1[i].LName, tk1[delCnt].LName) == 0)
{
printf(" %s %s has been deleted ", lt1[i].FName, lt1[i].LName);
lt1[i].FName = NULL;
lt1[i].LName = NULL;
lt1[i].PhoneNumber1 = NULL;
q = 1;
break;
}
}
if (q != 1)
{
printf(" Contact not in the Phonebook ");
}
delCnt++;
cnt--;
}
void Display1()
{
int i;
printf(" CONTACTS: ");
for (i = 0; i < cnt; i++)
{
if (lt1[i].FName != NULL && lt1[i].LName != NULL)
{
printf(" %s %s: %s ", lt1[i].FName, lt1[i].LName, lt1[i].PhoneNumber1);
}
}
system("pause");
}
//Alphabetically
void Alphabetical1()
{
int i;
int j;
char temp[50][50];
printf(" CONTACTS: ");
for (i = 0; i < cnt; i++)
{
for (j = i + 1; j < cnt; j++)
{
if (strcmp(lt1[i].LName, lt1[j].LName) > 0)
{
strcpy(temp[i], lt1[i].LName);
strcpy(lt1[i].LName, lt1[j].LName);
strcpy(lt1[j].LName, temp[i]);
}
}
}
for (i = 0; i < cnt; i++)
printf(" %s %s: %s ", lt1[i].FName, lt1[i].LName, lt1[i].PhoneNumber1);
system("pause");
}//End function
void Number1()
{
int i;
int q = 0;
char fName[25];
char lName[25];
printf(" Enter First Name: ");
scanf("%s", fName);
printf(" Enter Last Name: ");
scanf("%s", lName);
for (i = 0; i < cnt; i++)
{
if (strcmp(lt1[i].FName, fName) == 0 && strcmp(lt1[i].LName, lName) == 0)
{
printf(" %s %s's PHONE NUMBER IS: %s ", lt1[i].FName, lt1[i].LName, lt1[i].PhoneNumber1);
q = 1;
break;
}
}
if (q != 1)
{
printf(" Not in the Phonebook ");
}
system("pause");
}
void Random1()
{
srand(time(NULL));
int iRandomNum;
iRandomNum = (rand() % cnt) + 1;
printf("%s %s: %s ", lt1[iRandomNum].FName, lt1[iRandomNum].LName, lt1[iRandomNum].PhoneNumber1);
system("pause");
}
void DeleteAll1()
{
int i;
for (i = 0; i < cnt; i++)
{
do{
lt1[i].FName = NULL;
lt1[i].LName = NULL;
lt1[i].PhoneNumber1 = NULL;
break;
}
while (i <= cnt);
}
printf(" ALL CONTACTS DELETED ");
system("pause"); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.