Question

In: Computer Science

Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...

Can someone please convert this java code to C code?

import java.util.LinkedList;
import java.util.List;
public class Phase1 {
/* Translates the MAL instruction to 1-3 TAL instructions
* and returns the TAL instructions in a list
*
* mals: input program as a list of Instruction objects
*
* returns a list of TAL instructions (should be same size or longer than input list)
*/
public static List<Instruction> temp = new LinkedList<>();
public static List<Instruction> mal_to_tal(List<Instruction> mals) {
for (int i = 0; i < mals.size(); i++) {
Instruction current = mals.get(i);
int rs = current.rs;
int rd = current.rd;
int rt = current.rt;
int imm = current.immediate;
int jumpAddress = current.jump_address;
int shiftAmount = current.shift_amount;
String label = current.label;
String branchLabel = current.branch_label;
int upperImm = imm >>> 16;
int lowerImm = imm & 0xFFFF;
int t1 = 0;
int t2 = 0;
int t3 = 0;
int at = 0;
if ((current.instruction_id == Instruction.ID.addiu
|| current.instruction_id == Instruction.ID.ori) && (imm > 65535)) {
at = 1;
temp.add(InstructionFactory.CreateLui(at, upperImm, label));
temp.add(InstructionFactory.CreateOri(at, at, lowerImm));
if (current.instruction_id == Instruction.ID.addiu) temp.add(InstructionFactory.CreateAddu(rt, rs, at));
else temp.add(InstructionFactory.CreateOr(rt, rs, at));
}
else if (current.instruction_id == Instruction.ID.blt) {
//t1 = rt, t2 = rs
if (rt > rs) {
at = 1;
}
temp.add(InstructionFactory.CreateSlt(at, rt, rs));
temp.add(InstructionFactory.CreateBne(at, 0, branchLabel));
}
else if (current.instruction_id == Instruction.ID.bge) {
at = 1;
temp.add(InstructionFactory.CreateSlt(at, rt, rs));
temp.add(InstructionFactory.CreateBeq(at,0, branchLabel));
}
else temp.add(current);
}
return temp;
}

}

to C code:

void mal_to_tal(structArrayList *mals, structArrayList *tals) {

}

Solutions

Expert Solution

Working code implemented in C and appropriate comments provided for better understanding.

Note: I am not able to paste all the code here. That's why I am sharing whole project through a link.

Link: https://gofile.io/d/NvyckR

Mirror Link: https://anonymousfiles.io/oUDFfGmV/

Sample Codes:

Source Code for Phase1.c:

#include "Phase1.h"

void mal_to_tal(struct ArrayList *mals, struct ArrayList *tals) {
   for (int i = 0; i < mals->size; i++) {
       enum ID id = get(mals,i).instruction_id; // get the id number at the start of the loop

       // instruction is addiu
       if (id == 7 && (get(mals, i).immediate >= 65536 || get(mals, i).immediate <= -32768)) {
           struct Instruction inst;
           int shifted = get(mals, i).immediate >> 16; // upper 16 bits of the immediate
           if (shifted > 0xFFFF0000) { shifted -= 0xFFFF0000; }
           inst = newInstruction(lui, 0, 0, 1, shifted, 0, 0, get(mals, i).label, "");
           addLast(tals, inst);

           shifted = get(mals, i).immediate << 16;       // lower 16 bits of the immediate
           shifted = shifted >> 16;
           if (shifted > 0xFFFF0000) { shifted -= 0xFFFF0000; }
           inst = newInstruction(ori, 0, 1, 1, shifted, 0, 0, "", "");
           addLast(tals, inst);

           inst = newInstruction(addu, get(mals, i).rt, get(mals, i).rs, 1, 0, 0, 0, "", "");
           addLast(tals, inst);
       }

       // instruction is ori
       else if (id == 8 && (get(mals, i).immediate >= 65536 || get(mals, i).immediate <= 0)) {
           struct Instruction inst;
           int shifted = get(mals, i).immediate >> 16; // upper 16 bits of the immediate
           if (shifted > 0xFFFF0000) { shifted -= 0xFFFF0000; }
           inst = newInstruction(lui, 0, 0, 1, shifted, 0, 0, get(mals, i).label, "");
           addLast(tals, inst);

           shifted = get(mals, i).immediate << 16;       // lower 16 bits of the immediate
           shifted = shifted >> 16;
           if (shifted > 0xFFFF0000) { shifted -= 0xFFFF0000; }
           inst = newInstruction(ori, 0, 1, 1, shifted, 0, 0, "", "");
           addLast(tals, inst);

           inst = newInstruction(or, get(mals, i).rt, get(mals, i).rs, 1, 0, 0, 0, "", "");
           addLast(tals, inst);
       }

       // instruction is blt
       else if (id == 10) {
           struct Instruction inst = newInstruction(slt, 1, get(mals, i).rt, get(mals, i).rs, 0, 0, 0, get(mals, i).label, "");
           addLast(tals, inst);

           inst = newInstruction(bne, 0, 1, 0, 0, 0, 0, "", get(mals, i).branch_label);
           addLast(tals, inst);
       }

       // instruction is bge
       else if (id == 11) {
           struct Instruction inst = newInstruction(slt, 1, get(mals, i).rt, get(mals, i).rs, 0, 0, 0, get(mals, i).label, "");
           addLast(tals, inst);

           inst = newInstruction(beq, 0, 1, 0, 0, 0, 0, "", get(mals, i).branch_label);
           addLast(tals, inst);
       }

       // instruction is mov
       else if (id == 12) {
           struct Instruction inst = newInstruction(addu, get(mals, i).rd, get(mals, i).rs, get(mals, i).rt, 0 , 0, 0, get(mals, i).label, "");
           addLast(tals, inst);
       }

       // if instruction is already TAL, add it to tals
       else { addLast(tals, get(mals,i)); }  
   }
}

Source Code for Phase1.h:

#pragma once
#include "ArrayList.h"

/* Translates the MAL instruction to 1-3 TAL instructions
* and returns the TAL instructions in a list
*
* mals: input program as a list of Instruction objects
* tals: after the function returns, will contain TAL instructions (should be same size or longer than input list)
*/
void mal_to_tal(struct ArrayList *mals, struct ArrayList *tals);


Related Solutions

CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {    public static void main(String[] args)    {        for(int i = 1; i <= 4; i++)               //loop for i = 1 to 4 folds        {            String fold_string = paperFold(i);   //call paperFold to get the String for i folds            System.out.println("For " + i + " folds we get: " + fold_string);        }    }    public static String paperFold(int numOfFolds)  ...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution {...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); HashMap labs = new HashMap(); while (true) { System.out.println("Choose operation : "); System.out.println("1. Create a Lab"); System.out.println("2. Modify a Lab"); System.out.println("3. Delete a Lab"); System.out.println("4. Assign a pc to a Lab"); System.out.println("5. Remove a pc from a Lab"); System.out.println("6. Quit"); int choice = sc.nextInt(); String name=sc.nextLine(); switch (choice) { case 1:...
Can someone convert this to C++ Please! import java.util.*; // for Random import java.util.Scanner; // for...
Can someone convert this to C++ Please! import java.util.*; // for Random import java.util.Scanner; // for Scanner class game{    public static void main(String args[])    {        // generating a random number        Random rand = new Random();        int code = rand.nextInt(99999) + 1, chances = 1, help, turn, i,match, sum;               // for input        Scanner sc = new Scanner(System.in);               // running for 10 times   ...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
import java.util.LinkedList; import java.util.ListIterator; public class ProjectList { /** This is the main function for the...
import java.util.LinkedList; import java.util.ListIterator; public class ProjectList { /** This is the main function for the program */ public static void main() { System.out.println("\n\tBegin ProjectList demo program");    // 1. TO DO: Describe your project (1 Pt) System.out.println("Project: . . . . . . \n"); System.out.println("Create a LinkedList to store the tasks"); // 2. TO DO: Declare a LinkedList of String objects called "tasks" (1 Pt)       System.out.println("Add initial tasks into the list"); // 3. TO DO: Add initial...
import java.util.LinkedList; import java.util.Queue; import java.time.*; public class CheckOutLine { /** This is the main function...
import java.util.LinkedList; import java.util.Queue; import java.time.*; public class CheckOutLine { /** This is the main function for the program */ public static void main() { System.out.println("\n\tBegin CheckOutLine Demo\n"); System.out.println("\nCreating a Queue called \"register\" based on a LinkedList"); Queue<Customer> register = new LinkedList<Customer>();    System.out.println("\nAdding Customers to the Register queue"); // 1. TO DO: add five or more customers to the register line (5 Pts) // format: register.add(new Customer(name, checkout time));                System.out.printf("Register line has %d customers\n",...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please convert this java program to a program with methods please. import java.io.*; import java.util.*; public...
Please convert this java program to a program with methods please. import java.io.*; import java.util.*; public class Number{ public static void main(String[] args) {    Scanner scan = new Scanner(System.in); System.out.println("Enter 20 integers ranging from -999 to 999 : "); //print statement int[] array = new int[20]; //array of size 20 for(int i=0;i<20;i++){ array[i] = scan.nextInt(); //user input if(array[i]<-999 || array[i]>999){ //check if value is inside the range System.out.println("Please enter a number between -999 to 999"); i--; } } //...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public static void main(String[] args)    {        max_cards=45;        arr->new ArraryList        col=1;        card=0;        left=max_cards;        do{            col->random number            row->new ArrayList;            for i=0 to i<col            {                card++                add card into row            }   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT