CS220 Lab 6 Assembly Language
Shift Operations and Sign Extensions
Signed Multiplication and Division

Trish Cornez


Part I: Shifting Operations
The instructions in this part of the lab deal with manipulation of individual bits and bytes.
The shift operations move the set of bits in a register a specified number of bit positions to the left or right.

Logical Shifts:
In general, logical operations treat the number as a string of bits rather than as a signed number.
SHL and SHR are the two logical shifts.

Arithmetic Shifts:
Arithmetic shifts differ from logical shifts in that they preserve the sign bit.
SAL and SAR are the two arithmetic shifts.
  1. mov eax, 0FACE2435h
    SHL eax, 2h

  2. mov eax, 0FACE2435h
    SAR eax, 4h

  3. mov eax, 0FACE2435h
    SAL eax, 4h

  4. mov eax, 0FACE2435h
    SHR eax, 4h

  5. mov eax, 0FACE2435h
    SHL ax, 4h

  6. mov eax, 0FACE2435h
    SAR ax, 4h

  7. mov eax, 0FACE2435h
    SAL ax, 4h

  8. mov eax, 0FACE2435h
    SHR ax, 4h

  9. mov eax, 0FACE2435h
    SHL ah, 8h

  10. mov eax, 0FACE2435h
    SHR ah, 2h

  11. mov eax, 0FACE2435h
    SAR ah, 2h

  12. mov eax, 0FACE2435h
    SAL ah, 2h

  13. mov eax, 0FACE2435h
    SHL al, 2h

  14. mov edx, 0FACE2435h
    SHR dl, 2h

  15. mov edx, 0FACE2435h
    SAR dl, 2h

  16. mov edx, 0FACE2435h
    SAL dl, 2h





Part II: Shifting Operations
Write and run a complete Assembly program that prompts the user for a 32-bit signed number X and prints out the following values:
TIP: Use the shifting operations.
  1. X * 2
  2. 16 * X
  3. X / 32
  4. X / 8
  5. 128 * X




Part III: Sign Extensions
Often it is necessary to sign-extend word data to a double word or byte data to a word.
The three "convert" instructions covered in this part of the lab allow us to extend a byte to a word and a word to a double word.
These instructions are:
  • CBW - the sign in AL is extended through AH.
  • CWD - the sign in AX is extended through DX.
  • CWDE- the sign in AX is extended through EAX.
  1. mov al, 0Ah
    mov edx, 7C90FFFE
    CBW

  2. mov eax, 0Ah
    mov edx, 7C90FFFE
    CWD

  3. mov eax, 0Ah
    mov edx, 7C90FFFE
    CWDE

  4. mov al, -2d
    and eax, 0FFFFh
    mov edx, 7C90000h
    CBW

  5. mov ax, -2d
    and eax, 0FFFFh
    mov edx, 7C90000h
    CWD

  6. mov eax, -2d
    mov edx, 7C92345h
    CWDE




Part IV: Signed Division and Signed Multiplication
Show the content of all registers modified by the instruction segments beow.
NOTE:
    DB = BYTE

    DW = WORD

    DD = DWORD


    Assume the following data declarations:
    B1 DB 80H
    B2 DB 10H
    W1 DW 8000h
    W2 DW 1000h
    D1 DD 80000000h
    D2 DD 10000000h


  1. MOV AL, B1
    MUL B2

  2. MOV AL, B1
    IMUL B2

  3. MOV AX, W1
    MUL W2

  4. MOV AX, W1
    IMUL W2

  5. MOV AL, B1
    SUB AH, AH
    MUL W1

  6. MOV AL, B1
    CBW
    IMUL B2

  7. MOV EAX, D1
    MUL D2

  8. MOV EAX, D1
    IMUL D2


    Assume the following data declarations:
    B1 DB 10H
    B2 DB FEH
    B3 DB 10H
    W1 DW 8000h
    W2 DW 0010h
    W3 DW 100h



  9. MOV AX, W1
    DIV B1

  10. MOV AX, W1
    IDIV B1

  11. MOV AL, B2
    AND AH, 0H
    DIV B1

  12. MOV AL, B2
    CBW
    IDIV B1

  13. MOV DX, 0H
    MOV AX, W1
    DIV W3

  14. MOV DX, 0H
    MOV AX, W1
    IDIV W2




Part V: Assembly Code
Write an efficient portion of an assembly language program to calculate each of the following expressions. Declare each variable as a WORD. Assume that the result in each case fits exactly into a WORD.
  1. (A + B) * (A - B)
    Try executing your program with the following data:
    A DW 5d
    B DW 15d


  2. (A^ 2 - B^2) / 12
    Note: ^ means exponentiation.
    Try executing your program with the following data:
    A DW -5d
    B DW -10d


  3. A / 3 * B * A * E
    Try executing your program with the following data:
    A DW 4d
    B DW -2d
    E DW 3d