Question

I need to perfom the following operations in both signed and unsigned convention: x*x-y+2, where x is a byte and y a double word. Here is what I've tried so far using the unsigned convention, but it appears I wrote something wrong because it throws an error. What is the mistake?
mov al, x
mov ah, 0
mul x
mov dx, 0
sub dword ptr ax, y
add ax, 2

Was it helpful?

Solution

1) For the unsigned option:

First you need to load x into a register to square it:

 mov eax,0
 mov al, x
 mul al

this essentially did this: AX = x*x, and AX is word size, since the upper part of EAX is all zeros, then essentially EAX = x*x, and EAX is already double word size. Now we need to subtract y and add 2:

 mov ebx, y
 sub eax, ebx
 add eax, 2

and there you have it! the answer is inside EAX.

2) For the signed option:

We will basically do the same, however this time we will make use of the CWDE instruction which will extend the value inside AX into EAX, while keeping the sign information correct, so:

 mov al, x
 imul ax
 cwde 

 mov ebx, y
 sub eax, ebx
 add eax, 2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top