All Exams Test series for 1 year @ ₹349 only
Question

Consider the following program fragment in assembly language:

mov ax, 0h

mov cx, 0A h

doloop:

dec ax

loop doloop

What is the value of ax and cx registers after the completion of the doloop?

The correct answer is

ax = FFF6 h and cx = 0 h

Assembly Language Program Analysis: Finding Register Values

This problem asks us to determine the final values of the ax and cx registers after executing a given assembly language program fragment. Let's break down the program line by line and trace the execution.

Understanding the Assembly Code Fragment

The program fragment is as follows:

mov ax, 0h
mov cx, 0A h
doloop:
dec ax
loop doloop

Here's what each instruction does:

  • mov ax, 0h: This instruction moves the hexadecimal value 0h into the ax register. So, initially, ax is set to 0.
  • mov cx, 0A h: This instruction moves the hexadecimal value 0A h into the cx register. 0A h is equivalent to decimal 10. The cx register is often used as a counter for loops.
  • doloop:: This is a label that marks the beginning of the loop.
  • dec ax: This instruction decrements the value in the ax register by 1.
  • loop doloop: This instruction performs two actions:
    1. It decrements the cx register by 1.
    2. It checks if the value in cx is non-zero. If cx is not zero, it jumps back to the label specified (doloop). If cx is zero, execution continues with the next instruction after the loop instruction.

Tracing the Program Execution

Let's trace the values of ax and cx through each iteration of the loop.

Initial state:

ax = 0h

cx = 0A h (which is 10 in decimal)

The loop doloop instruction will cause the code between the doloop label and the loop instruction (which is just dec ax) to execute cx times. Since cx starts at 10, the loop body will execute 10 times.

Let's see how the register values change during the loop iterations:

Iteration dec ax Action ax Value Before dec ax ax Value After dec ax loop doloop Action cx Value Before loop cx Value After loop Jump? (cx != 0)
1 Decrement ax by 1 0000h FFFFh ($0-1$ in 16-bit is FFFFh) Decrement cx, check if zero 0Ah (10) 09h (9) Yes
2 Decrement ax by 1 FFFFh FFFEh Decrement cx, check if zero 09h (9) 08h (8) Yes
3 Decrement ax by 1 FFFEh FFFDh Decrement cx, check if zero 08h (8) 07h (7) Yes
4 Decrement ax by 1 FFFDh FFC h Decrement cx, check if zero 07h (7) 06h (6) Yes
5 Decrement ax by 1 FFC h FFFBh Decrement cx, check if zero 06h (6) 05h (5) Yes
6 Decrement ax by 1 FFFBh FFFAh Decrement cx, check if zero 05h (5) 04h (4) Yes
7 Decrement ax by 1 FFFAh FFF9h Decrement cx, check if zero 04h (4) 03h (3) Yes
8 Decrement ax by 1 FFF9h FFF8h Decrement cx, check if zero 03h (3) 02h (2) Yes
9 Decrement ax by 1 FFF8h FFF7h Decrement cx, check if zero 02h (2) 01h (1) Yes
10 Decrement ax by 1 FFF7h FFF6h Decrement cx, check if zero 01h (1) 00h (0) No (loop terminates)

Alternatively, since ax starts at 0 and is decremented 10 times, the final value can be calculated as $0 - 10$. In 16-bit two's complement representation, $0 - 10$ is equal to FFF6h.

The loop terminates when cx becomes 0. The loop instruction decrements cx from 1 down to 0, and then the loop finishes.

Final Register Values

After the loop completes, the value of ax is FFF6h, and the value of cx is 0h.

Conclusion

By tracing the execution of the assembly code fragment, we found that the ax register is decremented 10 times, starting from 0, resulting in FFF6h. The cx register is used as a loop counter and is decremented from 0A h (10) down to 0, at which point the loop terminates.

Revision Table: Assembly Language Basics

Concept Description
Registers (AX, CX) Small storage locations within the CPU used to hold data or addresses temporarily. AX is a general-purpose register often used for arithmetic operations. CX is often used as a counter for loops.
mov Instruction Moves data from a source operand to a destination operand. Example: mov destination, source.
dec Instruction Decrements the value of the operand by 1. Example: dec register.
loop Instruction Decrements the CX register by 1 and jumps to a specified label if CX is not zero.
Hexadecimal (h) A base-16 number system often used in assembly language. 0A h is 10 in decimal.
Two's Complement A method used to represent signed numbers in binary. Decrementing 0 can result in the maximum value for the data size when using two's complement (e.g., 0000h - 1 = FFFFh for 16-bit). $0 - 10$ in 16-bit two's complement is FFF6h.

Additional Information: Assembly Loop Concepts

  • The loop instruction is a composite instruction that combines decrementing cx and a conditional jump.
  • The loop instruction is efficient for simple counted loops where the count is in cx.
  • Other conditional loop instructions exist, such as loope/loopz (loop while equal/zero flag is set) and loopne/loopnz (loop while not equal/not zero flag is set), which also decrement cx but additionally check a flag condition.
  • Register sizes: In 16-bit assembly (like for the 8086 processor), AX and CX are 16-bit registers. They can hold values from 0 to FFFFh (65535 decimal) for unsigned numbers, or -32768 to +32767 for signed numbers using two's complement.
Was this answer helpful?

Important Questions from Machine Instructions and Addressing Modes

  1. Arrange the following types of machine in descending order of complexity.

    (A) SISD

    (B) MIMD

    (C) SIMD

    Choose the correct answer from the options given below:

  2. Consider the following assembly program fragment:

    stc

    mov al, 11010110b

    mov cl, 2

    rcl al, 3

    rol al, 4

    shr al, cl

    mul cl

    The contents of the destination register ax (in hexadecimal) and the status of Carry Flag (CF) after the execution of above instructions, are:

  3. A micro-instruction format has micro-ops field which is divided into three subfields F1, F2, F3 each having seven distinct micro-operations, condition field CD for four status bits, branch field BR having four options used in conjunction with address field ADF. The address space is of 128 memory locations. The size of micro-instruction is :  

  4. This instruction is of what type?

    ADD R1, A, B

  5. The following language uses mnemonic OP codes

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App