SOLUTIONS 5-1 SOLUTIONS 6-1

4-4 There is more than one solution. Here is one: 0000011 001 001 101

CHAPTER 5 SOLUTIONS 5-1

The important point in making the translation to SPARC is how to manipulate the stack frame. Prior to the function call, the calling routine places the parameters on the stack. When the called routine min is invoked, its first task should be to save the return address on the stack. There is no need to do it here since min makes no nested calls. So, the stack pointer currently points to j, and i is one word deeper into the stack: addcc r14, -4, r14 Push m onto stack frame ld r14, 8, r1 r1 - i ld r14, 4, r2 r2 - j orncc r2, r0, r3 r3 - ~r2 addcc r3, 1, r3 r3 - r3+1 2s comp of j addcc r1, r3, r4 r4 - i - j bneg J_GT_I branch if ij ji actually st r2, r14 m - j ba DONE J_GT_I: st r1, r14 m - i DONE: jmpl r15, 4, r0 Return value m is on stack. 5-2 10 00010 100110 00010 1 0000000001010 5-3 be 3 5-4 Symbol Value All numbers are given in base 10 K EQU 77 P EQU 1 ORG 32 JUMP MAIN PMUL: SUB 2,R0 LOAD A,R1 BZERO ANOTZ LOAD 0,R1 JUMP DONE ANOTZ: LOAD B,R3 BZERO BNOTZ LOAD 0,R3 JUMP DONE BNOTZ: LOAD 0,R1 STORE R1,A BZERO PMUL DONE: JUMP DONE A: 12 B: 15 K 77 P 1 MAIN X PMUL 36 A 88 ANOTZ 56 DONE 84 B 92 BNOTZ 72 5-5 .macro mov arg1,arg2 orcc r0, arg1, arg2 .endmacro 5-6 The program would still work correctly, but the return would go to the nop instruction that follows the call, thus wasting an instruction cycle. 5-7 When the code finishes execution: 1 A is incremented by 2; 2 B has the original value of A; 3 The stack is restored to its original state.

CHAPTER 6 SOLUTIONS 6-1

Note that r1 goes in the rd field for an st instruction: 11 00001 000100 00010 0 00000000 00011 op rd op3 rs1 i rs2 DECODE: 1 11 000100 00 = 1808 Microinstructions: 0, 1, 1808, 1809, 40, 41, 42, 43, 44, 2047 6-2 1615: 000000 1 000000 0 100010 0 0 0 0001 110 00000010101 21: 100001 0 100001 0 100011 0 0 0 0010 000 00000000000 6-3 00000000 00000000 0 00 00000000 0 00000111 00000011 0 01 00010101 0 11111111 11111111 1 11 11111110 1

CHAPTER 7 SOLUTIONS 7-1