G
G
gd1xza2022-03-11 17:26:44
assembler
gd1xza, 2022-03-11 17:26:44

What is the correct way to use imul signed multiplication?

The task is to multiply 2 numbers.
When there are positive numbers in the tests, everything works with mul.
And when there was a negative answer does not converge.

How can I fix the code so that unsigned numbers work too?

Test:

int multiply(int, int);

Test(multiply, should_correctly_multiply_3_and_5_to_give_15) {    
  cr_assert_eq(multiply(3, 5), 15);
}


The code:
global multiply
section .text
multiply:  
  mov ax, si
  mov cx, di
  imul cx
  ret

si and di are 4 byte signed numbers to multiply.
There should be an answer in eax.

When tested with a negative, it does not work:
The expression (multiply(-3, 5)) == (-15) is false.


in assembler for the first time!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2022-03-11
@gd1xza

deal with int- most likely this is a 32-bit number, and its sign cannot get into ax in any way. Or use shortor full registers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question