News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Mixed programming in masm32

Started by pps, March 02, 2011, 09:13:29 AM

Previous topic - Next topic

pps

I am trying to make a mixt program in masm32, with c++ and assembly language.  How should i do? Is any tutorial or something to read about it? I was able to do that in turbo assembler but in masm32 i don't know how. Please help me!

I corrected the spelling mistake in the title.

hutch--

The basics are that ML.EXE and CL.EXE have compatible object module formats so you can link one with the other AS LONG AS you know how to prototype the called functions in either direction. You will have no joy with straight C++ although it is possible but if you use the EXTERN syntax and C it works fine.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

clive

The word you're looking for is MIXED. Here is a quick example, not much in the way of comments, but gives the idea about parameters and linkage. The command line options are mainly to provide MAP and DEBUG information in the resultant file.

MAKE60.BAT
ml -Zi -Zd -c -coff -Fl test60a.asm
cl -Ox -Zd test60c.c test60a.obj -link /MAP


TEST60A.ASM
        .486
        .MODEL FLAT, C
        .CODE

        align 16

SwapString      PROC USES esi edi ebx Src:PTR BYTE, Len:DWORD

        mov     esi,Src
        mov     edi,esi
        mov     ecx,Len
        xor     ebx,ebx

; ebx ecx
;  0   9

        mov     eax,ecx
        sub     eax,ebx
        cmp     eax,8
        jb      swap_final

swap_loop:

        sub     ecx,4
        mov     eax,[esi+ebx]
        mov     edx,[edi+ecx]
        bswap   eax
        bswap   edx
        mov     [esi+ebx],edx
        mov     [edi+ecx],eax
        add     ebx,4

; ebx ecx
;  4   5

        mov     eax,ecx
        sub     eax,ebx
        cmp     eax,8
        jae     swap_loop

swap_final:

        jmp     swap_tbl[eax * 4]

        align   4

swap_tbl:
        dd      offset swap_done
        dd      offset swap_done
        dd      offset swap_2
        dd      offset swap_3
        dd      offset swap_4
        dd      offset swap_5
        dd      offset swap_6
        dd      offset swap_7

        align   16

swap_2:
        mov     ax,[esi+ebx]
        xchg    al,ah
        mov     [esi+ebx],ax
swap_done:
        ret

        align   16

swap_3:
        mov     eax,[esi+ebx]
        bswap   eax
        shr     eax,8
        mov     [esi+ebx],al
        shr     eax,8
        mov     [esi+ebx+1],ax
        ret

        align   16

swap_4:
        mov     eax,[esi+ebx]
        bswap   eax
        mov     [esi+ebx],eax
        ret

        align   16

swap_5:
        mov     eax,[esi+ebx]
        mov     dl,[esi+ebx+4]
        bswap   eax
        mov     [esi+ebx],dl
        mov     [esi+ebx+1],eax
        ret

        align   16

swap_6:
        mov     eax,[esi+ebx]
        mov     dx,[esi+ebx+4]
        bswap   eax
        xchg    dl,dh
        mov     [esi+ebx],dx
        mov     [esi+ebx+2],eax
        ret

        align   16

swap_7:
        mov     eax,[esi+ebx]
        mov     edx,[esi+ebx+4]
        bswap   eax
        bswap   edx
        shr     edx,8
        mov     [esi+ebx],edx
        mov     [esi+ebx+3],eax
        ret

SwapString      ENDP

        END


TEST60C.C
#include <windows.h>

#include <stdio.h>
#include <stdlib.h>

void test(char *src, char *valid)
{
  printf("%s - ",src);

  SwapString(src, strlen(src));

  puts(src);

  if (strcmp(src,valid) != 0)
    puts("FAIL");
}

int main(int argc, char **argv)
{
  test("a","a");
  test("in","ni");
  test("dog","god");
  test("food","doof");

  test("12345","54321");
  test("123456","654321");
  test("1234567","7654321");
  test("12345678","87654321");
  test("123456789","987654321");
  test("1234567890","0987654321");
  test("1234567890a","a0987654321");
  test("1234567890ab","ba0987654321");
  test("1234567890abc","cba0987654321");
  test("1234567890abcd","dcba0987654321");
  test("1234567890abcde","edcba0987654321");
  test("1234567890abcdef","fedcba0987654321");

  test("the quick brown fox jumped over the lazy dog","god yzal eht revo depmuj xof nworb kciuq eht");

  return(1);
}

It could be a random act of randomness. Those happen a lot as well.

Vortex

Hi pps,

Have a look at the Compiler Based Assembler forum. You will find there the examples.

pps

where can i find cl? i looked at the masm folder but i didn't found. sorry for the late answer but i don;t have internet acces all the time.

clive

Quote from: pps on March 08, 2011, 03:16:26 PM
where can i find cl? i looked at the masm folder but i didn't found. sorry for the late answer but i don;t have internet acces all the time.

Mixed programming kind of assumes you have the compilers to do the job. Perhaps you could download an Express Edition of the Microsoft C/C++ if you need it.
It could be a random act of randomness. Those happen a lot as well.

dedndave


pps

For pelle's compiler what comand should i use?

On cl -Ox -Zd test60c.c test60a.obj -link /MAP it gave me an error with mspdb80.dll. Is saying  that the program can't start because  mspdb80.dll is missing from your computer. Try reinstalling the program to fix this problem. How should i solve it. I found mspdb80.dll and put it in the same directory but still doesn't work.

dedndave

you probably need to register the DLL
create a text file and add the following text
REGEDIT4

[HKEY_CLASSES_ROOT\.dll]
@="dllfile"

[HKEY_CLASSES_ROOT\dllfile\shell\regdll]
@="Register ActiveX DLL"

[HKEY_CLASSES_ROOT\dllfile\shell\regdll\command]
@="regsvr32.exe \"%L\""

[HKEY_CLASSES_ROOT\dllfile\shell\unregdll]
@="Unregister ActiveX DLL"

[HKEY_CLASSES_ROOT\dllfile\shell\unregdll\command]
@="regsvr32.exe /u \"%L\""

[HKEY_CLASSES_ROOT\.ocx]
@="ocxfile"

[HKEY_CLASSES_ROOT\ocxfile\shell\regocx]
@="Register OCX Control"

[HKEY_CLASSES_ROOT\ocxfile\shell\regocx\command]
@="regsvr32.exe \"%L\""

[HKEY_CLASSES_ROOT\ocxfile\shell\unregocx]
@="Unregister OCX Control"

[HKEY_CLASSES_ROOT\ocxfile\shell\unregocx\command]
@="regsvr32.exe /u \"%L\""


close it and rename it from .TXT to .REG
then double-click on the REG file and import it

after that, you will be able to register and unregister DLL and OCX files by right-clicking on them
place the DLL where you want it to live and right-click - Register

pps

I did that but I still get an error. I uploaded here a pic of the error. Thank you for the quick answer.

Vortex

Hi pps,

Here is an example for you :



#include <stdio.h>

extern int __stdcall testproc(int x, int y);

int main(int argc,char *argv[])
{
    printf("125 + 75 = %d",testproc(125,75));
    return 0;
}




.386
.model flat,stdcall
option casemap:none

.code

testproc PROC x:DWORD,y:DWORD

    mov     eax,x
    add     eax,y
    ret

testproc ENDP

END



\masm32\bin\ml /c /coff testproc.asm

pocc /Ze /Zx /Ot Mixed.c

polink /SUBSYSTEM:CONSOLE Mixed.obj testproc.obj


Don't forget to run \PellesC\bin\povars32.bat to initialize the Pelles C environment. The path pointing povars32.bat can be different in your Pelles C installation.

pps

I installed pelles compiler, lunched povars32.bat and I tried the program. Assambling works fine but when i try to compile Mixed.c i have the error : Can't finde the include file <stdio.h>. What should I do?

dedndave

if it cannot locate an include file, check your include paths
also notice that the masm32 libraries are written to work only if the masm32 folder is in the root of the current drive

for the other error...


locate the 32-bit version of the DLL   :P

the attached file is the above PNG image renamed to ZIP

pps

I took cl.exe from visual studio 2008, that is the only one that i have in my computer. From where can I get a good one? Or a 32bit dll.
About pelle's Compiler the program works, i have the version 6.5, i just made a program in c for testing with stdio.h and works fine.
I use windows 7, on 32bits.

dedndave

well, i don't have visual studio installed on this machine, yet
there are certain things that make me incompatible with newer versions
that is because i am using XP Media Center Edition, which is not fully compatible with .NET 4.0
i have requested MS support to fix it - they may not, as XP MCE2005 is in or beyond the extended service phase

if i had to venture a guess, i would look in the C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\intel32 folder   :P