NASM - The Netwide Assembler
NASM Forum => Using NASM => Topic started by: nobody on September 02, 2009, 07:12:08 AM
-
Hello
I have reading nasm-doc looking for information about mixing pascal and nasm assembler. Well what i found did not satisfied me. And that's the question how can i write an nasm procedure to be externally called by a pascal routine?. Let's say:
; Nasm-routine (PasAsN1.asm)
[section .text]
global WriteChar
WriteChar:
MOV AH, 02h
MOV DL, BYTE [BP+6]
INT 21h
retf 2
; nasmw -f obj PasAsN1.asm -o PasAsN1.obj
Program MixPascalNasm; (* PasAsm.pas *)
uses crt;
PROCEDURE WriteChar(Car: Char); External;
{$L PasAsN1.obj}
begin
WriteChar('A');
ReadKey;
end.
Thank you very much
-
Well... I have no experience at all with Pascal, but I think I understand how it would work... I think you're quite close (assuming Pascal doesn't complain about this), but you've missed setting up your "stack frame":
WriteChar:
push bp
mov bp, sp
; or "enter 0, 0"
MOV AH, 02h
MOV DL, BYTE [BP+6]
INT 21h
mov sp, bp
pop bp
; or "leave"
retf 2
See if that helps...
Best,
Frank
-
Thank you for your replay
I don't believe using SP it will work. BP-SP are used to define internal variables if they are needed. In this case i don't need them.
The problem is that with these codes, nasm gives an obj file, but turbo pascal says "Invalid PUBLIC definition (PASASN1.OBJ)."
-
You expect "mov dl, [bp + 6]" to work by magic? Well, once you get an executable, you'll be able to find out easily. I don't know what to do about the error message. "PUBLIC" is what Nasm calls "global", so apparently it's "global WriteChar" that it's complaining about - but that looks okay to me. The only thing I can think of to try is to replace "[section .text]" with "section code class=code". Nasm's "-f obj" (OMF) output format doesn't know any "special names" like ".text" (most other output formats know what to do with ".text"), so specifying "class=code" might help. Since it's "far", the name shouldn't matter (?). Hmmm, I guess that's not right. Looking at the Friendly Nasm Manual:
# Procedures and functions must be in a segment whose name is either CODE, CSEG, or something ending in _TEXT.
So maybe make it "section CODE class=code"... maybe if you use one of those names, you wouldn't need "class=code"... Fool around in that area and see if it changes your results any.
Best,
Frank