Week 24 Coursework – Emulating MIPS
Chalarampos Rotsos, ,
• Implement a MIPS Assembler Emulator
Copyright By PowCoder代写 加微信 powcoder
1. Translating assembler programs to bytecode (Week 20).
2. Execute the code in an emulator (Week 22/24 – C Coursework). • SimilartotheMARSemulatorwithouttheUI.
• Implementalimitedsetofinstructions.
• Use supplied template to implement your code.
• A half-written emulator with basic functionality. • Read input file, store it in memory.
• Convert MIPS code into bytecode
Why this Task?
• It covers all aspects of 150
✓ Development is on Linux
✓ We need to understand machine architectures ✓ We use assembler
✓ We use c
• Should show how all elements of SCC150 connect
• … and, it is fun (hopefully)!
Generating Bytecode
add $s0 $s0 $s2
Week 20 Practical
0x02128020
Week 24 Coursework
Week 20 Practical
Week 24 CW
Test Code:
0: addi $a0 $zero 88 1: addi $a1 $zero 77 2: jal mult
4: mult: add $v0 $zero $zero 5: loop: andi $t1 $a0 1
6: blez $t1 skipadd
7: add $v0 $v0 $a1
8: skipadd: srl $a0 $a0 1 9: sll $a1 $a1 1
10: bne $a0 $zero loop 11: jr $ra
0x00400000 0x20040058
0x00400004 0x2005004d Week 24 0x00400008 0x0c100004 CW 0x0040000c 0x00000000
executing executing executing executing …
Week 20 Practical
0x00400000 0x20040058 0x00400004 0x2005004d 0x00400008 0x0c100004 0x0040000c 0x00000000 0x00400010 0x00001020 0x00400014 0x30890001 0x00400018 0x18090001 0x0040001c 0x00451020 0x00400020 0x00042042 0x00400024 0x00052840 0x00400028 0x1480fffa 0x0040002c 0x03e00008 6
Template main()
int main(){
if (load_program(filename)<0)
return(-1);
if (make_bytecode()<0)
return(-1);
if (exec_bytecode()<0)
return(-1); return(0);
Week 20 Practical
Week 24 CW
Emulator Template
• A template is provided
• emulator_w24_template.zip
• You do not have to start from scratch
• Basic functionality is provided
• Fill in the gaps to obtain a working emulator
• Input assumptions
• Program file contains an instruction on each line
• A line may include in addition a label (within the line)
MIPS ASSEMBLER – Instructions
• The emulator should support the following instructions • NOP - no operation
• ADD - addition
• ADDI - addition immediate
• ANDI - bitwise immediate and
• SRL - shift right logical
• SLL - shift left logical
• BLEZ - branch if less or equal to zero • BNE - branch on not equal
• JAL - Jump and link
• JR - Jump register
• Any SCC150 Assembler operation is based on these instructions, the 32 register names, labels and immediate values.
Instruction Set (I)
• Basic concept
• loweringofthecompilertothehardwarelevel
• notraisingofhardwaretothesoftwarelevel(aswithCISC)
• MIPS is a 32-bit architecture. This defines • therangeofvaluesinbasicarithmetic
• thenumberofaddressablebytes
• thewidthofastandardregister
• Simple set of instructions
• Allinstructionshavethesame32-bitformat • Instructionsoperateon3232-bitregisters
• Designedtoruninasingleclockcycle
Instruction Set (II)
• 3 basic types of MIPS instructions
• Register type (R-type)
• Immediate type (I-type)
• Jump type (J-type)
• What do we need to specify in an instruction?
add $s1,$s1,$t0
the operation
the registers
32 possible registers; 2^5 = 32
i.e. five bits needed to specify each register
• The elements of an R-Type instruction
operation (“op”) code shift amount
misc. function code
6bits 5bits 5bits 5bits 5bits 6bits
source registers
destination register
R-Type Example (1)
Example instruction: addition with overflow Registers:
P&H fig. 2.14
add $s1,$s1,$t0
op rs rt rd shamt funct
R-Type Example (2)
add $s1,$s1,$t0
op rs rt rd shamt funct
0x02288820
The elements of an I-Type instruction
operation (“op”) code
lw/sw: address offset
addi: signed number andi: literal value
} two’s complem
meaning depends on opcode,
constant or address
6 bits 5 bits 5 bits 16 bits
source register
destination register
• The elements of an J-Type instruction
instruction word address
6 bits 26 bits
operation (e.g. j)
jump destination
J-Type (JAL) Target Address
instruction word address
26 bits of address are replaced
bottomtwobitsarethebyteoffset: always“00”!
top four bits of PC are kept
Jump Register JR
“jump register” op
register holding address to jump to (PC=$s0)
funct 6 bits
only one register is used
rt, rd and shamt not used (set to zeros)
jr is an R-type Instruction
Testing code
• Provided Makefile can help you to build code automatically. • In a Linux system type: make
• Generate binary file emulator with debug information
• Run the program using: ./emulator –i filename.asm • Assembles file filename.asm
• Three sample asm files:
• simple_add.asm: MIPS add program
• simple_loop.asm: MIPS loop program
• eth_mult.asm: MIPS Ethiopian multiplication
• eth_mult_procedure.asm: MIPS Ethiopian multiplication using a procedure (will not work in MARS)
Test Code: Ethiopian multiplication
• Multiply a and b (for example, a=17 b=34) • Create 2 columns
• Halve a until reaching 1; double b
• Add 2nd column if a is odd
17 8 4 2 1
• This can be implemented using assembler ...
(68) <-- even
(136) <-- even (272) <-- even
544 ----- 578
What to do
Week 24 CW
• Write a loop to read one-by-one the instruction using the pc variable.
• Extract opcode and funct (if R-type) to figure out the instruction type.
• Implement code to extract the fields of an instruction.
• Perform appropriate changes on the registers for each instruction.
• Manipulate correctly the pc for branch operations.
• Use a method to terminate the program (e.g. nop).
• Comment code and use functions to improve the readability of your program.
Template Walkthrough (1)
/* function to execute bytecode */
Week 24 CW
int exec_bytecode() {
printf("EXECUTING PROGRAM ...\n");
pc = ADDR_TEXT; // set program counter to the start of our program
// here goes the code to run the byte code
print_registers(); // print out the state of registers at the end of execution
printf("... DONE!\n");
return (0); }
Template walkthrough (2)
• Constants
Week 24 CW
// Array of 32 integer values, representing the CPU registers.
unsigned int registers[MAX_REGISTER] = {0};
// Use this variable to emulate the program counter of the CPU // and as a pointer to the next instruction
unsigned int pc = 0;
// int array containing the bytecode of program instruction. // Use this array to access instruction.
unsigned int text[MAX_PROG_LEN] = {0};
#define ADDR_TEXT 0x00400000 //where the .text area starts in which the program lives
#define TEXT_POS(a) ((a==ADDR_TEXT)?(0):(a - ADDR_TEXT)/4) //can be used to access text[]
Template walkthrough (3)
• Helper function to print registers and pc
int print_registers(){ int i;
printf("registers:\n"); for(i=0;i