Write two-pass assembler for subset of mips instruction set

Assignment Help Assembly Language
Reference no: EM131065207

Project

This assignment will reinforce your knowledge of the assembly process. You will need to go through all of the steps of converting an assembly source file to object code.

Your goal is to write a two-pass assembler for a subset of the MIPS instruction set. It should be able to read an assembly file from the command line and write the object code to standard output. You can make the following assumptions:

- The code segment will precede the data segment
- The source file will contain no more than 32768 distinct instructions
- The source file will define no more than 32768B of data
- The source file will not contain comments
- There will be no whitespace between arguments in each instruction
- Each line may have a symbolic label, terminated with a colon

Table 1 provides a list of the assembly directives that your assembler must recognize. Table 2 provides a list of the instructions that your assembler must recognize. Be sure that you note the arguments for each instruction. It may be helpful to refer to Appendix A.10 when writing your parser.

Table 1. List of Assembly Directives

Directive

Explanation

.text

Place items following this directive in the user text segment

.data

Place items following this directive in the data segment

.word w1,w2,...,wn

Store n 32b integer values in successive words in memory

.space n

Allocate n bytes of space in memory, initialized to zero

Table 2. List of MIPS Instructions

Mnemonic

Format

Args

Descriptions

addiu

I

 

Add immediate with no overflow

addu

R

3 (rd, rs, rt)

Add with no overflow

and

R

3 (rd, rs, rt)

Bitwise logical AND

beq

I

 

Branch when equal

bne

I

 

Branch when not equal

div

R

2 (rs, rt)

Signed integer divide

j

J

 

Jump

lw

I

 

Load 32b word

mfhi

R

1 (rd)

Move from hi register

mflo

R

1 (rd)

Move from low register

mult

R

2 (rs, rt)

Signed integer multiply

or

R

3 (rd, rs, rt)

Bitwise logical OR

slt

R

3 (rd, rs, rt)

Set when less than

subu

R

3 (rd, rs, rt)

Subtract with no overflow

sw

I

 

Store 32b word

syscall

R

0

System call

In addition to the instructions above, your assembler must be able to resolve symbolic labels. These labels may be targets used for changes in the control flow (branch or jump instructions) or as names for memory elements. The way labels are handled differs depending on their usage. Targets for branch instructions should be referenced as the location of the target in memory relative to the current instruction (remember that the PC points to the next instruction). For example, consider the code below:

00400400 <L4>:

400400:

400404:

1100000c

00000000

beqz nop

t0,400434

<L5>

400408:

40040c:

400410:

01084021

1100fffc 00000000

addu beqz nop

t0,t0,t0 t0,400400

 

<L4>

400414:

400418:

40041c:

01084021

1100fff9 00000000

addu beqz nop

t0,t0,t0 t0,400400

 

<L4>

400420:

400424:

01084021

1100fff6

addu beqz

t0,t0,t0 t0,400400

 

<L4>

400428:

00000000

nop

 

 

40042c:

11000001

beqz

t0,400434

<L5>

400430:

00000000

nop

 

 

00400434 <L5>:

  400434: 00000000 nop

You can see that the forward branches to L5 (in pink) have distances of 12 and 1. If you count the instructions from the two branch instructions, you can see that the actual numbers of instructions are 13 and 2 - the PC will have already advanced to the next instruction. The same is true for the backward branches to L4 (the non-colored branches). The branches use two's complement for the target calculations, so the first branch, 0x1100fffc, is at an offset of 0xfffc from the target. If you calculate the decimal value, you should get -4, which is the distance of the label from the PC.

Targets for jump instructions should use the absolute location of the target. For example, assume that label L1 is located in memory at 0x400370. The instruction j L1 will resolve to j 400370.

Data labels should be referenced by their offset from the global pointer, $gp, which is assumed to point to the start of the data segment.

You should use the linprog servers for all of your compilation and testing. Your output should match mine exactly. You can determine if the results are identical by calculating the md5sum or by using diff. You must use C/C++ as your language and your solution should be a single file (e.g. ch03c.pr01.c or ch03c.pr01.cpp). You should submit this file through Blackboard. Your program should have comments inline and a header at the top. For example:

/**
* @file main.cpp
* @author hughes <>, (C) 2014, 2015, 2016
* @date 05/11/16
* @brief Simple MIPS assembler
*
* @section DESCRIPTION
* This program implements an assembler for a subset
* of the MIPS assembly language. Can compile with debug
* by including -DDEBUG in the compiler options.
************************************************************/

Please test your output against the results from the sample binary before submission. The test script uses md5 and diff to compare your output with the baseline. Your submissions will also be processed for plagiarism. The script will use the following for compilation: g++ -Werror -mtune=generic -O0 -std=c++11

If you write it in C instead of C++, the script will use gcc -Werror -mtune=generic -O0 -std=c11

You can access my binary using the following command:
~chughes/cda3101/assembler <input>

There is an example assembly program below in Figure 1 along with the machine code. You can access the assembly source at ~chughes/cda3101/test01.s and the object code at ~chughes/cda3101/test01.obj. You should note that the machine code is in hexadecimal.

 

.text

addu $s0,$zero,$zero addu $s1,$zero,$zero addiu $v0,$zero,5 syscall

sw $v0,n($gp)

 

L1:

lw $s2,n($gp) slt $t0,$s1,$s2 beq $t0,$zero,L2

addiu $v0,$zero,5 syscall

addu $s0,$s0,$v0 addiu $s1,$s1,1 j L1

 

L2:

addu $a0,$s0,$zero addiu $v0,$zero,1 syscall

addiu $v0,$zero,10 syscall

 

.data n: .word 0

m: .word 1,9,12

q: .space 10

 

00008021

00008821

24020005

0000000c af820000 8f920000

0232402a

11000005

24020005

0000000c

02028021

26310001

08000005

02002021

24020001

0000000c

2402000a

0000000c

00000000

00000001

00000009

0000000c

00000000

00000000

00000000

00000000

00000000

00000000

00000000

00000000

00000000

00000000


Figure 1 - Sample source code (left) and object code (right)

A second test file is included in the directory and is named test02.s. These are samples and are not the inputs that will be used for grading. Feel free to write your own inputs and share them via the discussion boards. If you find an error in assembler, please let me know (extra credit)!

While you are free to use any string parsing method you choose, you may find it helpful to use the getline function. getline extracts characters from an input stream and stores them in a string until a delimiter is reached or a newline character is found.

istream& getline (istream& is, string& str);

For example, the code below discards whitespace at the current pointer, reads a line from the input, and pushes the line to a list as a string type.

do
{
std::ws(asmFile); std::getline(asmFile, lineIn);

sourceCode.push_back(lineIn); //add to the list of instructions from source
}while(asmFile.eof() == 0);

You may also find the Boost tokenizer class useful. The tokenizer will parse the input sequence and break the sequence into pieces, depending on a delimiter. The code below takes an input string, input, and seperates it based on the characters defined in delimeter. The for-loop then iterates through those tokens.

boost::char_separator<char> delimeter(", ()");
boost::tokenizer< boost::char_separator< char > > tokens(input, delimeter);

for(boost::tokenizer< boost::char_separator<char> >::iterator it = tokens.begin(); it != tokens.end(); it++)
{
//stuff
}

These are just some of the tools that I used in my solution; you are not required to use them! C/C++ has plenty of functions that you may find useful such as fgets and sscanf. Be creative!

I don't know how many pages it would be since it is programming and the details in the file i uploaded

Verified Expert

The work has been done focusing on the C/C++ language where the assumptions are made where Your goal is to write a two-pass assembler for a subset of the MIPS instruction set. It should be able to read an assembly file from the command line and write the object code to standard output.

Reference no: EM131065207

Questions Cloud

Which division provides the highest operating margin : Which division provides the highest operating margin? Which division provides the lowest after-tax profit margin? Which division has the lowest after-tax return on assets? Compute net income (after-tax) to sales for the entire corporatio..
Check the accuracy of his calculation : With this information, Justin calculated the total standard time required to be within the 320 hours available. Is he correct? Calculate the time required and check the accuracy of his calculation.
Tasked with creating an informative essay : This week, you were tasked with creating an Informative Essay. This essay was a bit different than our last essay because it took a third person point of view and some research to complete!
What is the company unlevered cost of equity capital : Weston Industries has a debt-equity ratio of 1.5. Its WACC is 10.5 percent, and its cost of debt is 6 percent. The corporate tax rate is 35 percent. What is the company's cost of equity capital What is the company's unlevered cost of equity capital W..
Write two-pass assembler for subset of mips instruction set : Write a two-pass assembler for a subset of the MIPS instruction set. It should be able to read an assembly file from the command line and write the object code to standard output.
What is the most he should have paid for the annuity : Mark Ventura has just purchased an annuity to begin payment at the end of 2016 (that is the date of the first payment). Assume it is now the beginning of the year 2014. The annuity is for $20,000 per year and is designed to last 8 years. If the inter..
How much interest will he pay over the life of the loan : Larry Davis borrows $74,000 at 10 percent interest toward the purchase of a home. His mortgage is for 20 years. Use Appendix D for an approximate answer, but calculate your final answer using the formula and financial calculator methods. How much sho..
What is a functional dependency : What is a functional dependency? What are the possible sources of the information that defines the functional dependencies that hold among the attributes of a relation schema
Mitigated by taking forward market positions : Transaction exposure can be mitigated by taking forward market positions (hedging). If you are a US importer of German textiles and have just ordered a one year supply at a cost of 20 million euros, Payment is due in one year. How can you protect (fi..

Reviews

Write a Review

Assembly Language Questions & Answers

  Multiply by using shift and add instructions

How does real time PCR work to quantify the initial amount of the DNA template? How should the method be modified to quantify the initial amount of RNA template extracted from tissue or cells?

  Under what condition is the instruction sequence useful

On a certain computer it is possible to move a number from one register to another, shift each of them left by different amounts. Under what condition is this instruction sequence useful for computing ‘‘constant ´ variable''?

  Program a maze-type game using assembly language

This project requires you to program a maze-type game using Assembly Language. It is not acceptable to use any other programming language for this project. T

  Write assembly program print binary representation-integers

Write the assembly program called hw6_ex1, stored in file hw6_ex1.asm. This program must prompt user to enter signed 32-bit integer. Program must print out binary representation of the integer.

  Analog measurements prepare an assembly program for the

analog measurements prepare an assembly program for the correctly measures the wind direction to a precision of 45deg n

  How many mul operations can this system perform

Compute number of lines in the address bus required so that the CPU can access all memory locations in the given RAM - Write a function that accepts a char array, its size and a char to search for in the given array. Use PROTO, INVOKE and LOCAL keyw..

  Write an assembly language code segment to insert

Write an assembly language code segment to insert into the main.asm file that takes an input controlled by a potentionmeter, converts that input to a digital value, and displays that value on the LEDs according to the following

  What would be the ieee 754 double precision binary

What would be the IEEE 754 double precision binary representation of the floating point value 2.71828×10-13? Express your final answer as a 16-hexdigit number and explain how your answer was obtained for full credit.

  Write an arm assembly program that prints all the integers

Write an ARM assembly program that prints all the integers greater than 0 and less than 1000 that not divisible by 5. You must submit only a single file named "proj1.s" that is capable of running under ARMSim.

  Write an lc-3 assembly language program

Write an Lc-3 assembly language program to read in a sequence of single-digit positive integers from the keyboard(one integer per line) until the sentinel value of 0 is reached and then display the largest integer on the screen

  A screenshot of the modified assembly code

Modify the program to step the lights through a realistic sequence. Include conditional statements that ensure that assembly program only executes for 10 times.

  Prepare a program in mips assembly language

Prepare a program in MIPS assembly language that reads in a sequence of signed 32-bit integers (words) and return the maximum, the minimum, and the median value of the input sequence.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd