0% found this document useful (0 votes)
2 views7 pages

Write Up

This document details the implementation of a basic calculator using MIPS assembly language, covering operations such as addition, subtraction, multiplication, and division. It provides insights into the necessary software, design, and implementation procedures, including utility macros and procedures for handling binary operations. The document also includes truth tables for logical operations and describes how to manage binary representations, including two's complement for negative numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Write Up

This document details the implementation of a basic calculator using MIPS assembly language, covering operations such as addition, subtraction, multiplication, and division. It provides insights into the necessary software, design, and implementation procedures, including utility macros and procedures for handling binary operations. The document also includes truth tables for logical operations and describes how to manage binary representations, including two's complement for negative numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CS47 1

Basic Calculator Implemented By MIPS Logic


Operations
Parameswaran Ranganathan, Computer Scientist, San Jose State University

Abstract—This write up explains the implementation of basic once, load each of them separately. After all the files have
math operations, including addition, subtraction, multiplication been loaded and opened, MARS should look like this.
and division, using the MIPS logical and normal procedures in
Mars 4.5. Pictures of the code are included to help understand
the program.

I. INTRODUCTION
Fig.1. Opening/loading files onto MARS

T HE purpose of logical operators is to calculate various


expressions, which is the basis of digital circuits in
computer hardware. This project serves to implement a
C. Boolean Algebra/logic
Implemented circuitry in computer hardware systems
simple calculator that can perform addition, subtraction, requires a deep understanding of Boolean logic and algebra.
multiplication, and division through the usage of logical In circuits, only the numbers 1 and 0 exists, nothing else.
operators. One must use truth tables to find out the output of a
The calculator that is being implemented is written using Boolean expression.
Here is an example of the AND Boolean operation where t
the MIPS. MIPS is a type of assembly language and the
represents a T and 0 represents a F. AND is the
MARS software (IDE) is what is used to simulate the MIPS.
multiplication of two values.
II. REQUIREMENTS
Table 1: Truth Table for Logical AND
Section II discusses the necessary software needed to write the A B A.B
basic mathematical calculator and also provides the needed 0 0 0
background information to create the calculator and 0 1 0
understand how it works. 1 0 0
A. Necessary Software’s 1 1 1
To run MIPS assembly language, one must use the MARS
In addition to the AND operation, there exists an OR
software as their interactive development environment (IDE).
operation. OR is used for addition of two binary integers in
Mars is a simulator, which acts as a runtime environment for
Boolean algebra. The OR operation returns 0 if both A and
MIPS. MARS can be downloaded on Missouri State B are 0. The following is the truth table of Logical OR.
University website as it is developed by them.
Table 2: Truth Table for Logical OR
B. Setting up the project A B A+B
0 0 0
Go to SJSU Canvas and download the provided zip files.
0 1 1
https://sjsu.instructure.com/courses/1208160/assignments/425 1 0 1
2547 - submit 1 1 1
Download the “CS47project1.zip” and unzip it. It should
include the following files. Finally, there exists an XOR operation, which means
1. Cs47_common_macro.asm exclusive OR. XOR returns 0 if both A and B are either 0 or
2. Cs47_proj_alu_logical.asm 1. The XOR operation returns 1 if only one of the two is 1.
3. Cs47_proj_alu_normal.asm
4. Cs47_proj_macro.asm Table 3: Truth Table for Logical XOR
5. Cs47_proj_procs.asm A B A.B
6. Proj-auto-test.asm 0 0 1
0 1 1
1 0 1
Open the Mars 4_5 jar file downloaded from the Missouri 1 1 0
state university webpage. Go to “File” and click on “open”.

Find your way to the directory, which contains the unzipped


files. Since MARS does not allow you to load all the files at
CS47 2

D. Binary 2) Multiplication
Binary is a number system with base 2, unlike the decimal For unsigned multiplication, a counter ($t0) is initially set to
system, which uses base 10. Since this system is base 2, it 0. The first input, which is the multiplicand, is saved in a
consists of only 2 symbols that can be used, 0 or 1. For temporary register ($t4). The second input, which is the
example, if we used a 4 bit long word, 0 is represented as 0000 multiplier, is put into a temporary register ($t1). This
and 1 is represented as 0001. Once a digit reaches 1, it represents the lo of the multiplication. The high is initially set
becomes 0 and rolls over to the next larger digit and places a 1 to 0 and put into temporary register $t2.
there. Thus, 2 would be represented as 0010. The binary In short, binary multiplication is simply repeated addition.
system is extremely useful when it comes to computers as a What we must know is that 0 x 0 = 0, 1 x 0 = 0, and 1 x 1 = 1.
computer system can only read 1’s and 0’s. If the current bit of the multiplier is 0, then 0 gets placed in
that bit position of the answer. If the multiplier is 1, then
Now the question is, how does one represent a negative simply put the multiplicand into that bit position of the
number in binary? This is where the “two’s compliment” answer.
form comes into play. In two’s compliment, if a 1 exists in the
MSB position, that means the number is negative. If a 0 exists 3) Division
in the MSB position, then the number is positive. For First, we must align the divisor with the MSB bits of the
example, 3 in twos compliment is 0011, where as -3 is 1101. dividend. Compare these bits to the divisor. If these bits are
To obtain the negative of a number, inverse all the bits of the greater than the divisor, the quotient bit is set to 1 and then
positive number and add 1. If we inverse all the bits of +3, we subtraction is performed with MSB bits – divisor. If the MSB
get 1100. Add 1 to this, and we obtain 1101, which is -3. bits are less than the divisor, the quotient bit is set to 0 and
subtraction is not performed. The divisor is then shifted one
III. DESIGN AND IMPLEMENTATION bit to the right and we compare the MSB bits to the divisor
again. This continues until division is over.
Section 3 discusses the design of the arithmetic calculator
and how it’s being implemented in MIPS. This calculator
implements addition, subtraction, multiplication, and division B. Implementation
operations. To implement the calculator operations, many macros and
utility procedures are designed to help make the
A. Design implementation easier.
The design section talks about the design of the addition,
subtraction, multiplication, and division operations. 1) Utility Macros
The 4 macros created are extract_nth_bit, insert_to_nth_bit,
1) Addition and subtraction store_stack_all, and restore_stack_all.
Addition and subtraction both relate in the sense that
subtraction is the addition of the negation of the second i. Extract_nth_bit
integer. For example, A-B = A + (-B). For the addition The extract_nth_bit macro obtains a bit value at a given
operation, a full adder must be implemented since the carry bit position for any integer.
has to be taken into consideration. A half adder can only add
two bits.
The way addition works is a carry bit starts off as 0 and is
added with the LSB bits of the two inputs. The sum is put into
the LSB position of the answer, and the carry bit is found and
added to the next two bits of the inputs. To get the nth digit,
the logical XOR is called on the first bits and is stored. Then,
the logical XOR is called on the stored value and the carry in. The macro contains three arguments passed in as regD,
This results in the sum bit and is stored temporarily. To find regS, regT. regD is the destination register where the
the carry bit, first initialize the carry bit to 0. To find the carry result will be stored. regS is the source register which
bit, first we use the XOR operation on the two inputs and store contains the bit pattern which the calculator will be
it in lets say $t7. AND is called on $t7 and the initial carry bit extracting from. regT holds the position of the bit that
($v1) and is stored in $t8. Then, AND is used on the nth bits will be extracted.
of the two inputs. Finally, OR is called on $t8 and $t1 and is First, $regS is moved into $s0 just so that the original
stored back in the carry bit register which is $v1. source register does not get messed up. Then, $s0 is
These operations are performed 32 times, since there are 32 shifted right by the bit position number and is stored back
bits in each register. Finally, the sum of the two inputs will be into $s0. Finally, AND is used on $s0 and 1 to extract
calculated. the bit and is put into the destination register. Basically,
For subtraction, the only change that must be made is the if $s0 is 0x0, AND’ing it with 1 will result in 0. If $s0, is
complement of the second input must be taken. Then pass the 0x1, AND’ing it with 1 will result in 1. This properly
first input and the complement of the second input into the add extracts the needed bit.
loop and the difference (in this case a sum) will be calculated.
ii. Insert_to_nth_bit
CS47 3

The insert_to_nth_bit macro inserts a given bit into the


nth position of a bit pattern and returns the bit pattern.

The macro contains four arguments passed in as regD, regS,


regT, and maskReg. regD contains the bit pattern in which the
proper bit will be inserted into. regS contains the insertion
position. regT contains what bit will be inserted, either a 1 or
0. Finally, maskReg is any temporary register in which a Although it is not necessary to always store and restore
mask will be created. these registers, these macros still do it regardless.
Initially the mask register is set to 1 and is then shifted left
by the regS amount, which is the insertion position. All of the
bits in the mask register are then inverted by using the NOT 2) Utility Procedures
operation. Next, AND is used on the mask register and the bit
pattern in which we are inserting to force 0 into the position in i. twos_complement
which we are inserting. regT, which contains the bit to be An argument $a0 is passed into the twos_complement
inserted is then shifted left by the regS amount to get into the subroutine and the complement of the argument is returned in
right position. Finally, or is used on regD and regT to finish the $v0 register.
the insertion process. In the twos_complement subroutine, the NOT of $a0 is
taken and stored back in $a0. The number 1 is then put into
iii. Store_stack_all and restore_stack_all $a1 and both $a0 and $a1 are passed into the add_loop
These two macros serve to store and restore registers $fp, method. This way, the complement of $a0 is taken.
$ra, $a0-$a3 and $s0-$s7. Unlike the other macros, these
macros don’t take in any arguments.

ii. Twos_complement_if_neg
Twos_complement_if_neg branches to
twos_complement if the argument is less than 0, or
returns the argument itself if it is above 0.
CS47 4

iii. Twos_complement_64bit
Twos_complement_64bit returns the complement of the
lo and hi of the multiplied result. The lo exists in the $a0
register and the hi exists in the $a1 register.
$a0 and $a0 are inverted using not. $a1 is then saved in
$a3 and 1 is loaded into $a1. Add_loop is then called on $a0
and $a1, which is the lo and 1 respectively. Once add_loop is
done, twos_complement_64bit is returned to and the answer
($v0) is stored in $t0. The carry bit ($v1) is saved in $a1 and
$a3 is moved back into $a0. Add_loop is then called again
with the new $a0 and $a1. After this, the
twos_complement_64bit is done.

Addition_au_logical sets up the addition process. It creates


the necessary variables and jumps to the add_loop. For
subtraction_au_logical, the second input is moved into a0, and
the twos complement is taken. Then all the variables are
stored back in their correct position and the method jumps to
addition_au_logical.
Add_loop is where the addition actually takes place. This
iv. bit_replicator and replicate loop calculates the sum of the $a0 and $a1 registers. The
These two procedures replicate bits of a given register. method first sets a register to the immediate value of 32 so that
Bit_replicator checks if the argument is 0, and if it is, the loop has a value to compare to. The loop checks if the
branches to replicate_zero. Replicate_zero will load 0 into counter is equal to 32. If it is, it branches to “done” and
$v0 and returns back to the caller address. If bit_replicator returns to the caller. If it does not equal 32, the loop is
doesn’t branch, -1 is loaded into $v0 and returns back to the executed. The nth bits of the two inputs are extracted and
caller address. stored in $t5 and $t6 respectively. XOR is used to calculated
the sum bit and AND/OR are used to calculate the final carry
out bit. The sum bit is then inserted into the correct position
of the final answer ($v0) and the counter is then incremented
by 1. The procedure then jumps back to itself.

3) Addition/subtraction implementation
CS47 5

3) Multiplication implementation

i. Multiplication_au_logical
Multiplication_au_logical sets up the entire
multiplication process. It first checks to see if both number
are negative by using twos_complement_if_neg, then jumps
to mul_unsigned.
Mul_unsigned gets all the necessary registers needed for
storage and continues on to extract_beginning.
Extract_beginning first gets the 0th bit of the lo and stores it
in $a0. $a0 is then replicated using bit_replicator and is
stored in $v0. AND is then called on the replicated bit
pattern and $t4, which holds $a0 (MCND). Now we want
to add the original Hi with the mcnd or 0, depending on
what the 0th bit was, and the value is stored back into the
register, which holds the Hi. Next, the Lo is shifted right by
1 bit and the 31st bit of Lo gets the 0th bit of the Hi. Then,
the Hi is shifted right by 1 bit and the counter is
incremented by 1. Next, we check if the counter equals 32,
and if it does, that means all the multiplicating is over and
we jump to “done_mult”. If it does not equal 32, we jump
back to the “extract_beginning”.

ii. done_mult
Done_mult begins with storing the lo ($t1) into $v0 and
storing the hi ($t2) into $v1. Next, the code finds out if one
of the initial inputs were negative or if both are either
positive or negative. We check to see if both are negative
or if both are positive by calling an XOR on the 31st bit of
each number. If the XOR is equal to 1, that means only one
of the inputs is negative, meaning that the sign bit should be
negative. If the XOR is 0, we branch to “positive” which
restores the stack and jumps back to the caller. If the code
does not jump to positive, twos complement 64 bit is called
on hi and lo and then the stack is restored and returned back
to the caller.
CS47 6

4) Division implementation

i) Division_au_logical
Division_au_logical obtains the twos complement of the
two inputs if they are below 0. Then the method jumps to
div_unsigned.

ii) div_unsigned
Div_unsigned begins by setting up the proper arguments
as variables. A counter, $s7, is initialized to 0. $s0, which
holds the Quotient, is set to $s1. $a1, which holds the dvsr,
is set to $s2. And finally $s3, which is the remainder, is set
to 0.

iii) left_shift_and_extract
In left_shift_and_extract, the remainder is shifted left by
1 bit. Then the 31st bit of the quotient ($s1) is put into the
0th bit position of the remainder ($s3). The quotient is then
shifted left by one bit. Next, an intermediate value (S) is
calculated by subtracting the dvsr from the remainder. I
was not able to call the actual sub_logical routine since that
will always jump back to the caller code, so I copy and
pasted by sub_logical code into this method. The difference
is taken and is stored into S ($v0). Next, we check if S is 0,
basically if the Dvsr is greater than the remainder. If it is
less than 0, we jump to increment counter. If S is not
greater than 0 , we set R to equal S and make the 0th bit of Q
a 1. Then we jump to increment counter and do the same
checking.

iv) increment_counter
Increment_counter increments the counter by 1 and
compares it to 32. If the counter equals 32, we jump to
end_division, or else we go back to left_shift_and_extract.

v) end_division
End_division takes care of adding the proper sign to the
quotient and remainder. The 31st bits of the original inputs
are extracted and XOR is called on both of them. If the
XOR is 1, that means only one number is negative, meaning
that the sign bit should be negative. If the XOR’d answer is
equal to 0, we branch to positive_div. If it equals 1, we
figure out the correct signs to put on the remainder and the
quotient by using various twos_complements calls.
CS47 7

vi) positive_div output.


Positive_div restores the stack and returns to the caller.

IV. TESTING
Another class, called “proj_alu_normal” is created. This is
also an arithmetic calculator, but does not use logic to
perform the operations. Instead, it uses MIPS inbuilt
arithmetic operations such as “add”, “sub”, “mul”, and
“div”.

A) Implementation

1) addition_au_normal
Addition_au_normal calls the MIPS add instruction to add
the two inputs. Just like in au_logical, the arguments are
passed through the method in $a0 and $a1, and the result is
stored in $v0.

2) subtraction_au_normal
Subtraction_au_normal calls the MIPS subtraction
instruction to subtract the two inputs. Similarly to
addition_au_normal, the arguments are passed through the
method in $a0 and $a1, and the result is stored in $v0.

3) Multiplication_au_normal
Multiplication_au_normal calls the MIPS “mul” instruction
and stores the result in $v0. The hi value is then moved into
the $v1 register. V. CONCLUSION
This project, although a tough and tedious one, was made
4) Division_au_normal to implement a calculator using logical operations only.
Division_au_normal calls the MIPS “div” instruction on It was basically writing software that simulates the
$a0 and $a1, which are the two inputs. The hi value hardware, which performs these logical operations. The
contains the remainder and the lo value contains the program was written in the MIPS assembly language and
quotient. The remainder is moved into $v1 and the quotient tested using a tester file that Professor Patra provided.
is moved into $v0.

B) Proj-auto-test
Professor Patra wrote assembly code which tests that the
calculator works properly. The code provides sample inputs
and does the operations using the au_normal and au_logical.
The file see’s if the au_normal results match the au_logical
results. The following is a picture of the proj_auto_test

You might also like