Question: Need help with this assembler program Write an assembler that reads the source code of an assembly program (for an imaginary machine) and displays the




Need help with this assembler program
Write an assembler that reads the source code of an assembly program (for an imaginary machine) and displays the machine language for that program The following assembler instructions must be supported. sub div Instruction opcode Length Format Example add 0 2 0 add R1, R2, R3 R3-R1 + R2 2 0 sub R1, R2, R3 R3-RI - R2 mult 0 mult R1, R2, R3 R3-R1 * R2 0 div R1, R2, R3 R3 =R1/R2 load 4 load Rl, addr R71 RI memory value laddr laddr RI,addr[R7) RI. address store I store RI,addr[R71 RI memory call 2 call addr push retum addr on stack and jump to addr rtn 4 Itn pop address from stack and jump to there jump 4 2 Jump addr (R7] jump to address | jz Ri,addr[R71 jump to address only if Rl is zero jneg 4 jn R1, addr[R71 jump to address only if Rl is negative push C13 push Ri push R1 on stack pop D 3 pop R1 pop stack into RI Ipsw E 1 3 lpsw RL load PSW into R1 F 1 3 spsw R1 store Rl into PSW data 4 S data 123 created a data word JZ Spsw Machine language instructions have the following formats: 0: arithmetic opcode Reg 1 Reg 2 Reg 3 4 bits 4 bits 4 bits 4 bits 1: Load, store, conditional jump opcode Reg index addr 4 bits4 bits 4 bits 20 bits 2: call, jump opcode unused index 4 bits 4 bits 4 bits addr 20 bits 3: push, pop, Ipsw, spsw opcode Reg 4 bits 4 bits 4: In opcode unused 4 bits 4 bits 5: data integer data value 32 bits A comment in the assembly program is indicated by an asterisk, "*" in the first column of the source code. Labels must start in the first column. Your assembler does not have to detect every possible error in the assembler program, but it should produce a reasonable error message if an undefined label is used. For a maximum score of 95 points, your program can ignore all index registers. The presence of an index register) may or may not be considered an error. The index field of all instructions must be set to zero if index registers are not implemented. For a maximum score of 100, all memory addressing instructions must support an optional index register. Example output: address machine Source * Example assembler program for COMP 360 14 0 58000034 Laddr R8, cat 4 41800030 load Ri, dog (R8) B 42800038 load R2, goat [R] 123 more add R1, R2, R3 2324 mult R3, R2, R4 10 5400000 sn R4 ore 1415 sub R4, R1, R5 16 a 3000010 R3, nodiv 3536 div R5, R3, R6 le 66000025 nodiv store R6, addr C6 push R5 70000020 Call mthd fa addr spsw RIO add R10, R2, R7 Ipsw R7 9000000 jump more * example method 128 mthd add R1, R2, R8 80 a 22 NNNNNNNN th - data 1 dog data 1 O cat data 0 801 goat data 2049 You may wish to create an object to hold the symbol table entries. This would contain: opcode, address or other value of the symbol length of instruction in bytes format of the machine instruction A hash table works well to hold the symbol table. Symbol table entries can be keyed by the mnemonic or address label. The assembler can be written to use one or two passes. After the first pass, a two pass assembler closes the input file and then reads the input again. The goal of the first pass is to create a symbol table containing the address of all symbols. The second pass creates the output. It uses the symbol table to get the value of all names used in the program. General outline for pass 1. Initialize the current address to zero Read a line of assembler Split the line into tokens If label. put label and current address in symbol table Get mnemonic and find in symbol table Add instruction length to current address repeat until end of file General outline for pass 2 Initialize the current address to zero Read a line of assembler Split the line into tokens Get mnemonic information from symbol table Get additional fields and create the machine language Display the results The following Java classes and methods may be useful in writing your assembler. Similar C++ methods exist. java.util. HashMap java.util.StringTokenizer Integer.toUnsigned String intNum, 16)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
