ECE243 Practice Question Website


Main Assembly Programming Input/Output Memory Computer Architecture Advanced Topics

Assembly Programming Questions





Question 11

Consider the assembly in (i), which implements the C-code factorial function in (ii).

(i)
	003000: FACT	link	a6,#0
	003004: 	tst.l 	8(a6)
	003008: 	bne 	NEXT
	00300c: 	move.l 	#1,d0
	003012: 	bra 	DONE

	003016: NEXT 	move.l 	8(a6),d0
	00301a: 	subq.l 	#1,d0
	00301c: 	move.l 	d0,-(a7)
	00301e: 	bsr 	FACT
	003022: 	addq.l 	#4,a7
	003024: 	mulu.w 	8(a6),d0

	003028: DONE 	unlk 	a6
	00302a: 	rts
	
(ii)
	int factorial(int x){
		if (x == 0){
			return 1;
		}
		return x * factorial(x-1);
	}
	
Consider a call to factorial() where x is 2. The appropriate state of memory and registers at the point just before the link instruction executes for the first time is shown in (iii).

(iii)
Memory:
... ?
$24C $2000
$250 2
Resisters:
a7: $24C
a6: $254
PC: $3000


Your task: fill in the memory table and the two registers below, showing the configuration just before the unlk instruction executes for the first time. Also write one of the following descriptions to the right of each memory table entry: “return address”, “local variable”, “parameter x”, “callee-saved register”, “caller-saved register”, or “old a6”.

Address Value Description
$214    
$218    
$21C    
$220    
$224    
$228    
$22C    
$230    
$234    
$238    
$23C    
$240    
$244    
$248    
$24C    
$250    

Registers:

a7:

a6:

Hints

  1. d0 is used for the return value;
  2. FACT calls itself recursively, but this behaves no differently than one subroutine calling another;

Answer

Address Value Description
$214    
$218    
$21C    
$220    
$224    
$228    
$22C    
$230 $23C old a6
$234 $3022 return address
$238 0 parameter x
$23C $248 old a6
$240 $3022 return address
$244 1 parameter x
$248 $254 old a6
$24C $2000 return address
$250 2 parameter x

Registers:

a7: $230

a6: $230