Question: Please write an assembler to turn MIPS code to machine code. Please write in CPP language. 3.2 Assembler 3.2.1 Overview The first task you are






Please write an assembler to turn MIPS code to machine code. Please write in CPP language.
3.2 Assembler 3.2.1 Overview The first task you are doing is assembling the given MIPS code to their corresponding machine code. Here's a quick example of what you need to do: #r instructions MIPS code: .text R: add $50, $51, $s2 addu $s0, $s1, $s2 sub $50, $51, $s2 subu $s0, $s1, $s2 Machine code: 00000010001100101000000000100000 00000010001100101000000000100001 00000010001100101000000000100010 00000010001100101000000000100011 Remember that you are not assembling the .data section, nor the labels. The data section will be loaded to your memory in your simulation part. The labels in your text section will be translated to its corresponding address when needed. For example, when you see j R, you will put the address of the line of instruction label R is indicating. 3.2.2 Details This part of your program is easy but needs your patience. You need to be able to: 1. Read file line by line. 2. Find the segments of the MIPS file. (.data, .text) 3. Tokenize the line read in. 4. Discard useless information, such as comments. Here are some ideas of how to implement this part: 1. You need to scan through the file for the first time. This time, discard all of the comments (following a "#"). Remember you only need to deal with text segment for assembling. Find all of the labels in the code, and store them with their corresponding address for later reference. 2. You need to scan through the file for the second time, line by line. This time, you need to identify which instruction the line is (R, 1, 1). According to the instruction type, you can assemble the line. For lines with the label, you can refer to the stored information for the label's address. 1. The list of instructions you need to support: NO Pseudo-instructions or co-processor instructions required. If there are any conflicts between the book and the official documents on instruction format, follow the book. 1. add 2. addu 3. addi 4. addiu 5. and 6. andi 7. clo 8. clz 9. div 10. divu 11. mult 12. multu 13. mul 14. madd 15. msub 16. maddu 17. msubu 18 nor 19. or 20. ori 21. sll 22. silv 23. sra 24. srav 25. srl 26. srlv 27. sub 28. subu 29. xor 30. xori 31. lui 32. sit 33. situ 34. siti 35. sitiu 36. beq 37. bgez 38. bgezal 39. bgtz 40. blez 41. bltzal 42. bltz 43. bne 44] 45. jal 46. jalr 47.jr 48 teg 49. teqi 50. tne 51. tnei 52. tge 53. tgeu 54. tgei 55. tgelu 56. tit 57. titu 58. titi 59. titiu 60. lb 61. Ibu 62. In 63. Ihu 64. lw 65. lwi 66. lwr 67. 68. sb 69. sh 70. sw 71. swi 72. swr 73. SC 74. mihi 75. mflo 76. mthi 77. mtlo 78 syscall The detailed meanings of these instructions, and their format can be found in Appendix A.10. (Syscall on Page A-80.) *For jumping and branching instructions, you need to support both labels and addresses (offsets). *For instructions with trap (i.e. overflow trap), print out the error message and terminate the program execution. 3. The input MIPS code format You will need to consider the following situations while reading the input MIPS file: 1. There will only be .data and .text sections. 2. There could be spaces or tabs before and after each line. 3. There could be spaces before and after each element within a line. e.g. add $t0, $th, $t2. 4. There could be empty lines. 5. There could be comments after the line of code. There could also be a line with only comments. Comments are always following a "#". 6. Labels can be followed by a line of code, or can have it's own line. Labels are labeling the same line of code in both situations. casel label: add $to, $t1, $t2 case2 label: add $t0,$t1, $t2 4. The data types you need to support The data types you need to support are: 1. ascii 2. asciiz 3. word 4. byte 5. half
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
