S
S
SomeManEa2020-12-16 15:06:02
assembler
SomeManEa, 2020-12-16 15:06:02

Why can't multiply and divide assembler?

I kind of began to understand that if I assign the value to al, then I assign the value to cl
and write mul cl, then I can assign ax the multiplication of al and cl (ax=al*cl)
But for some reason I get errors: operand size conflict, illegal number of operands when I assign to al the number entered from the keyboard and then multiply it by 5

I need to solve the equation using assembler: X = 5A + 2B * C - B / 4 + 131

Here is my code:

#include <iostream>

using namespace std;

int main()
{
    short a, b, c, x;
    cout << "a = "; cin >> a;
    cout << "b = "; cin >> b;
    cout << "c = "; cin >> c;

    _asm {
        mov al,a
        mul al,5
        mov bx,ax
        mov cl,b
        shl cl,1
        mov al,c
        mul cl
        mov cx,b
        idiv cx,4
        add bx,ax
        sub bx,cx
        add bx,131
        mov x,bx
       }
    cout <<"x="<< x<<endl;
}


And I don't know why it doesn't work, what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Kuts, 2020-12-16
@fox_12

short: represents an integer in the range -32768 to 32767. Occupies 2 bytes (16 bits) of memory

short a, b, c, x;
...
    _asm {
        mov al, a

In al - one byte is placed

N
none7, 2020-12-16
@none7

I think it's the C compiler. sizeof(al) != sizeof(short) hence the error. The second error is that idiv takes only one argument and divides dx:ax by the specified argument.

J
jcmvbkbc, 2020-12-16
@jcmvbkbc

mul al,5

There is no such opcode. mul generally cannot multiply by an immediate value. imul which can, multiplies at least the 16-bit value in ax.
Instead of mul, you can multiply by 5 using lea (starting with i386) or by shifting by 2 and adding.
idiv cx,4

idiv can't do that. How much easier would it be to dosar cx, 2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question