CSE 30 -- Lecture 14 -- Nov 18


Assignment number 6:

Create a memory copying "wmemcpy" function that is efficient and is capable of dealing with the case where the two segments of memory (destination and source) are overlapping. This assignment will have a due date announced later. This function should fit the following prototype:

	void wmemcpy ( int *srcp, int *destp, int size );

I discussed the relationships between for, while, and do while loops. A general loop will have: A beginning. A body. A branch (to beginning or end). An end. This would appear in well-structured assembly code as

			# loop begins on next line
loop:			# beginning of the body
			# body instructions go here
test:	beq $t0,$0,loop	# branch back to repeat, forward to end
done:			#loop has finished (end)
A do-while loop will fit the above exactly. A while loop would have a jump to the test label before the beginning of the loop. A for loop would include an explicit iterator before the test, as well as an explicit initialization before the jump to the label.

I also discussed if-then statements and switch (or case) statements. Switch statements can be seen as a special case of nested if-then statements. There is a relatively common case where switch statements can be much more quickly implemented than the associated if-then sequence. To do so, the case statement should include an argument in some range that can be normalized to a sequence of integers, like (1, 2, 3, ... ). This allows the cases to be enumerated, and have their labels put into a JumpTable.

Pseudo-assembly code that would have this effect:

	.data
JTable:	.word L0,L1,L2

	.text
switch: bgtu $t0,2,End		# unsigned compare means negatives work too -- bsy
	li $t1,4
	mult $s0,$t0,$t1	# ($s0 = index) gets input * 4
	lw $s1, JTable($s0)
	jr $s1
L0:	add $s2, $s2, $s3
	j End
L1:	add $s4, $s4, $s5
	j End
L2:	sub $s2, $s2, $s3
	j End
End:

Similar C code:

switch(k) {
	case 0:	x = x + y; break;
	case 1: z = z + t; break;
	case 2: x = x - y; break;
}

If I (Matt Hohlfeld) discussed anything else during this lecture, that isn't covered here, please send me mail at hohlfeld@cs.ucsd.edu.

A page for lecture 15 will be forthcoming. Since I didn't take detailed notes during lecture, if anyone with complete notes that would like to make them available to me for creating the lecture 15 page, I would greatly appreciate it.

Thank you for your attention and questions. It was fun leading class.


[ CSE 80 | ACS home | CSE home | CSE calendar | bsy's home page ]
picture of bsy

bsy@cse.ucsd.edu, last updated Mon Nov 25 17:03:16 PST 1996.

email bsy