MUL function in assembly

James Clark picture James Clark · Nov 30, 2016 · Viewed 81.5k times · Source

I am trying to execute simple multiplication in Assembly. However, I do not see the registers change when the MUL function is called.

mov bx, 5    
mov cx, 10

mul cx

Answer

Cody Gray picture Cody Gray · Dec 2, 2016

These are called instructions, and they specify operations that are to be performed by the processor. mov is a mnemonic for move, while mul is a mnemonic for multiply. Other common instructions include add, sub, and div. I trust you can figure out what operation these specify!

Most instructions take two parameters. In the technical jargon, these are often known as operands. The first (on the left) is the destination, and the second (on the right) is the source. So, in the case of mov bx, 5, this moves the literal value 5 into the destination register bx. The order of these parameters matters, of course, because you could not move the contents of the register bx into the literal value 5!

The mul instruction is a little bit strange because some of its operands are implicit. That is, they are not explicitly specified as parameters. For the mul instruction, the destination operand is hard-coded as the ax register. The source operand is the one that you pass as a parameter: it can be either a register or a memory location.

Therefore, you could imagine that mul cx means mul ax, cx, but you don't write it that way because the ax destination register is implicit.

Now, a mul instruction commands the processor to multiply the destination operand by the source operand, and store the result in the destination. In code, you can imagine that mul cx would translate into ax = ax * cx. And now you should see the problem: you have not initialized the contents of the ax register, so you are multiplying 10 (which is the value you placed in cx) by whatever garbage was left in ax. As such, the result is meaningless!

If you in fact want to do 5 * 10, then you just have to change one character in your code:

mov  ax, 5     ; ax = 5
mov  cx, 10    ; cx = 10
mul  cx        ; ax = ax * cx   ; actually dx:ax = ax * cx

The result will be stored in ax, which is the implicit destination register.

Well, technically, the result will be stored in dx:ax. This is a register pair, and means that the high portion of the result will be stored in dx, while the low portion of the result will be stored in ax. Why this extra complication? Because multiplying two 16-bit values may result in a value that is larger than 16 bits! Returning the full multiply result in a pair of 16-bit registers allows the mul instruction to return a 32-bit result. When you're just learning, though, you don't need to worry about this. You can just ignore the possibility of overflow, and extract the low portion of the result from ax. (But remember that 16-bit mul overwrites dx whether you want it or not. On 386 and later you can use imul ax, cx to really do ax *= cx without wasting time writing dx.)

And while I'm sure that this is just a toy example, there is really no reason to write code that multiplies two constants together. This can be done at build time, either using a calculator and hard-coding the value, or writing out the multiplication of the constants symbolically and letting your assembler do the computation. That is, mov ax, 50. Or let your assembler do it for you with mov ax, 5 * 10. But like I said, I'm sure you knew this already!

If all else fails, do consult the documentation for the instruction that is giving you trouble. You can almost always find this online by Googling the name of the instruction and "x86". For example, the mul documentation can be found here, as well as several other sites. This information can be kind of complicated, but with a bit of effort, you should be able to extract the information you need. You will also find lots of other great information and links in the tag wiki.

but for some reason i do not see the registers change when the MUL function is marked.

I should also point out that, if you are using a debugger to step through your code, the currently marked/highlighted line is the line that is about to execute. It has not executed yet, so its effects on registers, memory, etc. will not be visible yet. You have to step over the instruction, so that the mark/highlight is on the next line, and then you will see the effects of the previous (just-executed) instruction.

If you understood my explanation above, after a mul instruction, you should see the contents of the ax and dx registers change. You will also see flags and the instruction pointer change, if your debugger shows either of them. Nothing else should change! (Intel's instruction reference manual entry for mul doesn't list any other effects on the architectural state of the machine.)