CIS-77 Home http://www.c-jump.com/CIS77/CIS77syllabus.htm

Lab M11. Integer Arithmetic Part 2: Extended Precision Techniques


  1. Lab Description
  2. Multiplication Instructions
  3. Integer Multiplication and Division
  4. MUL Instruction
  5. MUL Example
  6. IMUL Instruction
  7. IMUL Examples
  8. DIV Instruction
  9. DIV Examples
  10. Signed Integer Division
  11. CBW, CWD, CDQ Instructions
  12. The IDIV Instruction
  13. Divide Overflow
  14. Extended Precision Addition and Subtraction
  15. ADC Instruction, Add with Carry
  16. ADC Instruction, Cont,
  17. ADC Examples
  18. SBB Instruction, Integer Subtraction with Borrow
  19. SBB Instruction, Cont
  20. Assignment: Adding 64-bit and Subtracting 96-bit Integers

1. Lab Description



2. Multiplication Instructions



3. Integer Multiplication and Division



4. MUL Instruction



5. MUL Example



6. IMUL Instruction



7. IMUL Examples


  1. The following fragment computes 8-bit signed multiplication (48 × 4):

        mov  al, 48
        mov  bl, 4
        imul bl      ; AX = 00C0h (decimal +192), OF = 1
    

    Because AH is not a sign extension of AL, the Overflow flag is set to 1.

     

  2. The following instructions perform 8-bit signed multiplication of (-4 × 4), producing -16 in AX:

        mov  al, -4
        mov  bl, 4
        imul bl      ; AX = FFF0h, OF = 0
    

    AH is a sign extension of AL, meaning that the signed result fits within AL, therefore the Overflow flag is cleared.

     

  3. The following instructions perform 16-bit signed multiplication (48 × 4), producing +192 in DX:AX:

        mov  ax, 48
        mov  bx, 4
        imul bx      ; DX:AX = 0000h:00C0h , OF = 0
    

    Here, DX is a sign extension of AX, so the Overflow flag is cleared.

     

  4. 32-bit signed multiplication example:

        .data
        signed_val SDWORD ?  ; Data type is signed doubleword
        .code
        mov  eax, +4823424
        mov  ebx, -423
        imul ebx             ; EDX:EAX = FFFFFFFFh:86635D80h, OF = 0
        move signed_val, eax ; store the result
    

    EDX is a sign extension of EAX, so the Overflow flag is cleared.

     


8. DIV Instruction



9. DIV Examples


  1. The following fragment performs 8-bit unsigned division (83h/2), producing a quotient of 41h and a remainder of 1:

        mov ax, 83h ; dividend
        mov bl, 2   ; divisor
        div bl      ; quotient AL = 41h, remainder AH = 01h
    

     

  2. The following instructions perform 16-bit unsigned division (8003h/100h), producing a quotient of 80h and a remainder of 3.
    Note that DX contains the high part of the dividend, so it must be cleared before the DIV instruction executes:

        mov dx, 0       ; clear high part of the dividend
        mov ax, 8003h   ; set low part of the dividend
        mov cx, 100h    ; set divisor
        div cx          ; quotient AX = 0080h, remainder DX = 0003h
    

     

  3. The following fragment performs 32-bit unsigned division using a memory operand as the divisor:

        .data
        dividend QWORD 0000000800300020h
        divisor  DWORD 00000100h
        .code
        mov edx, DWORD PTR [dividend + 4] ; high doub1eword
        mov eax, DWORD PTR [dividend]     ; low doubleword
        div divisor                       ; quotient EAX = 08003000h, remainder EDX = 00000020h
    

     


10. Signed Integer Division



11. CBW, CWD, CDQ Instructions



12. The IDIV Instruction



13. Divide Overflow



14. Extended Precision Addition and Subtraction



15. ADC Instruction, Add with Carry



16. ADC Instruction, Cont,



17. ADC Examples


  1. The following fragment adds two 8-bit integers (FFh + FFh), producing a 16-bit sum in DL:AL, which is 01h:FEh.

        mov dl, 0
        mov al, 0FFh
        add al, 0FFh  ; AL = FEh, CF = 1
        adc dl, 0     ; DL += CF, add "leftover" carry
    

     

  2. Similarly, the following instructions add two 32-bit integers (FFFFFFFFh + FFFFFFFFh).

    The result is a 64-bit sum in EDX:EAX, 0000000lh:FFFFFFFEh,

        mov edx, 0
        mov eax, 0FFFFFFFFh
        add eax, 0FFFFFFFFh
        adc edx, 0    ; EDX += CF, add "leftover" carry
    

     

  3. The following instructions add two 64-bit numbers received in EBX:EAX and EDX:ECX:

        add eax, ecx ; add low parts EAX += ECX, set CF
        adc ebx, edx ; add high parts EBX += EDX, EBX += CF
        ; The result is in EBX:EAX
        ; NOTE: check CF or OF for overflow (*)
    

     

  4. The 64-bit subtraction is also simple and similar to the 64-bit addition:

        sub eax, ecx ; subtract low parts EAX -= ECX, set CF (borrow)
        sbb ebx, edx ; subtract high parts EBX -= EDX, EBX -= CF
        ; The result is in EBX:EAX
        ; NOTE: check CF or OF for overflow (*)
    

18. SBB Instruction, Integer Subtraction with Borrow



19. SBB Instruction, Cont



20. Assignment: Adding 64-bit and Subtracting 96-bit Integers


  1. Given:

        .DATA
        op1   QWORD 0A2B2A40675981234h ; first 64-bit operand for addition
        op2   QWORD 08010870001234502h ; second 64-bit operand for addition
        sum   DWORD 3 dup(?)           ; 96-bit sum = ????????????????????????h
        op3   DWORD 3 dup(2)           ; 96-bit oper to sub  20000000200000002h
                                       ; Result sum = ????????????????????????h
    
  2. Write an assembly program to compute

  3. Every CPU instruction in your program must have a brief comment!
    The penalty for not following this rule is 15 pts deduction...

  4. Enter computation results by replacing corresponding question marks in the program comments.

  5. Things to consider:

  6. Optional (25 xtra pts.) Improve the program by having it display both sum and diff in hexadecimal format on the screen.

  7. What to submit: