[fpc-pascal] Interface with NASM

leledumbo leledumbo_cool at yahoo.co.id
Tue Sep 14 08:35:34 CEST 2010


I need to teach assembler, and the easiest way would be to create a driver
and simple I/O library in high level language that can be called from
assembler so that students can focus in programming with assembler more than
dealing with system specific features. It's been quite a while since I go
down to this level, so I might miss something.

Here's the IO library (asmio.pas):

unit asmio;

interface

procedure printc(const c: Char); pascal;
procedure printi(const i: LongInt); pascal;

implementation

procedure printc(const c: Char); pascal; [public, alias: 'printc'];
begin
  Write(c);
end;

procedure printi(const i: LongInt); pascal; [public, alias: 'printi'];
begin
  Write(i);
end;

end.

The driver program (driver.pas):

program driver;

{$L asmio.o}
{$L test.o}

procedure tes; pascal; external name 'tes';

begin
  tes;
end.

And example assembler program which suppose to print 'A' (test.asm):

[bits 32]
extern printc,printi

segment .text
global tes
tes:
  push ebp
  mov  ebp,esp
  push 65
  call printc
  leave
  ret

build command:
nasm -f coff test.asm
fpc asmio.pas
fpc -o test driver.pas

As you can see, I didn't use any optimizations because AFAIR FPC would
change parameter passing mechanism when a certain optimizations are
performed. Of course if it works I won't ask here, but it doesn't. I got
access violation instead. However changing the driver program to:

procedure printc(const c: Char); pascal; external name 'printc';

begin
  printc('A');
end.

generates the same instruction sequence for calling printc:

push 65
call printc

I've tried omitting the stackframe (with the corresponding modifier in
driver program) but no effect. I wonder where my mistake is...

Additional information:
I've made it to work for non IO functions

-- 
View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Interface-with-NASM-tp2838671p2838671.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.



More information about the fpc-pascal mailing list