Developing Encoder Using Python and Tiny Calculator Using Assembly Language Programming
Developing Encoder Using Python and Tiny Calculator Using Assembly Language Programming
net/publication/322538460
Developing Encoder Using Python and Tiny Calculator Using Assembly Language
Programming
CITATIONS READS
0 1,487
4 authors, including:
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.
Project Report
---------------------------------------------------------------------------------------
Project Specification (Part 1) – Encoder
---------------------------------------------------------------------------------------
Project Specification (Part 2) – Calculator
Group Members
S M Raju ([email protected])
Md. Shariful Islam ([email protected])
Ali Mohammad Tarif ([email protected])
Mohammod Al Amin Ashik ([email protected])
Supervised by
Table of Contents:
Part-1 Encoder…………………………………………………………..3
Introduction…………………………………………………….4
General Instructions……………………………………………4
General Overview……………………………………………...5
Technical Documentation……………………………………...6
Conclusion……………………………………………………..6
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
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: -
© 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.
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
© 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
li $v0,4
la $a0,sum
© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
9
li $v0,1
move $a0,$t0
li $v0,4
la $a0,comma
li $v0,1
move $a0,$t1
add $t2,$t0,$t1
li $v0,1
move $a0,$t2
li $v0,4
la $a0,difference
neg $t3,$t1
add $t2,$t0,$t3
li $v0,1
move $a0,$t2
© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
10
li $v0,4
la $a0,product
mul $t2,$t0,$t1
li $v0,1
move $a0,$t2
li $v0,4
la $a0,quotient
div $t0,$t1
mflo $t6
mfhi $t7
li $v0,4
la $a0,remainder
li $v0, 10
j loop
© 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.
© 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.
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
© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
17
© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
18
funct = 0x2b
else:
func_type = None
opcode = None
funct = None
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":
© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
20
# return[rs, rt , immediate ]
return [0, registers[regs[0]], imm]
except:
return None
# return[ rs rt immediate ]
return [registers[regs[1]], registers[regs[0]], imm]
except:
return None
# 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
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
© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
22
else:
print("Not a valid MIPS statement")
return
return
# main
Part-2(source code)
.data
© 2017 S M Raju, Md. Shariful Islam, Ali Mohammad Tarif, Mohammod Al Amin Ashik ALL
RIGHT RESERVED
23
.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
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
li $v0,5
syscall
#branches
beq $s0,0,exit
j errorExit
addition:
li $v0,4
la $a0,enterNumber1
syscall
li $v0,5
syscall
© 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
li $v0,5
syscall
li $v0,4
la $a0,sum
syscall
li $v0,1
move $a0,$t0
syscall
#printing comma
li $v0,4
la $a0,comma
syscall
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
li $v0,1
move $a0,$t2
syscall
#printing newline
li $v0,4
la $a0,newline
syscall
j loop
subtraction:
li $v0,4
la $a0,enterNumber1
syscall
li $v0,5
syscall
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
li $v0,5
syscall
li $v0,4
la $a0,difference
syscall
li $v0,1
move $a0,$t0
syscall
#printing comma
li $v0,4
la $a0,comma
syscall
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
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:
li $v0,4
la $a0,enterNumber1
syscall
li $v0,5
syscall
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
li $v0,5
syscall
li $v0,4
la $a0,product
syscall
li $v0,1
move $a0,$t0
syscall
#printing comma
li $v0,4
la $a0,comma
syscall
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
mul $t2,$t0,$t1
li $v0,1
move $a0,$t2
syscall
#printing newline
li $v0,4
la $a0,newline
syscall
j loop
division:
li $v0,4
la $a0,enterNumber1
syscall
li $v0,5
syscall
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
li $v0,5
syscall
li $v0,4
la $a0,quotient
syscall
li $v0,1
move $a0,$t0
syscall
#printing comma
li $v0,4
la $a0,comma
syscall
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
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
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:
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