Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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:

(for codes ADD, AND)

(for codes SRL, SLL)

(for codes ADDI, ANDI)

BEQ

LW immediate

SW immediate

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 makeI(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 $s5, it will return 21.

I have provided starter code which includes testing methods for the three methods you are recommended to write:

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 < regs.length; i++) { passedTest &= testReg(regs[i], i); } return passedTest; }; private static boolean testReg(String regName, int expValue) { if (regToByte(regName) != expValue) System.out.printf("Fail: regToByte(\"%s\") = %d, should be %d ", regName, regToByte(regName), expValue); return regToByte(regName) == expValue; } private static boolean test_makeR() { boolean passedTest = true; int i = makeR((byte) 1, (byte) 1,(byte) 1,(byte) 1,(byte) 1,(byte) 1); if (i != 0b00000100001000010000100001000001) { System.out.println("Test: makeR(1,1,1,1,1,1) = " + Integer.toBinaryString(i) + " failed"); passedTest = false; } i = makeR((byte) 63, (byte) 0,(byte) 31,(byte) 0,(byte) 31,(byte) 0); if (i != 0b11111100000111110000011111000000) { System.out.println("Test: makeR(63,0,31,0,31,0) = " + Integer.toBinaryString(i) + " failed"); passedTest = false; } return passedTest; }; private static boolean test_makeI() { boolean passedTest = true; int i = makeI((byte) 1, (byte) 1,(byte) 1,(short) 1); if (i != 0b00000100001000010000000000000001) { System.out.println("Test: makeI(1,1,1,1) = " + Integer.toBinaryString(i) + " failed"); passedTest = false; } i = makeI((byte) 63, (byte) 0,(byte) 31,(short) 0); if (i != 0b11111100000111110000000000000000) { System.out.println("Test: makeI(63,0,31,0) = " + Integer.toBinaryString(i) + " failed");

passedTest = false; } i = makeI((byte) 63, (byte) 0,(byte) 31,(short) -1); if (i != 0b11111100000111111111111111111111) { System.out.println("Test: makeI(63,0,31,-1) = " + Integer.toBinaryString(i) + " failed"); passedTest = false; } return passedTest; }; }

Required Main Class:

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:

Read a string Operand

If it's DONE, leave

Read in three more strings (the Arguments)

Translate the Operand and and Arguments into a 32 bit number

Print the 32 bit number as binary (hint in instructions)

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. makeI() 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 - (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.

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions

Question

How do you explain the growth experienced by Pepperfry.com ?

Answered: 1 week ago

Question

Develop a program for effectively managing diversity. page 303

Answered: 1 week ago

Question

List the common methods used in selecting human resources. page 239

Answered: 1 week ago