CS401 Computer Architecture and Assembly Language Assignment 2 Solution 2013

Assignment comprises of lectures No. 9-13.

No assignment will be accepted after the due date via email in any case (whether it is the case of load shedding or internet malfunctioning etc.). Hence refrain from uploading assignment in the last hour of deadline. It is recommended to upload solution file at least two days before its deadline.

For any query, feel free to email at: cs401@vu.edu.pk

            [ORG 0100H]

MOV AX, 5

MOV CX, AX

XOR DX, DX

DEC CX

CONT:            MOV BX, CX

MUL BX

DEC CX

JNZ CONT

MOV DX, AX

MOV AX, 0X4C00

INT 0X21

Assemble above assembly language code using NASM and answer the following questions:

Q.1. Execute the code in order to examine how changes are made in registers and memory. Students are required to fill in below table after step by step execution of each instruction in the debugger.          

                                                                                                                        (15 marks)

S No.

 

INSTRUCTION

 

REGISTER VALUES

 

INSTRUCTION POINTER (IP)

FLAG/STATUS REGISTER

CF

ZF

PF

SF

AF

1

[ORG 0100H] AX
BX
CX
DX

2

MOV AX,5 AX
BX
CX
DX

3

MOV CX,AX AX
BX
CX
DX

4

XOR DX,DX AX
BX
CX
DX

5

DEC CX AX
BX
CX
DX

6

CONT: MOV BX,CX AX
BX
CX
DX

7

MUL BX AX
BX
CX
DX

8

DEC CX AX
BX
CX
DX

9

JNZ CONT AX
BX
CX
DX

10

CONT: MOV BX,CX AX
BX
CX
DX

11

MUL BX AX
BX
CX
DX

12

DEC CX AX
BX
CX
DX

13

JNZ CONT AX
BX
CX
DX

14

CONT: MOV BX,CX AX
BX
CX
DX

15

MUL BX AX
BX
CX
DX

16

DEC CX AX
BX
CX
DX

17

JNZ CONT AX
BX
CX
DX

18

CONT: MOV BX,CX AX
BX
CX
DX

19

MUL BX AX
BX
CX
DX

20

DEC CX AX
BX
CX
DX

21

JNZ CONT AX
BX
CX
DX

22

MOV DX,AX

AX
BX
CX
DX

26

MOV AX,0X4C00 AX
BX
CX
DX

 Solution:

INSTRUCTION

 

REGISTER VALUES

 

INSTRUCTION POINTER (IP)

FLAG/STATUS REGISTER

CF

ZF

PF

SF

AF

 

 

 

 

 

1

 

[ORG 0100H]

AX       0000  0100  0 0 0 0 0
BX       0000
CX  0016
DX  0000

2

 

MOV AX,5

AX  0005  0103  0 0 0 0 0
BX  0000
CX  0016
DX  0000

3

 

MOV CX,AX

AX  0005  0105  0  0  0  0 0
BX  0000
CX  0016
DX  0000

4

 

XOR DX,DX

AX  0005  0107  0 1 1 0 0
BX  0000
CX  0016
DX  0000

5

 

DEC CX

AX  0005  0108  0 0 0 0 0
BX  0000
CX  0005
DX  0000

6

CONT: MOV BX,CX AX  0005  010A  0  0  0  0  0
BX  0000
CX  0004
DX  0000

7

MUL BX AX  0014  010C  0  0 0 0 0
BX  0004
CX  0004
DX  0000

8

DEC CX AX  0014  010D  0  0 1 0 0
BX  0004
CX  0004
DX  0000

9

JNZ CONT AX  0014  0108  0  0 1 0 0
BX  0004
CX  0003
DX  0000

10

CONT: MOV BX,CX AX  0014  010A  0 0 1 0 0
BX  0004
CX  0003
DX  0000

11

  MUL BX AX  0014  010C  0 0 1 0 0
BX  0003
CX  0003
DX  0000

12

DEC CX AX  003C  010D  0 0 0 0 0
BX  0003
CX  0002
DX  0000

13

JNZ CONT AX  003C  0108  0  0  0 0 0
BX  0003
CX  0002
DX  0000

14

CONT: MOV BX,CX AX  003C  010A  0 0 0 0  0
BX  0002
CX  0002
DX  0000

15

MUL BX AX  0078  010C  0 0 0 0 0
BX  0002
CX  0002
DX  0000

16

DEC CX AX  0078  010D  0 0 0 0 0
BX  0002
CX  0001
DX  0000

17

JNZ CONT AX  0078  0108  0  0  0  0  0
BX  0002
CX  0001
DX  0000

18

CONT: MOV BX,CX AX  0078  010A  0 0  0  0  0
BX  0001
CX  0001
DX  0000

19

MUL BX AX  0078  010C  0  0  0  0  0
BX  0001
CX  0001
DX  0000

20

DEC CX AX  0078  010D  0  1  1  0  0
BX  0001
CX  0000
DX  0000

21

JNZ CONT AX  0078  010F  0 1  1 0 0
BX  0001
CX  0000
DX  0000

22

MOV DX,AX

AX  0078  0111  0  1 1  0  0
BX  0001
CX  0000
DX  0078

26

MOV AX,0X4C00 AX  4C00  0114  0  1  1 0 0
BX  0001
CX  0000
DX  0078

Q.2. Provide the snapshot of debugger showing calculated result stored in DX at the end of our program.                                                                                                                                                                                                                                       (3 marks)

Q.3. Briefly explain the purpose of the given code.


Answer:

This program is calculating the factorial of number 5 which is  1*2*3*4*5 = 120(Decimal) = 78(Hex).

MOV AX, 5

MOV CX, AX

XOR DX, DX

DEC CX

These lines just perform simple initialization. The value 5 is store in registers ax and cx. We clear all bits of dx so that final result saves here. Then we decrement the value of cx because the value of cx will be served as multiplier.

CONT:            MOV BX, CX

MUL BX

DEC CX

JNZ CONT

MOV DX, AX

This is the part where we calculate the factorial. We start by assigning the value of cx to bx. Then wemultiply the value in bx with value in ax and the multiplication result will be saved in ax.We decrement the value in cx and check that whether cx becomes zero or not.If not we repeat same steps.Finally if cx becomes zero this means that factorial is calculated so we assign the final value of factorial available in ax to dx.

DOWNLOAD SOLUTION HERE
loading...
  • ali asgar

    solution please today is last day. 🙁 🙁

  • ali amzat vu lahori parinda

    assignment no.3 solution today is extended day………………… plzzzzzz