CST121 Final Exam

  1. Next to the code given below, fill in the blanks with the values that will be in the registers when the code is executed.
    
    	;;;;;;;;;;;;;;;;;;;;;;;;; Fill in the blanks with the 
    	;;;;;;;;;;;;;;;;;;;;;;;;;   values of the registers.
    
    	mov	cx, 2
    	mov	dx, 32
    	mov	ah, 2
    				;	AH	AL	(hex)
    	mov	al, "9"		;	02	39
    
    	inc	al		;	02	3A
    
    	idiv	dx		;	_ _	_ _
    
    	shl	ax, cx		;	_ _	_ _
    
    	inc	ax		;	_ _	_ _
    
    ;;;;;;;;;;;;;;;;;;;;;
    
    				;	BH	BL	(hex)
    
    	mov	bh, "6"
    	mov	bl, 2		;	_ _	_ _
    
    	add	bl, bh		;	_ _	_ _
    
    	shl	bx		;	_ _	_ _
    
    	mov	P, ax
    	mov	Q, bx
    
    				;	MEMORY at 0400h
    
    				;	"__ __ __ __"
    
    	.DATA
    	.ORG	0400h
    P	DW	"Ne"
    Q	DW	"eD"
    

  2. Write a sequence of instructions (using assembler code for the Intel 80x86 processor) that will perform each of the following tasks.   Assume that memory locations P, Q, R, S, etc. are 16-bit words, each containing an integer value.
    1. Add up the values in P and Q, and store the sum in R.

    2. Exchange the values stored in P and Q. (Put the value that was in Q into P, and put the value that was in P into Q.

    3. Set the value of S in accordance with the following Java (or C) statements:
                        if (P > Q) {S=  P} else {S=  Q};
      
                        // This could have been written as "S=  P>Q ? P : Q;
      

    4. [[[... FOR EXTRA CREDIT ONLY -- do this ONLY if all others are completed. ...]]]
      Set the values of U and V as follows:
                        if (U>V && U>W) // then
                        {
                            P= U;
                            Q= V;
                        } else {
                            P=  V;
                            Q=  U;
                        }
      Optimize your code to reduce the number of instructions as much as possible.

  3. (a.) Consider the following Java/C++ code to compute the factorial of eight (8).
    	//////// Compute 8! = 8*7*6*5*483*2*1
    	int fac=1;
    	int num=8;
    	do {
    		fac=  fac * num
    		
    		num=  num - 1
    
    	} while (num>0);
    	
    	// //This might also have been written as:
    	//	for ( ; fac *= num--; num>0 )
    
    Now complete the following assembler code, to do the same thing.
    	
    			.DATA
    		NUM	DW	8
    		FAC	DW
    	
    			mov	ax, NUM		; Starting number
    			mov	dx, 1
    

    (b.) Explain what would happen if the starting value for "NUM" was changed from seven to seventeen (17), and how you would change this code to fix the problem.




  4. Consider the following (silly) code.
    			
    		; Save and restore the registers
    			mov	Asave, ax	; Store AX in memory
    			mov	ah, bh		; Move BH to AX
    			mov	al, bl		; Move BL to AX
    			mov	Bsave, ax	; Save contents of BX
    					
    			call	UnsafeSub	; Call the procedure
    
    			mov	ax, Bsave	; Fetch saved value of BX
    			mov	bx, ax
    			mov	ax, Asave	; Restore AX
    
(a.) Simplify the above code, but without using any other instructions, i.e.use only "mov" (and "call") instructions.

(b.) Simplify the code further, using more-appropriate instructions.