Search code examples
assemblynumbersx86-16multiplicationemu8086

Can I use a registered variable as multiplicand in assembly language?


I want to use a variable as a multiplicand in my program, below is one of the variables, which I'm not sure if it's declared correctly

chickenPrice db 19

This is to declare that the price for chicken is $19. Also, I learned about AX:DX that it's 16-bit registers

I want to take the user input as the multiplier, but I'm not sure if mul ax, chickenPrice is the correct way to do this.

And I want to know is there a way to show the answer in 3 digits? I have multiple variables for this and the sum might for the multiplication of the variables might require me to display it in 3 digits.


Solution

  • chickenPrice db 19
    

    The declaration of chickenPrice is fine.

    However with the above declaration, using mul ax, chickenPrice is wrong for a couple of reasons.

    The mul instruction only takes 1 operand. This instruction's use of the accumulator AX (or AL) is implied. We don't need to mention it explicitly.

    And we must be aware that the mul instruction can operate on 2 different sizes. Either we multiply the byte in AL with any byte-sized operand, or we multiply the word in AX with any word-sized operand. The choice is yours, but since you defined chickenPrice a byte, the byte-sized multiplication will be correct.

    mov al, ...       <-- Here the number gotten from user input
    mul chickenPrice
    

    The product of both numbers is now in the AX register

    Displaying numbers with DOS explains in detail how you can display multi-digit numbers. Look for the code snippet in the Method 2 paragraph of the answer.