Author Topic: nasm and borland c++  (Read 8524 times)

nobody

  • Guest
nasm and borland c++
« on: October 29, 2008, 02:04:59 PM »
i want to know how i can run nasm asm file using borland c++ on windows o/s.  

a hello world program as shown below may be used to illustrate the process

/**********Begin Code Sample********/

; helloworld.asm
;
; This is a Win32 console program that writes "Hello, World" on one line and
; then exits.  It needs to be linked with a C library.
; ----------------------------------------------------------------------------

global   _main
   extern   _printf

section .text
_main:
   push   message
   call   _printf
   add   esp, 4
   ret
message:
   db   'Hello, World Coding', 10, 0

/********End of Code Sample***/

mcamember

  • Guest
Re: nasm and borland c++
« Reply #1 on: October 29, 2008, 10:15:21 PM »
Hi,
Inserting the %INCLUDE directive is needed.

Of course the path to MSVCRT.INC on your machine might be different.

global _main

%include "f:\nasm32\inc\win32\msvcrt.inc"


extern _printf

section .text
_main:
push message
call _printf
add esp, 4
ret
message:
db 'Hello, World Coding', 10, 0
;---------------
Then :
nasm -f win32  helloworld.asm -o helloworld.obj


polink /subsystem:console /entry:main helloworld.obj  msvcrt.lib

Note that polink must be able to find MSVCRT.LIB so either put it in the same dir as the file or enter a /LIBPATH: pointer to the dir with the NASM .LIB files such as this :

polink /subsystem:console /entry:main  /libpath:f:\nasm32\bin\lib helloworld.obj msvcrt.lib

GOLINK is a more readily available linker (and the one which I use most) but I'm having trouble getting it to link this file so this is the best I can do for now.

hth
;mcamember

nobody

  • Guest
Re: nasm and borland c++
« Reply #2 on: November 01, 2008, 11:41:50 PM »
Okay... but if the "Original Nobody" explicitly wanted to use Borland tools... I believe they use OMF rather than msCOFF, so you'd want "nasm -f obj ...". Nasm's "-f obj" defaults to 16 bit code, so we'd need to ask for 32 bit code (assuming this is 32-bit bcc???)...

section .text use32

"-f obj" doesn't know the special name ".text". There *may* be advantages to using the same name the Borland tools use, which I think is "CODE" or "_CODE"... or "_TEXT"(???). Probably use "tdump" or some such to see what names Borland's using(?). It may also help to add "class=CODE" to the section declaration. I'm not sure matching names (and/or case) is necessary... can't hurt. Whatever works.

Another possible "gotcha" is that C++ will "decorate" (mutilate) function names in external modules. If you've got, in your C(++) file, "myasmfunc(foo, bar);", prototype it as:

extern "C" myasmfunc (int foo, int bar);

(or the correct syntax) to keep it from happening. You'll still need the underscore. Won't be an issue with this example...

We don't touch ebx, esi, edi, ebp in this example, but if we'd altered their values, we'd need to restore the original values before returning. C functions will preserve those registers for us - but *will* trash ecx and edx, return value is in eax - and it expects the same...

Assuming you've got Borland C++ installed, you should be able to just:

nasm -f obj helloworld.asm
bcc helloworld.obj

(with "use32" added!), "or whatever you usually type". If you *haven't* got Borland tools installed - and know how to use 'em - Polink or Golink or gcc/ld from Mingw, or whatever M$ is giving away would be a better bet. That Borland stuff is getting pretty "obsolete" by now... not that there's anything wrong with "obsolete"...

Best,
Frank