Take the 2-minute tour ×

I've been tasked with converting IA32 code to Y86. The original program was written in C and is intended to take an array of integers in which the even positioned values call one of three functions and the odd positioned values are operated on within that function. The functions include the negation of a number, the square of a number, and the sum from 1 to the supplied number.

Most of the instructions are easily converted from IA32 to Y86, but there are a number of instructions that are giving me a really hard time.

0000001e <negation>:
  1e:   55                      push   %ebp
  1f:   89 e5                   mov    %esp,%ebp
  21:   8b 45 08                mov    0x8(%ebp),%eax
  24:   f7 d8                   neg    %eax
  26:   5d                      pop    %ebp
  27:   c3                      ret    

The neg instruction is not a valid instruction in Y86. This is what I have in Y86:

# int Negation(int x)
    pushl %ebp
    pushl %esi
    rrmovl %esp,%ebp
    mrmovl 0x8(%ebp),%eax
    irmovl %esi,$0
    subl %eax, %esi
    rrmovl %esi, %eax
    popl %esi
    popl %ebp

Is this the correct way to go about this problem?

Another instruction is the imul instruction in my square function:

00000028 <square>:
  28:   55                      push   %ebp
  29:   89 e5                   mov    %esp,%ebp
  2b:   8b 45 08                mov    0x8(%ebp),%eax
  2e:   0f af c0                imul   %eax,%eax
  31:   5d                      pop    %ebp
  32:   c3                      ret 

Does anyone know how the "imul" instruction can be converted in this situation?

Thanks for the help! Any tips on IA32/Y86 Conversion would be greatly appreciated too.

share|improve this question
Just to be clear, the square function code is given in IA32 –  Matt Koz Dec 1 '12 at 0:05
I don't know the details of Y86, but it looks like at least mrmovl 0x8(%ebp),%eax should be mrmovl 0xc(%ebp),%eax since you're pushing an extra register before setting up ebp. –  Michael Burr Dec 1 '12 at 0:29

2 Answers 2

For implementing imul, you might want to look at using a shift and add routine to implement a mul routine:

Then for imul just use the following steps:

  • figure out what sign the result should have
  • convert the operands to absolute values (using your negation routine)
  • call your mul routine on the positive values
  • convert the result to negative if necessary
share|improve this answer
I'm familiar with the bitwise operations shift left and shift right, but how would those be implemented in Y86? And the expected values are positive integers greater than 0 so I do not have to deal with negative values. –  Matt Koz Dec 1 '12 at 1:06
Oh - I missed that shift instructions aren't part of Y86. You can implement a single left shift by just adding a register to itself. I'm honestly not sure what might be a decent way to implement a right shift with the Y86 instruction set. A loop doing 31 bit tests (using and) and or-ing the appropriate bit in the result? –  Michael Burr Dec 1 '12 at 1:18
Fortunately, the shift/add algorithm for multiplication doesn't need a right shift. –  Michael Burr Dec 1 '12 at 1:50

1) is mrmovl 0x4(%esp),%eax allowed?

  ixorl %eax, 0xffffffff  
  iaddl %eax, 1  

should be slightly more efficient (also ebp can be used as GPR -- no need to push esi)

2) for multiplication there are indeed shift and add-options,
but also a LUT based approach, exploiting the fact that 4*a*b = (a+b)^2 - (a-b)^2. for each 8x8 bit or NxN bit multiplication.

For a=h<<8+l, B=H<<8|L, aB = Ll + (hL+Hl)<<8 + hH<<16;
could be handled using 3 different tables:
s1[n] = n^2 >>2; s2[n]=n^2 << 6; s3[n]=n^2 << 14;

share|improve this answer
Of course nice thing that the original problem was squaring, not multiplication! The decomposing algorithm is still relevant... –  Aki Suihkonen Dec 1 '12 at 15:48

Your Answer


