Homework 5


Overview The assignment provides practice passing parameters in registers and checking for computation errors, due in this case to overflow during multiplication.

Assignment

Design an algorithm as a function to perform integer exponentiation. The inputs to the function should be a signed base and an unsigned exponent. The result of the function should be the base raised to the exponent, for example for base=-4 and exponent=3, then -43 would result in -64.

The inputs to the function should be the 32-bit signed base in the eAx register and the 8-bit unsigned exponent in the Dl register. The 32-bit signed function result is returned in the eAx register. Additionally, the BL register serves as an overflow indicator, returning a 0 to indicate that no overflow occurred and a 1 on overflow. The exponentiation function must save and restore all registers and flags except eAx and BL.

Turn in

  1. Cover Page - Your name, date, and Homework 5. Staple all pages together.
  2. Assembler program (what you typed in).
  3. Output - Use test input of: -43, -20,  230, 231, -231 and 43.
Hints and Warnings :
  1. Remember to use the correct (signed/unsigned) arithmetic operations
  2. Remember that ReadInt alters the eAx register.
  3. Use only registers in the Exponent function, no global variables.
  4. The input parameters are a double word in eAx and byte in Dl and the result is a double word in eAx when a valid result is produced. Don't restore eAx!
  5. Use 32-bit multiplication (e.g. eAx*eCx) which produces a 64-bit result in eDx:eAx. Remember that as only a 32-bit result can be returned, the result cannot extend into eDx. Recall that when the multiplication extends into 64 bits. the overflow flag is set to 1.
  6. An overflow should be tested for after multiplication using the JO branching instruction. Returning Bl=0 indicates no overflow, returning Bl=1 indicates overflow occurred and the result in eAx is wrong..
  7. Test the value of the Bl register in your calling function to determine whether to print the eAx register as a valid result.
A possible pseudo code

Overflow Checking Example

 
Overflow Checking - Factorial Function
;Put needed Library functions here

.data
  Error  db 'Error...Cannot compute N!'
  Prompt db 'Enter unsigned number:'
.code
Main   Proc   Near

       Mov    eCx, 22	 ; cout << "Enter unsigned number:";
       Lea    eDi, Prompt 
       Call   PutStrng   ; cin >> eAx;
       Call   ReadDec

       Call   Factorial  ; eAx = Factorial(Ax)
   Ifa:                  ; if (Ch == 0)
       Cmp    Ch, 0      ;   cout << eAx;
       Je     Thena      ; else cout << Error;
       Jmp    Elsea
     Thena:
       Call   PutDec
       Jmp    EndIfa
     Elsea:
       Mov    eDi, offset Error
       Call   WriteString
   Endifa:
      
Main   Endp
;; int Factorial (int eAx);
;;             - Calculate N!
;; Parameters  - eAx positive integer value 
;; Returns     - eAx factorial value on return
;; Registers Altered - Ch, eAx

Factorial Proc  Near     ;; int Factorial(int Al)
                         ;; { ______________
        Push    eDx      ;;  |Save registers|
        Push    eBx      ;;  |______________|
        PushfD 
        Mov     eDx, 0   ;; 
       
	Mov	eAx, eDx
        Mov     eBx, eAx
        Mov     eAx, 1   ;; F := 1
        Mov     Ch, 0    ;; Ch = 0
 Fora:  Cmp     eBx, 1   ;; for(eBx=eAx,Ch=0; eBx>1 && Ch==0; eBx--)
        Ja      Anda     ;;   F := F * N
        Jmp     EndFora  ;;   If Overflow 
  Anda: Cmp     Ch, 0    ;;      Ch = 1;
        Je      Doa      ;;   eBx = eBx - 1;
        Jmp     EndFora
     Doa:  
        Mul     eBx
      Ifc:        
        Jo      Thenc
        Jmp     EndIfc
       Thenc:
        Mov     Ch, 1
      EndIfc:
        Dec     eBx
        Jmp     Fora
EndFora: 
        PopfD
        Pop     eBx   ;; _________________
        Pop     eDx   ;;|Restore registers|
        Ret           ;;|_________________|
Factorial Endp

         End     Main
Document last modified: