Assigned Byte 0 Assigned Byte 1 B0 B1: Computer Organization & Assembly Language ASSIGNMENT#2 (FALL 2023)
Assigned Byte 0 Assigned Byte 1 B0 B1: Computer Organization & Assembly Language ASSIGNMENT#2 (FALL 2023)
❖ You are required to solve the assignment on this document and submit it on GCR (SoftCopy)
❖ You have to use your Roll No and consider it as decimal for the unique declaration purpose.
❖ Exclude the characters and the first two digits from your Roll number.
FOR EXAMPLE: If your number is 22i-7823, use 7823, where A0 assign the first digit of it.
                                             Assign Digit 0       Assign Digit    Assign Digit 2         Assign Digit 3
                                                                       1
          Short for Assigned Digit                A0                  A1               A2                     A3
                                                    Page 1 of 37
                       COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
                                                  Page 2 of 37
                      COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
   NOTE: AND performs a Boolean(bitwise) AND operation between each pair of matching bits in two
   operands and place result in the destination. AND instruction always clear overflow and carry flags. It
   modifies Sign, Zero and Parity flags.
Sign 1
Zero 0
                                                           Carry           0
                                                 Flags
                                                           Overflow        0
Parity 1
                                                           Auxiliary       0
3. Update memory after executing code given below
CODE
                                                   Page 3 of 37
                      COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
MEMORY
            0 1       2     3     4    5     6    7     8    9     A    B       C      D     E    F
   4000     61   62   63   64    65   66    67    68   69    6a   6b    6c      6d     6e    6f   70
   4010     71   72   73   74    75   76    77    78   79    7a   41    42      43     44    45   46
   4020     47   48   49   4a    4b   4c    4d    4e   4f    50   51    52      53     54    55   56
                                                   Page 4 of 37
                     COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
                                           CODE
 .data ary db 26 dup(?) arysize=
      $-ary ary_copy db arysize
      dup(0) endmem db 1
LOOP L1
MEMORY
           0 1       2    3     4    5    6    7     8    9    A    B     C    D    E    F
  4000     41   42   43   44   45   46   47    48   49   4a    4b   4c   4d   4e    4f   50
  4010     51   52   53   54   55   56   57    58   59   5a    61   62   63   64    65   66
  4020     67   68   69   6a   6b   6c   6d    6e   6f   70    71   72   73   74    75   76
6. Update register after each line of code and update Flags after executing the following code
                                                Page 5 of 37
                       COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
                                              AX      a5         93
                                              AX      5a         6c
                                              AX     00          00
                                              AX     ff          ff
                                              AX     00          00
                                              AX      5a         37
                                              Al     3           7
                                              AH     5           b
                                              AL     6           c
                                                     Sign        0
                                                     Zero        0
                                                     Carry       0
                                             Flags
                                                     Overflow    0
                                                     Parity      1
                                                     Auxiliary   0
7. Write a program that finds parity of number given below? HINT: Use XOR and LOOP to find parity
Answer:
.386
.model flat, stdcall
.stack 4096
.code
main PROC
                                               Page 6 of 37
                        COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
  l1:
     mov ah,
     [esi] xor al,
     ah inc esi
  loop l1
INSTRUCTIONS: (Question 8-11) Perform each of the following operations on word size 2’s complement
numbers and update the answer and value of flags after performing the arithmetic. Perform all the steps in
the “calculation” box, only filling the answer will not get any credit.
8. Update flags after arithmetic instruction? Also state which of the following jumps will taken or not
   taken
Js ✓
jp ✓
                Sign        1                                                           Calculation
     Flags Zero            0               1111 1111 1111 1110
                                           + 1111 1100 0111 0000
                Carry      1        ———————————————
                                    1 | 1111 1100 0110 1110 PE=0, ZF=0
                Overflow 0
                                                 Page 7 of 37
                        COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
                                     OV = CVxorPL = 0 also, adding 2 negative numbers and getting a negative answer, so OV=
                                     also, answer = -914 > -32,768, so OV=0
9. Update flags after arithmetic instruction? Also state which of the following jumps will taken or not
   taken
Jz ✓
Jo ✓
Js ✓
                                                         jp                            ✓
                Sign            1                                                           Calculation
                Zero            0            0111 1011 0001 1010
                                             + 0011 0001 0001 0101
                Carry           1      ———————————————
                                       0    | 1010 1100 0010 1111 PE=0, ZF=0
                Overflow        1
       Flags                           ^ CV=0 ^PL=1           ^ AC=0
                Parity          0
                                       OV = CVxorPL = 1
                                       also, subtracting a negative number from a positive number (adding 2 positive numbe
                                       answer, so OV=1 since 7b1a < ceeb, CV=1
10. Update flags after arithmetic instruction? Also state which of the following jumps will taken or not
    taken
Jnz ✓
                                                          Jo                            ✓
                                                          Js       ✓
jnp ✓
                                                 Page 8 of 37
                        COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
11. Update flags after arithmetic instruction? Also state which of the following jumps will taken or not
    taken
Jz ✓
Jno ✓
Jns ✓
jp ✓
          Sign       0                                           Calculation
          Zero       0                 1111 1010 1011 1101
                                + 0110 1000 0100 1010
          Carr       1
                             ———————————————
          y                  1       | 0110 0011 0000 0111       PE=0, ZF=0
    Fla Overf        0       ^ CV=1 ^PL=0               ^ AC=1
    gs low
          Parit      0       answer = 25,351 < 32,767, so OV=0
          y
          Auxil      1
          iary
12. Update value of ax and cx registers after every iteration. Update any changes to the done to flag
                                                    Page 9 of 37
                      COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
                                              1           2           3       4       5       6
                                       CX   5         4           3       2       1       0
                                       AX   1         2           3       4       5       6
                  Sign       0                                                        Calculation
                  Zero       1                    The final calculation is ECX being decremented to 0
                  Carry      0
          Fla     Overfl     0
          gs      ow
                  Parity     1
                  Auxili     0
                  ary
13. Fill flag after every CMP instruction
                                                                                  1                    Calculation
                                                              Sign
                                                   N
                                                  TAOT                            0                   0111 1111
                                                                   ro
                                                              Ze                                 + 1000 0000
                                                      KEN                                     ——————————
                                                    Page 10 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
                            NOT
            TAK    EN       TA    KENSig       n        1                      C
    Jnl                            Ze          ro       0                1111 1
                            ✓
                                                                   + 0000 0
    Jnle                    ✓      Ca          rry      0
                                                                ———————
    JL                             Ov          erflow   0   0        | 1111 1
            ✓
                                                            ^ CV=0 ^PL=1
                    Flags          Parity               1
                                                                since negative
                                                                (0 is just 0) = ne
                                                                since bigger nu
                                                                1=65,535) -
                  Page 11 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
                 Page 12 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
                                                   OV=CVxorPL=0
                                                   also, answer=67 < 32767, so OV=0
                     NOT
                                       0                Calculation
         TAKEN T AKENSign
                  Zero                 1                0000 0000 0000 0000
   JG             ✓                              + 0000 0000 0000 0000
   JNL                Carr      y      0       ——————————————
         ✓                                     —
   JLE                Ove      rflow   0       0    | 0000 0000 0000 0000
         ✓                                     ^ CV=0 ^PL=0             ^ AC=0
                      Parity           1
             Flags    Auxiliary        0       PE=1, ZF=1
                                               OV=CVxorPL=0
                 NOT                           also, 0 < 32,767 and 0>-32768,
         TAKEN T AKEN                          so
                                               OV=0
   JL             ✓
   JNG   ✓
   JGE                Sign             0                Calculation
         ✓
             Flags    Zero             1       same as above
Carry 0
                      Page 13 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
                 Page 14 of 37
                     COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
                                          CODE
 .data data dw 66h,0,045h,8,040h,9,-025h,9,-010h,011h
        swap db 0
 .code main
     PROC
     start:
           mov swap,0 ;Reset swap flag to no swap mov ebx,0
             ;initialize array index to zero
 loop1:
          mov ax,[ebx+data]         ;load number to ax
          cmp ax,[ebx+data+2]       ;compare with next number
          jge noswap               ;no swap if already inorder
              INVOKE ExitProcess,0
      main ENDP END main
                                                                                          COUNT      SWAP
                                               Page 15 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
                                            (after last
                                            switch, swap
                                            resets to 0 if
                 Page 16 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
                                            resets to 0 if
                                            it was 1
                                            before)
                                            (after last
                                            switch, swap
                                            resets to 0 if
                 Page 17 of 37
                                                                      37       0
                                              38 LANGUAGE
               COMPUTER ORGANIZATION & ASSEMBLY      0
66   45   40      9     9       11        8          0   -10   -25   41    1
                                                                     39    0
                                                                           it was 1
                                                                           before)
66   45   40      9     9       11        8          0   -10   -25         (carrying
                                                                      43       1
                                                                           from above)
66   45   40      9     9       11        8          0   -10   -25
                                                                      44       1
66   45   40      9     11      9         8          0   -10   -25         (carrying
                                                                      45       0
                                                                           from above)
                                                                           (carrying
66   45   40      9     11      9         8          0   -10   -25         from above)
                                                                           (after last
                                                                           switch, swap
                                                                           resets to 0 if
66   45   40      9     11      9         8          0   -10   -25         it was 1
                                                                           before)
66 45 40 9 11 9 8 0 -10 -25
66 45 40 9 11 9 8 0 -10 -25
66 45 40 9 11 9 8 0 -10 -25 46 0
66 45 40 9 11 9 8 0 -10 -25 47 0
66 45 40 9 11 9 8 0 -10 -25 48 0
66 45 40 11 9 9 8 0 -10 -25 49 1
66 45 40 11 9 9 8 0 -10 -25 50 1
66 45 40 11 9 9 8 0 -10 -25 51 1
66 45 40 11 9 9 8 0 -10 -25 52 1
66 45 40 11 9 9 8 0 -10 -25 53 1
66 45 40 11 9 9 8 0 -10 -25 54 0
66 45 40 11 9 9 8 0 -10 -25 55 0
66 45 40 11 9 9 8 0 -10 -25 56 0
66 45 40 11 9 9 8 0 -10 -25 57 0
66 45 40 11 9 9 8 0 -10 -25 58 0
                                     Page 18 of 37
                          COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
66 45 40 11 9 9 8 0 -10 -25 60 0
66 45 40 11 9 9 8 0 -10 -25 61 0
66 45 40 11 9 9 8 0 -10 -25 62 0
66 45 40 11 9 9 8 0 -10 -25 63 0
15. You are working on the development of an encryption process for a secure communication protocol for a
    highly sensitive application. As part of the encryption process, the system needs to perform a series of
    bitwise operations on the provided data. Your task is to design and implement an assembly program that
    can do these series of transformations on a 32-bit integer number mDATA.
Suppose an 16-bit number mFLAGS, you have to transform the data by applying the series of following 4-
bitwise operations on mDATA according to the specific bit value in mFLAGS as defined in the operation
description provided below:
                                Note: Not allowed to use any predefined rotate instruction like (ROL,ROR etc.)
2               3               Bitwise RIGHT Rotation:
                                Perform the right bitwise rotation if the bit is ON. The rotations must be
                                performed X times. The number X must be extracted from the mFLAGS using its
                                first 5 bits. Ensure that the rotation is circular, meaning bits shifted out from one
                                end are rotated back to the other end.
Note: Not allowed to use any predefined rotate instruction like (ROL,ROR etc.)
                                                     Page 19 of 37
                       COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
You must provide your permutation table in a char array in the data section.
                                 For Example: the above permutation table can be represented in char array as
                                 “160709 … 15”, where the bit positions are in ascending order, i.e., 16 is the
                                 new position for bit 0, and 07 is the new position for bit 1, and so on.
Note: You have to generate 16-bit number mFLAGS using your Roll Number excluding the character and the
first digit. For example if the number is 22i-9986 the mFLAGS must be 29986.
Answer:
.386
.model flat, stdcall
.stack 4096
.data
   mData dd 1378cef6h
   separate1 db 0
   mFLAGS dw 20899
   separate2 db 0 permPosition db 14, 15, 29, 3, 20, 4, 0, 6, 23, 5, 25, 7, 10, 11, 8, 28, 1, 24, 21, 9, 30, 16, 2, 18,
22, 17, 31, 26, 13, 12, 27, 19
                                                    Page 20 of 37
                       COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
jnc step2
  step2:
     mov ax, mFLAGS
     shl ax, 3
jnc step3
    mov ebx,
    mDATA mov
    edx, 0 shrd ebx,
    edx, cl or ebx,
    edx mov
    mDATA, ebx
step3:
jnc step4
                                        Page 21 of 37
                   COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
step4:
endCode
  mov ecx, 32
  mov esi, 3
  xor eax, eax
  xor ebx, ebx
  xor edx, edx
  l1:
     push eax
       test ax, 0FFh jz
       doNotDecrementESI
       mov bl, 8 div bl
       test ah, ah jnz
doNotDecrementESI
dec esi
      doNotDecrementESI:
    pop eax
                                          Page 22 of 37
                      COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
test bl, dl
     jp replacing0
     jnp replacing1
     replacing0with0:
     replacing1with1:
     inc eax
   loop l1
endCode:
main ENDP
END main
                                               Page 23 of 37
                         COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
Result db 0
.code
            mov ax,0
           mov cl,4
         mov al,multiplicand
         mov bl,multiplier
     checkbit: shr
         bl,1 jnc skip
         add result,al
     skip:
         shl al,1 loop
         checkbit
                                al                                    bl
     CF                   multiplicand                         Multiplier                    CF   result
 0            0    0        0        0 0 1     0    01        0       0      0      0
                                                              (carr   ying   from   above)
 0                                                      0      0      0      0      0
              0    0        0        00 0      1    0
                                                              (carr   ying   from   above)
 0                                                      0     0       0      0      0
              0    0        0        00 0      0    1
 0                                                      1      0      0      0      0
              0    0        0        00 0      0    0
17. Consider the following code and fill given registers and memory accordingly after each step?
    Note: Considering it as a 16 bit architecture
                                                   CODE
          NOTE
          ;            ;ADC is add through carry
                                                        Page 24 of 37
                    COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
.code
          mov ax,0
          mov ecx,16                                  ;initialize bit count to
16
         mov dx, multiplier                            ;initialize bit mask
checkbit:
        shr
        dx,1
        jnc noadd                                 ;skip addition if no carry
10 02d5 0 0 0 0 - 0 0 0706 0 0
                                            Page 25 of 37
                      COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
multiplicand = 03830000
multiplier = 05aa result
= 0013e3fe
18. Modify and Rewrite the code given above for following data declaration?
 .data
          multiplicand DQ 78237823h ;value of Assigned doubleword in hexa
          multiplier DW 383h ;Assigned word in hexa
          result DQ 0         ;result of the multiplication
Answer:
                                              Page 26 of 37
                        COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
        checkbit:
          shr dx,1
          jnc noadd
19. Perform unsigned binary multiplication using following given flow chart?
     NOTE: Your computer width is 8-bit, Multiplicand is (1000)2 of the assigned number and Multiplier is (1100011)2 of
    the assigned number
                                                     Page 27 of 37
                    COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
0 0 0 0 1 0 00
                                                                        M (MULTIPLICAND)
                                                                    0   0 0 0 1 0      00
0 0 0 0 1 0 00
                                                                        M (MULTIPLICAND)
                                                                    0   0 0 0 1 0      00
0 0 0 0 1 0 00
                                                                        M (MULTIPLICAND)
                                                                    0   0 0 0 1 0      00
0 0 0 0 1 0 00
                                                                        M (MULTIPLICAND)
                                                                    0   0 0 0 1 0      00
0 0 0 0 1 0 00
                                                                        M (MULTIPLICAND)
                                                                    0   0 0 0 1 0      00
0 0 0 0 1 0 00
                                                                        M (MULTIPLICAND)
                                                                    0   0 0 0 1 0      00
0 0 0 0 1 0 00
                                                                        M (MULTIPLICAND)
                                                                    0   0 0 0 1 0      00
                                                Page 28 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
0 0 0 0 1 0 00
                 Page 29 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
                  Q (MULTIPLIER)
          0   0    1   1   0   0   0 1
0 0 0 1 1 0 0 0
Q (MULTIPLIER)
0 0 0 0 1 1 0 0
Q (MULTIPLIER)
1 0 0 0 0 1 1 0
Q (MULTIPLIER)
1 1 0 0 0 0 1 1
                  Q (MULTIPLIER)
          1   1    0   0   0   0   1 1
0 1 1 0 0 0 0 1
                  Q (MULTIPLIER)
          0   1    1   0   0   0   0 1
0 0 1 1 0 0 0 0
                       Page 30 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
Q (MULTIPLIER)
0 0 0 1 1 0 0 0
                       Page 31 of 37
                COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
    A (ACCUMULATOR)
0   0   0   0   1   1   0 0
0 0 0 0 0 1 1 0
A (ACCUMULATOR)
0 0 0 0 0 0 1 1
A (ACCUMULATOR)
0 0 0 0 0 0 0 1
A (ACCUMULATOR)
0 0 0 0 0 0 0 0
    A (ACCUMULATOR)
0   0   0   0   1   0   0 0
0 0 0 0 0 1 0 0
    A (ACCUMULATOR)
0   0   0   0   1   1   0 0
0 0 0 0 0 1 1 0
                                     Page 32 of 37
                            COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
0Shift
            0   0   0   0   0   0   1 1
 C
 Add
0Shift
      C
      Add
Shift
 C
 Add
0Shift
 C
 0Add
0Shift
 C
 0Add
0Shift
    0 C
      Add
Shift
                                                       Page 33 of 37
                    COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
AX Q Q-1 M Count
                                             Page 34 of 37
                   COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
00 0110 0011 - 1
                                     Page 35 of 37
                       COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
Answer:
AX Q M Count
                                                    Page 36 of 37
            COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
Page 37 of 37