Comp226 - Practical Week 3

Some more interesting programs

Remember:

  1. Write a SPARC program which reads the value of the memory location labelled n, and prints the value of n factorial, that is, the product of the first n natural numbers (not including zero), 1.2.3. ... n.

    You need to use trap 4 to print the contents of %r8 as an integer, and the operation umul to multiply two numbers:

    	umul %r4, %r5, %r6
    stores the product of %r4 and %r5 in %r6.

  2. Write a SPARC program which prints
    	1 and 1 is 2
    	2 and 2 is 4
    	4 and 4 is 8
    	8 and 8 is 16
    and continues until the final number printed is bigger than the word value stored in the memory location labelled max. For example, the above output is appropriate if max is defined by
    	max: .word 13

    Use trap 6 to print strings and trap 4 to print integers. Examples of using these traps can be found in the sample code. At first don't worry too much about formatting the numbers - it's ok if they are in hex (which is what trap 4 will do). When you have this working you can work out how to print numbers in decimal to match the above output correctly.

  3. Write SPARC code which reads the word values of the memory locations labelled x and max, and prints the x times tables as far as max. For example, with
    	x:   .word 3
    	max: .word 5
    your program should print
    	1 times 3 is 3
    	2 times 3 is 6
    	3 times 3 is 9
    	4 times 3 is 12
    	5 times 3 is 15

    Again I don't mind if the numbers appear in hexadecimal at first.

    Always keep your source --- Mike might want to look at what you've done later in the semester.


Dom, Kate and Mike, 2013. COMP226 home page