0% found this document useful (0 votes)
169 views

Developing Encoder Using Python and Tiny Calculator Using Assembly Language Programming

Project report computer science
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
169 views

Developing Encoder Using Python and Tiny Calculator Using Assembly Language Programming

Project report computer science
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/322538460

Developing Encoder Using Python and Tiny Calculator Using Assembly Language
Programming

Technical Report · January 2018


DOI: 10.13140/RG.2.2.33117.51681

CITATIONS READS
0 1,487

4 authors, including:

S M Raju Ali Mohammad Tarif


International Islamic University Malaysia International Islamic University Malaysia
2 PUBLICATIONS   0 CITATIONS    2 PUBLICATIONS   0 CITATIONS   

SEE PROFILE SEE PROFILE

Sam Shariful
International Islamic University Malaysia
1 PUBLICATION   0 CITATIONS   

SEE PROFILE

Some of the authors of this publication are also working on these related projects:

Developing Encoder Using Python and Calculator Using Assembly Language View project

All content following this page was uploaded by S M Raju on 17 January 2018.

The user has requested enhancement of the downloaded file.


Computer Architecture & Assembly Language

Project Report
---------------------------------------------------------------------------------------
Project Specification (Part 1) – Encoder

---------------------------------------------------------------------------------------
Project Specification (Part 2) – Calculator

Project Name: “Tiny Calculator”

Group Members

S M Raju ([email protected])
Md. Shariful Islam ([email protected])
Ali Mohammad Tarif ([email protected])
Mohammod Al Amin Ashik ([email protected])

International Islamic University Malaysia

Supervised by

DR. HAFIZAH BINTI MANSOR


2

Table of Contents:
Part-1 Encoder…………………………………………………………..3
 Introduction…………………………………………………….4

 General Instructions……………………………………………4

 General Overview……………………………………………...5

 Technical Documentation……………………………………...6

 Conclusion……………………………………………………..6

Part-2 Tiny Calculator………………………………………………….7


 Introduction……………………………………………………8

 Background…………………………………………………8-10

 Project Description………………………………………...11-12

 Flowchart……………………………………………………..13

 Experimental Result……………………….............................14

 Advantage……………………………………………………15

 Disadvantage…………………………………………………15

 Conclusion…………………………………………………...15

Appendix…………………………………………………………………....16-33

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
3

Part-1

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
4

Introduction
An assembly language, often abbreviated asm is a low-level programming
language for a computer, or another programmable device, in which there is a very
strong (generally one-to-one) correspondence between the language and the
language and the architecture’s machine code instructions. Each assembly
language is specific to computer architecture. In contrast, most high-level
Programming languages are generally portable across multiple architectures but
require interpreting or compiling. Assembly language may also be called symbolic
machine code. Assembly language is converted into executable machine code by a
utility program referred to as an assembler. The conversion process is referred to as
an assembly, or assembling the source code. Assembly time is the computational
step where an assembler is run. Assembly language uses a mnemonic to represent
each low-level machine instruction or opcode, typically also each architectural
register, flag, etc.1

We used Python Programming Language to develop the encoder for converting


asm file into a hex file. In this assembler, all the registers (32 registers) and 3 types
of instructions (R, I & J) are included. When a user writes the valid instruction, it
will show Function type, Formatted binary, Hexadecimal(Hex) number and Binary
number. The process will continue until user type “exit”.

General Instructions
The encoder can run the following subset of the MIPS instruction set in the format
described.

➢ add $rd $rs $rt [immediate value can also be given instead of $rs & $rt]
➢ sub $rd $rs $rt [immediate value can also be given instead of $rs & $rt]
➢ and $rd $rs $rt [immediate value can also be given instead of $rs & $rt]
➢ or $rd $rs $rt [immediate value can also be given instead of $rs & $rt]
➢ slt $rd $rs $rt [immediate value can also be given instead of $rs & $rt]
➢ move $rd $rs
➢ li $rd immediate_value
➢ lw $reg var_name

1
https://en.wikipedia.org/wiki/Assembly_language

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
5

➢ sw $reg var_name
➢ beq $rs $rt label
➢ bne $rs $rt label
➢ j label
And many more instructions set can run on our encoder.

General Overview
The program works as follows: -

➢ Read instructions from input file


➢ Convert instructions into opcodes
➢ Load the converted instructions into the instruction memory
➢ Execute: -
• Start with the first instruction
• Encode and perform necessary operations
• Proceed sequentially unless jump/beq encountered.

Screenshot of encoder program

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
6

Technical Documentation
Instruction Memory

The instruction memory is implemented as a structure holding the opcodes for each
instruction, which is then used to perform the appropriate functions on execution.

Data Memory

The data memory is implemented as a structure too. It holds the name of the
variable and the corresponding value.

Labels (for jump and beq)

The labels are stored in an array of structure of a string and and integer. The
integer holds the instruction number the label points to. To make this a 'one-pass'
assembler instead of a 'two-pass'; if a label is encountered in a jump statement (or
beq statement), then the label is stored in the array with the instruction number
variable set to -1. Once the label is found, the value storing the instruction number
is appropriately set.

Conclusion
The MIPS encoding system identifies three major classes: R-type, I-type, and J-
type. We develop the encoder to convert MIPS instruction set into hex file also
binary form and number. However, the encoder performs well when the valid
statement input by the user.

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
7

Part 2
Tiny Calculator

Why “Tiny Calculator”?


- We named it Tiny Calculator because it can operate only Addition
(+), Subtraction (-), Multiplication (*) and Division (/) with the
remainder (%).

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
8

Introduction
Many operations require one or more operands in order to form a complete
instruction and most assembler can take expressions of numbers and named
constants as well as registers and labels as operands, freeing the programmer from
tedious repetitive calculations. Depending on the architecture, these elements are
also being combined with specific instructions or addressing mode using offsets or
other data as well as fixed addresses. Many assemblers offer additional
mechanisms to facilitate program development to control the assembly process and
to aid debugging.

Background
In this project we used some syntax and they are shortly described below:

li $v0,4

la $a0, newline

➢ Above code is used for printing newline. Also entering number, welcome
message by changing newline the code.

li $v0,5

➢ For getting input from the user.

move $s0, $v0

➢ For moving number from user out of v0 to s0.

move $t1, $v0

➢ For moving number from user out of v0 to t1.

li $v0,4

la $a0,sum

➢ For printing sum string.

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
9

li $v0,1

move $a0,$t0

➢ Printing user first entered number.

li $v0,4

la $a0,comma

➢ Printing comma (,) between two entered number.

li $v0,1

move $a0,$t1

➢ Printing user second entered number.

add $t2,$t0,$t1

➢ For performing addition.

li $v0,1

move $a0,$t2

➢ For printing user sum of numbers.

li $v0,4

la $a0,difference

➢ For printing difference string.

neg $t3,$t1

add $t2,$t0,$t3

li $v0,1

move $a0,$t2

➢ Performing subtraction and printing result.

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
10

li $v0,4

la $a0,product

➢ To print the multiplication string.

mul $t2,$t0,$t1

li $v0,1

move $a0,$t2

➢ Performing multiplication from two number and print.

li $v0,4

la $a0,quotient

➢ Print the quotient string.

div $t0,$t1

mflo $t6

mfhi $t7

➢ Perform division and print.

li $v0,4

la $a0,remainder

➢ Print the remainder string.

li $v0, 10

➢ Exit from the sequence.

j loop

➢ Jump to the loop and continue the process.

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
11

Project Description
At the very first of our program, it will show the options to be calculated.

If the input is valid then it will go to the next step and if it is not, then it will show
the invalid number and exiting from the program.

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
12

If the input is valid then the next step it will do the operations that user wants to
operate.

• If user input 1 it will take input from the user and will do the addition of two
entered numbers. And the result is distributed in Decimal.

• If user input 2 it will take input from the user and will do the subtraction of
two entered numbers. And the result is distributed in Decimal.

• If user input 3 it will take input from the user and will do the multiplication
of two entered numbers. And the result is distributed in Decimal.

• If user input 4 it will take input from the user and will do the division of two
entered numbers. In this case, it will also show the remainder of the numbers
after divided. And the result is distributed in Decimal.

After every operation, it will take your opinion that either you want to exit, or you
want further calculation. If you want to exit, then it will simply exit by entering 0.
Otherwise, it will start from the beginning of the program.

That was the description of our project.

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
13

Flowchart

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
14

Experimental Result
Here is the snapshot of Addition, Subtraction, Multiplication, and Division
accordingly.

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
15

Advantage
▪ It can perform Addition.
▪ It can perform Subtraction.
▪ It can perform Division with the remainder.
▪ It can perform Multiplication.
▪ It can distribute the result into Decimal.
▪ It can calculate any integer digit number.
▪ It can check inputs whether it is valid or invalid.
▪ It can calculate with 4 functions until user input 0 to exit.
▪ It can calculate signed value. (e.g. +2 and -2 =0)

Disadvantage
▪ It can’t divide 0.

▪ It can’t produce a binary and hexadecimal result.

▪ After inputting an invalid number of options, it will exit the program.

Conclusion
Assembly language still taught in most computer science and electronic
engineering programs. Although few programmers today regularly work with
assembly language as a tool, the underlying concepts remain very important.

Our calculator can calculate with big values. Despite having some limitations, we
can get the concept of more perfect programs with this.

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
16

Appendix
Part-1(source code)
Instruction_encoder.py
# converts the instruction part of a line of MIPS code
# param 'instr' is the instruction given
# returns an arrya in the form [function type, opcode, function number]
def instr_encode(instr):
if instr == "add":
func_type = "r"
opcode = 0
funct = 0x20

elif instr == "addi":


func_type = "i"
opcode = 0x8
funct = None

elif instr == "addiu":


func_type = "i"
opcode = 0x9
funct = None

elif instr == "addu":


func_type = "r"
opcode = 0
funct = 0x21

elif instr == "and":


func_type = "r"
opcode = 0
funct = 0x24

elif instr == "andi":


func_type = "i"
opcode = 0xc
funct = None

elif instr == "beq":


func_type = "i"
opcode = 0x4
funct = None

elif instr == "bne":


func_type = "i"
opcode = 0x5
funct = None

elif instr == "j":


func_type = "j"
opcode = 0x2
funct = None

elif instr == "jal":


func_type = "j"
opcode = 0x3
funct = None

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
17

elif instr == "jr":


func_type = "r"
opcode = 0
funct = 0x8

elif instr == "lbu":


func_type = "i"
opcode = 0x24
funct = None

elif instr == "lhu":


func_type = "i"
opcode = 0x25
funct = None

elif instr == "ll":


func_type = "i"
opcode = 0x30
funct = None

elif instr == "lui":


func_type = "i"
opcode = 0xf
funct = None

elif instr == "lw":


func_type = "i"
opcode = 0x23
funct = None

elif instr == "nor":


func_type = "r"
opcode = 0
funct = 0x27

elif instr == "or":


func_type = "r"
opcode = 0
funct = 0x25

elif instr == "ori":


func_type = "i"
opcode = 0xd
funct = None

elif instr == "slt":


func_type = "r"
opcode = 0
funct = 0x2a

elif instr == "slti":


func_type = "i"
opcode = 0xa
funct = None

elif instr == "sltiu":


func_type = "i"
opcode = 0xb
funct = None

elif instr == "sltu":


func_type = "r"
opcode = 0

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
18

funct = 0x2b

elif instr == "sll":


func_type = "r"
opcode = 0
funct = 0x00

elif instr == "srl":


func_type = "r"
opcode = 0
funct = 0x02

elif instr == "sb":


func_type = "i"
opcode = 0x28
funct = None

elif instr == "sc":


func_type = "i"
opcode = 0x38
funct = None

elif instr == "sh":


func_type = "i"
opcode = 0x29
funct = None

elif instr == "sw":


func_type = "i"
opcode = 0x2b
funct = None

elif instr == "sub":


func_type = "r"
opcode = 0
funct = 0x22

elif instr == "subu":


func_type = "r"
opcode = 0
funct = 0x23

else:
func_type = None
opcode = None
funct = None

return [func_type, opcode, funct]

Register_encoder.py
# Array used to contain register numeric values
registers = {
"$zero": 0,
"$at": 1,
"$v0": 2,
"$v1": 3,
"$a0": 4,
"$a1": 5,
"$a2": 6,

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
19

"$a3": 7,
"$t0": 8,
"$t1": 9,
"$t2": 10,
"$t3": 11,
"$t4": 12,
"$t5": 13,
"$t6": 14,
"$t7": 15,
"$s0": 16,
"$s1": 17,
"$s2": 18,
"$s3": 19,
"$s4": 20,
"$s5": 21,
"$s6": 22,
"$s7": 23,
"$t8": 24,
"$t9": 25,
"$k0": 26,
"$k1": 27,
"$gp": 28,
"$sp": 29,
"$fp": 30,
"$ra": 31
}

# Given the function type, reg_decode will output an array containing the
# numeric values of the registers and immediates in MIPS code.
# param 'func_type' is the function type of the MIPS code
# param 'instr' is the instruction given in the MIPS code
# param 'regs' is an array containing the registers used in the MIPS code
# returns an array in the form [rs, rt, rd, shamt] for r-type functions
# returns an array in the form [rs, rt, immediate] for i-type functions
def reg_encode(func_type, instr, regs):
# execution for r-type functions
if func_type == "r":

# special case for MIPS shifts


if (instr == "sll") or (instr == "srl"):
try:
# return[rs, rt, rd, shamt]
return [0, registers[regs[1]], registers[regs[0]], int(regs[2])]
except:
return None

# special case for MIPS jr


if (instr == "jr"):
try:
# return[ rs, rt, rd,shamt]
return [registers[regs[0]], 0, 0, 0]
except:
return None

# standard r-type MIPS instructions


try:
# return[ rs, rt, rd, shamt]
return [registers[regs[1]], registers[regs[2]], registers[regs[0]], 0]
except:
return None

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
20

# execution for i-type functions


elif func_type == "i":

# special case for lui


if (instr == "lui"):
try:
if len(regs[1]) > 1 and regs[1][1] == "x":
imm = int(regs[1], base=16)
else:
imm = int(regs[1])

# return[rs, rt , immediate ]
return [0, registers[regs[0]], imm]
except:
return None

# special case for lw, sb, sw, ll sc


if (instr == "lw") or (instr == "sb") or (instr == "sw") or (instr == "ll") or
(instr == "sc"):
try:
if len(regs[1]) > 1 and regs[1][1] == "x":
imm = int(regs[1], base=16)
else:
imm = int(regs[1])

# return[ rs, rt , immediate ]


return [registers[regs[2]], registers[regs[0]], imm]
except:
return None

# standard i-type MIPS instructions


try:
if len(regs[2]) > 1 and regs[2][1] == "x":
imm = immnt(regs[2], base=16)
else:
imm = int(regs[2])

# return[ rs rt immediate ]
return [registers[regs[1]], registers[regs[0]], imm]
except:
return None

# execution for j-type functions


elif func_type == "j":
try:
if len(regs[0]) > 1 and regs[0][1] == "x":
imm = int(regs[0], base=16)
else:
imm = int(regs[0])

# return [ immediate ]
return [imm]
except:
return None

else:
return None

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
21

Mips_encoder.py
# Converts MIPS instructions into binary and hex
import sys
from instruction_encoder import, instr_encode # converts the instruction part of a
line of MIPS code
from register_encoder import, reg_encode # converts the register and immediate parts
of the MIPS code

# the main conversion function


def convert(code):
code = code.replace("(", " ")
code = code.replace(")", "")
code = code.replace(",", " ")
code = code.replace(" ", " ")
args = code.split(" ")
instruction = args[0]

if instruction == "exit":
sys.exit()

codes = instr_encode(instruction)
func_type = codes[0]
reg_values = reg_encode(func_type, instruction, args[1:]) # get the numeric
values of the registers

# the following if statement below prints an error if needed


if reg_values == None:
print("Not a valid MIPS statement")
return

# execution for r-type functions


if func_type == "r":
opcode = '{0:06b}'.format(codes[1])
rs = '{0:05b}'.format(reg_values[0])
rt = '{0:05b}'.format(reg_values[1])
rd = '{0:05b}'.format(reg_values[2])
shamt = '{0:05b}'.format(reg_values[3])
funct = '{0:06b}'.format(codes[2])
print("Function type: R-Type")
print("Instruction form: opcode| rs | rt | rd |shamt| funct")
print("Formatted binary: " + opcode + "|" + rs + "|" + rt + "|" + rd + "|" +
shamt + "|" + funct)
binary = opcode + rs + rt + rd + shamt + funct
print("Binary: " + binary)
hex_string = '{0:08x}'.format(int(binary, base=2))
print("Hex: 0x" + hex_string)

# execution for i-type functions


elif func_type == "i":
opcode = '{0:06b}'.format(codes[1])
rs = '{0:05b}'.format(reg_values[0])
rt = '{0:05b}'.format(reg_values[1])
imm = '{0:016b}'.format(reg_values[2])
print("Function type: I-Type")
print("Instruction form: opcode| rs | rt | immediate ")
print("Formatted binary: " + opcode + "|" + rs + "|" + rt + "|" + imm)
binary = opcode + rs + rt + imm
print("Binary: " + binary)
hex_string = '{0:08x}'.format(int(binary, base=2))
print("Hex: 0x" + hex_string)

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
22

# execution for j-type functions


elif func_type == "j":
opcode = '{0:06b}'.format(codes[1])
imm = '{0:026b}'.format(reg_values[0])
print("Function type: J-Type")
print("Instruction form: opcode| immediate ")
print("Formatted binary: " + opcode + "|" + imm)
binary = opcode + imm
print("Binary: " + binary)
hex_string = '{0:08x}'.format(int(binary, base=2))
print("Hex: 0x" + hex_string)

else:
print("Not a valid MIPS statement")
return

return

# main

print("\n\t\t\t\t\t\tWELCOME TO THE MIPS ENCODER!")


print("\t\t\t\t\t\tType MIPS code below to see it in binary and hex form")
print("\t\t\t\t\t\tSyntax: If using hex, use the '0x' label")
print("\t\t\t\t\t\tType 'exit' to exit")
print("-------------------------------------------------------------------------------
-")
while True:
mips = input("\nType MIPS code here: ")
print()
convert(mips)
print("---------------------------------------------------------------------------
-----")

Part-2(source code)
.data

welcome: .asciiz "\t\t\t\t\tWelcome to Tiny Calculator \n \t\t\t\t\t\t1 - Addition \n \t\t\t\t\t\t2 - Subtraction


\n \t\t\t\t\t\t3 - Multiplication \n \t\t\t\t\t\t4 - Division \n \t\t\t\t\t\t0 - Exit "

choose: .asciiz "\tChoose an option: "

enterNumber1: .asciiz "\tPlease enter first number: "

enterNumber2: .asciiz "\tPlease enter second number: "

newline: .asciiz "\n"

sum: .asciiz "\tThe addition of "

comma: .asciiz " , "

is: .asciiz " is = "

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
23

difference: .asciiz "\tThe subtraction of "

product: .asciiz "\tThe multiplication of "

quotient: .asciiz "\tThe quotient of "

remainder: .asciiz "\tAnd the remainder is: "

error: .asciiz "\tInvalid number, exiting... "

finishing: .asciiz "\t\t\t\t\tThank You For Using Tiny Calculator. \n"

.text

.globl main

main:

loop:

#printing newline

li $v0,4

la $a0,newline

syscall

#printing welcome

li $v0,4

la $a0,welcome

syscall

#printing newline

li $v0,4

la $a0,newline

syscall

#printing option to choose

li $v0,4

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
24

la $a0,choose

syscall

#get int from user

li $v0,5

syscall

#move number from user out of v0 to s0

move $s0, $v0

#branches

beq $s0,0,exit

beq $s0, 1, addition

beq $s0, 2, subtraction

beq $s0, 3, multiplication

beq $s0, 4, division

j errorExit

addition:

#printing Enter Number

li $v0,4

la $a0,enterNumber1

syscall

#get int from user

li $v0,5

syscall

#move number from user out of v0 to temp0

move $t0, $v0

#printing Enter Number

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
25

li $v0,4

la $a0,enterNumber2

syscall

#get int from user

li $v0,5

syscall

#move number from user out of v0 to temp1

move $t1, $v0

#printing Sum String

li $v0,4

la $a0,sum

syscall

#print user number1

li $v0,1

move $a0,$t0

syscall

#printing comma

li $v0,4

la $a0,comma

syscall

#print user number2

li $v0,1

move $a0,$t1

syscall

#printing is

li $v0,4

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
26

la $a0,is

syscall

#perform Addition

add $t2,$t0,$t1

#print user sum of numbers

li $v0,1

move $a0,$t2

syscall

#printing newline

li $v0,4

la $a0,newline

syscall

j loop

subtraction:

#printing Enter Number

li $v0,4

la $a0,enterNumber1

syscall

#get int from user

li $v0,5

syscall

#move number from user out of v0 to temp0

move $t0, $v0

#printing Enter Number

li $v0,4

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
27

la $a0,enterNumber2

syscall

#get int from user

li $v0,5

syscall

#move number from user out of v0 to temp1

move $t1, $v0

#printing Difference String

li $v0,4

la $a0,difference

syscall

#print user number1

li $v0,1

move $a0,$t0

syscall

#printing comma

li $v0,4

la $a0,comma

syscall

#print user number2

li $v0,1

move $a0,$t1

syscall

#printing is

li $v0,4

la $a0,is

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
28

syscall

#perform subtraction and print

neg $t3,$t1

add $t2,$t0,$t3

li $v0,1

move $a0,$t2

syscall

#printing newline

li $v0,4

la $a0,newline

syscall

j loop

multiplication:

#printing Enter Number

li $v0,4

la $a0,enterNumber1

syscall

#get int from user

li $v0,5

syscall

#move number from user out of v0 to temp0

move $t0, $v0

#printing Enter Number

li $v0,4

la $a0,enterNumber2

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
29

syscall

#get int from user

li $v0,5

syscall

#move number from user out of v0 to temp1

move $t1, $v0

#printing Product String

li $v0,4

la $a0,product

syscall

#print user number1

li $v0,1

move $a0,$t0

syscall

#printing comma

li $v0,4

la $a0,comma

syscall

#print user number2

li $v0,1

move $a0,$t1

syscall

#printing is

li $v0,4

la $a0,is

syscall

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
30

#perform multiplication and print

mul $t2,$t0,$t1

li $v0,1

move $a0,$t2

syscall

#printing newline

li $v0,4

la $a0,newline

syscall

j loop

division:

#printing Enter Number

li $v0,4

la $a0,enterNumber1

syscall

#get int from user

li $v0,5

syscall

#move number from user out of v0 to temp0

move $t0, $v0

#printing Enter Number

li $v0,4

la $a0,enterNumber2

syscall

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
31

#get int from user

li $v0,5

syscall

#move number from user out of v0 to temp1

move $t1, $v0

#printing Quotient String

li $v0,4

la $a0,quotient

syscall

#print user number1

li $v0,1

move $a0,$t0

syscall

#printing comma

li $v0,4

la $a0,comma

syscall

#print user number2

li $v0,1

move $a0,$t1

syscall

#printing is

li $v0,4

la $a0,is

syscall

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
32

#perform division and print

div $t0,$t1

mflo $t6

mfhi $t7

#printing lo

li $v0,1

move $a0,$t6

syscall

#printing newline

li $v0,4

la $a0,newline

syscall

#printing Remainder String

li $v0,4

la $a0,remainder

syscall

#printing hi

li $v0,1

move $a0,$t7

syscall

#printing newline

li $v0,4

la $a0,newline

syscall

j loop

© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
33

exit:

#finishing

li $v0,4

la $a0,finishing

syscall

#exit sequence

li $v0, 10

syscall

errorExit:

#printing error message

li $v0,4

la $a0,error

syscall

#exit sequence

li $v0, 10

syscall

-------------------------------------------------------------------------------------------------------------------------------

Thank you
© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED

View publication stats

You might also like