D
D
Dorothy2015-06-14 00:22:44
Delphi
Dorothy, 2015-06-14 00:22:44

How to change the function code in assembler?

Hello. There is this function:

function sar32(value, shift: longint): longint;
asm
  mov ecx, edx
  sar eax, cl
  end;

Can you please tell me how to change this function so that it works with such parameters?
function sar32(value: Int64; shift: longint): longint;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
zed, 2015-06-14
@Dorothy

In this case, the variable value is passed through the stack and needs to be put into registers, and shift flies into the eax register. So the code will be like this:

function sar64(value: Int64; shift: LongInt): LongInt;
asm
  mov ecx, eax
  mov eax, dword ptr [value]
  mov edx, dword ptr [value+4]
  shrd eax, edx, cl
end;

Have you messed up with the return type? If the input parameter is Int64, then in my opinion the same type should be used at the output.

J
jcmvbkbc, 2015-06-14
@jcmvbkbc

how to change this function so that it converts the result to Integer and not LongInt

If an integer has the same or smaller width than a longint (which is the case for the fpc compiler, for example), then nothing needs to be changed.
For such a prototype, you need to change something like this (for borland register calling convention):
function sar32(value: Int64; shift: longint): longint;
asm
  shrd eax, edx, cl
  end;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question