CSE 30 -- Lecture 5 -- Oct 8


In this lecture, we went through in more detail what happens with a macro assembler stages: macro expansion and local label generation, address assignment to instructions and data placed into the .text and .data streams, and finally the generation of machine code.

Throughout, we used the example program from last time, which would correspond roughly to the following (if we were writing C code):

main()
{
	int i;
	for (i = 0; i < M; i++) {
		array[i] = gauss(i);
	}
}
and
gauss(N)
{
	int i, sum;
	sum = 0;
	for (i = 0; ++i >= N; ) {
		sum = sum + i;
	}
	return sum;
}
Note: The retLabel in the virtual handouts should have been LretLabel, since it must be different from every use of the call macro. This has been fixed in the on-line version, but if you printed it out, you should mark this on your printed copy.

In the subroutine call / array-indexing example (masm, asm, oic), we used a pattern of 5 instructions twice, once for clearbuf and once for xferbuf. This code synthesizes the last two instructions in a move sequence:

	subge tmp,tmp,next
	subge tmp,sum,next
	subge array,array,next
	sugge array,tmp,next
except that we want all the array's to be array only on the first time through the loop -- they should change to array+1 on the second time through, and array+2 on the third time through, and so forth. Make sure you understand the use of the *template, *Offset, and *Incr variables to accomplish this.

Assignment 2

Write a macro assembly program to compute A * B + C * D, where A is located in memory location 0, B in location 1, etc. The result should be placed in memory location 4. The entry point for your program should be at address 0x0100.

It is okay to treat the input values as unsigned numbers.

Hand translate this macro assembly language program to ``naked'' assembly by hand expanding all macros and generating local labels. Next, hand translate this ``naked'' assembly program to machine code, by first assigning addresses to all instructions and data words, and then generating the machine code.

Test your program by using the oic simulator with various inputs. Use the -v flag if you need to see a verbose, instruction-by-instruction execution trace. If, for example, you had an input file input.oic containing:

0x0
0x000000000010
0x000000000003
0x000000000020
0x000000000004
and your machine-code program is in the file assn2.oic, you can obtain an instruction-by-instruction execution trace with the command:
% oic -v -e 0x100 -s 0x4 -c 1 assn2.oic input.oic
or, if you just want to see the final result, you can type in
% oic -e 0x100 -s 0x4 -c 1 assn2.oic input.oic
which dumps out a single memory word at location 0x4. If you need to have separate input files due to the use of .org statements, your testing might look like:
$ oic -e 0x100 -s 0 -c 5 assn2.txt.oic assn2.data.oic input.oic 
Starting program at PC = 256 (0x0100)
End-of-program infinite loop at 281
0000: 0000 0000 0010
0001: 0000 0000 0003
0002: 0000 0000 0020
0003: 0000 0000 0004
0004: 0000 0000 00b0
Program completed in 227 (0xe3) steps
$ oic -e 0x100 -s 0 -c 5 assn2.txt.oic assn2.data.oic input2.oic 
Starting program at PC = 256 (0x0100)
End-of-program infinite loop at 281
0000: 0000 0000 dead
0001: 0000 0000 beef
0002: 0000 0000 c001
0003: 0000 0000 d00d
0004: 0001 421e d990
Program completed in 424667 (0x67adb) steps
$
Make sure you test your code to make sure that it works with all inputs. (Note that if your input numbers are too large, then your program may take a long time to run, since 2^48 is a huge number.)

To turn in your assignment, place all of your program files in a subdirectory assn2:

% cd ~/assn2
% ls
assn2.asm	assn2.masm	assn2.oic	input.oic
subr.asm	subr.masm	subr.oic
Turn in the assignment by typing in:
% cd
% ls
assn2		...
% tar cf - assn2 | turnin -c cs30f
See the Q&A page for commenting requirements.
[ search CSE | CSE home | bsy's home page | webster i/f | yahoo | hotbot | lycos | altavista ]
picture of bsy

bsy+www@cs.ucsd.edu, last updated Mon Nov 30 21:53:24 PST 1998.

email bsy & tutors


Don't make me hand over my privacy keys!