This is a somewhat challenging routine I've been trying to develop for z80 assembly. I'm trying to find a way to multiply two signed 16-bit operands together and get a signed 32-bit result.
I already have one working method, although it's fairly hacky. Its general structure is as follows:
- Xor the high bytes of the two operands and push af
- Convert the operands into their absolute values
- Multiply these two values with the normal restoring multiplication algorithm
- Pop af and return if sign flag is unset
- Negate the result of the multiplication and return
Somehow, I get the feeling there are faster and/or more elegant solutions than this.
The most elegant solution would just be a modified restoring multiplication algorithm that deals with signed numbers correctly. After having stared down the restoring multiplication algorithm for about the past hour, I don't know how reasonable this is, though. And unless there's some simple change I'm not seeing that would suddenly make this work, any solution of this form would probably consume a lot of extra cycles in each iteration.
Something that has occurred to me as a more achievable and probably faster solution, albeit larger, consists of a test that branches into one of three different versions of the restoring multiplication algorithm. Which algorithm to use would be determined based on whether neither, one, or both of the operands are negative. This is the method I am currently trying to develop. After a bit of messing around with the restoring multiplication algorithm, though, I'm still fairly confused about how to form the two modified algorithms that would be needed.
Any ideas?