S
S
samsungovetch2018-10-02 12:38:34
Delphi
samsungovetch, 2018-10-02 12:38:34

Assembler - Is the number in the interval?

Hello
We are doing assembler code inserts into Pascal (Delphi) at the university.
The problem in this problem is to find if a number is in an interval of numbers.

label less;
label greater;
label ennnd;

var k: real;
    n: byte;
    result: byte;
    min: byte;
    max: byte;
    i: Byte;
    digit: byte;

Function Interval(digit, min, max: byte):byte;
  label ennnd;
  label greater;
  label less;
  var res: byte;
  begin
    res:=0;
    asm
        mov al,digit;  //приравниваю al=digit
        cmp al,min;    //сравниваю с минимальной границей
        ja greater;    //если больше, то переходим
        jna ennnd;     //если нет, то на выход


      greater:
        cmp al,max;    //сравниваю с максимальной границей
        jb less;       //если меньше, то переходим
        jnb ennnd;     //если нет, то на выход

      less:
        mov res,1;     //если меньше, то попадает в интервал, то рес = 1

      ennnd:


    end;
  end;

begin
  writeln('Number of digit?: ');
  readln(n);

  writeln('Min digit?: ');
  readln(min);
  writeln('Max digit: ');
  readln(max);

  for i:= 0 to n-1 do
  begin
    writeln('Digit? ');
    readln(digit);
    if (Interval(digit,min,max) > 0) then
      writeln('In Interval')
    else
      writeln('Not In Interval');
  end;

  readln(k);

end.

I did this, but when checking, any number is in the interval, instead of moving to the ennnd label, the greater and less labels are still executed, respectively. Where did I go wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
zed, 2018-10-02
@samsungovetch

In Delphi (for Win32), the register calling convention is used by default : up to 3 parameters are passed through the EAX, ECX, EDX registers, the result is passed through EAX (for more details, see: Using Assembler in Delphi ).
Accordingly, we get the following code:

function IntervalAsm(const AValue, AMin, AMax: Byte): Boolean; register;
asm
  // Входные параметры уже лежат в регистрах:
  // al = AValue
  // dl = AMin
  // cl = AMax

  cmp dl, al     // сравнение с AMin
  ja @@RetFalse  // если al < dl, то выход с False
  cmp cl, al     // сравнение с AMax
  jae @@RetTrue  // если al <= cl, то выход с True

@@RetFalse:
  xor eax, eax   // eax = 0 -> Result = False
  ret

@@RetTrue:
  mov al, 1      // eax = 1 -> Result = True
  ret
end;

If you're having trouble writing code in assembler, then your best bet is to write the code in Delphi (or even the simplest C), compile it, and then decompile it and see what kind of code Delphi itself generates. For educational projects in Delphi, the Interactive Delphi Reconstructor (IDR) is perfect .

S
samsungovetch, 2018-10-02
@samsungovetch

Understood. Here is the resulting code:

uses
  SysUtils;

label less;
label greater;
label ennnd;

var k: real;
    n: byte;
    min: byte;
    max: byte;
    i: byte;
    digit: byte;

Function Interval(digit, min, max: byte):byte;
  label ennnd;
  label greater;
  label less;
  var res: byte;
  begin
    asm
        mov res,0;
        mov al,digit;  //приравниваю al=digit
        cmp al,min;    //сравниваю с минимальной границей
        ja greater;    //если больше, то переходим
        jna ennnd;     //если нет, то на выход


      greater:
        cmp al,max;    //сравниваю с максимальной границей
        ja ennnd;     //если нет, то на выход
        mov res,1;     //если меньше, то попадает в интервал, то рес = 1


      ennnd:


    end;
    Interval:=res;
  end;

begin
  writeln('Number of digit?: ');
  readln(n);

  writeln('Min digit?: ');
  readln(min);
  writeln('Max digit: ');
  readln(max);

  for i:= 0 to n-1 do
  begin
    writeln('Digit? ');
    readln(digit);
    writeln(Interval(digit,min,max));
    if (Interval(digit,min,max) > 0) then
      writeln('In Interval')
    else
      writeln('Not In Interval');
  end;

  readln(k);

end.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question