Example #1
0
def divide_int():
  '''Divides one integer by another and returns the address of a new integer
  equal to the result.

  2 Params:
    1. The address of an integer (left operand - dividend)
    2. The address of another integer (right operand - divisor)'''
  N_PARAMS = 2

  div_not_zero = CodeGenManager.get_label('div_not_zero')

  return [
    '_divide_int:',
    common.function_prologue(),
    '; get the value for the left operand and put it in eax',
    common.get_param('eax', 0, N_PARAMS),
    common.unwrap_primitive('eax', 'eax'),
    '; get the value for the right operand and put in in ebx',
    common.get_param('ebx', 1, N_PARAMS),
    common.unwrap_primitive('ebx', 'ebx'),
    '; check for division by zero:',
    'cmp ebx, 0',
    'jne {0}'.format(div_not_zero),
    'call __exception',
    '{0}:'.format(div_not_zero),
    common.fill_high_order_bit('eax', 'edx'),
    'idiv ebx  ; sets eax to edx:eax/ebx',
    '; create an int with the result',
    'push eax  ; the result of div has to be in eax',
    'call _create_int',
    'pop ebx ; pop param',
    '; eax is an integer object with the old value of eax',
    common.function_epilogue()
  ]
Example #2
0
def mod_int():
  '''Computes the modulus of two integers and returns the address of the result

  2 Params:
    1. The address of an integer (dividend)
    2. The address of an integer (dividend)
  '''
  N_PARAMS = 2

  return [
    '_mod_int:',
    common.function_prologue(),
    '; get the value for the left operand and put it in eax',
    common.get_param('eax', 0, N_PARAMS),
    common.unwrap_primitive('eax', 'eax'),
    '; get the value for the right operand and put in in ebx',
    common.get_param('ebx', 1, N_PARAMS),
    common.unwrap_primitive('ebx', 'ebx'),
    common.fill_high_order_bit('eax', 'edx'),
    'idiv ebx  ; sets edx to edx:eax mod ebx',
    '; create an int with the result',
    'push edx ; the result of mod is placed in ebx',
    'call _create_int',
    'pop ebx ; pop param',
    '; eax is an integer object with the old value of edx',
    common.function_epilogue()
  ]