Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write the code where it is supposed to be. Do not give me the answer as a MIPS, but a java code. The code

Please write the code where it is supposed to be. Do not give me the answer as a MIPS, but a java code. The code that I will handle have a tester which can be used to test the code and make sure that it works. Please test the code completly before giving it to me.

image text in transcribed

image text in transcribed

image text in transcribed

Code:

import java.util.Scanner; public class Assembler { private static final boolean TEST_MODE = true; public static void main(String[] args) { int count = 0; if (TEST_MODE) { testCases(); } else { System.out.println("Assembler - Your Name Here "); // Your code here System.out.println(" *** Assembly complete. Program required " + count + " words of memory."); } } private static int makeR(byte opcode, byte rs, byte rt, byte rd, byte shamt, byte funct) { int returnValue = 0; return returnValue; } private static int makeI(byte opcode, byte rs, byte rt, short immed) { int returnValue = 0; return returnValue; } private static byte regToByte(String r) { byte returnValue = 0; return returnValue; } /************************************************************* * * Test Code below * Do NOT modify * ************************************************************/ private static void testCases() { if (test_regToByte()) System.out.println("RegToByte working well "); else System.out.println("RegToByte failed "); if (test_makeR()) System.out.println("makeR working well "); else System.out.println("makeR failed "); if (test_makeI()) System.out.println("makeI working well "); else System.out.println("makeI failed "); } private static boolean test_regToByte() { boolean passedTest = true; String[] regs = { "$ZERO", "$AT", "$V0", "$V1", "$A0", "$A1", "$A2", "$A3", "$T0", "$T1", "$T2", "$T3", "$T4", "$T5", "$T6", "$T7", "$S0", "$S1", "$S2", "$S3", "$S4", "$S5", "$S6", "$S7", "$T8", "$T9" }; for (int i = 0; i  

image text in transcribed

Output:

Assembler - (Your Name) *** Begin entering Assembler: ADD $v0 $v1 $zero AND $a0 $a1 $a2 ADDI $a3 $t4 -321 ANDI $t0 $t5 123 BEQ $s0 $t1 +517 LW $s1 -12 $t2 SW $s2 20 $t3 SRL $a2 $a3 3 SLL $a3 $a2 31 HALT ***: 00000000011000000001000000100000 ***: 00000000101001100010000000100100 ***: 00100001100001111111111010111111 ***: 00110001101010000000000001111011 ***: 00010010000010010000001000000101 ***: 10001101010100011111111111110100 ***: 10101101011100100000000000010100 ***: 00000000000001110011000011000010 ***: 00000000000001100011111111000000 *** Assembly complete. Program required 9 words of memory.

Remember to test the code before sending it to me and make sure that the output it is the same as shown.

Problem Description: An Assembler translates a program in symbolic assembly code into native machine code. Create a mini-Assembler for a 32-bit MIPS processor. Your program should accept input lines from the user until HALT is read. With each line, translate the line into MIPS machine code. Print the total memory space needed for the completed program. To make the parsing easier, there is one instruction per line, and each token is separated by spaces (no commas, parentheses or labels). You should convert all input to upper case. You do not need to check for illegal codes or registers. If a , , or is too large for the field, mask off the bits you need. The form for all assembly lines will be: - code >rdrsrt (for codes ADD, AND) - code rdrt shift_amount (for codes SRL, SLL) - code rr rs immediate (for codes ADDI, ANDI) - BEQrsrt branch_address - LW immediate - SW immediate rs> Your Assembler should accept the following subset of the complete ISA: - Functions: ADD, ADDI, AND, ANDI, BEQ, LW, SW, SRL, SLL - Registers: \$zero, \$v0-1, \$a0-3, \$t0-9, \$s0-7 - Immediates and shift amounts should be read in decimal - Branch addresses should be read as a decimal offset from PC +4 (positive or negative). That is, you don't need to do any address arithmetic, just use the immediate given. Notes: - Do not create these classes in a package. - Turn in only your Java source files. - Despite what the example below looks like, your assembler can read and translate one line at a time. It does not need to read in the whole file first. In the example below, I cut and pasted nine lines into the running program. So you main program can be something like: - Until HALT - Read a line - Translate it - Print it - Printing a 32-bit binary number can be done in Java with the following: System.out.println(" :"+ String.format("\%32s", Integer.toBinaryString(word)).replace(" ", "0")); - You may find it useful to define methods similar to the following: int makeR(byte opcode, byte rs, byte rt, byte rd, byte shamt, byte funct) Constructs a 32-bit integer from the component parts for an R-type instruction. int makel(byte opcode, byte rs, byte rt, short immed) Constructs a 32-bit integer from the component parts for an I-type instruction. byte regToByte(String r ) Converts a string representation of a register to its numeric equivalent. For example, if you pass in " $55 ", it will return 21 . Why 21 ? Check you green sheet, bottom righthand corner). Assembler Required Input: Lines of assembler code, separated with newlines, followed by a line with "HALT". Some Additional Thoughts: Despite what the sample run looks like, the program doesn't read in a bunch of input, translate it all, then spit out all the binary. Instead, it processes just one line at a time. It looks the way it does in the Sample because I cut and paste all the input in at once. Therefore, your program could follow logic like this: 1. Read a string Operand 2. If it's DONE, leave 3. Read in three more strings (the Arguments) 4. Translate the Operand and and Arguments into a 32 bit number 5. Print the 32 bit number as binary (hint in instructions) 6. Repeat If you read these instructions carefully, I give a couple important hints. I strongly suggest writing and TESTING reg2Byte() and makeR() first. Once these are working, the translation is very straight-forward and covered in the lectures where I discuss instruction encoding. makel() is very similar to makeR(), once you have that working. In the starter file, I have included code that will test your methods. reg2Byte might just be a large switch-case statement, or a bunch or if's. Required Output: Your output should look something like the following example. It should include your name. Assembler Required Input: Lines of assembler code, separated with newlines, followed by a line with "HALT". Some Additional Thoughts: Despite what the sample run looks like, the program doesn't read in a bunch of input, translate it all, then spit out all the binary. Instead, it processes just one line at a time. It looks the way it does in the Sample because I cut and paste all the input in at once. Therefore, your program could follow logic like this: 1. Read a string Operand 2. If it's DONE, leave 3. Read in three more strings (the Arguments) 4. Translate the Operand and and Arguments into a 32 bit number 5. Print the 32 bit number as binary (hint in instructions) 6. Repeat If you read these instructions carefully, I give a couple important hints. I strongly suggest writing and TESTING reg2Byte() and makeR() first. Once these are working, the translation is very straight-forward and covered in the lectures where I discuss instruction encoding. makel() is very similar to makeR(), once you have that working. In the starter file, I have included code that will test your methods. reg2Byte might just be a large switch-case statement, or a bunch or if's. Required Output: Your output should look something like the following example. It should include your name

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Administration The Essential Reference

Authors: Brian Laskey, David Kreines

1st Edition

1565925165, 978-1565925168

More Books

Students also viewed these Databases questions