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 16-bit signed base in the Ax 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. To convert signed 16-bit Ax to signed 32-bit eAx use CwdE.
  2. Remember that GetDec alters the eAx register.
  3. Use only registers in the Exponent function, no global variables.
  4. The input parameters are word in Ax and byte in Dl but the result is a double word in eAx when a valid result is produced. Don't restore e Ax!
  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 and carry flags are set to 1.
  6. An overflow should be tested for after multiplication using the JO or JC conditional branching instruction. Returning Bl=0 indicates no overflow, returning Bl=1 indicates overflow occurred and the result in Ax 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
.386
.model flat, stdcall
 include v:\common\user\c335\masm32\include\kernel32.inc
 include v:\common\user\c335\masm32\include\masm32.inc
 includelib v:\common\user\c335\masm32\lib\kernel32.lib
 includelib v:\common\user\c335\masm32\lib\masm32.lib

  PutDec 	proto
  GetDec	proto
  PutStrng  proto

.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   GetDec

       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:
       Lea    eDi, Error
       Mov    eCx, 25
       Call   PutStrng
   Endifa:
       Push     0        ; Stop    
       Call     ExitProcess
Main   Endp
;; int Factorial (int Ax);
;;             - Calculate N!
;; Parameters  - Ax 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   ;; Convert Ax to unsigned 32-bit 
        Mov	Dx, Ax
	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: