The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: pps on March 02, 2011, 09:13:29 AM

Title: Mixed programming in masm32
Post by: pps on March 02, 2011, 09:13:29 AM
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.
Title: Re: Mixt programming in masm32
Post by: hutch-- on March 02, 2011, 11:33:26 AM
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.
Title: Re: MIXED programming in masm32
Post by: clive on March 02, 2011, 12:28:03 PM
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);
}

Title: Re: Mixt programming in masm32
Post by: Vortex on March 02, 2011, 08:47:15 PM
Hi pps,

Have a look at the Compiler Based Assembler (http://www.masm32.com/board/index.php?board=29.0) forum. You will find there the examples.
Title: Re: Mixt programming in masm32
Post by: 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.
Title: Re: Mixt programming in masm32
Post by: clive on March 08, 2011, 04:55:30 PM
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.
Title: Re: Mixt programming in masm32
Post by: dedndave on March 08, 2011, 05:01:22 PM
or Pelle's C compiler
Title: Re: Mixt programming in masm32
Post by: pps on April 23, 2011, 03:05:25 PM
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.
Title: Re: Mixt programming in masm32
Post by: dedndave on April 23, 2011, 04:12:08 PM
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
Title: Re: Mixt programming in masm32
Post by: pps on April 23, 2011, 05:54:56 PM
I did that but I still get an error. I uploaded here a pic of the error. Thank you for the quick answer.
Title: Re: Mixt programming in masm32
Post by: Vortex on April 23, 2011, 06:00:56 PM
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.
Title: Re: Mixt programming in masm32
Post by: pps on April 23, 2011, 06:32:00 PM
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?
Title: Re: Mixt programming in masm32
Post by: dedndave on April 23, 2011, 06:57:14 PM
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...

(http://www.masm32.com/board/index.php?action=dlattach;topic=16197.0;id=9140)
locate the 32-bit version of the DLL   :P

the attached file is the above PNG image renamed to ZIP
Title: Re: Mixt programming in masm32
Post by: pps on April 23, 2011, 07:24:40 PM
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.
Title: Re: Mixt programming in masm32
Post by: dedndave on April 23, 2011, 07:31:56 PM
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
Title: Re: Mixt programming in masm32
Post by: pps on April 23, 2011, 07:43:05 PM
I don't have an intel folder, neither amd32,, just 2 folders.
What cl do you use. can you give it to me?
Title: Re: Mixt programming in masm32
Post by: Vortex on April 23, 2011, 08:26:41 PM
Quote from: pps on April 23, 2011, 06:32:00 PM
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?

Hi pps,

Can you build the sample projects coming with the Pelles C installation?
Title: Re: Mixt programming in masm32
Post by: pps on April 25, 2011, 07:58:10 AM
Yes, I made a simple program, i included stdio.h and it woks. I have my project for masm on desktop and pelles c, on c\program files.
Title: Re: Mixt programming in masm32
Post by: Vortex on April 25, 2011, 05:04:09 PM
Hi pps,

Did you try to rebuild the project with the asm module?
Title: Re: Mixt programming in masm32
Post by: pps on April 26, 2011, 09:21:06 AM
yes. i even deleted the obj and exe file. i started to run the comands  one by one from the bat file in cmd. When i use masm, it works, and i get the .obj file, then then i call pocc pocc /Ze /Zx /Ot Mixed.c i het that error. i think it must be from my computer because what you did works fine. i Don;t uderstand where i am wrong.
Title: Re: Mixt programming in masm32
Post by: pps on April 30, 2011, 07:12:52 PM
Is anyone having any ideas please?
Title: Re: Mixt programming in masm32
Post by: Vortex on April 30, 2011, 08:06:29 PM
After running povars32.bat, can you post here the value of the environment variable PATH?

Type path in the command prompt. You should see something like this one :

D:\PellesC>path
PATH=D:\PellesC\Bin;C:\WINDOWS\system32;...


My Pelles C installation is located in the D:\PellesC directory.
Title: Re: Mixt programming in masm32
Post by: pps on May 02, 2011, 08:26:42 AM
I tapped but I got something wrong....
Title: Re: Mixed programming in masm32
Post by: pps on May 09, 2011, 10:18:20 AM
I can't understand why i don't get the path of the bin folder like in your example....
Title: Re: Mixed programming in masm32
Post by: dedndave on May 09, 2011, 11:20:13 AM
you can add a folder to the path inside the assembly batch file
if you do it that way, it will only stay in there as long as that console window is open

in the batch file...
set path=D:\PellesC\bin;%PATH%

otherwise, you can add it permanently by...

Control Panel - System icon (or right-click on My Computer - Properties)
Advanced tab
Environment Variables button
use the upper window for the current windows user only or use the lower window to apply for all windows logon users
highlight the Path variable, then click the Edit button (or New button if it does not exist)

usually, the path variable is too long to fit in the box
i like to copy the entire string into a temporary text file, edit it, then paste it back into the box
folders in the path variable are seperated by semicolons