Addition and Subtraction

Each assembler statement has a mnemonic for the operation to be carried out followed by the operand(s) on which it operates. Using Intel syntax, the first operand is the destination and the second operand is the source.

The statement ADD Int, 5 adds 5 to Int1 and stores the result in Int1.

The statement SUB Int2, 30 subtracts 30 from Int2 and stores the result in Int2.

program ASM_AddSub;
  {$APPTYPE CONSOLE}
  {$ASMMODE INTEL}//Delete this line in Delphi
uses
  SysUtils;
var
  Int1 : integer = 10;
  Int2 : integer = 50;
begin
  asm
    ADD Int1, 5
    SUB Int2, 30 
  end;
  writeln('10 + 5 = ', Int1);
  writeln('50 - 30 = ', Int2);
  readln;
end.   

Using AT&T syntax, the first operand is the source and the second operand is the destination.

The statement ADD $5, Int1 adds 5 to Int1 and stores the result in Int1.

The statement SUB Int2, $30 subtracts 30 from Int2 and stores the result in Int2.

The $ sign indicates that the operand is a value rather than an address.

program ASM_AddSubA;
  {$mode objfpc}{$H+}
  {$ASMMODE ATT}
uses
  SysUtils;
var
  Int1 : integer = 10;
  Int2 : integer = 50;
begin
  asm
    ADD $5, Int1
    SUB $30, Int2
  end;
  writeln('10 + 5 = ', Int1);
  writeln('50 - 30 = ', Int2);
  readln;
end. 

Programming - a skill for life!

Getting started with in-line assembler