Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

NEED IT ASAP PLEASE 3 Functions to be written The header file raisin.h contains definitions and the prototypes of the functions you must write. The

NEED IT ASAP PLEASE

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

3 Functions to be written The header file raisin.h contains definitions and the prototypes of the functions you must write. The functions use an unsigned int to represent a MAD Raisin word. On the Grace machines an int is four bytes. 3.1 unsigned short print_instruction(unsigned int instruction) This function should try to interpret its parameter as a MAD Raisin instruction, use C's bit operators to extract its fields (see the description and diagram in Section 2.4), and print the instruction's components on an output line. However, if its parameter doesn't represent a valid MAD Raisin instruction, according to the criteria discussed in Section 4 below, nothing at all should be printed and the function should just return 0. Otherwise, if the instruction is valid, the function should return 1 after printing the instruction in this format: - The instruction's opcode (the opcode field of the parameter, meaning its leftmost five bits) should be printed using its name in the table in Section 2.4 (halt, syscall, add, sub, etc.), spelled exactly as shown there. For example, if the value of the opcode field is the enum value HALT (which has the value 0), then halt should be printed. If the opcode field is ADD then add should be printed, etc. - Following the opcode, the register operands that are actually used by the instruction should be printed, in the order register 1 followed by register 2 if an instruction uses both register operands. For example, an and instruction uses both registers as operands, so both should be printed, with the first register operand followed by the second one, but a neg instruction uses the first register as its only operand, so just the first register operand should be printed. Register names should be printed in decimal with an R immediately preceding the register number. For example, register values 0,1 , and 12 would be printed as R0, R1, and R12. - If an instruction uses a memory address operand or an immediate operand that operand should be printed last, in decimal. If the instruction uses the memory address/immediate field for storing a memory address its value should be printed using exactly five places, with addresses less than 1000010 printed using as many leading zeros as necessary so they occupy exactly five places. For example, the address 21610 should be printed as 00216 . (This can be done the hard way, but note that it is trivial to accomplish in C with printf() formatting options covered in discussion.) But for the 1i instruction, which uses the memory address/immediate field for storing an immediate (constant) value, the field's value should not be printed with any leading zeros (except zero itself should be printed as 0). Only memory address operands should have leading zeros if needed. The printed fields must be separated with one or more tabs or spaces; the exact number of tabs or spaces (or both) doesn't matter and is up to you. The printed instruction should not have any tabs or spaces before the opcode or following the last field printed. A newline must be printed after the instruction. For example, the call print_instruction (06a4e0000) should print something like lt R9 R7, where the exact number of spaces or tabs separating the three things printed is up to you, as long as it is one or more. (Remember that there is no such thing as a hexadecimal, decimal, or octal number in memory. There are just numbers in binary, although programmers can use constants in different bases in programs, and programs can read or write numbers in different bases. So even though the argument passed into this function above is in hexadecimal, it is stored in memory in binary, and you can perform bit operations on it to extract its fields.) Note: in projects where output has to be produced, students sometimes lose credit for minor spelling or formatting mistakes. The submit server checks your output automatically, consequently even trivial typos would cause tests to fail. Due to the size of the course we would not be able to give back any credit lost for failed tests due to spelling or formatting errors, even if they are extremely minor, because that would necessitate manual checking of every output for 600 students' projects, which is not feasible. Therefore it's up to you to check carefully that your output is correct, register names and opcodes are spelled exactly, and that the output format described above is followed. 3.2 int load_program(unsigned int memory[], const unsigned int program[], unsigned int start_addr, unsigned short pgm_size, unsigned short data_segment_size) This function simulates the operating system loading a machine language program into the MAD Raisin's memory so it could be executed. It should copy the contents of the array program (representing the machine language program) into the array memory, beginning at the address start_addr. (Note that start_addr is a MAD Raisin byte address, not an array subscript or a number of array elements. For example, if the program should be loaded starting at the fourth word of the MAD Raisin's memory the value of start_addr will be 12, because each word is four bytes.) The number of words in program is given by pgm_size. However, not all of the words of the program might be instructions. Each program has an area of its memory for its actual code (called the text segment) and another area for its data or variables (called the data segment). (Note that although they are much more complex this is the case for real machines, for example, see slide \#15 in Lecture \#1, where the data segment consists of both regions labeled "Dynamic data (heap)" and "Global and static data".) The parameter data_segment_size represents the size of the program's data segment, which will always be at the end of its memory. For example, if pgm_size is 100, meaning the program has 100 total words of memory, and data_segment_size is 25, this means that the program's first 75 words are instructions and its last 25 words are data. If its parameters are valid the function should load the program into memory starting at address start_addr and return 1. If its parameters are invalid it should not modify anything and just return 0 . Its parameters would be invalid if: - start_addr is larger than the maximum word address on the MAD Raisin, which is given by the value of the symbolic constant NUM_WORDS defined in raisin.h. - start_addr is not a valid word address that is divisible by 4. - The size of the program in program, given by pgm_size, is larger than the number of words in the MAD Raisin's memory. - Copying pgm_size words starting at the address start_addr would go beyond the end of the MAD Raisin's memory. (Note that it's not invalid if pgm_size is zero; in that case no instructions would be copied to the memory array, and the function would just return 1.) - The values of the parameters pgm_size and data_segment_size would mean that there is not at least one instruction in the program's text segment. (A program cannot have no instructions, and it cannot consist only of data- there has to be at least one instruction.) The function should not modify the array memory outside of storing the indicated number of instructions into it beginning at the indicated address. When a program is loaded there may be values in other memory locations left from prior programs; those just remain as garbage values. Note that nothing prevents one program from being loaded into memory locations where a different program had previously been loaded. Since its parameter memory represents the memory of the MAD Raisin, the function may assume that it has NUM_WORDS elements. A C function has no way to test whether an array passed in is the right size; this is up to the caller to ensure. The function may also assume that the array program has at least pgm_size elements (which it also cannot check itself). 3.3 unsigned short disassemble(const unsigned int memory[], unsigned int start_addr, unsigned int pgm_size, unsigned int data_segment_size) A disassembler converts machine language to assembly language, which is the reverse of what an assembler does. This function will do that for a MAD Raisin program stored somewhere its first parameter memory, which is an array of words representing the MAD Raisin's memory. The function may assume that memory is a valid C array that has at least NUM_WORDS elements. The program to be disassembled begins at the MAD Raisin address start_addr in the array. (As in Section 3.2 above, start_addr is a byte address, not an array subscript.) The parameters pgm_size and data_segment_size have the same meanings as they do in the description of load_program() in Section 3.2 above. If its parameters are valid the function should print the program in the format described below and return 1 . If its parameters are invalid it should not produce any output at all and just return 0 . What would make the parameters invalid is described below. (Look at some of the public test outputs for examples.) - Each word in the array memory that represents an instruction should be printed exactly the same way that the function print_instruction() (Section 3.1) prints instructions, with each one followed by a newline. - Then the function should print any subsequent elements of the array that represent data, based on the value of data_segment_size, in hexadecimal. Values of memory words that are less than 1000000016 should be printed using enough leading zeros so they occupy exactly eight places. The values should be printed in the way that print f() prints hexadecimal values using the %x format specifier. (A leading 0x should not be printed.) Instructions must be valid but there are no validity checks for elements in the data segment, because data can have any value. Note that if its parameters are valid the function will produce exactly pgm_size lines of output, but not all of them will necessarily be printed instructions- if data_segment_size is greater than 0 the last data_segment_size lines will represent data and just be printed in hexadecimal. The function's parameters would be invalid if: - Any of the words of memory, starting at the address start_addr (recall that it is a byte address, not an array index), which represent instructions (not data) are invalid, using the criteria described in Section 4 below. - The size of the program in program to be printed, as given by pgm_size, is larger than the number of words in the MAD Raisin's memory. (It's not invalid if pgm_size is zero; in that case nothing would be printed, and the function would just return 1.) - If printing pgm_size words starting at the address start_addr would go past the end of the MAD Raisin's memory. - The values of the parameters pgm_size and data_segment_size would mean that there is not at least one instruction in the program's text segment. (It's not invalid if the data segment has no elements though.) 4 Instruction validity As mentioned, some values that can be stored in a MAD Raisin word or register don't represent valid machine instructions. The reasons that a word could be invalid as a machine instruction are: - The value in its opcode field (leftmost five bits) is wrong. The MAD Raisin has 22 instructions (hence opcodes) on (with enum values in raisin.h between HALT and BRANCH), so values outside that range don't represent opcodes. - If a register operand that is actually used by the instruction has an invalid number. There are only eighteen registers on the MAD Raisin with numbers 0-17 (and enum values R0 through R17 defined in raisin.h), so values that can fit into the five-bit register fields that are outside that range don't represent valid register numbers. - If it is an instruction that uses its address/immediate field (rightmost 17 bits) to store a memory address and the value in the field is not evenly divisible by 4. Since everything on the MAD Raisin occupies a 4-byte word, and all data and instructions are accessed only in whole-word units, the only valid memory addresses are divisible by 4. Note: if the parameters represent an instruction that uses the address or constant field for storing a memory address its value must be divisible by 4 . But if the instruction is an li instruction, which is using the field to represent an immediate (constant) value instead, it does not have to be divisible by 4 . - If the parameter represents an instruction that modifies its first register operand's value and the instruction's register 1 field contains the number of one of the special unmodifiable registers (the program counter register R17 or the zero register R16). The instructions that use and modify the first register operand are indicated in Section 2.4. R16 and R17 could be used as the second register operand of any instructions that use two registers, or as the first operand of instructions that use but don't modify their first register operand, but not as the first operand of instructions that would modify their first register operand's value. Note: the values of fields that are not used by instructions have no effect on validity, and it's immaterial what they contain. For instance, a neg instruction may have anything at all in its rightmost 22 bits (the second register operand field is valid. But instructions that do use fields, and have incorrect values in them, are invalid

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Information Quality Dimensions, Principles And Techniques

Authors: Carlo Batini, Monica Scannapieco

1st Edition

3319241060, 9783319241067

More Books

Students also viewed these Databases questions

Question

What are Measures in OLAP Cubes?

Answered: 1 week ago

Question

How do OLAP Databases provide for Drilling Down into data?

Answered: 1 week ago

Question

How are OLAP Cubes different from Production Relational Databases?

Answered: 1 week ago