⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 http:^^www.cs.bu.edu^students^grads^carter^courses^cs210^hwk2-notes

📁 This data set contains WWW-pages collected from computer science departments of various universities
💻 EDU^STUDENTS^GRADS^CARTER^COURSES^CS210^HWK2-NOTES
字号:
Date: Tue, 14 Jan 1997 20:29:29 GMTServer: NCSA/1.5Content-type: text/plainLast-modified: Tue, 27 Feb 1996 16:35:59 GMTContent-length: 5695Notes from Section meeting discussing the assembler.Two-Pass Assembly.You will need to write a two-pass assembler.  The first pass willnote the line numbers for each label.  The second pass will generatecode, and when branch/call statements are encountered, will use theoffset from the the current line to the label as noted in pass 1.So, you will need two loops that pass over the code.  One way to loopover the code would be:line_t ln;start();for (ln = next_line(); END_OF_FILE != ln.format; ln = next_line())To help do the label calculation I have provided two routines:associate() and lookup().  void associate(char *s, int i);	/* associates string with given integer */int  lookup(char *s);		/* returns -1 if string is not found */associate takes a string and an integer (line number) and stores them.lookup() takes a string and returns the associated integer.  It wouldmake sense to use associate() in the first pass and lookup() in thesecond pass.Note that for this code:	foo: 	add ....		sub ....		ba  foowhen you generate code, the number you put into the branch target fieldfor the ba instruction would be the number -2.  That is, you encode thedistance in INSTRUCTIONS between the branch and its target.Code Generation.The front end converts all the text of the source file into binary.  Inparticular, numbers will be supplied to you in binary.  You can tellthat by the fact that they are declared in C as int's.  In C, an int isa binary represented integer (in two's complement).You will need to take binary ints, and place them into the bitfields described in chapter 8 of your book.  To do this in C:	Use the << operator to shift bits to the left.  The places		on the right will be filled with zeros.	Use the | (or) operator to "or" two bit patterns together.		This is a good way to combine bit patterns.	Use the & operator along with a "mask" bit pattern to set		certain bits to 0.   For example, to put the number -7 into a bit field from position 14 to18 in the instruction:	int instr;    /* the instruction */	int num	      /* the number; contains the value -7, say */	/* at the outset num contains 11111111111111111111111111111001 */	/* first mask all bits to 0 except those we want to keep */	/* this is a pattern of 5 1's since we want to keep 5 bits */	num = num & 0x1f;	/* now num contains 00000000000000000000000000011001 */	/* now shift the bits to their proper location */	num = num << 14;	/* now num contains 00000000000001100100000000000000 */	/* now "or" them into the instruction */	instr = instr | num;	Finally, when you have filled all the bitfields in the instruction,write the bits to stdout as follows:	fwrite(&instr, sizeof(instr), 1, stdout);Frequently asked questions about the SPARC assembler project:--When I run the program I get "Illegal instruction (core dumped)".  Doesthis mean that my assembler is not converting the assembly filecorrectly into machine code?  Exactly.   Your assembler generated a bit pattern that the processor cannotinterpret as an instruction.--Is there any way I can look at the machine code produced by my assemblerprogram?  There are two ways.   First, use "od -X":% od -X fib.ao0000000 9de3bfc0 a0080010 a2102001 a61020010000020 a8100018 10800006 01000000 a40400110000040 a0100011 a2100012 a604e001 80a4c0140000060 06bffffb 01000000 b0100012 81c7e0080000100 81e80000The lefthand column is the byte adress of the first word, in octal.  Theother columns are the binary words in your machine language file.An even more useful thing to do is to ask gdb to tell you whatinstructions you have generated, using gdb's "disassemble" command.Load the "runner" into gdb, set a breakpoint at the line that calls"pgm", run the program (hitting the breakpoint), use "si" until gdb tellsyou you are in the subroutine you've written, which is called pgm(),then "disass" the subroutine (your code).You should look at the source for "runner" to see how it works tounderstand why this works. --What's going to happen when we try to stick a 32 bit number into a 22 bit spot, like in a format 2 .... or in format 3, put a 32 bit immediate into a spot that's too small for it? Also, if the address in format 1 is negative, using 2's complement don't we need all those bits?Take the case of moving a 32 bit number into a 22 bit field.  Firstassume the number is positive.  It's OK to throw away the high 10 bitsif they are all zero, right?  That is, if the actual value contained inthe 32-bit field can be represented in 22 bits, then the 10 high orderbits are zero anyway.  If the number can't be represented in 22 bits(bigger than 4M), then there is nothing we can do anyway -- the user hasto be alerted of an error condition.  So, the answer is, make sure thehigh order bits are zero, and if they are, throw them away, otherwisesignal an error.The case of negative numbers is analagous, with 1's replacing 0's in thehigh order positions.   If the negative number can be encoded in 22bits, then the 10 high order bits are all 1's and they can be thrownaway. (Think about it -- to get the positive version of the number wewould flip the bits, so those ones would become zeroes).--The "ta" instruction:  This is just one of the family of trapinstructions, which are format 3 instructions.  Chapter 8 refers to thewhole set of them as the "ticc" instruction.  Instead of using Chapter 8for this instruction, look at p. 386 in Appendix C.--How can I send an error message to the terminal if output is redirected?Send your error messages to "stderr" using fprintf:	fprintf(stderr, "Error: bad input\n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -