How to use MASM tools for convert from alphacode to Assemblycode by using ....

Started by Newbie, February 21, 2005, 05:04:19 AM

Previous topic - Next topic

Newbie

How to use MASM tools for convert from alphacode to Assemblycode by using  C# language

I have used to use CStools45  for convert it  but it's not work....

example the inputfile

function factorial(n)
{
definr sum;
if n <= 1 then
  return 1;
sum = 1;
for i = 2 to n do
   sum = sum * i;
return sum;
}




the outputfile  is  Assambly code

I would like to do it


Regards
Jaka.

Dark Schneider

You have two alternatives:

1. Compile your code to executable file and 'disassemble' it, however the resulting assembly code will be harder to understand if you don't have enough knowledge in assembly language.
2. Learn assembly language just like all of us here, when you gained enough knowledge then you can easily convert any code of any language you know to assembly code manually.

hutch--

Jaka,

Dark Schneider has given you some good advice here. If you use Microsoft CL.EXE it IS the tool to convert C code to MASM. Just as a suggestion, put in in a seperate file by itself, probably convert it to __stdcall and build it will ALL of the optimisation turned off. The MASM output will be something like readable then, especially if you clean up the mess that the C compiler leaves in terms of comments and commented out code.

Tour code will not build as is, it needs to be correct C for the suggestion I had in mind. It seems to be a combination of Pascal and C at the moment.

This builds in Microsoft C.


int __stdcall factorial(int num)
  {
    int sum;
    int i;
    if (num <= 1)
      {
      return 1;
      }
    sum = 1;
    for (i=2; i=num;)
      {
       sum = sum * i;
      }
    return sum;
  }


A very crude and unoptimised translation to MASM code is as follows,


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

factorial proc num:DWORD

    LOCAL sum:DWORD
    LOCAL i:DWORD

    cmp DWORD PTR num, 1
    jg lbl0
    mov eax, 1
    jmp lbl3

  lbl0:
    mov DWORD PTR sum, 1
    mov DWORD PTR i, 2

  lbl1:
    mov eax, DWORD PTR num
    mov DWORD PTR i, eax
    cmp DWORD PTR i, 0
    je lbl2
    mov ecx, DWORD PTR sum
    imul ecx, DWORD PTR i
    mov DWORD PTR sum, ecx
    jmp lbl1

  lbl2:
    mov eax, DWORD PTR sum

  lbl3:
    ret

factorial endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

raymond

The following would be a lot simpler and does not need LOCALs


factorial proc num:DWORD

    mov eax, 1
    cmp num, eax
    jle lbl1

lbl0:
    mul num
    dec num
    jnz lbl0

lbl1:
    ret

factorial endp


Raymond

When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com


tenkey

CSTools45 may generate Assembly code, but I suspect it's not MASM code.

Or does CSTools45 generate alphacode? (Alphacode sounds like a VM binary to me.)
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8