Our task was to design an assembler which will convert the assembly code(MIPS) to machine language.
Our main goal was to generate a machine code from a file containing assembly language. The assembler reads a program written in an assembly language, then translate it into binary code and generates output file containing machine code.
In the input file the user has to give some instructions to convert into machine codes. The system will convert valid MIPS instructions into machine language and generate those codes into output file.
The input file is located in a folder named “File”. User will write down the MIPS code in this file.
We have selected registers from $zero, $t0-$t7 and $s0-$s7 and assigned 5 bits for each of the register as we know in the instruction field in MIPS containing the register rs, rt and rd contains 5 bits.
We have selected following op codes and assigned functionality values (6 bits) for each of the op codes as we know in the instruction field in MIPS, the functionality contains 6 bits.
We have selected following op codes and assigned functionality values (6 bits) for each of the op codes as we know in the instruction field in MIPS, the functionality contains 6 bits.
We have selected following op codes and assigned functionality values (6 bits) for each of the op codes as we know in the instruction field in MIPS, the functionality contains 6 bits.
Op-code
j
jal
Functionality
000010
000011
Instruction Description
add: It adds two registers and stores the result in destination register.
· Operation: $d = $s + $t
· Syntax: add $d, $s, $t
sub: It subtracts two registers and stores the result in destination register.
· Operation: $d = $s - $t
· Syntax: sub $d, $s, $t
addi: It adds a value from register with an integer value and stores the result in destination register.
· Operation: $d = $s + offset
· Syntax: addi $d, $s, offset
lw: It loads required value from the memory and write it back into the register.
· Operation: $d = MEM[$s + offset]
· Syntax: lw $d, offset($s)
sw: It stores specific value from register to memory.
· Operation: MEM[$d + offset] = $s
· Syntax: sw $s, offset($d)
and: It AND’s two register values and stores the result in destination register. Basically, it sets some bits to 0.
· Operation: $d = $s && $t
· Syntax: and $d, $s, $t
or: It OR’s two register values and stores the result in destination register. Basically, it sets some bits to 1.
· Operation: $d=$s || $t
· Syntax: or $d, $s, $t
nor: It NOR’s two register values and stores the result in destination register. Sometimes we use nor to get NOT of register value.
· Operation: $d=$s nor $t
· Syntax: nor $d, $s, $t
andi: Bitwise AND’s a register and an immediate value and stores the result in a register.
· Operation: $d = $s AND offset
· Syntax: andi $d, $s, offset
ori: Bitwise ors a register and an immediate value and stores the result in a register.
· Operation: $d = $s OR offset
· Syntax: ori $d, $s, offset
sll: It shifts bits to the left and fill the empty bits with zeros. The shift amount is depended on the offset value.
· Operation: $d= $s << offset
· Syntax: sll $d, $s, offset
srl: It shifts bits to the right and fill the empty bits with zeros. The shift amount is depended on the offset value.
· Operation: $d= $s >> offset
· Syntax: srl $d, $s, offset
beq: It checks whether the values of two register s are same or not. If it’s same it performs the operation located in the address at offset value.
· Operation: if ($s==$t) jump to offset
else goto next line
· Syntax: beq $s, $t, offset
bne: It checks whether the values of two register s are same or not. If it’s not same it performs the operation located in the address at offset value.
· Operation: if ($s!=$t) jump to offset
else goto next line
· Syntax: bne $s, $t, offset
Slt: If $s is less than $t, $d is set to one. It gets zero otherwise.
· Operation: if $s < $t $d = 1
else $d = 0
· Syntax: slt $d, $s, $t
J: Jumps to the calculated address.
· Operation: PC = nPC
· Syntax: j target
jr: Jump to the address contained in register $s
· Operation: PC = nPC;
nPC = $s;
· Syntax: jr $s
Jal: Jumps to the calculated address and stores the return address in $31
· Operation: $31 = PC + 8 (or nPC + 4);
PC = nPC;
· Syntax: jal target
The user has to give spaces between instruction words in the input file. If user don’t follow this format the system will show a valid code as invalid.
Please consider that our design uses both file and console I/O. To enable file I/O in code-blocks stdC++11 box in compiler setting should be checked.
Compiler setting can be found here: Settings > Compiler settings.