Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MUL function in assembly

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
like image 418
James Clark Avatar asked Nov 30 '16 16:11

James Clark


People also ask

What does MUL do in assembly?

The mul instruction multiplies the contents of general-purpose register (GPR) RA and GPR RB, and stores bits 0-31 of the result in the target GPR RT and bits 32-63 of the result in the MQ Register. The mul instruction has four syntax forms.

What does MUL do in 8086?

MUL is used to multiply two 16-bit numbers. HLT is used to stop the program. AX is an accumulator which is used to store the result. BX, DX are general purpose registers where BX is used for multiplication and DX is used for result.

What does MUL mean in C?

The MUL (unsigned multiply) instruction multiplies an 8-, 16-, or 32-bit operand by either AL, AX, or EAX. • The instruction formats are: MUL r/m8.


2 Answers

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 x86 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.)

like image 109
Cody Gray Avatar answered Oct 21 '22 09:10

Cody Gray


The mul instruction has 2 operands: one is specified and the other one is implicit.

When you write mul cx it means something like: ax = ax * cx.

Actually it means dx:ax = ax * cx - the high half of the full 32-bit product is always written to dx. dx will be zero for small products where the result "fits" in ax. If you only want the low 16 bits of the result, you can just think of it as destroying dx1.

The answer of your question, if you really want to multiply 5 with 10, you can do something like this:

  mov  ax, 5   ; ax = 5   mov  cx, 10  ; cx = 10    mul  cx      ; dx:ax = ax * cx ; So   ax = ax * cx ; and dx=0 because the product happens to be small. 

Footnote 1: 186 allows imul dst, src, constant like imul ax, cx, 5, allowing any registers with no implicit use of AX or DX.
386 allows imul reg,reg non-widening multiply with no implicit registers.
https://www.felixcloutier.com/x86/imul covers both forms.

Of course if you can use 386 features, you'd use lea instead of imul to multiply by 3, 5, or 9, using 32-bit addressing modes that allow a scaled index (i.e. built-in shift count).

  mov  ecx, 10   lea  ax, [ecx + ecx*4] 
like image 22
Mostafa Wael Avatar answered Oct 21 '22 07:10

Mostafa Wael