Answered step by step
Verified Expert Solution
Question
1 Approved Answer
How would I go about typing out a code for this in Java? I'm a bit confused. Would love some help and insight. Thank you!
How would I go about typing out a code for this in Java? I'm a bit confused. Would love some help and insight. Thank you!
Im trying to get it compiled thru Java
COMP360 Assembler 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. Instruction opcode Length add 0 2 sub mult div load laddr store call rtn Format Example 0 add R1, R2, R3 R3 = R1 + R2 0 sub R1, R2, R3 R3 = R1 - R2 mult R1, R2, R3 R3 = R1 * R2 div R1, R2, R3 R3 = R1 /R2 load Rl,addr[R7] RI memory value laddr Ri,addr[R7] R1 + address store R1, addr[R7] R1 memory call addr push return addr on stack and jump to addr rtn pop address from stack and jump to there jump addr[R7] jump to address jz R1, addr[R7] jump to address only if R1 is zero jn Rl,addr[R7] jump to address only if R1 is negative push R1 push Rl on stack pop R1 pop stack into R1 Ipsw R1 load PSW into R1 3 spsw R1 store R1 into PSW 5 data 123 created a data word jump 2 jz jneg push 3 pop Ipsw spsw data F 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 bits 4 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 page 1 of 4 COMP360 Assembler 4: rtn 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. page 2 of 4 COMP360 Assembler Example output: address machine source * Example assembler program for COMP360 c6 NNNNNNN 0 58000034 laddr R8, cat 4 41800030 load Ri, dog (R8] 42800038 load R2, goat(R8) 123 more add R1, R2, R3 2324 mult R3, R2, R4 10 b4000000 in R4, more 14 1415 sub R4, R1, R5 16 a 300001c jz R3, nodiv la 3536 div R5, R3, R6 66000025 nodiv store R6, addr push R6 70000020 call mthd fa addr spsw R10 a 27 add R10, R2, R7 Ipsw R7 9000000C jump more * example method 128 mthd add R1, R2, R8 80 rtn * data 1 dog data 1 O cat data 0 801 goat data 2049 ez Hints: 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. page 3 of 4 COMP360 Assembler . 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.toUnsignedString( intNum, 16) page 4 of 4 COMP360 Assembler 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. Instruction opcode Length add 0 2 sub mult div load laddr store call rtn Format Example 0 add R1, R2, R3 R3 = R1 + R2 0 sub R1, R2, R3 R3 = R1 - R2 mult R1, R2, R3 R3 = R1 * R2 div R1, R2, R3 R3 = R1 /R2 load Rl,addr[R7] RI memory value laddr Ri,addr[R7] R1 + address store R1, addr[R7] R1 memory call addr push return addr on stack and jump to addr rtn pop address from stack and jump to there jump addr[R7] jump to address jz R1, addr[R7] jump to address only if R1 is zero jn Rl,addr[R7] jump to address only if R1 is negative push R1 push Rl on stack pop R1 pop stack into R1 Ipsw R1 load PSW into R1 3 spsw R1 store R1 into PSW 5 data 123 created a data word jump 2 jz jneg push 3 pop Ipsw spsw data F 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 bits 4 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 page 1 of 4 COMP360 Assembler 4: rtn 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. page 2 of 4 COMP360 Assembler Example output: address machine source * Example assembler program for COMP360 c6 NNNNNNN 0 58000034 laddr R8, cat 4 41800030 load Ri, dog (R8] 42800038 load R2, goat(R8) 123 more add R1, R2, R3 2324 mult R3, R2, R4 10 b4000000 in R4, more 14 1415 sub R4, R1, R5 16 a 300001c jz R3, nodiv la 3536 div R5, R3, R6 66000025 nodiv store R6, addr push R6 70000020 call mthd fa addr spsw R10 a 27 add R10, R2, R7 Ipsw R7 9000000C jump more * example method 128 mthd add R1, R2, R8 80 rtn * data 1 dog data 1 O cat data 0 801 goat data 2049 ez Hints: 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. page 3 of 4 COMP360 Assembler . 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.toUnsignedString( intNum, 16) page 4 of 4 Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started