0

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.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76

1 Answers1

2
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.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • Wouldn't this multiply AL by the memory location where chickenPrice is stored? I thought you would have to do ```mul byte ptr [ds:chickenPrice]``` – puppydrum64 Nov 02 '21 at 17:31
  • @puppydrum64 The OP uses an emulator **emu8086** that follows MASM-style. In MASM, mentioning the name of the variable is tantamount to using the value stored at that address. To obtain the address itself, we would have to write `offset chickenPrice`. Also in MASM, there's no need to mention `byte ptr` since MASM remembers the size the variable was given from the `db` directive. And for MASM the square brackets are more of syntactical sugar than anything else. Plus there's no point including the redundant `ds:` segment overrride prefix. The data segment is already the default for the CPU. – Sep Roland Nov 02 '21 at 22:29
  • @SepRoland I've been using UASM and I'll admit I've picked up a few bad habits. – puppydrum64 Nov 03 '21 at 00:35