Author Topic: [dos 16-bit] assembly from c using openwatcom  (Read 8079 times)

Offline mohammadk

  • Jr. Member
  • *
  • Posts: 4
[dos 16-bit] assembly from c using openwatcom
« on: July 27, 2019, 04:00:08 PM »
Hi,

I'm having trouble compiling a c program which calls a simple assembly procedure.
This is intended as a medium 16-bit exe.

asm code: ADDINT.ASM
Code: [Select]
%define xparam [bp+6]            ; using medium memory model
%define yparam [bp+8]           

segment _TEXT public class=CODE   
global add_int
add_int:
push bp
mov bp,sp
sub sp, 0x40 ; 64 bytes of local stack space
mov ax, xparam
add ax, yparam
mov sp, bp ; undo "sub sp, 0x40"
pop bp
retf                            ; medium model ret


C code: EXT.C
Code: [Select]
#include <stdio.h>

extern int add_int(int x, int y);

void main(void)
{
int third = add_int(9, 1);
printf("third = %d\n", third);
}


compilation:
Code: [Select]
nasm -f obj ADDINT.ASM
wcl -mm ADDINT.OBJ EXT.C

Errors:
Code: [Select]
Open Watcom C/C++ x86 16-bit Compile and Link Utility
Version 2.0 beta Jul 16 2019 17:31:29 (16-bit)
Copyright (c) 2002-2019 The Open Watcom Contributors. All Rights Reserved.
Portions Copyright (c) 1988-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
wcc EXT.C  -mm
Open Watcom C x86 16-bit Optimizing Compiler
Version 2.0 beta Jul 16 2019 17:23:09 (32-bit)
Copyright (c) 2002-2019 The Open Watcom Contributors. All Rights Reserved.
Portions Copyright (c) 1984-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
EXT.C: 9 lines, included 799, 0 warnings, 0 errors
Code size: 35
wlink @__wcl__.lnk
Open Watcom Linker Version 2.0 beta Jul 16 2019 17:20:15 (32-bit)
Copyright (c) 2002-2019 The Open Watcom Contributors. All Rights Reserved.
Portions Copyright (c) 1985-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
loading object files
searching libraries
Error! E2028: add_int_ is an undefined reference
creating a DOS executable
file EXT.obj(C:\DOS\DEV\TRICKS\EXT\EXT.C): undefined symbol add_int_
Error: Linker returned a bad status


I'm running on dosbox
« Last Edit: July 27, 2019, 04:08:35 PM by mohammadk »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: [dos 16-bit] assembly from c using openwatcom
« Reply #1 on: July 27, 2019, 04:45:37 PM »
Try "add_int_" with the underscore at the end (in your .asm file). I think that's the way Watcom likes it.

Best,
Frank


Offline mohammadk

  • Jr. Member
  • *
  • Posts: 4
Re: [dos 16-bit] assembly from c using openwatcom
« Reply #2 on: July 27, 2019, 05:22:52 PM »
 :-[  That solved it
many thanks  8)

Offline mohammadk

  • Jr. Member
  • *
  • Posts: 4
Re: [dos 16-bit] assembly from c using openwatcom
« Reply #3 on: July 27, 2019, 09:18:08 PM »
For anyone wondering,
I've been confusing myself between nasm and ow documentations.
ow uses it's own calling conventions by default, which explains the needed trailing "_ " after the procedure name like this "add_int_".
but when used with -ecc option it can revert to __cdecl which will require an "_" prefix like this "_add_int"
This also affects parameter passing which by default uses the registers (dx, ax then bx then cx then the stack) instead of the stack all the way.

so finally the modified ADDINT.ASM looks like this:
Code: [Select]

; nasm -f obj ADDINT.ASM
; wcl -mm ADDINT.OBJ EXT.C (for ow conventions - parameters pssed on registers, use add_int_)
; wcl -mm -ecc ADDINT.OBJ EXT.C (for __cdecl conventions - parameters passed on the stack, use _add_int)
segment _TEXT public class=CODE   
global add_int_
add_int_:
push bp
mov bp,sp
sub sp, 0x40 ; 64 bytes of local stack space
add ax, dx ; first parameter is passed into dx, 2nd into ax
mov sp, bp ; undo "sub sp, 0x40"
pop bp
retf                ; medium model ret


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: [dos 16-bit] assembly from c using openwatcom
« Reply #4 on: July 27, 2019, 11:02:17 PM »
Thanks for the update Mohammadk. I don't know how we're supposed to know this stuff. Sometimes C wants a leading underscore, sometimes it wants a trailing underscore, sometimes it wants no underscore at all. Nasm has "--prefix" and "--postfix" which will help with it. Glad you figured it out!

Best.
Frank


Offline mohammadk

  • Jr. Member
  • *
  • Posts: 4
Re: [dos 16-bit] assembly from c using openwatcom
« Reply #5 on: July 28, 2019, 07:56:09 AM »
Agreed, and thanks for the tip, I tried it "--postfix _" and it worked :)
combined with macros I can now write asm code compatible with multiple compilers.
Moh.K.