Selection

The loop in the previous pair of programs contains the equivalent of an if statement. If the comparison CMP ECX, 0 does not result in the zero flag being reset then there is a jump to start the loop again, otherwise the next statement is executed. Program ASM_Selection gives an assembler code equivalent of the Pascal if statement:

if CurrentNum > Max then 
   Max := CurrentNum; 

program ASM_Selection;
  {$APPTYPE CONSOLE}
  {$ASMMODE INTEL}//Delete this line in Delphi
uses
  SysUtils;
var
  CurrentNum, Max : integer;
begin
  Max := 2;
  Randomize;
  CurrentNum := random(5);
  asm
    MOV EAX, CurrentNum
    CMP EAX, MAX
    JL @Skip
      MOV Max, EAX;
    @Skip:
  end;
  writeln('CurrentNum: ', CurrentNum);
  writeln('Max: ', Max);
readln;
end.


program ASM_SelectionA;
  {$mode objfpc}{$H+}
  {$ASMMODE ATT}
uses
  SysUtils;
var
  CurrentNum, Max : integer;
begin
  Max := 2;
  Randomize;
  CurrentNum := random(5);
  asm
    MOV CurrentNum, %EAX
    CMP MAX, %EAX
    JL .LSkip
      MOV %EAX, Max
    .LSkip:
  end;
  writeln('CurrentNum: ', CurrentNum);
  writeln('Max: ', Max);
readln;
end.
 
Programming - a skill for life!

Getting started with in-line assembler